2020-08-11 04:38:42 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-19 05:27:00 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2021-01-07 04:36:29 +00:00
|
|
|
"github.com/apex/log"
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/events"
|
2021-02-02 05:28:46 +00:00
|
|
|
"github.com/pterodactyl/wings/remote"
|
2020-11-07 05:14:29 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
2020-08-11 04:38:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Metadata struct {
|
2020-09-05 19:08:40 +00:00
|
|
|
Image string
|
2021-02-02 05:28:46 +00:00
|
|
|
Stop remote.ProcessStopConfiguration
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Ensure that the Docker environment is always implementing all the methods
|
2020-08-11 04:38:42 +00:00
|
|
|
// from the base environment interface.
|
|
|
|
var _ environment.ProcessEnvironment = (*Environment)(nil)
|
|
|
|
|
|
|
|
type Environment struct {
|
2022-02-23 22:01:03 +00:00
|
|
|
mu sync.RWMutex
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
// The public identifier for this environment. In this case it is the Docker container
|
|
|
|
// name that will be used for all instances created under it.
|
|
|
|
Id string
|
|
|
|
|
|
|
|
// The environment configuration.
|
|
|
|
Configuration *environment.Configuration
|
|
|
|
|
|
|
|
meta *Metadata
|
|
|
|
|
|
|
|
// The Docker client being used for this instance.
|
|
|
|
client *client.Client
|
|
|
|
|
|
|
|
// Controls the hijacked response stream which exists only when we're attached to
|
|
|
|
// the running container instance.
|
|
|
|
stream *types.HijackedResponse
|
|
|
|
|
|
|
|
// Holds the stats stream used by the polling commands so that we can easily close it out.
|
|
|
|
stats io.ReadCloser
|
|
|
|
|
2022-01-18 03:23:29 +00:00
|
|
|
emitter *events.Bus
|
|
|
|
|
|
|
|
logCallbackMx sync.Mutex
|
|
|
|
logCallback func([]byte)
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
// Tracks the environment state.
|
2020-12-26 01:04:18 +00:00
|
|
|
st *system.AtomicString
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 04:56:18 +00:00
|
|
|
// New creates a new base Docker environment. The ID passed through will be the
|
|
|
|
// ID that is used to reference the container from here on out. This should be
|
|
|
|
// unique per-server (we use the UUID by default). The container does not need
|
|
|
|
// to exist at this point.
|
2020-08-11 04:38:42 +00:00
|
|
|
func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) {
|
2021-01-15 04:19:28 +00:00
|
|
|
cli, err := environment.Docker()
|
2020-08-11 04:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &Environment{
|
|
|
|
Id: id,
|
|
|
|
Configuration: c,
|
|
|
|
meta: m,
|
|
|
|
client: cli,
|
2020-11-07 06:22:33 +00:00
|
|
|
st: system.NewAtomicString(environment.ProcessOfflineState),
|
2022-01-30 17:56:25 +00:00
|
|
|
emitter: events.NewBus(),
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:29 +00:00
|
|
|
func (e *Environment) log() *log.Entry {
|
|
|
|
return log.WithField("environment", e.Type()).WithField("container_id", e.Id)
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) Type() string {
|
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// SetStream sets the current stream value from the Docker client. If a nil
|
|
|
|
// value is provided we assume that the stream is no longer operational and the
|
|
|
|
// instance is effectively offline.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) SetStream(s *types.HijackedResponse) {
|
|
|
|
e.mu.Lock()
|
|
|
|
e.stream = s
|
2022-01-30 17:56:25 +00:00
|
|
|
e.mu.Unlock()
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// IsAttached determine if the this process is currently attached to the
|
|
|
|
// container instance by checking if the stream is nil or not.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) IsAttached() bool {
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
return e.stream != nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// Events returns an event bus for the environment.
|
2022-01-18 03:23:29 +00:00
|
|
|
func (e *Environment) Events() *events.Bus {
|
2020-08-11 04:38:42 +00:00
|
|
|
return e.emitter
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// Exists determines if the container exists in this environment. The ID passed
|
|
|
|
// through should be the server UUID since containers are created utilizing the
|
|
|
|
// server UUID as the name and docker will work fine when using the container
|
|
|
|
// name as the lookup parameter in addition to the longer ID auto-assigned when
|
|
|
|
// the container is created.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) Exists() (bool, error) {
|
2022-01-23 22:13:49 +00:00
|
|
|
_, err := e.ContainerInspect(context.Background())
|
2020-08-11 04:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
// If this error is because the container instance wasn't found via Docker we
|
|
|
|
// can safely ignore the error and just return false.
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-09-11 21:13:19 +00:00
|
|
|
// IsRunning determines if the server's docker container is currently running.
|
|
|
|
// If there is no container present, an error will be raised (since this
|
|
|
|
// shouldn't be a case that ever happens under correctly developed
|
|
|
|
// circumstances).
|
2020-08-11 04:38:42 +00:00
|
|
|
//
|
2021-09-11 21:13:19 +00:00
|
|
|
// You can confirm if the instance wasn't found by using client.IsErrNotFound
|
|
|
|
// from the Docker API.
|
2020-08-11 04:38:42 +00:00
|
|
|
//
|
|
|
|
// @see docker/client/errors.go
|
2021-09-11 21:13:19 +00:00
|
|
|
func (e *Environment) IsRunning(ctx context.Context) (bool, error) {
|
2022-01-23 22:13:49 +00:00
|
|
|
c, err := e.ContainerInspect(ctx)
|
2020-08-11 04:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return c.State.Running, nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// ExitState returns the container exit state, the exit code and whether or not
|
2020-08-11 04:38:42 +00:00
|
|
|
// the container was killed by the OOM killer.
|
|
|
|
func (e *Environment) ExitState() (uint32, bool, error) {
|
2022-01-23 22:13:49 +00:00
|
|
|
c, err := e.ContainerInspect(context.Background())
|
2020-08-11 04:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
// I'm not entirely sure how this can happen to be honest. I tried deleting a
|
|
|
|
// container _while_ a server was running and wings gracefully saw the crash and
|
|
|
|
// created a new container for it.
|
|
|
|
//
|
|
|
|
// However, someone reported an error in Discord about this scenario happening,
|
|
|
|
// so I guess this should prevent it? They didn't tell me how they caused it though
|
2020-09-05 19:08:40 +00:00
|
|
|
// so that's a mystery that will have to go unsolved.
|
2020-08-11 04:38:42 +00:00
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2003
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return 1, false, nil
|
|
|
|
}
|
2020-11-28 23:57:10 +00:00
|
|
|
return 0, false, err
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
return uint32(c.State.ExitCode), c.State.OOMKilled, nil
|
|
|
|
}
|
2020-08-28 04:08:33 +00:00
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// Config returns the environment configuration allowing a process to make
|
|
|
|
// modifications of the environment on the fly.
|
2020-08-28 04:08:33 +00:00
|
|
|
func (e *Environment) Config() *environment.Configuration {
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
|
|
|
return e.Configuration
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:56:25 +00:00
|
|
|
// SetStopConfiguration sets the stop configuration for the environment.
|
2021-02-02 05:28:46 +00:00
|
|
|
func (e *Environment) SetStopConfiguration(c remote.ProcessStopConfiguration) {
|
2020-08-28 04:08:33 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
e.meta.Stop = c
|
2022-01-30 17:56:25 +00:00
|
|
|
e.mu.Unlock()
|
2020-09-05 19:08:40 +00:00
|
|
|
}
|
2020-09-18 03:20:39 +00:00
|
|
|
|
|
|
|
func (e *Environment) SetImage(i string) {
|
|
|
|
e.mu.Lock()
|
2021-01-08 15:15:19 +00:00
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
2020-09-18 03:20:39 +00:00
|
|
|
e.meta.Image = i
|
|
|
|
}
|
2021-01-19 05:27:00 +00:00
|
|
|
|
|
|
|
func (e *Environment) State() string {
|
|
|
|
return e.st.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetState sets the state of the environment. This emits an event that server's
|
|
|
|
// can hook into to take their own actions and track their own state based on
|
|
|
|
// the environment.
|
|
|
|
func (e *Environment) SetState(state string) {
|
|
|
|
if state != environment.ProcessOfflineState &&
|
|
|
|
state != environment.ProcessStartingState &&
|
|
|
|
state != environment.ProcessRunningState &&
|
|
|
|
state != environment.ProcessStoppingState {
|
|
|
|
panic(errors.New(fmt.Sprintf("invalid server state received: %s", state)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the event to any listeners that are currently registered.
|
|
|
|
if e.State() != state {
|
|
|
|
// If the state changed make sure we update the internal tracking to note that.
|
|
|
|
e.st.Store(state)
|
|
|
|
e.Events().Publish(environment.StateChangeEvent, state)
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 03:23:29 +00:00
|
|
|
|
|
|
|
func (e *Environment) SetLogCallback(f func([]byte)) {
|
|
|
|
e.logCallbackMx.Lock()
|
|
|
|
defer e.logCallbackMx.Unlock()
|
|
|
|
|
|
|
|
e.logCallback = f
|
|
|
|
}
|