Use environment namespace for power state, don't duplicate them across server

This commit is contained in:
Dane Everitt
2020-08-19 18:58:48 -07:00
parent b9fb922e91
commit 4d3a860604
13 changed files with 51 additions and 55 deletions

View File

@@ -13,7 +13,6 @@ import (
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/system"
"io"
"strconv"
"strings"
@@ -54,7 +53,7 @@ func (e *Environment) Attach() error {
defer cancel()
defer e.stream.Close()
defer func() {
e.setState(system.ProcessOfflineState)
e.setState(environment.ProcessOfflineState)
e.SetStream(nil)
}()
@@ -230,7 +229,7 @@ func (e *Environment) convertMounts() []mount.Mount {
// it will be forcibly stopped by Docker.
func (e *Environment) Destroy() error {
// We set it to stopping than offline to prevent crash detection from being triggeree.
e.setState(system.ProcessStoppingState)
e.setState(environment.ProcessStoppingState)
err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{
RemoveVolumes: true,
@@ -246,7 +245,7 @@ func (e *Environment) Destroy() error {
return nil
}
e.setState(system.ProcessOfflineState)
e.setState(environment.ProcessOfflineState)
return err
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/api"
"github.com/pterodactyl/wings/system"
"github.com/pterodactyl/wings/environment"
"os"
"strings"
"time"
@@ -57,8 +57,8 @@ func (e *Environment) Start() error {
// If we don't set it to stopping first, you'll trigger crash detection which
// we don't want to do at this point since it'll just immediately try to do the
// exact same action that lead to it crashing in the first place...
e.setState(system.ProcessStoppingState)
e.setState(system.ProcessOfflineState)
e.setState(environment.ProcessStoppingState)
e.setState(environment.ProcessOfflineState)
}
}()
@@ -74,7 +74,7 @@ func (e *Environment) Start() error {
} else {
// If the server is running update our internal state and continue on with the attach.
if c.State.Running {
e.setState(system.ProcessRunningState)
e.setState(environment.ProcessRunningState)
return e.Attach()
}
@@ -89,7 +89,7 @@ func (e *Environment) Start() error {
}
}
e.setState(system.ProcessStartingState)
e.setState(environment.ProcessStartingState)
// Set this to true for now, we will set it to false once we reach the
// end of this chain.
@@ -136,8 +136,8 @@ func (e *Environment) Stop() error {
// If the process is already offline don't switch it back to stopping. Just leave it how
// it is and continue through to the stop handling for the process.
if e.State() != system.ProcessOfflineState {
e.setState(system.ProcessStoppingState)
if e.State() != environment.ProcessOfflineState {
e.setState(environment.ProcessStoppingState)
}
// Only attempt to send the stop command to the instance if we are actually attached to
@@ -153,7 +153,7 @@ func (e *Environment) Stop() error {
// an error.
if client.IsErrNotFound(err) {
e.SetStream(nil)
e.setState(system.ProcessOfflineState)
e.setState(environment.ProcessOfflineState)
return nil
}
@@ -209,16 +209,16 @@ func (e *Environment) Terminate(signal os.Signal) error {
// If the container is not running but we're not already in a stopped state go ahead
// and update things to indicate we should be completely stopped now. Set to stopping
// first so crash detection is not triggered.
if e.State() != system.ProcessOfflineState {
e.setState(system.ProcessStoppingState)
e.setState(system.ProcessOfflineState)
if e.State() != environment.ProcessOfflineState {
e.setState(environment.ProcessStoppingState)
e.setState(environment.ProcessOfflineState)
}
return nil
}
// We set it to stopping than offline to prevent crash detection from being triggeree.
e.setState(system.ProcessStoppingState)
e.setState(environment.ProcessStoppingState)
sig := strings.TrimSuffix(strings.TrimPrefix(signal.String(), "signal "), "ed")
@@ -226,7 +226,7 @@ func (e *Environment) Terminate(signal os.Signal) error {
return err
}
e.setState(system.ProcessOfflineState)
e.setState(environment.ProcessOfflineState)
return nil
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/system"
)
// Returns the current environment state.
@@ -18,10 +17,10 @@ func (e *Environment) State() string {
// Sets the state of the environment. This emits an event that server's can hook into to
// take their own actions and track their own state based on the environment.
func (e *Environment) setState(state string) error {
if state != system.ProcessOfflineState &&
state != system.ProcessStartingState &&
state != system.ProcessRunningState &&
state != system.ProcessStoppingState {
if state != environment.ProcessOfflineState &&
state != environment.ProcessStartingState &&
state != environment.ProcessRunningState &&
state != environment.ProcessStoppingState {
return errors.New(fmt.Sprintf("invalid server state received: %s", state))
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/system"
"io"
"math"
"sync/atomic"
@@ -16,7 +15,7 @@ import (
// Attach to the instance and then automatically emit an event whenever the resource usage for the
// server process changes.
func (e *Environment) pollResources(ctx context.Context) error {
if e.State() == system.ProcessOfflineState {
if e.State() == environment.ProcessOfflineState {
return errors.New("attempting to enable resource polling on a stopped server instance")
}
@@ -43,7 +42,7 @@ func (e *Environment) pollResources(ctx context.Context) error {
}
// Disable collection if the server is in an offline state and this process is still running.
if e.State() == system.ProcessOfflineState {
if e.State() == environment.ProcessOfflineState {
return nil
}

View File

@@ -11,6 +11,13 @@ const (
ResourceEvent = "resources"
)
const (
ProcessOfflineState = "offline"
ProcessStartingState = "starting"
ProcessRunningState = "running"
ProcessStoppingState = "stopping"
)
// Defines the basic interface that all environments need to implement so that
// a server can be properly controlled.
type ProcessEnvironment interface {