Fix issue with config file saving, fix issue with state file saving, fix issue when merging a uuid into a server without one

This commit is contained in:
Matthew Penner
2020-04-10 17:55:36 -06:00
parent 4ea1b90560
commit 1f4aca8210
5 changed files with 12 additions and 47 deletions

View File

@@ -1,7 +1,6 @@
package server
import (
"encoding/json"
"fmt"
"github.com/creasty/defaults"
"github.com/patrickmn/go-cache"
@@ -188,7 +187,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
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))
zap.S().Debugw("loaded server state from cache", zap.String("server", s.Uuid), zap.String("state", s.State))
}
servers.Add(s)
@@ -214,12 +213,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
s.Init()
j, err := json.Marshal(data)
if err != nil {
return nil, err
}
if err := s.UpdateDataStructure(j, false); err != nil {
if err := s.UpdateDataStructure(data.Settings, false); err != nil {
return nil, err
}
@@ -243,7 +237,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
s.Resources = ResourceUsage{}
// Forces the configuration to be synced with the panel.
zap.S().Debug("syncing config with panel", zap.String("server", s.Uuid))
zap.S().Debugw("syncing config with panel", zap.String("server", s.Uuid))
if err := s.SyncWithConfiguration(data); err != nil {
return nil, err
}

View File

@@ -3,6 +3,7 @@ package server
import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"os"
"sync"
)
@@ -54,7 +55,7 @@ func FetchServerStates() (map[string]string, error) {
// Convert the json object to a map.
states := map[string]string{}
if err := json.NewDecoder(f).Decode(states); err != nil {
if err := json.NewDecoder(f).Decode(&states); err != nil {
return nil, errors.WithStack(err)
}
@@ -75,36 +76,13 @@ func SaveServerStates() error {
return errors.WithStack(err)
}
// Check if the states file exists.
exists, err := DoesStatesFileExist()
if err != nil {
return errors.WithStack(err)
}
// Request a lock after we check if the file exists.
statesLock.Lock()
defer statesLock.Unlock()
// 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 {
if err := ioutil.WriteFile(statesFile, data, 0644); err != nil {
return errors.WithStack(err)
}
// Save the file basically.
return f.Sync()
return nil
}

View File

@@ -24,7 +24,7 @@ func (s *Server) UpdateDataStructure(data []byte, background bool) error {
// Don't allow obviously corrupted data to pass through into this function. If the UUID
// doesn't match something has gone wrong and the API is attempting to meld this server
// instance into a totally different one, which would be bad.
if src.Uuid != "" && src.Uuid != s.Uuid {
if src.Uuid != "" && s.Uuid != "" && src.Uuid != s.Uuid {
return errors.New("attempting to merge a data stack with an invalid UUID")
}