2019-03-25 00:27:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-06-23 04:38:16 +00:00
|
|
|
"context"
|
2019-04-21 19:02:28 +00:00
|
|
|
"fmt"
|
2020-05-29 05:07:53 +00:00
|
|
|
"github.com/apex/log"
|
2019-11-17 01:01:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-09-23 03:47:38 +00:00
|
|
|
"github.com/pterodactyl/wings/api"
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/environment/docker"
|
|
|
|
"github.com/pterodactyl/wings/events"
|
2020-09-27 19:24:08 +00:00
|
|
|
"github.com/pterodactyl/wings/server/filesystem"
|
2020-06-23 04:38:16 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2019-03-25 01:00:21 +00:00
|
|
|
"strings"
|
2019-11-24 23:08:38 +00:00
|
|
|
"sync"
|
2019-04-06 23:53:22 +00:00
|
|
|
"time"
|
2019-03-25 00:27:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// High level definition for a server instance being controlled by Wings.
|
|
|
|
type Server struct {
|
2020-07-19 23:27:55 +00:00
|
|
|
// Internal mutex used to block actions that need to occur sequentially, such as
|
|
|
|
// writing the configuration to the disk.
|
|
|
|
sync.RWMutex
|
2020-08-11 04:38:42 +00:00
|
|
|
emitterLock sync.Mutex
|
|
|
|
powerLock *semaphore.Weighted
|
2020-09-18 03:13:04 +00:00
|
|
|
throttleLock sync.Mutex
|
2020-07-19 23:27:55 +00:00
|
|
|
|
|
|
|
// Maintains the configuration for the server. This is the data that gets returned by the Panel
|
|
|
|
// such as build settings and container images.
|
|
|
|
cfg Configuration
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// The crash handler for this server instance.
|
|
|
|
crasher CrashHandler
|
2020-05-21 20:53:00 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
resources ResourceUsage
|
2020-08-11 04:38:42 +00:00
|
|
|
Archiver Archiver `json:"-"`
|
|
|
|
Environment environment.ProcessEnvironment `json:"-"`
|
2020-09-27 19:24:08 +00:00
|
|
|
|
|
|
|
fs *filesystem.Filesystem
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2020-01-18 22:04:26 +00:00
|
|
|
// Events emitted by the server instance.
|
2020-08-11 04:38:42 +00:00
|
|
|
emitter *events.EventBus
|
2019-09-23 03:47:38 +00:00
|
|
|
|
|
|
|
// Defines the process configuration for the server instance. This is dynamically
|
|
|
|
// fetched from the Pterodactyl Server instance each time the server process is
|
|
|
|
// started, and then cached here.
|
2020-07-18 23:03:25 +00:00
|
|
|
procConfig *api.ProcessConfiguration
|
2019-11-24 23:08:38 +00:00
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
// Tracks the installation process for this server and prevents a server from running
|
|
|
|
// two installer processes at the same time. This also allows us to cancel a running
|
|
|
|
// installation process, for example when a server is deleted from the panel while the
|
|
|
|
// installer process is still running.
|
|
|
|
installer InstallerDetails
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
// The console throttler instance used to control outputs.
|
|
|
|
throttler *ConsoleThrottler
|
2020-10-04 03:46:29 +00:00
|
|
|
|
|
|
|
// Tracks open websocket connections for the server.
|
|
|
|
wsBag *WebsocketBag
|
|
|
|
wsBagLocker sync.Mutex
|
2019-03-25 00:27:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
type InstallerDetails struct {
|
|
|
|
// The cancel function for the installer. This will be a non-nil value while there
|
|
|
|
// is an installer running for the server.
|
|
|
|
cancel *context.CancelFunc
|
|
|
|
|
|
|
|
// Installer lock. You should obtain an exclusive lock on this context while running
|
|
|
|
// the installation process and release it when finished.
|
|
|
|
sem *semaphore.Weighted
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Returns the UUID for the server instance.
|
2020-07-18 23:03:25 +00:00
|
|
|
func (s *Server) Id() string {
|
2020-07-20 00:46:39 +00:00
|
|
|
return s.Config().GetUuid()
|
2019-12-22 21:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
// Returns all of the environment variables that should be assigned to a running
|
|
|
|
// server instance.
|
|
|
|
func (s *Server) GetEnvironmentVariables() []string {
|
|
|
|
zone, _ := time.Now().In(time.Local).Zone()
|
|
|
|
|
|
|
|
var out = []string{
|
|
|
|
fmt.Sprintf("TZ=%s", zone),
|
2020-07-19 23:27:55 +00:00
|
|
|
fmt.Sprintf("STARTUP=%s", s.Config().Invocation),
|
2020-08-28 04:08:33 +00:00
|
|
|
fmt.Sprintf("SERVER_MEMORY=%d", s.MemoryLimit()),
|
2020-09-07 22:37:35 +00:00
|
|
|
fmt.Sprintf("SERVER_IP=%s", s.Config().Allocations.DefaultMapping.Ip),
|
2020-07-19 23:27:55 +00:00
|
|
|
fmt.Sprintf("SERVER_PORT=%d", s.Config().Allocations.DefaultMapping.Port),
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eloop:
|
2020-07-19 23:27:55 +00:00
|
|
|
for k := range s.Config().EnvVars {
|
2019-12-28 22:57:19 +00:00
|
|
|
for _, e := range out {
|
|
|
|
if strings.HasPrefix(e, strings.ToUpper(k)) {
|
|
|
|
continue eloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s=%s", strings.ToUpper(k), s.Config().EnvVars.Get(k)))
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:07:53 +00:00
|
|
|
func (s *Server) Log() *log.Entry {
|
2020-07-20 00:53:41 +00:00
|
|
|
return log.WithField("server", s.Id())
|
2020-05-29 05:07:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
// 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 (s *Server) Sync() error {
|
|
|
|
cfg, rerr, err := s.GetProcessConfiguration()
|
|
|
|
if err != nil || rerr != nil {
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 04:47:35 +00:00
|
|
|
if rerr.Status == "404" {
|
2019-12-22 21:21:21 +00:00
|
|
|
return &serverDoesNotExist{}
|
2019-12-17 04:47:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
return errors.New(rerr.String())
|
2019-09-23 04:22:16 +00:00
|
|
|
}
|
2019-09-23 03:47:38 +00:00
|
|
|
|
2020-04-10 21:39:07 +00:00
|
|
|
return s.SyncWithConfiguration(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SyncWithConfiguration(cfg *api.ServerConfigurationResponse) error {
|
2019-12-22 21:21:21 +00:00
|
|
|
// Update the data structure and persist it to the disk.
|
2020-08-28 04:08:33 +00:00
|
|
|
if err := s.UpdateDataStructure(cfg.Settings); err != nil {
|
2019-12-22 21:21:21 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:03:25 +00:00
|
|
|
s.Lock()
|
|
|
|
s.procConfig = cfg.ProcessConfiguration
|
|
|
|
s.Unlock()
|
|
|
|
|
2020-09-27 19:24:08 +00:00
|
|
|
// Update the disk space limits for the server whenever the configuration
|
|
|
|
// for it changes.
|
|
|
|
s.fs.SetDiskLimit(s.DiskSpace())
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// If this is a Docker environment we need to sync the stop configuration with it so that
|
|
|
|
// the process isn't just terminated when a user requests it be stopped.
|
|
|
|
if e, ok := s.Environment.(*docker.Environment); ok {
|
|
|
|
s.Log().Debug("syncing stop configuration with configured docker environment")
|
2020-09-18 03:20:39 +00:00
|
|
|
e.SetImage(s.Config().Container.Image)
|
2020-08-11 04:38:42 +00:00
|
|
|
e.SetStopConfiguration(&cfg.ProcessConfiguration.Stop)
|
|
|
|
}
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
return nil
|
2019-03-25 00:27:14 +00:00
|
|
|
}
|
2019-04-04 05:01:11 +00:00
|
|
|
|
2019-04-06 19:27:44 +00:00
|
|
|
// Reads the log file for a server up to a specified number of bytes.
|
2020-09-07 20:04:56 +00:00
|
|
|
func (s *Server) ReadLogfile(len int) ([]string, error) {
|
2019-04-20 23:26:55 +00:00
|
|
|
return s.Environment.Readlog(len)
|
2019-04-06 23:53:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 05:01:11 +00:00
|
|
|
// 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 (s *Server) IsBootable() bool {
|
2019-04-20 23:26:55 +00:00
|
|
|
exists, _ := s.Environment.Exists()
|
2019-04-20 23:20:08 +00:00
|
|
|
|
|
|
|
return exists
|
2019-04-04 05:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 19:08:40 +00:00
|
|
|
// Initializes a server instance. This will run through and ensure that the environment
|
2019-04-04 05:01:11 +00:00
|
|
|
// for the server is setup, and that all of the necessary files are created.
|
|
|
|
func (s *Server) CreateEnvironment() error {
|
2020-08-11 04:38:42 +00:00
|
|
|
// Ensure the data directory exists before getting too far through this process.
|
2020-09-27 19:24:08 +00:00
|
|
|
if err := s.EnsureDataDirectoryExists(); err != nil {
|
2020-08-11 04:38:42 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2019-04-20 23:26:55 +00:00
|
|
|
return s.Environment.Create()
|
2019-04-21 19:02:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
// Gets the process configuration data for the server.
|
2019-12-22 21:21:21 +00:00
|
|
|
func (s *Server) GetProcessConfiguration() (*api.ServerConfigurationResponse, *api.RequestError, error) {
|
2020-07-20 00:53:41 +00:00
|
|
|
return api.NewRequester().GetServerConfiguration(s.Id())
|
2019-11-24 23:08:38 +00:00
|
|
|
}
|
2020-04-06 01:00:33 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Checks if the server is marked as being suspended or not on the system.
|
|
|
|
func (s *Server) IsSuspended() bool {
|
|
|
|
return s.Config().Suspended
|
|
|
|
}
|
2020-08-11 04:38:42 +00:00
|
|
|
|
2020-08-20 02:08:15 +00:00
|
|
|
func (s *Server) ProcessConfiguration() *api.ProcessConfiguration {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.procConfig
|
2020-09-07 20:04:56 +00:00
|
|
|
}
|