feat: add automation features (auto-update, auto-start, scheduled updates)
This commit is contained in:
@@ -50,6 +50,7 @@ func main() {
|
||||
modlistMgr := services.NewModlistManager(dataDir)
|
||||
process := services.NewProcessManager(serverfileDir, modsDir, cfgDir, profilesDir, settings, modlistMgr, configMgr, streamer)
|
||||
steamcmd := services.NewSteamCmdManager(serverfileDir, streamer)
|
||||
scheduler := services.NewScheduler(settings, modlistMgr, steamcmd)
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
@@ -62,9 +63,58 @@ func main() {
|
||||
log.Printf("serving frontend from %s", frontendDir)
|
||||
}
|
||||
|
||||
handler := api.New(settings, configMgr, modlistMgr, process, steamcmd, streamer, serverfileDir, modsDir, cfgDir, profilesDir)
|
||||
handler := api.New(settings, configMgr, modlistMgr, process, steamcmd, scheduler, streamer, serverfileDir, modsDir, cfgDir, profilesDir)
|
||||
handler.SetupRoutes(r)
|
||||
|
||||
// Startup auto-tasks
|
||||
s, err := settings.Load()
|
||||
if err != nil {
|
||||
log.Printf("startup: load settings: %v", err)
|
||||
} else {
|
||||
if s.AutoUpdateOnStartup && s.SteamUser != "" {
|
||||
go func() {
|
||||
log.Print("startup: auto-updating gameserver")
|
||||
if err := steamcmd.UpdateGame(s.SteamBranch, s.SteamUser); err != nil {
|
||||
log.Printf("startup: auto-update game failed: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if s.AutoUpdateModsOnStartup && s.ActiveModlist != "" {
|
||||
go func() {
|
||||
log.Print("startup: auto-updating mods")
|
||||
ml, err := modlistMgr.Get(s.ActiveModlist)
|
||||
if err != nil {
|
||||
log.Printf("startup: load modlist for auto-update: %v", err)
|
||||
return
|
||||
}
|
||||
var modIDs []string
|
||||
for _, m := range ml.Mods {
|
||||
if m.Enabled {
|
||||
modIDs = append(modIDs, m.ID)
|
||||
}
|
||||
}
|
||||
if len(modIDs) > 0 {
|
||||
if err := steamcmd.DownloadMods(modIDs); err != nil {
|
||||
log.Printf("startup: auto-update mods failed: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if s.AutoStartOnStartup && s.WasRunning {
|
||||
go func() {
|
||||
log.Print("startup: auto-restarting server (was running before)")
|
||||
if err := process.Start(); err != nil {
|
||||
log.Printf("startup: auto-start failed: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// Start the scheduled updater
|
||||
scheduler.Start()
|
||||
|
||||
log.Printf("starting server on %s", listen)
|
||||
|
||||
srv := &http.Server{Addr: listen, Handler: r}
|
||||
@@ -80,6 +130,7 @@ func main() {
|
||||
<-quit
|
||||
|
||||
log.Print("shutting down server...")
|
||||
scheduler.Stop()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -33,6 +33,7 @@ require (
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.59.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
|
||||
@@ -65,6 +65,8 @@ github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SA
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
|
||||
@@ -12,6 +12,7 @@ type Handler struct {
|
||||
modlists *services.ModlistManager
|
||||
process *services.ProcessManager
|
||||
steamcmd *services.SteamCmdManager
|
||||
scheduler *services.Scheduler
|
||||
streamer *services.LogStreamer
|
||||
serverfileDir string
|
||||
modsDir string
|
||||
@@ -25,6 +26,7 @@ func New(
|
||||
modlists *services.ModlistManager,
|
||||
process *services.ProcessManager,
|
||||
steamcmd *services.SteamCmdManager,
|
||||
scheduler *services.Scheduler,
|
||||
streamer *services.LogStreamer,
|
||||
serverfileDir, modsDir, cfgDir, profilesDir string,
|
||||
) *Handler {
|
||||
@@ -34,6 +36,7 @@ func New(
|
||||
modlists: modlists,
|
||||
process: process,
|
||||
steamcmd: steamcmd,
|
||||
scheduler: scheduler,
|
||||
streamer: streamer,
|
||||
serverfileDir: serverfileDir,
|
||||
modsDir: modsDir,
|
||||
|
||||
@@ -7,16 +7,20 @@ import (
|
||||
)
|
||||
|
||||
type updateSettingsInput struct {
|
||||
IPPort *string `json:"ip_port"`
|
||||
ServerParameters *string `json:"server_parameters"`
|
||||
SteamBranch *string `json:"steam_branch"`
|
||||
SteamUser *string `json:"steam_user"`
|
||||
Platform *string `json:"platform"`
|
||||
CBASettings *string `json:"cba_settings"`
|
||||
AILevelPresets *string `json:"ai_level_presets"`
|
||||
DifficultyPresets *string `json:"difficulty_presets"`
|
||||
ActiveConfig *string `json:"active_config"`
|
||||
ActiveModlist *string `json:"active_modlist"`
|
||||
IPPort *string `json:"ip_port"`
|
||||
ServerParameters *string `json:"server_parameters"`
|
||||
SteamBranch *string `json:"steam_branch"`
|
||||
SteamUser *string `json:"steam_user"`
|
||||
Platform *string `json:"platform"`
|
||||
CBASettings *string `json:"cba_settings"`
|
||||
AILevelPresets *string `json:"ai_level_presets"`
|
||||
DifficultyPresets *string `json:"difficulty_presets"`
|
||||
ActiveConfig *string `json:"active_config"`
|
||||
ActiveModlist *string `json:"active_modlist"`
|
||||
AutoUpdateOnStartup *bool `json:"auto_update_on_startup"`
|
||||
AutoStartOnStartup *bool `json:"auto_start_on_startup"`
|
||||
AutoUpdateModsOnStartup *bool `json:"auto_update_mods_on_startup"`
|
||||
ScheduledUpdate *string `json:"scheduled_update"`
|
||||
}
|
||||
|
||||
func (h *Handler) GetSettings(c *gin.Context) {
|
||||
@@ -51,6 +55,10 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
|
||||
if input.DifficultyPresets != nil { s.DifficultyPresets = *input.DifficultyPresets }
|
||||
if input.ActiveConfig != nil { s.ActiveConfig = *input.ActiveConfig }
|
||||
if input.ActiveModlist != nil { s.ActiveModlist = *input.ActiveModlist }
|
||||
if input.AutoUpdateOnStartup != nil { s.AutoUpdateOnStartup = *input.AutoUpdateOnStartup }
|
||||
if input.AutoStartOnStartup != nil { s.AutoStartOnStartup = *input.AutoStartOnStartup }
|
||||
if input.AutoUpdateModsOnStartup != nil { s.AutoUpdateModsOnStartup = *input.AutoUpdateModsOnStartup }
|
||||
if input.ScheduledUpdate != nil { s.ScheduledUpdate = *input.ScheduledUpdate }
|
||||
|
||||
if err := h.process.WriteUserconfigFiles(s); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "write userconfig: " + err.Error()})
|
||||
@@ -61,6 +69,11 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if h.scheduler != nil {
|
||||
h.scheduler.Refresh()
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, s)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,20 @@ package models
|
||||
import "time"
|
||||
|
||||
type ServerSettings struct {
|
||||
IPPort string `json:"ip_port"`
|
||||
ServerParameters string `json:"server_parameters"`
|
||||
SteamBranch string `json:"steam_branch"`
|
||||
SteamUser string `json:"steam_user"`
|
||||
Platform string `json:"platform"`
|
||||
CBASettings string `json:"cba_settings"`
|
||||
AILevelPresets string `json:"ai_level_presets"`
|
||||
DifficultyPresets string `json:"difficulty_presets"`
|
||||
ActiveConfig string `json:"active_config"`
|
||||
ActiveModlist string `json:"active_modlist"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
IPPort string `json:"ip_port"`
|
||||
ServerParameters string `json:"server_parameters"`
|
||||
SteamBranch string `json:"steam_branch"`
|
||||
SteamUser string `json:"steam_user"`
|
||||
Platform string `json:"platform"`
|
||||
CBASettings string `json:"cba_settings"`
|
||||
AILevelPresets string `json:"ai_level_presets"`
|
||||
DifficultyPresets string `json:"difficulty_presets"`
|
||||
ActiveConfig string `json:"active_config"`
|
||||
ActiveModlist string `json:"active_modlist"`
|
||||
AutoUpdateOnStartup bool `json:"auto_update_on_startup"`
|
||||
AutoStartOnStartup bool `json:"auto_start_on_startup"`
|
||||
AutoUpdateModsOnStartup bool `json:"auto_update_mods_on_startup"`
|
||||
WasRunning bool `json:"was_running"`
|
||||
ScheduledUpdate string `json:"scheduled_update"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
92
backend/internal/services/scheduler.go
Normal file
92
backend/internal/services/scheduler.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type Scheduler struct {
|
||||
cron *cron.Cron
|
||||
entryID cron.EntryID
|
||||
settings *SettingsManager
|
||||
modlists *ModlistManager
|
||||
steamcmd *SteamCmdManager
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewScheduler(settings *SettingsManager, modlists *ModlistManager, steamcmd *SteamCmdManager) *Scheduler {
|
||||
return &Scheduler{
|
||||
cron: cron.New(),
|
||||
settings: settings,
|
||||
modlists: modlists,
|
||||
steamcmd: steamcmd,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) Start() {
|
||||
s.cron.Start()
|
||||
s.Refresh()
|
||||
}
|
||||
|
||||
func (s *Scheduler) Stop() {
|
||||
s.cron.Stop()
|
||||
}
|
||||
|
||||
func (s *Scheduler) Refresh() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if s.entryID != 0 {
|
||||
s.cron.Remove(s.entryID)
|
||||
s.entryID = 0
|
||||
}
|
||||
|
||||
sett, err := s.settings.Load()
|
||||
if err != nil || sett.ScheduledUpdate == "" {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := s.cron.AddFunc(sett.ScheduledUpdate, s.runScheduledUpdate)
|
||||
if err != nil {
|
||||
log.Printf("scheduler: invalid cron expression %q: %v", sett.ScheduledUpdate, err)
|
||||
return
|
||||
}
|
||||
s.entryID = id
|
||||
log.Printf("scheduler: scheduled update with cron %q", sett.ScheduledUpdate)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runScheduledUpdate() {
|
||||
sett, err := s.settings.Load()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if sett.SteamUser != "" && sett.SteamUser != "anonymous" {
|
||||
log.Print("scheduler: running gameserver update")
|
||||
if err := s.steamcmd.UpdateGame(sett.SteamBranch, sett.SteamUser); err != nil {
|
||||
log.Printf("scheduler: game update failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if sett.ActiveModlist != "" {
|
||||
ml, err := s.modlists.Get(sett.ActiveModlist)
|
||||
if err != nil {
|
||||
log.Printf("scheduler: load modlist %s: %v", sett.ActiveModlist, err)
|
||||
return
|
||||
}
|
||||
var modIDs []string
|
||||
for _, m := range ml.Mods {
|
||||
if m.Enabled {
|
||||
modIDs = append(modIDs, m.ID)
|
||||
}
|
||||
}
|
||||
if len(modIDs) > 0 {
|
||||
log.Printf("scheduler: updating %d mod(s)", len(modIDs))
|
||||
if err := s.steamcmd.DownloadMods(modIDs); err != nil {
|
||||
log.Printf("scheduler: mod update failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,11 @@ func (pm *ProcessManager) Start() error {
|
||||
return fmt.Errorf("load settings: %w", err)
|
||||
}
|
||||
|
||||
s.WasRunning = true
|
||||
if err := pm.settings.Save(s); err != nil {
|
||||
return fmt.Errorf("save was_running: %w", err)
|
||||
}
|
||||
|
||||
binName := "arma3server_x64"
|
||||
if s.Platform == "windows" {
|
||||
binName = "arma3server_x64.exe"
|
||||
@@ -141,6 +146,13 @@ func (pm *ProcessManager) Stop() error {
|
||||
|
||||
rp.cancel()
|
||||
<-rp.exited
|
||||
|
||||
s, err := pm.settings.Load()
|
||||
if err == nil {
|
||||
s.WasRunning = false
|
||||
pm.settings.Save(s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -54,13 +54,18 @@ func (sm *SettingsManager) Save(s *models.ServerSettings) error {
|
||||
|
||||
func (sm *SettingsManager) defaults() *models.ServerSettings {
|
||||
return &models.ServerSettings{
|
||||
IPPort: "0.0.0.0:2302",
|
||||
ServerParameters: "-server -world=empty -loadMissionToMemory -noPause",
|
||||
SteamBranch: "stable",
|
||||
SteamUser: "anonymous",
|
||||
Platform: "linux",
|
||||
ActiveConfig: "",
|
||||
ActiveModlist: "",
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
IPPort: "0.0.0.0:2302",
|
||||
ServerParameters: "-server -world=empty -loadMissionToMemory -noPause",
|
||||
SteamBranch: "stable",
|
||||
SteamUser: "anonymous",
|
||||
Platform: "linux",
|
||||
ActiveConfig: "",
|
||||
ActiveModlist: "",
|
||||
AutoUpdateOnStartup: false,
|
||||
AutoStartOnStartup: false,
|
||||
AutoUpdateModsOnStartup: false,
|
||||
WasRunning: false,
|
||||
ScheduledUpdate: "",
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user