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:
parent
4ea1b90560
commit
1f4aca8210
|
@ -96,9 +96,9 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
os.Exit(1)
|
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)))
|
zap.S().Errorw("failed to save configuration to disk", zap.Error(errors.WithStack(err)))
|
||||||
}*/
|
}
|
||||||
|
|
||||||
// Just for some nice log output.
|
// Just for some nice log output.
|
||||||
for _, s := range server.GetServers().All() {
|
for _, s := range server.GetServers().All() {
|
||||||
|
|
|
@ -298,8 +298,7 @@ func (c *Configuration) setSystemUser(u *user.User) error {
|
||||||
c.System.User.Uid = uid
|
c.System.User.Uid = uid
|
||||||
c.System.User.Gid = gid
|
c.System.User.Gid = gid
|
||||||
|
|
||||||
// return c.WriteToDisk()
|
return c.WriteToDisk()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensures that the configured data directory has the correct permissions assigned to
|
// 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
|
// lock on the file. This prevents something else from writing at the exact same time and
|
||||||
// leading to bad data conditions.
|
// leading to bad data conditions.
|
||||||
func (c *Configuration) WriteToDisk() error {
|
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
|
ccopy := *c
|
||||||
// If debugging is set with the flag, don't save that to the configuration file, otherwise
|
// If debugging is set with the flag, don't save that to the configuration file, otherwise
|
||||||
// you'll always end up in debug mode.
|
// you'll always end up in debug mode.
|
||||||
|
@ -376,7 +369,7 @@ func (c *Configuration) WriteToDisk() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.Write(b); err != nil {
|
if err := ioutil.WriteFile("config.yml", b, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
|
@ -188,7 +187,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
||||||
|
|
||||||
if state, exists := states[s.Uuid]; exists {
|
if state, exists := states[s.Uuid]; exists {
|
||||||
s.State = state
|
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)
|
servers.Add(s)
|
||||||
|
@ -214,12 +213,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
|
||||||
|
|
||||||
s.Init()
|
s.Init()
|
||||||
|
|
||||||
j, err := json.Marshal(data)
|
if err := s.UpdateDataStructure(data.Settings, false); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.UpdateDataStructure(j, false); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,7 +237,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
|
||||||
s.Resources = ResourceUsage{}
|
s.Resources = ResourceUsage{}
|
||||||
|
|
||||||
// Forces the configuration to be synced with the panel.
|
// 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 {
|
if err := s.SyncWithConfiguration(data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -54,7 +55,7 @@ func FetchServerStates() (map[string]string, error) {
|
||||||
|
|
||||||
// Convert the json object to a map.
|
// Convert the json object to a map.
|
||||||
states := map[string]string{}
|
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)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,36 +76,13 @@ func SaveServerStates() error {
|
||||||
return errors.WithStack(err)
|
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()
|
statesLock.Lock()
|
||||||
defer statesLock.Unlock()
|
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
|
// 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)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the file basically.
|
return nil
|
||||||
return f.Sync()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// 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
|
// 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.
|
// 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")
|
return errors.New("attempting to merge a data stack with an invalid UUID")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user