server

package
v0.0.0-...-ce8dd9f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2021 License: MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DaemonMessageEvent          = "daemon message"
	InstallOutputEvent          = "install output"
	InstallStartedEvent         = "install started"
	InstallCompletedEvent       = "install completed"
	ConsoleOutputEvent          = "console output"
	StatusEvent                 = "status"
	StatsEvent                  = "stats"
	BackupRestoreCompletedEvent = "backup restore completed"
	BackupCompletedEvent        = "backup completed"
	TransferLogsEvent           = "transfer logs"
	TransferStatusEvent         = "transfer status"
)

Defines all of the possible output events for a server. noinspection GoNameStartsWithPackageName

View Source
const (
	PowerActionStart     = "start"
	PowerActionStop      = "stop"
	PowerActionRestart   = "restart"
	PowerActionTerminate = "kill"
)

The power actions that can be performed for a given server. This taps into the given server environment and performs them in a way that prevents a race condition from occurring. For example, sending two "start" actions back to back will not process the second action until the first action has been completed.

This utilizes a workerpool with a limit of one worker so that all of the actions execute in a sync manner.

Variables

View Source
var (
	ErrIsRunning            = errors.New("server is running")
	ErrSuspended            = errors.New("server is currently in a suspended state")
	ErrServerIsInstalling   = errors.New("server is currently installing")
	ErrServerIsTransferring = errors.New("server is currently being transferred")
)
View Source
var ErrTooMuchConsoleData = errors.New("console is outputting too much data")

Functions

func IsServerDoesNotExistError

func IsServerDoesNotExistError(err error) bool

func IsTooFrequentCrashError

func IsTooFrequentCrashError(err error) bool

Types

type Configuration

type Configuration struct {

	// The unique identifier for the server that should be used when referencing
	// it against the Panel API (and internally). This will be used when naming
	// docker containers as well as in log output.
	Uuid string `json:"uuid"`

	// Whether or not the server is in a suspended state. Suspended servers cannot
	// be started or modified except in certain scenarios by an admin user.
	Suspended bool `json:"suspended"`

	// The command that should be used when booting up the server instance.
	Invocation string `json:"invocation"`

	// By default this is false, however if selected within the Panel while installing or re-installing a
	// server, specific installation scripts will be skipped for the server process.
	SkipEggScripts bool `default:"false" json:"skip_egg_scripts"`

	// An array of environment variables that should be passed along to the running
	// server process.
	EnvVars environment.Variables `json:"environment"`

	Allocations           environment.Allocations `json:"allocations"`
	Build                 environment.Limits      `json:"build"`
	CrashDetectionEnabled bool                    `default:"true" json:"enabled" yaml:"enabled"`
	Mounts                []Mount                 `json:"mounts"`
	Resources             ResourceUsage           `json:"resources"`
	Egg                   EggConfiguration        `json:"egg,omitempty"`

	Container struct {
		// Defines the Docker image that will be used for this server
		Image string `json:"image,omitempty"`
	} `json:"container,omitempty"`
	// contains filtered or unexported fields
}

func (*Configuration) GetUuid

func (c *Configuration) GetUuid() string

func (*Configuration) SetSuspended

func (c *Configuration) SetSuspended(s bool)

type ConsoleThrottler

type ConsoleThrottler struct {
	config.ConsoleThrottles
	// contains filtered or unexported fields
}

func (*ConsoleThrottler) Increment

func (ct *ConsoleThrottler) Increment(onTrigger func()) error

Handles output from a server's console. This code ensures that a server is not outputting an excessive amount of data to the console that could indicate a malicious or run-away process and lead to performance issues for other users.

This was much more of a problem for the NodeJS version of the daemon which struggled to handle large volumes of output. However, this code is much more performant so I generally feel a lot better about it's abilities.

However, extreme output is still somewhat of a DoS attack vector against this software since we are still logging it to the disk temporarily and will want to avoid dumping a huge amount of data all at once. These values are all configurable via the wings configuration file, however the defaults have been in the wild for almost two years at the time of this writing, so I feel quite confident in them.

This function returns an error if the server should be stopped due to violating throttle constraints and a boolean value indicating if a throttle is being violated when it is checked.

func (*ConsoleThrottler) Reset

func (ct *ConsoleThrottler) Reset()

Resets the state of the throttler.

func (*ConsoleThrottler) StartTimer

func (ct *ConsoleThrottler) StartTimer(ctx context.Context)

Starts a timer that runs in a seperate thread and will continually decrement the lines processed and number of activations, regardless of the current console message volume. All of the timers are canceled if the context passed through is canceled.

func (*ConsoleThrottler) Throttled

func (ct *ConsoleThrottler) Throttled() bool

Determines if the console is currently being throttled. Calls to this function can be used to determine if output should be funneled along to the websocket processes.

type CrashHandler

type CrashHandler struct {
	// contains filtered or unexported fields
}

func (*CrashHandler) LastCrashTime

func (cd *CrashHandler) LastCrashTime() time.Time

Returns the time of the last crash for this server instance.

func (*CrashHandler) SetLastCrash

func (cd *CrashHandler) SetLastCrash(t time.Time)

Sets the last crash time for a server.

type EggConfiguration

type EggConfiguration struct {
	// The internal UUID of the Egg on the Panel.
	ID string

	// Maintains a list of files that are blacklisted for opening/editing/downloading
	// or basically any type of access on the server by any user. This is NOT the same
	// as a per-user denylist, this is defined at the Egg level.
	FileDenylist []string `json:"file_denylist"`
}

type InstallationProcess

type InstallationProcess struct {
	Server *Server
	Script *remote.InstallationScript
	// contains filtered or unexported fields
}

func NewInstallationProcess

func NewInstallationProcess(s *Server, script *remote.InstallationScript) (*InstallationProcess, error)

Generates a new installation process struct that will be used to create containers, and otherwise perform installation commands for a server.

func (*InstallationProcess) AfterExecute

func (ip *InstallationProcess) AfterExecute(containerId string) error

Cleans up after the execution of the installation process. This grabs the logs from the process to store in the server configuration directory, and then destroys the associated installation container.

func (*InstallationProcess) BeforeExecute

func (ip *InstallationProcess) BeforeExecute() error

Runs before the container is executed. This pulls down the required docker container image as well as writes the installation script to the disk. This process is executed in an async manner, if either one fails the error is returned.

func (*InstallationProcess) Execute

func (ip *InstallationProcess) Execute() (string, error)

Executes the installation process inside a specially created docker container.

func (*InstallationProcess) GetLogPath

func (ip *InstallationProcess) GetLogPath() string

Returns the log path for the installation process.

func (*InstallationProcess) RemoveContainer

func (ip *InstallationProcess) RemoveContainer() error

Removes the installer container for the server.

func (*InstallationProcess) Run

func (ip *InstallationProcess) Run() error

Runs the installation process, this is done as in a background thread. This will configure the required environment, and then spin up the installation container.

Once the container finishes installing the results will be stored in an installation log in the server's configuration directory.

func (*InstallationProcess) StreamOutput

func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error

Streams the output of the installation process to a log file in the server configuration directory, as well as to a websocket listener so that the process can be viewed in the panel by administrators.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewEmptyManager

func NewEmptyManager(client remote.Client) *Manager

NewEmptyManager returns a new empty manager collection without actually loading any of the servers from the disk. This allows the caller to set their own servers into the collection as needed.

func NewManager

func NewManager(ctx context.Context, client remote.Client) (*Manager, error)

NewManager returns a new server manager instance. This will boot up all of the servers that are currently present on the filesystem and set them into the manager.

func (*Manager) Add

func (m *Manager) Add(s *Server)

Add adds an item to the collection store.

func (*Manager) All

func (m *Manager) All() []*Server

All returns all of the items in the collection.

func (*Manager) Client

func (m *Manager) Client() remote.Client

Client returns the HTTP client interface that allows interaction with the Panel API.

func (*Manager) Filter

func (m *Manager) Filter(filter func(match *Server) bool) []*Server

Filter returns only those items matching the filter criteria.

func (*Manager) Find

func (m *Manager) Find(filter func(match *Server) bool) *Server

Find returns a single element from the collection matching the filter. If nothing is found a nil result is returned.

func (*Manager) Get

func (m *Manager) Get(uuid string) (*Server, bool)

Get returns a single server instance and a boolean value indicating if it was found in the global collection or not.

func (*Manager) InitServer

func (m *Manager) InitServer(data remote.ServerConfigurationResponse) (*Server, error)

InitServer initializes 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 (*Manager) PersistStates

func (m *Manager) PersistStates() error

PersistStates writes the current environment states to the disk for each server. This is generally called at a specific interval defined in the root runner command to avoid hammering disk I/O when tons of server switch states at once. It is fine if this file falls slightly out of sync, it is just here to make recovering from an unexpected system reboot a little easier.

func (*Manager) Put

func (m *Manager) Put(s []*Server)

Put replaces all of the current values in the collection with the value that is passed through.

func (*Manager) ReadStates

func (m *Manager) ReadStates() (map[string]string, error)

ReadStates returns the state of the servers.

func (*Manager) Remove

func (m *Manager) Remove(filter func(match *Server) bool)

Remove removes all items from the collection that match the filter function.

type Mount

type Mount environment.Mount

To avoid confusion when working with mounts, assume that a server.Mount has not been properly cleaned up and had the paths set. An environment.Mount should only be returned with valid paths that have been checked.

type PowerAction

type PowerAction string

func (PowerAction) IsStart

func (pa PowerAction) IsStart() bool

func (PowerAction) IsValid

func (pa PowerAction) IsValid() bool

Checks if the power action being received is valid.

type ResourceUsage

type ResourceUsage struct {

	// Embed the current environment stats into this server specific resource usage struct.
	environment.Stats

	// The current server status.
	State *system.AtomicString `json:"state"`

	// The current disk space being used by the server. This value is not guaranteed to be accurate
	// at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a
	// hacky solution for now to avoid passing events all over the place.
	Disk int64 `json:"disk_bytes"`
	// contains filtered or unexported fields
}

Defines the current resource usage for a given server instance. If a server is offline you should obviously expect memory and CPU usage to be 0. However, disk will always be returned since that is not dependent on the server being running to collect that data.

func (*ResourceUsage) Reset

func (ru *ResourceUsage) Reset()

Resets the usages values to zero, used when a server is stopped to ensure we don't hold onto any values incorrectly.

type Server

type Server struct {
	// Internal mutex used to block actions that need to occur sequentially, such as
	// writing the configuration to the disk.
	sync.RWMutex

	Environment environment.ProcessEnvironment `json:"-"`
	// contains filtered or unexported fields
}

Server is the high level definition for a server instance being controlled by Wings.

func New

func New(client remote.Client) (*Server, error)

New returns a new server instance with a context and all of the default values set on the struct.

func (*Server) Backup

func (s *Server) Backup(b backup.BackupInterface) error

Backup performs a server backup and then emits the event over the server websocket. We let the actual backup system handle notifying the panel of the status, but that won't emit a websocket event.

func (*Server) Config

func (s *Server) Config() *Configuration

func (*Server) Context

func (s *Server) Context() context.Context

Returns a context instance for the server. This should be used to allow background tasks to be canceled if the server is removed. It will only be canceled when the application is stopped or if the server gets deleted.

func (*Server) CreateEnvironment

func (s *Server) CreateEnvironment() error

Initializes 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 (*Server) CtxCancel

func (s *Server) CtxCancel()

Cancels the context assigned to this server instance. Assuming background tasks are using this server's context for things, all of the background tasks will be stopped as a result.

func (*Server) DiskSpace

func (s *Server) DiskSpace() int64

Returns the amount of disk space available to a server in bytes.

func (*Server) EnsureDataDirectoryExists

func (s *Server) EnsureDataDirectoryExists() error

EnsureDataDirectoryExists ensures that the data directory for the server instance exists.

func (*Server) Events

func (s *Server) Events() *events.EventBus

Returns the server's emitter instance.

func (*Server) ExecutingPowerAction

func (s *Server) ExecutingPowerAction() bool

Check if there is currently a power action being processed for the server.

func (*Server) Filesystem

func (s *Server) Filesystem() *filesystem.Filesystem

Filesystem returns an instance of the filesystem for this server.

func (*Server) GetEnvironmentVariables

func (s *Server) GetEnvironmentVariables() []string

Returns all of the environment variables that should be assigned to a running server instance.

func (*Server) HandlePowerAction

func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error

Helper function that can receive a power action and then process the actions that need to occur for it. This guards against someone calling Start() twice at the same time, or trying to restart while another restart process is currently running.

However, the code design for the daemon does depend on the user correctly calling this function rather than making direct calls to the start/stop/restart functions on the environment struct.

func (*Server) Id

func (s *Server) Id() string

Id returns the UUID for the server instance.

func (*Server) Install

func (s *Server) Install(sync bool) error

Executes the installation stack for a server process. Bubbles any errors up to the calling function which should handle contacting the panel to notify it of the server state.

Pass true as the first argument in order to execute a server sync before the process to ensure the latest information is used.

func (*Server) IsBootable

func (s *Server) IsBootable() bool

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 (*Server) IsInstalling

func (s *Server) IsInstalling() bool

Determines if the server is actively running the installation process by checking the status of the installer lock.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning determines if the server state is running or not. This is different than the environment state, it is simply the tracked state from this daemon instance, and not the response from Docker.

func (*Server) IsSuspended

func (s *Server) IsSuspended() bool

Checks if the server is marked as being suspended or not on the system.

func (*Server) IsTransferring

func (s *Server) IsTransferring() bool

func (*Server) Log

func (s *Server) Log() *log.Entry

func (*Server) MemoryLimit

func (s *Server) MemoryLimit() int64

func (*Server) Mounts

func (s *Server) Mounts() []environment.Mount

Returns the default container mounts for the server instance. This includes the data directory for the server. Previously this would also mount in host timezone files, however we've moved from that approach to just setting `TZ=Timezone` environment values in containers which should work in most scenarios.

func (*Server) OnStateChange

func (s *Server) OnStateChange()

Sets the state of the server internally. This function handles crash detection as well as reporting to event listeners for the server.

func (*Server) Proc

func (s *Server) Proc() ResourceUsage

Returns the current resource usage stats for the server instance. This returns a copy of the tracked resources, so making any changes to the response will not have the desired outcome for you most likely.

func (*Server) ProcessConfiguration

func (s *Server) ProcessConfiguration() *remote.ProcessConfiguration

func (*Server) PublishConsoleOutputFromDaemon

func (s *Server) PublishConsoleOutputFromDaemon(data string)

Sends output to the server console formatted to appear correctly as being sent from Wings.

func (*Server) ReadLogfile

func (s *Server) ReadLogfile(len int) ([]string, error)

Reads the log file for a server up to a specified number of bytes.

func (*Server) Reinstall

func (s *Server) Reinstall() error

Reinstalls a server's software by utilizing the install script for the server egg. This does not touch any existing files for the server, other than what the script modifies.

func (*Server) RestoreBackup

func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (err error)

RestoreBackup calls the Restore function on the provided backup. Once this restoration is completed an event is emitted to the websocket to notify the Panel that is has been completed.

In addition to the websocket event an API call is triggered to notify the Panel of the new state.

func (*Server) SetTransferring

func (s *Server) SetTransferring(state bool)

func (*Server) StartEventListeners

func (s *Server) StartEventListeners()

Adds all of the internal event listeners we want to use for a server. These listeners can only be removed by deleting the server as they should last for the duration of the process' lifetime.

func (*Server) Sync

func (s *Server) Sync() error

Syncs the state of the server on the Panel with Wings. This ensures that we're always using the state of the server from the Panel and allows us to not require successful API calls to Wings to do things.

This also means mass actions can be performed against servers on the Panel and they will automatically sync with Wings when the server is started.

func (*Server) SyncInstallState

func (s *Server) SyncInstallState(successful bool) error

Makes a HTTP request to the Panel instance notifying it that the server has completed the installation process, and what the state of the server is. A boolean value of "true" means everything was successful, "false" means something went wrong and the server must be deleted and re-created.

func (*Server) SyncWithConfiguration

func (s *Server) SyncWithConfiguration(cfg remote.ServerConfigurationResponse) error

func (*Server) SyncWithEnvironment

func (s *Server) SyncWithEnvironment()

Updates the environment for the server to match any of the changed data. This pushes new settings and environment variables to the environment. In addition, the in-situ update method is called on the environment which will allow environments that make use of it (such as Docker) to immediately apply some settings without having to wait on a server to restart.

This functionality allows a server's resources limits to be modified on the fly and have them apply right away allowing for dynamic resource allocation and responses to abusive server processes.

func (*Server) Throttler

func (s *Server) Throttler() *ConsoleThrottler

Returns the throttler instance for the server or creates a new one.

func (*Server) UpdateConfigurationFiles

func (s *Server) UpdateConfigurationFiles()

Parent function that will update all of the defined configuration files for a server automatically to ensure that they always use the specified values.

func (*Server) UpdateDataStructure

func (s *Server) UpdateDataStructure(data []byte) error

UpdateDataStructure merges data passed through in JSON form into the existing server object. Any changes to the build settings will apply immediately in the environment if the environment supports it.

The server will be marked as requiring a rebuild on the next boot sequence, it is up to the specific environment to determine what needs to happen when that is the case.

func (*Server) Websockets

func (s *Server) Websockets() *WebsocketBag

Returns the websocket bag which contains all of the currently open websocket connections for the server instance.

type WebsocketBag

type WebsocketBag struct {
	// contains filtered or unexported fields
}

func (*WebsocketBag) CancelAll

func (w *WebsocketBag) CancelAll()

Cancels all of the stored cancel functions which has the effect of disconnecting every listening websocket for the server.

func (*WebsocketBag) Push

func (w *WebsocketBag) Push(u uuid.UUID, cancel *context.CancelFunc)

Adds a new websocket connection to the stack.

func (*WebsocketBag) Remove

func (w *WebsocketBag) Remove(u uuid.UUID)

Removes a connection from the stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL