Don't freak out if the server doesn't exist on the remote

This commit is contained in:
Dane Everitt 2019-12-16 20:47:35 -08:00
parent 514c16ccc8
commit 92a7c9d231
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
4 changed files with 41 additions and 14 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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)
}