2020-07-19 23:27:55 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-08-20 02:20:46 +00:00
|
|
|
"fmt"
|
2020-07-19 23:27:55 +00:00
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/creasty/defaults"
|
2020-08-01 04:40:43 +00:00
|
|
|
"github.com/gammazero/workerpool"
|
2020-07-19 23:27:55 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pterodactyl/wings/api"
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/environment/docker"
|
2020-07-31 23:01:02 +00:00
|
|
|
"os"
|
2020-08-01 04:40:43 +00:00
|
|
|
"runtime"
|
2020-07-19 23:27:55 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var servers = NewCollection(nil)
|
|
|
|
|
|
|
|
func GetServers() *Collection {
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterates over a given directory and loads all of the servers listed before returning
|
|
|
|
// them to the calling function.
|
|
|
|
func LoadDirectory() error {
|
|
|
|
if len(servers.items) != 0 {
|
|
|
|
return errors.New("cannot call LoadDirectory with a non-nil collection")
|
|
|
|
}
|
|
|
|
|
2020-08-20 02:20:46 +00:00
|
|
|
log.Info("fetching list of servers from API")
|
2020-07-19 23:27:55 +00:00
|
|
|
configs, rerr, err := api.NewRequester().GetAllServerConfigurations()
|
|
|
|
if err != nil || rerr != nil {
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(rerr.String())
|
|
|
|
}
|
|
|
|
|
2020-07-20 00:09:38 +00:00
|
|
|
log.Debug("retrieving cached server states from disk")
|
2020-07-19 23:27:55 +00:00
|
|
|
states, err := getServerStates()
|
|
|
|
if err != nil {
|
2020-08-20 02:20:46 +00:00
|
|
|
log.WithField("error", errors.WithStack(err)).Error("failed to retrieve locally cached server states from disk, assuming all servers in offline state")
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 02:20:46 +00:00
|
|
|
start := time.Now()
|
|
|
|
log.WithField("total_configs", len(configs)).Info("processing servers returned by the API")
|
2020-07-19 23:27:55 +00:00
|
|
|
|
2020-08-01 04:40:43 +00:00
|
|
|
pool := workerpool.New(runtime.NumCPU())
|
|
|
|
for uuid, data := range configs {
|
|
|
|
uuid := uuid
|
|
|
|
data := data
|
2020-07-19 23:27:55 +00:00
|
|
|
|
2020-08-01 04:40:43 +00:00
|
|
|
pool.Submit(func() {
|
2020-08-20 02:20:46 +00:00
|
|
|
log.WithField("server", uuid).Info("creating new server object from API response")
|
2020-07-20 00:26:53 +00:00
|
|
|
s, err := FromConfiguration(data)
|
2020-07-19 23:27:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithField("server", uuid).WithField("error", err).Error("failed to load server, skipping...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if state, exists := states[s.Id()]; exists {
|
2020-08-20 02:20:46 +00:00
|
|
|
s.Log().WithField("state", state).Debug("found existing server state in cache file; re-instantiating server state")
|
2020-07-19 23:27:55 +00:00
|
|
|
s.SetState(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
servers.Add(s)
|
2020-08-01 04:40:43 +00:00
|
|
|
})
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until we've processed all of the configuration files in the directory
|
|
|
|
// before continuing.
|
2020-08-01 04:40:43 +00:00
|
|
|
pool.StopWait()
|
2020-07-19 23:27:55 +00:00
|
|
|
|
2020-08-20 02:20:46 +00:00
|
|
|
diff := time.Now().Sub(start)
|
|
|
|
log.WithField("duration", fmt.Sprintf("%s", diff)).Info("finished processing server configurations")
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-07-20 00:26:53 +00:00
|
|
|
func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) {
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg := Configuration{}
|
|
|
|
if err := defaults.Set(&cfg); err != nil {
|
2020-08-20 02:08:15 +00:00
|
|
|
return nil, errors.WithMessage(err, "failed to set struct defaults for server configuration")
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := new(Server)
|
2020-08-20 02:08:15 +00:00
|
|
|
if err := defaults.Set(s); err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to set struct defaults for server")
|
|
|
|
}
|
2020-07-19 23:27:55 +00:00
|
|
|
|
2020-08-20 02:08:15 +00:00
|
|
|
s.cfg = cfg
|
2020-07-19 23:27:55 +00:00
|
|
|
if err := s.UpdateDataStructure(data.Settings, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
s.Archiver = Archiver{Server: s}
|
|
|
|
s.Filesystem = Filesystem{Server: s}
|
2020-07-19 23:27:55 +00:00
|
|
|
|
|
|
|
// Right now we only support a Docker based environment, so I'm going to hard code
|
|
|
|
// this logic in. When we're ready to support other environment we'll need to make
|
|
|
|
// some modifications here obviously.
|
2020-08-11 04:38:42 +00:00
|
|
|
envCfg := environment.NewConfiguration(s.Mounts(), s.cfg.Allocations, s.cfg.Build, s.cfg.EnvVars)
|
|
|
|
meta := docker.Metadata{
|
|
|
|
Invocation: s.Config().Invocation,
|
|
|
|
Image: s.Config().Container.Image,
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
if env, err := docker.New(s.Id(), &meta, envCfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
s.Environment = env
|
|
|
|
s.StartEventListeners()
|
2020-07-31 23:01:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Forces the configuration to be synced with the panel.
|
2020-07-20 00:26:53 +00:00
|
|
|
if err := s.SyncWithConfiguration(data); err != nil {
|
|
|
|
return nil, err
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 03:13:01 +00:00
|
|
|
// If the server's data directory exists, force disk usage calculation.
|
|
|
|
if _, err := os.Stat(s.Filesystem.Path()); err == nil {
|
|
|
|
go s.Filesystem.HasSpaceAvailable()
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
return s, nil
|
|
|
|
}
|