2017-07-30 18:05:06 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-10-01 18:42:17 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-07-30 18:05:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-02-20 20:25:31 +00:00
|
|
|
"github.com/pterodactyl/wings/constants"
|
2017-10-01 18:42:17 +00:00
|
|
|
|
2017-07-30 18:05:06 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
2018-02-20 20:25:31 +00:00
|
|
|
"github.com/pterodactyl/wings/api/websockets"
|
2017-07-30 18:05:06 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dockerEnvironment struct {
|
|
|
|
baseEnvironment
|
|
|
|
|
|
|
|
client *docker.Client
|
|
|
|
container *docker.Container
|
|
|
|
context context.Context
|
|
|
|
|
2018-02-20 22:38:29 +00:00
|
|
|
attached bool
|
2017-10-01 18:42:17 +00:00
|
|
|
containerInput io.Writer
|
|
|
|
containerOutput io.Writer
|
|
|
|
closeWaiter docker.CloseWaiter
|
|
|
|
|
2017-08-31 22:01:32 +00:00
|
|
|
server *ServerStruct
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure DockerEnvironment implements Environment
|
|
|
|
var _ Environment = &dockerEnvironment{}
|
|
|
|
|
|
|
|
// NewDockerEnvironment creates a new docker enviornment
|
|
|
|
// instance and connects to the docker client on the host system
|
|
|
|
// If the container is already running it will try to reattach
|
|
|
|
// to the running container
|
2017-08-31 22:01:32 +00:00
|
|
|
func NewDockerEnvironment(server *ServerStruct) (Environment, error) {
|
2017-07-30 18:05:06 +00:00
|
|
|
env := dockerEnvironment{}
|
|
|
|
|
|
|
|
env.server = server
|
|
|
|
|
|
|
|
client, err := docker.NewClient("unix:///var/run/docker.sock")
|
|
|
|
env.client = client
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("Failed to connect to docker.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.server.DockerContainer.ID != "" {
|
2017-10-01 18:42:17 +00:00
|
|
|
if err := env.checkContainerExists(); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to find the container with stored id, removing id.")
|
|
|
|
env.server.DockerContainer.ID = ""
|
|
|
|
env.server.Save()
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &env, nil
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
func (env *dockerEnvironment) checkContainerExists() error {
|
2017-07-30 18:05:06 +00:00
|
|
|
container, err := env.client.InspectContainer(env.server.DockerContainer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
env.container = container
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
func (env *dockerEnvironment) attach() error {
|
2018-02-20 22:38:29 +00:00
|
|
|
if env.attached {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-01 18:42:17 +00:00
|
|
|
pr, pw := io.Pipe()
|
2018-02-20 22:38:29 +00:00
|
|
|
env.containerInput = pw
|
|
|
|
|
|
|
|
cw := websockets.ConsoleWriter{
|
|
|
|
Hub: env.server.websockets,
|
|
|
|
}
|
2017-10-01 18:42:17 +00:00
|
|
|
|
|
|
|
success := make(chan struct{})
|
|
|
|
w, err := env.client.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
|
|
|
|
Container: env.server.DockerContainer.ID,
|
|
|
|
InputStream: pr,
|
2018-02-20 22:38:29 +00:00
|
|
|
OutputStream: cw,
|
|
|
|
ErrorStream: cw,
|
2017-10-01 18:42:17 +00:00
|
|
|
Stdin: true,
|
|
|
|
Stdout: true,
|
2018-02-20 22:38:29 +00:00
|
|
|
Stderr: true,
|
2017-10-01 18:42:17 +00:00
|
|
|
Stream: true,
|
|
|
|
Success: success,
|
|
|
|
})
|
|
|
|
env.closeWaiter = w
|
|
|
|
|
|
|
|
<-success
|
|
|
|
close(success)
|
2018-02-20 22:38:29 +00:00
|
|
|
env.attached = true
|
2017-10-01 18:42:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-30 18:05:06 +00:00
|
|
|
// Create creates the docker container for the environment and applies all
|
|
|
|
// settings to it
|
|
|
|
func (env *dockerEnvironment) Create() error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Creating docker environment")
|
2018-02-20 22:38:29 +00:00
|
|
|
// Split image repository and tag
|
|
|
|
imageParts := strings.Split(env.server.GetService().DockerImage, ":")
|
2017-07-30 18:05:06 +00:00
|
|
|
imageRepoParts := strings.Split(imageParts[0], "/")
|
|
|
|
if len(imageRepoParts) >= 3 {
|
2018-02-20 22:38:29 +00:00
|
|
|
// TODO: Handle possibly required authentication
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull docker image
|
|
|
|
var pullImageOpts = docker.PullImageOptions{
|
|
|
|
Repository: imageParts[0],
|
|
|
|
}
|
|
|
|
if len(imageParts) >= 2 {
|
|
|
|
pullImageOpts.Tag = imageParts[1]
|
|
|
|
}
|
2018-02-20 22:38:29 +00:00
|
|
|
log.WithField("image", env.server.GetService().DockerImage).Debug("Pulling docker image")
|
2017-07-30 18:05:06 +00:00
|
|
|
err := env.client.PullImage(pullImageOpts, docker.AuthConfiguration{})
|
|
|
|
if err != nil {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithError(err).WithField("server", env.server.ID).Error("Failed to create docker environment")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(env.server.dataPath(), constants.DefaultFolderPerms); err != nil {
|
2017-07-30 18:05:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create docker container
|
|
|
|
// TODO: apply cpu, io, disk limits.
|
|
|
|
containerConfig := &docker.Config{
|
2018-02-20 22:38:29 +00:00
|
|
|
Image: env.server.GetService().DockerImage,
|
|
|
|
Cmd: strings.Split(env.server.StartupCommand, " "),
|
|
|
|
OpenStdin: true,
|
|
|
|
ArgsEscaped: false,
|
|
|
|
Hostname: constants.DockerContainerPrefix + env.server.UUIDShort(),
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|
|
|
|
containerHostConfig := &docker.HostConfig{
|
|
|
|
Memory: env.server.Settings.Memory,
|
|
|
|
MemorySwap: env.server.Settings.Swap,
|
2018-02-20 22:38:29 +00:00
|
|
|
// TODO: Allow custom binds via some kind of settings in the service
|
|
|
|
Binds: []string{env.server.dataPath() + ":/home/container"},
|
|
|
|
// TODO: Add port bindings
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|
|
|
|
createContainerOpts := docker.CreateContainerOptions{
|
2018-02-20 22:38:29 +00:00
|
|
|
Name: constants.DockerContainerPrefix + env.server.UUIDShort(),
|
2017-07-30 18:05:06 +00:00
|
|
|
Config: containerConfig,
|
|
|
|
HostConfig: containerHostConfig,
|
|
|
|
Context: env.context,
|
|
|
|
}
|
|
|
|
container, err := env.client.CreateContainer(createContainerOpts)
|
|
|
|
if err != nil {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithError(err).WithField("server", env.server.ID).Error("Failed to create docker container")
|
2017-07-30 18:05:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-20 22:38:29 +00:00
|
|
|
|
2017-07-30 18:05:06 +00:00
|
|
|
env.server.DockerContainer.ID = container.ID
|
2017-10-01 18:42:17 +00:00
|
|
|
env.server.Save()
|
2017-07-30 18:05:06 +00:00
|
|
|
env.container = container
|
2018-02-20 22:38:29 +00:00
|
|
|
if env.closeWaiter != nil {
|
|
|
|
env.closeWaiter.Close()
|
|
|
|
}
|
|
|
|
env.attached = false
|
2017-07-30 18:05:06 +00:00
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Docker environment created")
|
2017-07-30 18:05:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy removes the environment's docker container
|
|
|
|
func (env *dockerEnvironment) Destroy() error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Destroying docker environment")
|
|
|
|
if _, err := env.client.InspectContainer(env.server.DockerContainer.ID); err != nil {
|
|
|
|
if _, ok := err.(*docker.NoSuchContainer); ok {
|
|
|
|
log.WithField("server", env.server.ID).Debug("Container not found, docker environment destroyed already.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.WithError(err).WithField("server", env.server.ID).Error("Could not destroy docker environment")
|
|
|
|
return err
|
|
|
|
}
|
2017-07-30 18:05:06 +00:00
|
|
|
err := env.client.RemoveContainer(docker.RemoveContainerOptions{
|
|
|
|
ID: env.server.DockerContainer.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithError(err).WithField("server", env.server.ID).Error("Failed to destroy docker environment")
|
2017-07-30 18:05:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-01 18:42:17 +00:00
|
|
|
|
2018-02-20 22:38:29 +00:00
|
|
|
if env.closeWaiter != nil {
|
|
|
|
env.closeWaiter.Close()
|
|
|
|
}
|
|
|
|
env.attached = false
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Docker environment destroyed")
|
2017-07-30 18:05:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:42:17 +00:00
|
|
|
func (env *dockerEnvironment) Exists() bool {
|
|
|
|
if env.container != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
env.checkContainerExists()
|
|
|
|
return env.container != nil
|
|
|
|
}
|
|
|
|
|
2017-07-30 18:05:06 +00:00
|
|
|
// Start starts the environment's docker container
|
|
|
|
func (env *dockerEnvironment) Start() error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Starting service in docker environment")
|
|
|
|
if err := env.attach(); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to attach to docker container")
|
|
|
|
}
|
2017-07-30 18:05:06 +00:00
|
|
|
if err := env.client.StartContainer(env.container.ID, nil); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to start docker container")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the environment's docker container
|
|
|
|
func (env *dockerEnvironment) Stop() error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Stopping service in docker environment")
|
2017-07-30 18:05:06 +00:00
|
|
|
if err := env.client.StopContainer(env.container.ID, 20000); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to stop docker container")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *dockerEnvironment) Kill() error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.WithField("server", env.server.ID).Debug("Killing service in docker environment")
|
2017-07-30 18:05:06 +00:00
|
|
|
if err := env.client.KillContainer(docker.KillContainerOptions{
|
|
|
|
ID: env.container.ID,
|
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to kill docker container")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec sends commands to the standard input of the docker container
|
|
|
|
func (env *dockerEnvironment) Exec(command string) error {
|
2017-10-01 18:42:17 +00:00
|
|
|
log.Debug("Command: " + command)
|
|
|
|
_, err := env.containerInput.Write([]byte(command + "\n"))
|
|
|
|
return err
|
2017-07-30 18:05:06 +00:00
|
|
|
}
|