From 92a7c9d2310b1612654f345e52a5bb0e06c43ada Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 16 Dec 2019 20:47:35 -0800 Subject: [PATCH] Don't freak out if the server doesn't exist on the remote --- api/server_endpoints.go | 11 +++++------ server/environment_docker.go | 12 +++++++----- server/errors.go | 13 +++++++++++++ server/server.go | 19 ++++++++++++++++--- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/api/server_endpoints.go b/api/server_endpoints.go index d463906..aa00592 100644 --- a/api/server_endpoints.go +++ b/api/server_endpoints.go @@ -3,7 +3,6 @@ package api import ( "encoding/json" "fmt" - "github.com/pkg/errors" "github.com/pterodactyl/wings/parser" ) @@ -29,25 +28,25 @@ type ServerConfiguration struct { } // Fetches the server configuration and returns the struct for it. -func (r *PanelRequest) GetServerConfiguration(uuid string) (*ServerConfiguration, error) { +func (r *PanelRequest) GetServerConfiguration(uuid string) (*ServerConfiguration, *RequestError, error) { resp, err := r.Get(fmt.Sprintf("/servers/%s/configuration", uuid)) if err != nil { - return nil, err + return nil, nil, err } defer resp.Body.Close() r.Response = resp if r.HasError() { - return nil, errors.WithStack(errors.New(r.Error().String())) + return nil, r.Error(), nil } res := &ServerConfiguration{} b, _ := r.ReadBody() if err := json.Unmarshal(b, res); err != nil { - return nil, err + return nil, nil, err } - return res, nil + return res, nil, nil } diff --git a/server/environment_docker.go b/server/environment_docker.go index 1a1c55f..00d673a 100644 --- a/server/environment_docker.go +++ b/server/environment_docker.go @@ -151,9 +151,11 @@ func (d *DockerEnvironment) InSituUpdate() error { // state. This ensures that unexpected container deletion while Wings is running does // not result in the server becoming unbootable. func (d *DockerEnvironment) OnBeforeStart() error { - c, err := d.Server.GetProcessConfiguration() + c, rerr, err := d.Server.GetProcessConfiguration() if err != nil { - return errors.WithStack(err) + return err + } else if rerr != nil { + return errors.New(rerr.String()) } d.Server.processConfiguration = c @@ -164,7 +166,7 @@ func (d *DockerEnvironment) OnBeforeStart() error { if d.Server.Container.RebuildRequired { if err := d.Client.ContainerRemove(context.Background(), d.Server.Uuid, types.ContainerRemoveOptions{RemoveVolumes: true}); err != nil { if !client.IsErrNotFound(err) { - return errors.WithStack(err) + return err } } @@ -181,7 +183,7 @@ func (d *DockerEnvironment) OnBeforeStart() error { zap.S().Warnw( "failed to write server configuration to disk after setting rebuild_required=false in configuration", zap.String("server", serv.Uuid), - zap.Error(err), + zap.Error(errors.WithStack(err)), ) } }(d.Server) @@ -195,7 +197,7 @@ func (d *DockerEnvironment) OnBeforeStart() error { // environment gets created properly if it is missing and the server is started. We're making // an assumption that all of the files will still exist at this point. if err := d.Create(); err != nil { - return errors.WithStack(err) + return err } return nil diff --git a/server/errors.go b/server/errors.go index 4579808..b880fb5 100644 --- a/server/errors.go +++ b/server/errors.go @@ -25,3 +25,16 @@ func IsTooFrequentCrashError(err error) bool { return ok } + +type serverDoesNotExist struct { +} + +func (e *serverDoesNotExist) Error() string { + return "server does not exist on remote system" +} + +func IsServerDoesNotExistError(err error) bool { + _, ok := err.(*serverDoesNotExist) + + return ok +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index 53c4edd..f78a626 100644 --- a/server/server.go +++ b/server/server.go @@ -179,7 +179,12 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error { s, err := FromConfiguration(b, cfg) if err != nil { - zap.S().Errorw("failed to parse server configuration, skipping...", zap.String("server", file.Name()), zap.Error(err)) + 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 } @@ -241,8 +246,16 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, e // This is also done when the server is booted, however we need to account for instances // where the server is already running and the Daemon reboots. In those cases this will // allow us to you know, stop servers. - if cfg, err := s.GetProcessConfiguration(); err != nil { + if cfg, rerr, err := s.GetProcessConfiguration(); err != nil { return nil, err + } else if rerr != nil { + // If the response error is because a server does not exist on the Panel return + // that so that we can handle that in the future. + if rerr.Status == "404" { + return nil, &serverDoesNotExist{} + } + + return nil, errors.New(rerr.String()) } else { s.processConfiguration = cfg } @@ -331,6 +344,6 @@ func (s *Server) SetState(state string) error { } // Gets the process configuration data for the server. -func (s *Server) GetProcessConfiguration() (*api.ServerConfiguration, error) { +func (s *Server) GetProcessConfiguration() (*api.ServerConfiguration, *api.RequestError, error) { return api.NewRequester().GetServerConfiguration(s.Uuid) }