Don't run pre-boot actions if the server is already running

This commit is contained in:
Dane Everitt 2020-09-11 23:11:57 -07:00
parent 8bcb3d7c62
commit 5a62f83ec8
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 12 additions and 14 deletions

View File

@ -159,9 +159,10 @@ func (h *Handler) TokenValid() error {
// error message, otherwise we just send back a standard error message. // error message, otherwise we just send back a standard error message.
func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error { func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error {
j := h.GetJwt() j := h.GetJwt()
expected := errors.Is(err, server.ErrSuspended) || errors.Is(err, server.ErrIsRunning)
message := "an unexpected error was encountered while handling this request" message := "an unexpected error was encountered while handling this request"
if server.IsSuspendedError(err) || (j != nil && j.HasPermission(PermissionReceiveErrors)) { if expected || (j != nil && j.HasPermission(PermissionReceiveErrors)) {
message = err.Error() message = err.Error()
} }
@ -171,7 +172,7 @@ func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error
wsm.Args = []string{m} wsm.Args = []string{m}
if len(shouldLog) == 0 || (len(shouldLog) == 1 && shouldLog[0] == true) { if len(shouldLog) == 0 || (len(shouldLog) == 1 && shouldLog[0] == true) {
if !server.IsSuspendedError(err) { if !expected {
h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}). h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}).
Error("failed to handle websocket process; an error was encountered processing an event") Error("failed to handle websocket process; an error was encountered processing an event")
} }

View File

@ -1,17 +1,9 @@
package server package server
type suspendedError struct { import "github.com/pkg/errors"
}
func (e *suspendedError) Error() string { var ErrIsRunning = errors.New("server is running")
return "server is currently in a suspended state" var ErrSuspended = errors.New("server is currently in a suspended state")
}
func IsSuspendedError(err error) bool {
_, ok := err.(*suspendedError)
return ok
}
type crashTooFrequent struct { type crashTooFrequent struct {
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment"
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
"os" "os"
"time" "time"
@ -87,6 +88,10 @@ func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
switch action { switch action {
case PowerActionStart: case PowerActionStart:
if s.GetState() != environment.ProcessOfflineState {
return ErrIsRunning
}
// Run the pre-boot logic for the server before processing the environment start. // Run the pre-boot logic for the server before processing the environment start.
if err := s.onBeforeStart(); err != nil { if err := s.onBeforeStart(); err != nil {
return err return err
@ -134,7 +139,7 @@ func (s *Server) onBeforeStart() error {
// Disallow start & restart if the server is suspended. Do this check after performing a sync // Disallow start & restart if the server is suspended. Do this check after performing a sync
// action with the Panel to ensure that we have the most up-to-date information for that server. // action with the Panel to ensure that we have the most up-to-date information for that server.
if s.IsSuspended() { if s.IsSuspended() {
return new(suspendedError) return ErrSuspended
} }
// Ensure we sync the server information with the environment so that any new environment variables // Ensure we sync the server information with the environment so that any new environment variables