Minimize code duplication for environment variables; ref pterodactyl/panel#2255

This commit is contained in:
Dane Everitt 2020-08-27 20:28:29 -07:00
parent 60acee2df5
commit 711ee2258c
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 10 additions and 43 deletions

View File

@ -1,17 +1,13 @@
package environment package environment
import ( import (
"fmt"
"strings"
"sync" "sync"
"time"
) )
type configurationSettings struct { type configurationSettings struct {
Mounts []Mount Mounts []Mount
Allocations Allocations Allocations Allocations
Limits Limits Limits Limits
Variables Variables
} }
// Defines the actual configuration struct for the environment with all of the settings // Defines the actual configuration struct for the environment with all of the settings
@ -19,16 +15,17 @@ type configurationSettings struct {
type Configuration struct { type Configuration struct {
mu sync.RWMutex mu sync.RWMutex
environmentVariables []string
settings configurationSettings 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{ return &Configuration{
environmentVariables: envVars,
settings: configurationSettings{ settings: configurationSettings{
Mounts: m, Mounts: m,
Allocations: a, Allocations: a,
Limits: l, Limits: l,
Variables: v,
}, },
} }
} }
@ -54,31 +51,9 @@ func (c *Configuration) Mounts() []Mount {
return c.settings.Mounts return c.settings.Mounts
} }
// Returns all of the environment variables that should be assigned to a running
// server instance.
func (c *Configuration) EnvironmentVariables() []string { func (c *Configuration) EnvironmentVariables() []string {
c.mu.RLock() c.mu.RLock()
c.mu.RUnlock() defer c.mu.RUnlock()
zone, _ := time.Now().In(time.Local).Zone() return c.environmentVariables
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
} }

View File

@ -148,7 +148,7 @@ func (e *Environment) Create() error {
Tty: true, Tty: true,
ExposedPorts: a.Exposed(), ExposedPorts: a.Exposed(),
Image: e.meta.Image, Image: e.meta.Image,
Env: e.variables(), Env: e.Configuration.EnvironmentVariables(),
Labels: map[string]string{ Labels: map[string]string{
"Service": "Pterodactyl", "Service": "Pterodactyl",
"ContainerType": "server_process", "ContainerType": "server_process",
@ -204,12 +204,6 @@ func (e *Environment) Create() error {
return nil 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 { func (e *Environment) convertMounts() []mount.Mount {
var out []mount.Mount var out []mount.Mount

View File

@ -13,7 +13,6 @@ import (
) )
type Metadata struct { type Metadata struct {
Invocation string
Image string Image string
Stop *api.ProcessStopConfiguration Stop *api.ProcessStopConfiguration
} }

View File

@ -26,7 +26,7 @@ func (e *Environment) OnBeforeStart() error {
// the Panel is usee. // the Panel is usee.
if err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{RemoveVolumes: true}); err != nil { if err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{RemoveVolumes: true}); err != nil {
if !client.IsErrNotFound(err) { if !client.IsErrNotFound(err) {
return err return errors.Wrap(err, "failed to remove server docker container during pre-boot")
} }
} }

View File

@ -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 // 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 // this logic in. When we're ready to support other environment we'll need to make
// some modifications here obviously. // 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{ meta := docker.Metadata{
Invocation: s.Config().Invocation,
Image: s.Config().Container.Image, Image: s.Config().Container.Image,
} }