Remove server configurations files and add shared state file
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/creasty/defaults"
|
||||
"github.com/patrickmn/go-cache"
|
||||
@@ -9,10 +10,7 @@ import (
|
||||
"github.com/pterodactyl/wings/config"
|
||||
"github.com/remeh/sizedwaitgroup"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -160,11 +158,6 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
||||
// the road to help big instances scale better.
|
||||
wg := sizedwaitgroup.New(10)
|
||||
|
||||
f, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configs, rerr, err := api.NewRequester().GetAllServerConfigurations()
|
||||
if err != nil || rerr != nil {
|
||||
if err != nil {
|
||||
@@ -174,39 +167,32 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
||||
return errors.New(rerr.String())
|
||||
}
|
||||
|
||||
states, err := FetchServerStates()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
servers = NewCollection(nil)
|
||||
|
||||
for _, file := range f {
|
||||
if !strings.HasSuffix(file.Name(), ".yml") || file.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
for uuid, data := range configs {
|
||||
wg.Add()
|
||||
|
||||
// For each of the YAML files we find, parse it and create a new server
|
||||
// configuration object that can then be returned to the caller.
|
||||
go func(file os.FileInfo) {
|
||||
go func(uuid string, data *api.ServerConfigurationResponse) {
|
||||
defer wg.Done()
|
||||
|
||||
b, err := ioutil.ReadFile(path.Join(dir, file.Name()))
|
||||
s, err := FromConfiguration(data, cfg)
|
||||
if err != nil {
|
||||
zap.S().Errorw("failed to read server configuration file, skipping...", zap.String("server", file.Name()), zap.Error(err))
|
||||
zap.S().Errorw("failed to load server, skipping...", zap.String("server", uuid), zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
s, err := FromConfiguration(b, cfg, configs[file.Name()[:len(file.Name())-4]])
|
||||
if err != nil {
|
||||
if IsServerDoesNotExistError(err) {
|
||||
zap.S().Infow("server does not exist on remote system", zap.String("server", file.Name()))
|
||||
} else {
|
||||
zap.S().Errorw("failed to parse server configuration, skipping...", zap.String("server", file.Name()), zap.Error(err))
|
||||
}
|
||||
|
||||
return
|
||||
if state, exists := states[s.Uuid]; exists {
|
||||
s.State = state
|
||||
zap.S().Debug("loaded server state from cache", zap.String("server", s.Uuid), zap.String("state", s.State))
|
||||
}
|
||||
|
||||
servers.Add(s)
|
||||
}(file)
|
||||
}(uuid, data)
|
||||
}
|
||||
|
||||
// Wait until we've processed all of the configuration files in the directory
|
||||
@@ -219,7 +205,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
||||
// 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 FromConfiguration(data []byte, cfg *config.SystemConfiguration, apiCfg *api.ServerConfigurationResponse) (*Server, error) {
|
||||
func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.SystemConfiguration) (*Server, error) {
|
||||
s := new(Server)
|
||||
|
||||
if err := defaults.Set(s); err != nil {
|
||||
@@ -228,7 +214,12 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration, apiCfg *api
|
||||
|
||||
s.Init()
|
||||
|
||||
if err := yaml.Unmarshal(data, s); err != nil {
|
||||
j, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.UpdateDataStructure(j, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -252,16 +243,9 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration, apiCfg *api
|
||||
s.Resources = ResourceUsage{}
|
||||
|
||||
// Forces the configuration to be synced with the panel.
|
||||
if apiCfg == nil {
|
||||
zap.S().Debug("syncing config with panel", zap.String("server", s.Uuid))
|
||||
if err := s.Sync(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
zap.S().Debug("syncing with local config", zap.String("server", s.Uuid))
|
||||
if err := s.SyncWithConfiguration(apiCfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zap.S().Debug("syncing config with panel", zap.String("server", s.Uuid))
|
||||
if err := s.SyncWithConfiguration(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -371,11 +355,15 @@ func (s *Server) SetState(state string) error {
|
||||
//
|
||||
// We also get the benefit of server status changes always propagating corrected configurations
|
||||
// to the disk should we forget to do it elsewhere.
|
||||
go func(server *Server) {
|
||||
if _, err := server.WriteConfigurationToDisk(); err != nil {
|
||||
go func() {
|
||||
/*if _, err := server.WriteConfigurationToDisk(); err != nil {
|
||||
zap.S().Warnw("failed to write server state change to disk", zap.String("server", server.Uuid), zap.Error(err))
|
||||
}*/
|
||||
|
||||
if err := SaveServerStates(); err != nil {
|
||||
zap.S().Warnw("failed to write server states to disk", zap.Error(err))
|
||||
}
|
||||
}(s)
|
||||
}()
|
||||
|
||||
zap.S().Debugw("saw server status change event", zap.String("server", s.Uuid), zap.String("status", s.State))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user