server

package
v1.11.13 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActivityConsoleCommand      = models.Event("server:console.command")
	ActivitySftpWrite           = models.Event("server:sftp.write")
	ActivitySftpCreate          = models.Event("server:sftp.create")
	ActivitySftpCreateDirectory = models.Event("server:sftp.create-directory")
	ActivitySftpRename          = models.Event("server:sftp.rename")
	ActivitySftpDelete          = models.Event("server:sftp.delete")
	ActivityFileUploaded        = models.Event("server:file.uploaded")
)
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"
	DeletedEvent                = "deleted"
)

Defines all the possible output events for a server.

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 the actions execute in a sync manner.

View Source
const ActivityPowerPrefix = "server:power."

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")
	ErrServerIsRestoring    = errors.New("server is currently being restored")
)

Functions

func IsServerDoesNotExistError

func IsServerDoesNotExistError(err error) bool

func IsTooFrequentCrashError

func IsTooFrequentCrashError(err error) bool

Types

type APIResponse

type APIResponse struct {
	State         string        `json:"state"`
	IsSuspended   bool          `json:"is_suspended"`
	Utilization   ResourceUsage `json:"utilization"`
	Configuration Configuration `json:"configuration"`
}

APIResponse is a type returned when requesting details about a single server instance on Wings. This includes the information needed by the Panel in order to show resource utilization and the current state on this system.

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"`

	Meta ConfigurationMeta `json:"meta"`

	// 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 `json:"skip_egg_scripts"`

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

	// Labels is a map of container labels that should be applied to the running server process.
	Labels map[string]string `json:"labels"`

	Allocations           environment.Allocations `json:"allocations"`
	Build                 environment.Limits      `json:"build"`
	CrashDetectionEnabled bool                    `json:"crash_detection_enabled"`
	Mounts                []Mount                 `json:"mounts"`
	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 ConfigurationMeta

type ConfigurationMeta struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type ConsoleThrottle

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

func (*ConsoleThrottle) Allow

func (ct *ConsoleThrottle) Allow() bool

Allow checks if the console is allowed to process more output data, or if too much has already been sent over the line. If there is too much output the strike callback function is triggered, but only if it has not already been triggered at this point in the process.

If output is allowed, the lock on the throttler is released and the next time it is triggered the strike function will be re-executed.

func (*ConsoleThrottle) Reset

func (ct *ConsoleThrottle) Reset()

Reset resets the console throttler internal rate limiter and overage counter.

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 `json:"id"`

	// 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)

NewInstallationProcess returns 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

AfterExecute 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

BeforeExecute 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)

Execute executes the installation process inside a specially created docker container.

func (*InstallationProcess) GetLogPath

func (ip *InstallationProcess) GetLogPath() string

GetLogPath returns the log path for the installation process.

func (*InstallationProcess) RemoveContainer

func (ip *InstallationProcess) RemoveContainer() error

RemoveContainer removes the installation container for the server.

func (*InstallationProcess) Run

func (ip *InstallationProcess) Run() error

Run 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 are stored in an installation log in the server's configuration directory.

func (*InstallationProcess) StreamOutput

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

StreamOutput 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 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 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) Keys

func (m *Manager) Keys() []string

Keys returns all of the server UUIDs stored in the manager set.

func (*Manager) Len

func (m *Manager) Len() int

Len returns the count of servers stored in the manager instance.

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 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

IsValid checks if the power action being received is valid.

type RequestActivity

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

RequestActivity is a wrapper around a LoggedEvent that is able to track additional request specific metadata including the specific user and IP address associated with all subsequent events. The internal logged event structure can be extracted by calling RequestEvent.Event().

func (RequestActivity) Event

func (ra RequestActivity) Event(event models.Event, metadata models.ActivityMeta) *models.Activity

Event returns the underlying logged event from the RequestEvent instance and sets the specific event and metadata on it.

func (RequestActivity) SetUser

func (ra RequestActivity) SetUser(u string) RequestActivity

SetUser clones the RequestActivity struct and sets a new user value on the copy before returning it.

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
}

ResourceUsage 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()

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

func (*ResourceUsage) UpdateStats

func (ru *ResourceUsage) UpdateStats(stats environment.Stats)

UpdateStats updates the current stats for the server's resource usage.

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) CleanupForDestroy

func (s *Server) CleanupForDestroy()

CleanupForDestroy stops all running background tasks for this server that are using the context on the server struct. This will cancel any running install processes for the server as well.

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) DestroyAllSinks

func (s *Server) DestroyAllSinks()

DestroyAllSinks iterates over all of the sinks configured for the server and destroys their instances. Note that this will cause a panic if you attempt to call Server.Sink() again after. This function is only used when a server is being deleted from the system.

func (*Server) DiskSpace

func (s *Server) DiskSpace() int64

DiskSpace 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.Bus

Events returns the server's emitter instance.

func (*Server) ExecutingPowerAction

func (s *Server) ExecutingPowerAction() bool

ExecutingPowerAction checks 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

HandlePowerAction is a 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) Id

func (s *Server) Id() string

Id returns the UUID for the server instance. This function is deprecated in favor of Server.ID().

Deprecated

func (*Server) Install

func (s *Server) Install() error

Install 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) IsInstalling

func (s *Server) IsInstalling() bool

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

func (*Server) IsRestoring

func (s *Server) IsRestoring() bool

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning determines if the server state is running or not. This is different from 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) NewRequestActivity

func (s *Server) NewRequestActivity(user string, ip string) RequestActivity

func (*Server) OnStateChange

func (s *Server) OnStateChange()

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

Proc 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)

PublishConsoleOutputFromDaemon 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

Reinstall reinstalls a server's software by utilizing the installation 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) SaveActivity

func (s *Server) SaveActivity(a RequestActivity, event models.Event, metadata models.ActivityMeta)

SaveActivity saves an activity entry to the database in a background routine. If an error is encountered it is logged but not returned to the caller.

func (*Server) SetRestoring

func (s *Server) SetRestoring(state bool)

func (*Server) SetTransferring

func (s *Server) SetTransferring(state bool)

func (*Server) Sink

func (s *Server) Sink(name system.SinkName) *system.SinkPool

Sink returns the instantiated and named sink for a server. If the sink has not been configured yet this function will cause a panic condition.

func (*Server) StartEventListeners

func (s *Server) StartEventListeners()

StartEventListeners adds all 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

Sync 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, reinstall bool) error

SyncInstallState makes an HTTP request to the Panel instance notifying it that the server has completed the installation process, and what the state of the server is.

func (*Server) SyncWithConfiguration

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

SyncWithConfiguration accepts a configuration object for a server and will sync all of the values with the existing server state. This only replaces the existing configuration and process configuration for the server. The underlying environment will not be affected. This is because this function can be called from scoped where the server may not be fully initialized, therefore other things like the filesystem and environment may not exist yet.

func (*Server) SyncWithEnvironment

func (s *Server) SyncWithEnvironment()

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() *ConsoleThrottle

Throttler returns the throttler instance for the server or creates a new one.

func (*Server) ToAPIResponse

func (s *Server) ToAPIResponse() APIResponse

ToAPIResponse returns the server struct as an API object that can be consumed by callers.

func (*Server) UpdateConfigurationFiles

func (s *Server) UpdateConfigurationFiles()

UpdateConfigurationFiles updates all the defined configuration files for a server automatically to ensure that they always use the specified values.

func (*Server) Websockets

func (s *Server) Websockets() *WebsocketBag

Websockets returns the websocket bag which contains all 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()

CancelAll cancels all 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)

Push adds a new websocket connection to the end of the stack.

func (*WebsocketBag) Remove

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

Remove removes a connection from the stack.

Directories

Path Synopsis
Package transfer handles all logic related to transferring servers between two nodes.
Package transfer handles all logic related to transferring servers between two nodes.

Jump to

Keyboard shortcuts

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