From 1f4aca8210b3b9797a29e6f6cd20625538e875d6 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Fri, 10 Apr 2020 17:55:36 -0600 Subject: [PATCH] Fix issue with config file saving, fix issue with state file saving, fix issue when merging a uuid into a server without one --- cmd/root.go | 4 ++-- config/config.go | 11 ++--------- server/server.go | 12 +++--------- server/state.go | 30 ++++-------------------------- server/update.go | 2 +- 5 files changed, 12 insertions(+), 47 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 81f0adc..14afe6a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -96,9 +96,9 @@ func rootCmdRun(*cobra.Command, []string) { os.Exit(1) } - /*if err := c.WriteToDisk(); err != nil { + if err := c.WriteToDisk(); err != nil { zap.S().Errorw("failed to save configuration to disk", zap.Error(errors.WithStack(err))) - }*/ + } // Just for some nice log output. for _, s := range server.GetServers().All() { diff --git a/config/config.go b/config/config.go index 0177672..c0c613e 100644 --- a/config/config.go +++ b/config/config.go @@ -298,8 +298,7 @@ func (c *Configuration) setSystemUser(u *user.User) error { c.System.User.Uid = uid c.System.User.Gid = gid - // return c.WriteToDisk() - return nil + return c.WriteToDisk() } // Ensures that the configured data directory has the correct permissions assigned to @@ -358,12 +357,6 @@ func (c *Configuration) EnsureFilePermissions() error { // lock on the file. This prevents something else from writing at the exact same time and // leading to bad data conditions. func (c *Configuration) WriteToDisk() error { - f, err := os.OpenFile("config.yml", os.O_WRONLY, os.ModeExclusive) - if err != nil { - return err - } - defer f.Close() - ccopy := *c // If debugging is set with the flag, don't save that to the configuration file, otherwise // you'll always end up in debug mode. @@ -376,7 +369,7 @@ func (c *Configuration) WriteToDisk() error { return err } - if _, err := f.Write(b); err != nil { + if err := ioutil.WriteFile("config.yml", b, 0644); err != nil { return err } diff --git a/server/server.go b/server/server.go index bf9508d..567fd11 100644 --- a/server/server.go +++ b/server/server.go @@ -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 } diff --git a/server/state.go b/server/state.go index f1c569b..6369827 100644 --- a/server/state.go +++ b/server/state.go @@ -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 } diff --git a/server/update.go b/server/update.go index d2b36ae..f852d64 100644 --- a/server/update.go +++ b/server/update.go @@ -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") }