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))
|
||||
|
||||
|
||||
95
server/state.go
Normal file
95
server/state.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var statesFile = "data/states.json"
|
||||
|
||||
// DoesStatesFileExist .
|
||||
func DoesStatesFileExist() (bool, error) {
|
||||
if _, err := os.Stat(statesFile); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return false, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// FetchServerStates .
|
||||
func FetchServerStates() (map[string]string, error) {
|
||||
// Check if the states file exists.
|
||||
exists, err := DoesStatesFileExist()
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Return an empty map if the file does not exist.
|
||||
if !exists {
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
|
||||
// Open the states file.
|
||||
f, err := os.Open(statesFile)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Convert the json object to a map.
|
||||
states := map[string]string{}
|
||||
if err := json.NewDecoder(f).Decode(states); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return states, nil
|
||||
}
|
||||
|
||||
// SaveServerStates .
|
||||
func SaveServerStates() error {
|
||||
// Get the states of all servers on the daemon.
|
||||
states := map[string]string{}
|
||||
for _, s := range GetServers().All() {
|
||||
states[s.Uuid] = s.State
|
||||
}
|
||||
|
||||
// Convert the map to a json object.
|
||||
data, err := json.Marshal(states)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Check if the states file exists.
|
||||
exists, err := DoesStatesFileExist()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Create the file if it doesn't exist or open it if it already does.
|
||||
var f *os.File
|
||||
if !exists {
|
||||
f, err = os.Create(statesFile)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
} else {
|
||||
f, err = os.Open(statesFile)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Write the data to the file
|
||||
if _, err := f.Write(data); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Save the file basically.
|
||||
return f.Sync()
|
||||
}
|
||||
@@ -69,9 +69,9 @@ func (s *Server) UpdateDataStructure(data []byte, background bool) error {
|
||||
s.Allocations.Mappings = src.Allocations.Mappings
|
||||
}
|
||||
|
||||
if _, err := s.WriteConfigurationToDisk(); err != nil {
|
||||
/*if _, err := s.WriteConfigurationToDisk(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
}*/
|
||||
|
||||
if background {
|
||||
s.runBackgroundActions()
|
||||
|
||||
Reference in New Issue
Block a user