Improve error handling and reporting for server installation & process boot

This commit is contained in:
Dane Everitt
2021-03-03 20:56:18 -08:00
parent 33f5cb7df4
commit 0919fb2da6
7 changed files with 38 additions and 29 deletions

View File

@@ -169,7 +169,7 @@ func (m *Manager) ReadStates() (map[string]string, error) {
func (m *Manager) InitServer(data remote.ServerConfigurationResponse) (*Server, error) {
s, err := New(m.client)
if err != nil {
return nil, errors.WithMessage(err, "loader: failed to instantiate empty server struct")
return nil, err
}
if err := s.UpdateDataStructure(data.Settings); err != nil {
return nil, err
@@ -222,7 +222,7 @@ func (m *Manager) init(ctx context.Context) error {
if !remote.IsRequestError(err) {
return errors.WithStackIf(err)
}
return errors.New(err.Error())
return errors.WrapIf(err, "manager: failed to retrieve server configurations")
}
start := time.Now()

View File

@@ -70,8 +70,8 @@ type Server struct {
wsBagLocker sync.Mutex
}
// Returns a new server instance with a context and all of the default values set on
// the instance.
// New returns a new server instance with a context and all of the default
// values set on the struct.
func New(client remote.Client) (*Server, error) {
ctx, cancel := context.WithCancel(context.Background())
s := Server{
@@ -82,16 +82,16 @@ func New(client remote.Client) (*Server, error) {
transferring: system.NewAtomicBool(false),
}
if err := defaults.Set(&s); err != nil {
return nil, err
return nil, errors.Wrap(err, "server: could not set default values for struct")
}
if err := defaults.Set(&s.cfg); err != nil {
return nil, err
return nil, errors.Wrap(err, "server: could not set defaults for server configuration")
}
s.resources.State = system.NewAtomicString(environment.ProcessOfflineState)
return &s, nil
}
// Returns the UUID for the server instance.
// Id returns the UUID for the server instance.
func (s *Server) Id() string {
return s.Config().GetUuid()
}

View File

@@ -2,6 +2,7 @@ package server
import (
"encoding/json"
"fmt"
"emperror.dev/errors"
"github.com/buger/jsonparser"
@@ -9,24 +10,25 @@ import (
"github.com/pterodactyl/wings/environment"
)
// Merges data passed through in JSON form into the existing server object.
// Any changes to the build settings will apply immediately in the environment
// if the environment supports it.
// UpdateDataStructure merges data passed through in JSON form into the existing
// server object. Any changes to the build settings will apply immediately in
// the environment if the environment supports it.
//
// The server will be marked as requiring a rebuild on the next boot sequence,
// it is up to the specific environment to determine what needs to happen when
// that is the case.
func (s *Server) UpdateDataStructure(data []byte) error {
src := new(Configuration)
fmt.Println("got data", string(data))
if err := json.Unmarshal(data, src); err != nil {
return err
return errors.Wrap(err, "server/update: could not unmarshal source data into Configuration struct")
}
// 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 != "" && s.Id() != "" && src.Uuid != s.Id() {
return errors.New("attempting to merge a data stack with an invalid UUID")
return errors.New("server/update: attempting to merge a data stack with an invalid UUID")
}
// Grab a copy of the configuration to work on.
@@ -48,7 +50,7 @@ func (s *Server) UpdateDataStructure(data []byte) error {
// Merge the new data object that we have received with the existing server data object
// and then save it to the disk so it is persistent.
if err := mergo.Merge(&c, src, mergo.WithOverride); err != nil {
return err
return errors.WithStack(err)
}
// Don't explode if we're setting CPU limits to 0. Mergo sees that as an empty value
@@ -62,7 +64,7 @@ func (s *Server) UpdateDataStructure(data []byte) error {
// request is going to be boolean. Allegedly.
if v, err := jsonparser.GetBoolean(data, "container", "oom_disabled"); err != nil {
if err != jsonparser.KeyPathNotFoundError {
return err
return errors.WithStack(err)
}
} else {
c.Build.OOMDisabled = v
@@ -71,7 +73,7 @@ func (s *Server) UpdateDataStructure(data []byte) error {
// Mergo also cannot handle this boolean value.
if v, err := jsonparser.GetBoolean(data, "suspended"); err != nil {
if err != jsonparser.KeyPathNotFoundError {
return err
return errors.WithStack(err)
}
} else {
c.Suspended = v
@@ -79,7 +81,7 @@ func (s *Server) UpdateDataStructure(data []byte) error {
if v, err := jsonparser.GetBoolean(data, "skip_egg_scripts"); err != nil {
if err != jsonparser.KeyPathNotFoundError {
return err
return errors.WithStack(err)
}
} else {
c.SkipEggScripts = v