From d534b2360677f0eabca3c87e92e8161cc4f09a5b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 6 Apr 2019 13:33:54 -0700 Subject: [PATCH] Move configuration to own directory, modify docker environment setup to be easier to manage --- config.go => config/config.go | 85 ++++++++++++++++++++++++++++------- config/servers/.gitkeep | 0 {config => data}/.gitignore | 0 go.mod | 2 +- server/environment_docker.go | 59 +++++++----------------- server/server.go | 17 ++++--- wings.go | 5 ++- 7 files changed, 102 insertions(+), 66 deletions(-) rename config.go => config/config.go (66%) delete mode 100644 config/servers/.gitkeep rename {config => data}/.gitignore (100%) diff --git a/config.go b/config/config.go similarity index 66% rename from config.go rename to config/config.go index 4f6e054..e3f03ae 100644 --- a/config.go +++ b/config/config.go @@ -1,7 +1,6 @@ -package main +package config import ( - "github.com/pterodactyl/wings/server" "gopkg.in/yaml.v2" "io/ioutil" "os" @@ -12,16 +11,9 @@ type Configuration struct { // if the debug flag is passed through the command line arguments. Debug bool - // Directory where the server data is stored at. - Data string - Api *ApiConfiguration - Docker *server.DockerConfiguration - - // Determines if permissions for a server should be set automatically on - // daemon boot. This can take a long time on systems with many servers, or on - // systems with servers containing thousands of files. - SetPermissionsOnBoot bool `yaml:"set_permissions_on_boot"` + System *SystemConfiguration + Docker *DockerConfiguration // The amount of time in seconds that should elapse between disk usage checks // run by the daemon. Setting a higher number can result in better IO performance @@ -59,6 +51,52 @@ type Configuration struct { AuthenticationToken string `yaml:"token"` } +// Defines basic system configuration settings. +type SystemConfiguration struct { + // Directory where the server data is stored at. + Data string + + // The user that should own all of the server files, and be used for containers. + User string + + // The path to the system's timezone file that will be mounted into running Docker containers. + TimezonePath string `yaml:"timezone_path"` + + // Determines if permissions for a server should be set automatically on + // daemon boot. This can take a long time on systems with many servers, or on + // systems with servers containing thousands of files. + SetPermissionsOnBoot bool `yaml:"set_permissions_on_boot"` +} + +// Defines the docker configuration used by the daemon when interacting with +// containers and networks on the system. +type DockerConfiguration struct { + // Network configuration that should be used when creating a new network + // for containers run through the daemon. + Network struct { + // The interface that should be used to create the network. Must not conflict + // with any other interfaces in use by Docker or on the system. + Interface string + + // The name of the network to use. If this network already exists it will not + // be created. If it is not found, a new network will be created using the interface + // defined. + Name string + } + + // If true, container images will be updated when a server starts if there + // is an update available. If false the daemon will not attempt updates and will + // defer to the host system to manage image updates. + UpdateImages bool `yaml:"update_images"` + + // The location of the Docker socket. + Socket string + + // Defines the location of the timezone file on the host system that should + // be mounted into the created containers so that they all use the same time. + TimezonePath string `yaml:"timezone_path"` +} + // Defines the configuration for the internal API that is exposed by the // daemon webserver. type ApiConfiguration struct { @@ -85,8 +123,10 @@ type ApiConfiguration struct { // of the places in the code using them will need to be doing checks, which is // a tedious thing to have to do. func (c *Configuration) SetDefaults() { - // Set the default data directory. - c.Data = "/srv/daemon-data" + c.System = &SystemConfiguration{ + Data: "/srv/daemon-data", + TimezonePath:"/etc/timezone", + } // By default the internal webserver should bind to all interfaces and // be served on port 8080. @@ -102,7 +142,7 @@ func (c *Configuration) SetDefaults() { // In production and heavy use environments where boot speed is essential, // this should be set to false as servers will self-correct permissions on // boot anyways. - c.SetPermissionsOnBoot = true + c.System.SetPermissionsOnBoot = true // Configure the default throttler implementation. This should work fine // for 99% of people running servers on the platform. The occasional host @@ -114,7 +154,7 @@ func (c *Configuration) SetDefaults() { c.Throttles.CheckInterval = 100 // Configure the defaults for Docker connection and networks. - c.Docker = &server.DockerConfiguration{} + c.Docker = &DockerConfiguration{} c.Docker.UpdateImages = true c.Docker.Socket = "/var/run/docker.sock" c.Docker.Network.Name = "pterodactyl_nw" @@ -142,3 +182,18 @@ func ReadConfiguration(path string) (*Configuration, error) { return c, nil } + +// Ensures that the Pterodactyl core user exists on the system. This user will be the +// owner of all data in the root data directory and is used as the user within containers. +// +// If files are not owned by this user there will be issues with permissions on Docker +// mount points. +func (c *Configuration) EnsurePterodactylUser() error { + return nil +} + +// Ensures that the configured data directory has the correct permissions assigned to +// all of the files and folders within. +func (c *Configuration) EnsureFilePermissions() error { + return nil +} diff --git a/config/servers/.gitkeep b/config/servers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/config/.gitignore b/data/.gitignore similarity index 100% rename from config/.gitignore rename to data/.gitignore diff --git a/go.mod b/go.mod index 6776ac6..56c3262 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/containerd/fifo v0.0.0-20190226154929-a9fb20d87448 // indirect github.com/davecgh/go-spew v1.1.0 // indirect - github.com/docker/distribution v2.7.1+incompatible // indirect + github.com/docker/distribution v2.7.1+incompatible github.com/docker/docker v0.0.0-20180422163414-57142e89befe github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.3.3 // indirect diff --git a/server/environment_docker.go b/server/environment_docker.go index 6e7b96b..9189452 100644 --- a/server/environment_docker.go +++ b/server/environment_docker.go @@ -20,62 +20,37 @@ import ( "time" ) -// Defines the docker configuration used by the daemon when interacting with -// containers and networks on the system. -type DockerConfiguration struct { - Container struct { - User string - } - - // Network configuration that should be used when creating a new network - // for containers run through the daemon. - Network struct { - // The interface that should be used to create the network. Must not conflict - // with any other interfaces in use by Docker or on the system. - Interface string - - // The name of the network to use. If this network already exists it will not - // be created. If it is not found, a new network will be created using the interface - // defined. - Name string - } - - // If true, container images will be updated when a server starts if there - // is an update available. If false the daemon will not attempt updates and will - // defer to the host system to manage image updates. - UpdateImages bool `yaml:"update_images"` - - // The location of the Docker socket. - Socket string - - // Defines the location of the timezone file on the host system that should - // be mounted into the created containers so that they all use the same time. - TimezonePath string `yaml:"timezone_path"` -} - // Defines the base environment for Docker instances running through Wings. type DockerEnvironment struct { Server *Server + // The user that containers should be running as. + User string + // Defines the configuration for the Docker instance that will allow us to connect // and create and modify containers. - Configuration DockerConfiguration + TimezonePath string // The Docker client being used for this instance. Client *client.Client } // Creates a new base Docker environment. A server must still be attached to it. -func NewDockerEnvironment(cfg DockerConfiguration) (*DockerEnvironment, error) { +func NewDockerEnvironment(opts ...func(*DockerEnvironment)) (*DockerEnvironment, error) { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { return nil, err } - return &DockerEnvironment{ - Configuration: cfg, - Client: cli, - }, nil + env := &DockerEnvironment{ + Client: cli, + } + + for _, opt := range opts { + opt(env) + } + + return env, nil } // Ensure that the Docker environment is always implementing all of the methods @@ -182,7 +157,7 @@ func (d *DockerEnvironment) Create() error { conf := &container.Config{ Hostname: "container", - User: d.Configuration.Container.User, + User: d.User, AttachStdin: true, AttachStdout: true, AttachStderr: true, @@ -214,8 +189,8 @@ func (d *DockerEnvironment) Create() error { ReadOnly: false, }, { - Target: d.Configuration.TimezonePath, - Source: d.Configuration.TimezonePath, + Target: d.TimezonePath, + Source: d.TimezonePath, Type: mount.TypeBind, ReadOnly: true, }, diff --git a/server/server.go b/server/server.go index 335f1e0..739462b 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,7 @@ package server import ( + "github.com/pterodactyl/wings/config" "github.com/remeh/sizedwaitgroup" "go.uber.org/zap" "gopkg.in/yaml.v2" @@ -107,7 +108,7 @@ type Allocations struct { // Iterates over a given directory and loads all of the servers listed before returning // them to the calling function. -func LoadDirectory(dir string, cfg DockerConfiguration) ([]*Server, error) { +func LoadDirectory(dir string, cfg *config.SystemConfiguration) ([]*Server, error) { // We could theoretically use a standard wait group here, however doing // that introduces the potential to crash the program due to too many // open files. This wouldn't happen on a small setup, but once the daemon is @@ -162,19 +163,24 @@ func LoadDirectory(dir string, cfg DockerConfiguration) ([]*Server, error) { // Initalizes a server using a data byte array. This will be marshaled into the // given struct using a YAML marshaler. This will also configure the given environment // for a server. -func FromConfiguration(data []byte, cfg DockerConfiguration) (*Server, error) { +func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, error) { s := &Server{} if err := yaml.Unmarshal(data, s); err != nil { return nil, err } + withConfiguration := func (e *DockerEnvironment) { + e.User = cfg.User + e.TimezonePath = cfg.TimezonePath + e.Server = s + } + // 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. var env Environment - if t, err := NewDockerEnvironment(cfg); err == nil { - t.Server = s + if t, err := NewDockerEnvironment(withConfiguration); err == nil { env = t } else { return nil, err @@ -183,8 +189,7 @@ func FromConfiguration(data []byte, cfg DockerConfiguration) (*Server, error) { s.environment = env s.fs = &Filesystem{ - // @todo adjust this to be configuration provided! - Root: "/srv/daemon-data", + Root: cfg.Data, Server: s, } diff --git a/wings.go b/wings.go index 8597eab..aeb9748 100644 --- a/wings.go +++ b/wings.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/server" "go.uber.org/zap" "net/http" @@ -18,7 +19,7 @@ func main() { zap.S().Infof("using configuration file: %s", configPath) - c, err := ReadConfiguration(configPath) + c, err := config.ReadConfiguration(configPath) if err != nil { panic(err) return @@ -37,7 +38,7 @@ func main() { zap.S().Debugw("running in debug mode") } - servers, err := server.LoadDirectory("config/servers", *c.Docker) + servers, err := server.LoadDirectory("data/servers", c.System) if err != nil { zap.S().Fatalw("failed to load server configurations", zap.Error(err)) return