installer: support 'start_on_completion' (#96)

This commit is contained in:
Matthew Penner 2021-07-04 16:08:05 -06:00 committed by GitHub
parent c0a487c47e
commit 73570c7144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 14 deletions

View File

@ -29,6 +29,7 @@ func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer,
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"),

View File

@ -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)

View File

@ -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()
}