2020-08-11 04:38:42 +00:00
|
|
|
package environment
|
2019-03-25 00:27:14 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-11 21:13:19 +00:00
|
|
|
"context"
|
2019-03-25 01:00:21 +00:00
|
|
|
"os"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
|
|
|
"github.com/pterodactyl/wings/events"
|
2019-03-25 00:27:14 +00:00
|
|
|
)
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
const (
|
2020-09-13 04:37:48 +00:00
|
|
|
ConsoleOutputEvent = "console output"
|
|
|
|
StateChangeEvent = "state change"
|
|
|
|
ResourceEvent = "resources"
|
|
|
|
DockerImagePullStarted = "docker image pull started"
|
|
|
|
DockerImagePullStatus = "docker image pull status"
|
|
|
|
DockerImagePullCompleted = "docker image pull completed"
|
2020-08-11 04:38:42 +00:00
|
|
|
)
|
|
|
|
|
2020-08-20 01:58:48 +00:00
|
|
|
const (
|
|
|
|
ProcessOfflineState = "offline"
|
|
|
|
ProcessStartingState = "starting"
|
|
|
|
ProcessRunningState = "running"
|
|
|
|
ProcessStoppingState = "stopping"
|
|
|
|
)
|
|
|
|
|
2019-03-25 00:27:14 +00:00
|
|
|
// Defines the basic interface that all environments need to implement so that
|
|
|
|
// a server can be properly controlled.
|
2020-08-11 04:38:42 +00:00
|
|
|
type ProcessEnvironment interface {
|
2019-04-06 19:27:44 +00:00
|
|
|
// Returns the name of the environment.
|
|
|
|
Type() string
|
|
|
|
|
2020-08-28 04:08:33 +00:00
|
|
|
// Returns the environment configuration to the caller.
|
|
|
|
Config() *Configuration
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// Returns an event emitter instance that can be hooked into to listen for different
|
|
|
|
// events that are fired by the environment. This should not allow someone to publish
|
|
|
|
// events, only subscribe to them.
|
|
|
|
Events() *events.EventBus
|
|
|
|
|
|
|
|
// 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, error)
|
|
|
|
|
2021-09-11 21:13:19 +00:00
|
|
|
// IsRunning determines if the environment is currently active and running
|
|
|
|
// a server process for this specific server instance.
|
|
|
|
IsRunning(ctx context.Context) (bool, error)
|
2019-04-20 23:20:08 +00:00
|
|
|
|
2019-11-24 23:08:38 +00:00
|
|
|
// Performs an update of server resource limits without actually stopping the server
|
|
|
|
// process. This only executes if the environment supports it, otherwise it is
|
|
|
|
// a no-op.
|
|
|
|
InSituUpdate() error
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
// Runs before the environment is started. If an error is returned starting will
|
|
|
|
// not occur, otherwise proceeds as normal.
|
2021-09-11 21:13:19 +00:00
|
|
|
OnBeforeStart(ctx context.Context) error
|
2019-09-23 03:47:38 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
// Starts a server instance. If the server instance is not in a state where it
|
|
|
|
// can be started an error should be returned.
|
2021-09-11 21:13:19 +00:00
|
|
|
Start(ctx context.Context) error
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
// Stops a server instance. If the server is already stopped an error should
|
|
|
|
// not be returned.
|
|
|
|
Stop() error
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2020-04-03 20:43:13 +00:00
|
|
|
// Waits for a server instance to stop gracefully. If the server is still detected
|
|
|
|
// as running after seconds, an error will be returned, or the server will be terminated
|
|
|
|
// depending on the value of the second argument.
|
2020-08-14 04:10:33 +00:00
|
|
|
WaitForStop(seconds uint, terminate bool) error
|
2020-04-03 20:43:13 +00:00
|
|
|
|
2019-03-25 01:00:21 +00:00
|
|
|
// 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
|
2019-04-04 05:01:11 +00:00
|
|
|
|
2019-12-22 07:23:56 +00:00
|
|
|
// Destroys the environment removing any containers that were created (in Docker
|
|
|
|
// environments at least).
|
|
|
|
Destroy() error
|
|
|
|
|
2019-12-01 00:43:18 +00:00
|
|
|
// Returns the exit state of the process. The first result is the exit code, the second
|
|
|
|
// determines if the process was killed by the system OOM killer.
|
|
|
|
ExitState() (uint32, bool, error)
|
|
|
|
|
2019-04-04 05:01:11 +00:00
|
|
|
// 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
|
2019-04-06 19:27:44 +00:00
|
|
|
|
2021-09-11 21:13:19 +00:00
|
|
|
// Attach attaches to the server console environment and allows piping the output
|
|
|
|
// to a websocket or other internal tool to monitor output. Also allows you to later
|
2019-04-20 23:20:08 +00:00
|
|
|
// send data into the environment's stdin.
|
2021-09-11 21:13:19 +00:00
|
|
|
Attach(ctx context.Context) error
|
2019-04-20 23:20:08 +00:00
|
|
|
|
|
|
|
// Sends the provided command to the running server instance.
|
|
|
|
SendCommand(string) error
|
2019-04-06 19:27:44 +00:00
|
|
|
|
|
|
|
// Reads the log file for the process from the end backwards until the provided
|
2020-09-07 20:04:56 +00:00
|
|
|
// number of lines is met.
|
|
|
|
Readlog(int) ([]string, error)
|
2020-11-07 05:53:00 +00:00
|
|
|
|
|
|
|
// Returns the current state of the environment.
|
|
|
|
State() string
|
|
|
|
|
|
|
|
// Sets the current state of the environment. In general you should let the environment
|
|
|
|
// handle this itself, but there are some scenarios where it is helpful for the server
|
|
|
|
// to update the state externally (e.g. starting -> started).
|
|
|
|
SetState(string)
|
2021-10-03 19:59:03 +00:00
|
|
|
|
|
|
|
// Uptime returns the current environment uptime in milliseconds. This is
|
|
|
|
// the time that has passed since it was last started.
|
|
|
|
Uptime(ctx context.Context) (int64, error)
|
2019-04-04 05:01:11 +00:00
|
|
|
}
|