From 711ee2258c2a2e09fbf2e0d6722b71877bbfc81f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 27 Aug 2020 20:28:29 -0700 Subject: [PATCH] Minimize code duplication for environment variables; ref pterodactyl/panel#2255 --- environment/config.go | 39 ++++++------------------------- environment/docker/container.go | 8 +------ environment/docker/environment.go | 1 - environment/docker/power.go | 2 +- server/loader.go | 3 +-- 5 files changed, 10 insertions(+), 43 deletions(-) diff --git a/environment/config.go b/environment/config.go index a5937b3..06c7197 100644 --- a/environment/config.go +++ b/environment/config.go @@ -1,17 +1,13 @@ package environment import ( - "fmt" - "strings" "sync" - "time" ) type configurationSettings struct { Mounts []Mount Allocations Allocations Limits Limits - Variables Variables } // Defines the actual configuration struct for the environment with all of the settings @@ -19,16 +15,17 @@ type configurationSettings struct { type Configuration struct { mu sync.RWMutex - settings configurationSettings + environmentVariables []string + settings configurationSettings } -func NewConfiguration(m []Mount, a Allocations, l Limits, v Variables) *Configuration { +func NewConfiguration(m []Mount, a Allocations, l Limits, envVars []string) *Configuration { return &Configuration{ + environmentVariables: envVars, settings: configurationSettings{ Mounts: m, Allocations: a, Limits: l, - Variables: v, }, } } @@ -54,31 +51,9 @@ func (c *Configuration) Mounts() []Mount { return c.settings.Mounts } -// Returns all of the environment variables that should be assigned to a running -// server instance. func (c *Configuration) EnvironmentVariables() []string { c.mu.RLock() - c.mu.RUnlock() + defer c.mu.RUnlock() - zone, _ := time.Now().In(time.Local).Zone() - - var out = []string{ - fmt.Sprintf("TZ=%s", zone), - fmt.Sprintf("SERVER_MEMORY=%d", c.settings.Limits.MemoryLimit), - fmt.Sprintf("SERVER_IP=%s", c.settings.Allocations.DefaultMapping.Ip), - fmt.Sprintf("SERVER_PORT=%d", c.settings.Allocations.DefaultMapping.Port), - } - -eloop: - for k := range c.settings.Variables { - for _, e := range out { - if strings.HasPrefix(e, strings.ToUpper(k)) { - continue eloop - } - } - - out = append(out, fmt.Sprintf("%s=%s", strings.ToUpper(k), c.settings.Variables.Get(k))) - } - - return out -} + return c.environmentVariables +} \ No newline at end of file diff --git a/environment/docker/container.go b/environment/docker/container.go index 96ed9c7..6baf51d 100644 --- a/environment/docker/container.go +++ b/environment/docker/container.go @@ -148,7 +148,7 @@ func (e *Environment) Create() error { Tty: true, ExposedPorts: a.Exposed(), Image: e.meta.Image, - Env: e.variables(), + Env: e.Configuration.EnvironmentVariables(), Labels: map[string]string{ "Service": "Pterodactyl", "ContainerType": "server_process", @@ -204,12 +204,6 @@ func (e *Environment) Create() error { return nil } -func (e *Environment) variables() []string { - v := e.Configuration.EnvironmentVariables() - - return append(v, fmt.Sprintf("STARTUP=%s", e.meta.Invocation)) -} - func (e *Environment) convertMounts() []mount.Mount { var out []mount.Mount diff --git a/environment/docker/environment.go b/environment/docker/environment.go index ef0e6d1..df3180d 100644 --- a/environment/docker/environment.go +++ b/environment/docker/environment.go @@ -13,7 +13,6 @@ import ( ) type Metadata struct { - Invocation string Image string Stop *api.ProcessStopConfiguration } diff --git a/environment/docker/power.go b/environment/docker/power.go index d691b4e..de1a3ba 100644 --- a/environment/docker/power.go +++ b/environment/docker/power.go @@ -26,7 +26,7 @@ func (e *Environment) OnBeforeStart() error { // the Panel is usee. if err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{RemoveVolumes: true}); err != nil { if !client.IsErrNotFound(err) { - return err + return errors.Wrap(err, "failed to remove server docker container during pre-boot") } } diff --git a/server/loader.go b/server/loader.go index 07196f7..36c92a7 100644 --- a/server/loader.go +++ b/server/loader.go @@ -103,9 +103,8 @@ func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) { // Right now we only support a Docker based environment, so I'm going to hard code // this logic in. When we're ready to support other environment we'll need to make // some modifications here obviously. - envCfg := environment.NewConfiguration(s.Mounts(), s.cfg.Allocations, s.cfg.Build, s.cfg.EnvVars) + envCfg := environment.NewConfiguration(s.Mounts(), s.cfg.Allocations, s.cfg.Build, s.GetEnvironmentVariables()) meta := docker.Metadata{ - Invocation: s.Config().Invocation, Image: s.Config().Container.Image, }