From 73570c714498c2bc465fe5ee576038ab634e112b Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 4 Jul 2021 16:08:05 -0600 Subject: [PATCH] installer: support 'start_on_completion' (#96) --- installer/installer.go | 9 +++++---- router/router_system.go | 22 ++++++++++++++++++++-- server/configuration.go | 14 ++++++-------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/installer/installer.go b/installer/installer.go index d2b9bd3..1e19e0c 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -25,10 +25,11 @@ func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer, } cfg := &server.Configuration{ - Uuid: getString(data, "uuid"), - Suspended: false, - Invocation: getString(data, "invocation"), - SkipEggScripts: getBoolean(data, "skip_egg_scripts"), + Uuid: getString(data, "uuid"), + Suspended: false, + Invocation: getString(data, "invocation"), + SkipEggScripts: getBoolean(data, "skip_egg_scripts"), + StartOnCompletion: getBoolean(data, "start_on_completion"), Build: environment.Limits{ MemoryLimit: getInt(data, "build", "memory"), Swap: getInt(data, "build", "swap"), diff --git a/router/router_system.go b/router/router_system.go index 60cf617..8921f8c 100644 --- a/router/router_system.go +++ b/router/router_system.go @@ -2,6 +2,8 @@ package router import ( "bytes" + "context" + "errors" "net/http" "strings" @@ -65,14 +67,30 @@ func postCreateServer(c *gin.Context) { // cycle. If there are any errors they will be logged and communicated back // to the Panel where a reinstall may take place. go func(i *installer.Installer) { - err := i.Server().CreateEnvironment() - if err != nil { + if err := i.Server().CreateEnvironment(); err != nil { i.Server().Log().WithField("error", err).Error("failed to create server environment during install process") return } if err := i.Server().Install(false); err != nil { log.WithFields(log.Fields{"server": i.Uuid(), "error": err}).Error("failed to run install process for server") + return + } + + if i.Server().Config().StartOnCompletion { + log.WithField("server_id", i.Server().Id()).Debug("starting server after successful installation") + if err := i.Server().HandlePowerAction(server.PowerActionStart, 30); err != nil { + if errors.Is(err, context.DeadlineExceeded) { + log.WithFields(log.Fields{"server_id": i.Server().Id(), "action": "start"}). + Warn("could not acquire a lock while attempting to perform a power action") + } else { + log.WithFields(log.Fields{"server_id": i.Server().Id(), "action": "start", "error": err}). + Error("encountered error processing a server power action in the background") + } + } + } else { + log.WithField("server_id", i.Server().Id()). + Debug("skipping automatic start after successful server installation") } }(install) diff --git a/server/configuration.go b/server/configuration.go index 100b4ab..dfc9ac5 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -33,7 +33,9 @@ type Configuration struct { // By default this is false, however if selected within the Panel while installing or re-installing a // server, specific installation scripts will be skipped for the server process. - SkipEggScripts bool `default:"false" json:"skip_egg_scripts"` + SkipEggScripts bool `json:"skip_egg_scripts"` + + StartOnCompletion bool `json:"start_on_completion"` // An array of environment variables that should be passed along to the running // server process. @@ -41,7 +43,7 @@ type Configuration struct { Allocations environment.Allocations `json:"allocations"` Build environment.Limits `json:"build"` - CrashDetectionEnabled bool `default:"true" json:"enabled" yaml:"enabled"` + CrashDetectionEnabled bool `json:"crash_detection_enabled"` Mounts []Mount `json:"mounts"` Egg EggConfiguration `json:"egg,omitempty"` @@ -54,34 +56,30 @@ type Configuration struct { func (s *Server) Config() *Configuration { s.cfg.mu.RLock() defer s.cfg.mu.RUnlock() - return &s.cfg } -// Returns the amount of disk space available to a server in bytes. +// DiskSpace returns the amount of disk space available to a server in bytes. func (s *Server) DiskSpace() int64 { s.cfg.mu.RLock() defer s.cfg.mu.RUnlock() - return s.cfg.Build.DiskSpace * 1024.0 * 1024.0 } func (s *Server) MemoryLimit() int64 { s.cfg.mu.RLock() defer s.cfg.mu.RUnlock() - return s.cfg.Build.MemoryLimit } func (c *Configuration) GetUuid() string { c.mu.RLock() defer c.mu.RUnlock() - return c.Uuid } func (c *Configuration) SetSuspended(s bool) { c.mu.Lock() + defer c.mu.Unlock() c.Suspended = s - c.mu.Unlock() }