Get basic environment creation beginning to work
This commit is contained in:
31
server/environment.go
Normal file
31
server/environment.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Defines the basic interface that all environments need to implement so that
|
||||
// a server can be properly controlled.
|
||||
type Environment interface {
|
||||
// Starts a server instance. If the server instance is not in a state where it
|
||||
// can be started an error should be returned.
|
||||
Start() error
|
||||
|
||||
// Stops a server instance. If the server is already stopped an error should
|
||||
// not be returned.
|
||||
Stop() error
|
||||
|
||||
// Determines if the server instance exists. For example, in a docker environment
|
||||
// this should confirm that the container is created and in a bootable state. In
|
||||
// a basic CLI environment this can probably just return true right away.
|
||||
Exists() bool
|
||||
|
||||
// Terminates a running server instance using the provided signal. If the server
|
||||
// is not running no error should be returned.
|
||||
Terminate(signal os.Signal) error
|
||||
|
||||
// Creates the necessary environment for running the server process. For example,
|
||||
// in the Docker environment create will create a new container instance for the
|
||||
// server.
|
||||
Create() error
|
||||
}
|
||||
132
server/environment_docker.go
Normal file
132
server/environment_docker.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"golang.org/x/net/context"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
type DockerEnvironment struct {
|
||||
Server *Server
|
||||
|
||||
// Defines the configuration for the Docker instance that will allow us to connect
|
||||
// and create and modify containers.
|
||||
Configuration DockerConfiguration
|
||||
}
|
||||
|
||||
// Ensure that the Docker environment is always implementing all of the methods
|
||||
// from the base environment interface.
|
||||
var _ Environment = (*DockerEnvironment)(nil)
|
||||
|
||||
func (d *DockerEnvironment) Exists() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *DockerEnvironment) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DockerEnvironment) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DockerEnvironment) Terminate(signal os.Signal) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Creates a new container for the server using all of the data that is currently
|
||||
// available for it. If the container already exists it will be returned.
|
||||
func (d *DockerEnvironment) Create() error {
|
||||
ctx := context.Background()
|
||||
cli, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If the container already exists don't hit the user with an error, just return
|
||||
// the current information about it which is what we would do when creating the
|
||||
// container anyways.
|
||||
if _, err := cli.ContainerInspect(ctx, d.Server.Uuid); err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
conf := &container.Config{
|
||||
Hostname: "container",
|
||||
User: "pterodactyl",
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Tty: true,
|
||||
|
||||
Cmd: strings.Split(d.Server.Invocation, " "),
|
||||
Image: d.Server.Container.Image,
|
||||
Env: d.environmentVariables(),
|
||||
|
||||
Labels: map[string]string{
|
||||
"Service": "Pterodactyl",
|
||||
},
|
||||
}
|
||||
|
||||
hostConf := &container.HostConfig{
|
||||
Resources: container.Resources{
|
||||
Memory: d.Server.Build.MemoryLimit * 1000000,
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the environment variables for a server in KEY="VALUE" form.
|
||||
func (d *DockerEnvironment) environmentVariables() []string {
|
||||
var out []string
|
||||
|
||||
for k, v := range d.Server.EnvVars {
|
||||
out = append(out, fmt.Sprintf("%s=\"%s\"", k, v))
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (d *DockerEnvironment) volumes() map[string]struct{} {
|
||||
return nil
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"github.com/remeh/sizedwaitgroup"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v2"
|
||||
@@ -35,7 +34,12 @@ type Server struct {
|
||||
Build *BuildSettings
|
||||
Allocations *Allocations
|
||||
|
||||
environment *environment.Environment
|
||||
Container struct {
|
||||
// Defines the Docker image that will be used for this server
|
||||
Image string
|
||||
}
|
||||
|
||||
environment Environment
|
||||
}
|
||||
|
||||
// The build settings for a given server that impact docker container creation and
|
||||
@@ -43,22 +47,22 @@ type Server struct {
|
||||
type BuildSettings struct {
|
||||
// The total amount of memory in megabytes that this server is allowed to
|
||||
// use on the host system.
|
||||
MemoryLimit int `yaml:"memory"`
|
||||
MemoryLimit int64 `yaml:"memory"`
|
||||
|
||||
// The amount of additional swap space to be provided to a container instance.
|
||||
Swap int
|
||||
Swap int64
|
||||
|
||||
// The relative weight for IO operations in a container. This is relative to other
|
||||
// containers on the system and should be a value between 10 and 1000.
|
||||
IoWeight int `yaml:"io"`
|
||||
IoWeight int64 `yaml:"io"`
|
||||
|
||||
// The percentage of CPU that this instance is allowed to consume relative to
|
||||
// the host. A value of 200% represents complete utilization of two cores. This
|
||||
// should be a value between 1 and THREAD_COUNT * 100.
|
||||
CpuLimit int `yaml:"cpu"`
|
||||
CpuLimit int64 `yaml:"cpu"`
|
||||
|
||||
// The amount of disk space in megabytes that a server is allowed to use.
|
||||
DiskSpace int `yaml:"disk"`
|
||||
DiskSpace int64 `yaml:"disk"`
|
||||
}
|
||||
|
||||
// Defines the allocations available for a given server. When using the Docker environment
|
||||
@@ -79,7 +83,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 environment.DockerConfiguration) ([]*Server, error) {
|
||||
func LoadDirectory(dir string, cfg DockerConfiguration) ([]*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
|
||||
@@ -134,7 +138,7 @@ func LoadDirectory(dir string, cfg environment.DockerConfiguration) ([]*Server,
|
||||
// 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 environment.DockerConfiguration) (*Server, error) {
|
||||
func FromConfiguration(data []byte, cfg DockerConfiguration) (*Server, error) {
|
||||
s := &Server{}
|
||||
|
||||
if err := yaml.Unmarshal(data, s); err != nil {
|
||||
@@ -144,12 +148,25 @@ func FromConfiguration(data []byte, cfg environment.DockerConfiguration) (*Serve
|
||||
// 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.Environment
|
||||
env = &environment.Docker{
|
||||
var env Environment
|
||||
env = &DockerEnvironment{
|
||||
Server: s,
|
||||
Configuration: cfg,
|
||||
}
|
||||
|
||||
s.environment = &env
|
||||
s.environment = env
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Determine if the server is bootable in it's current state or not. This will not
|
||||
// indicate why a server is not bootable, only if it is.
|
||||
func (s *Server) IsBootable() bool {
|
||||
return s.environment.Exists()
|
||||
}
|
||||
|
||||
// Initalizes a server instance. This will run through and ensure that the environment
|
||||
// for the server is setup, and that all of the necessary files are created.
|
||||
func (s *Server) CreateEnvironment() error {
|
||||
return s.environment.Create()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user