Minimize code duplication for environment variables; ref pterodactyl/panel#2255
This commit is contained in:
parent
60acee2df5
commit
711ee2258c
|
@ -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
|
||||
}
|
|
@ -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
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ import (
|
|||
)
|
||||
|
||||
type Metadata struct {
|
||||
Invocation string
|
||||
Image string
|
||||
Stop *api.ProcessStopConfiguration
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user