2020-04-10 22:33:30 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-04-13 00:43:16 +00:00
|
|
|
"io"
|
2020-04-10 23:55:36 +00:00
|
|
|
"io/ioutil"
|
2020-04-10 22:33:30 +00:00
|
|
|
"os"
|
2020-04-10 22:37:10 +00:00
|
|
|
"sync"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-04-10 22:33:30 +00:00
|
|
|
)
|
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
var stateMutex sync.Mutex
|
2020-04-10 22:33:30 +00:00
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
// Returns the state of the servers.
|
2020-09-13 05:08:50 +00:00
|
|
|
func CachedServerStates() (map[string]string, error) {
|
2020-04-10 22:37:10 +00:00
|
|
|
// Request a lock after we check if the file exists.
|
2020-04-11 01:07:57 +00:00
|
|
|
stateMutex.Lock()
|
|
|
|
defer stateMutex.Unlock()
|
2020-04-10 22:37:10 +00:00
|
|
|
|
2020-04-10 22:33:30 +00:00
|
|
|
// Open the states file.
|
2020-04-17 21:27:06 +00:00
|
|
|
f, err := os.OpenFile(config.Get().System.GetStatesPath(), os.O_RDONLY|os.O_CREATE, 0644)
|
2020-04-10 22:33:30 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Convert the json object to a map.
|
|
|
|
states := map[string]string{}
|
2020-04-13 00:43:16 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&states); err != nil && err != io.EOF {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
2020-04-11 01:22:18 +00:00
|
|
|
// saveServerStates .
|
|
|
|
func saveServerStates() error {
|
2020-04-10 22:33:30 +00:00
|
|
|
// Get the states of all servers on the daemon.
|
|
|
|
states := map[string]string{}
|
|
|
|
for _, s := range GetServers().All() {
|
2020-11-20 21:35:29 +00:00
|
|
|
states[s.Id()] = s.Environment.State()
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the map to a json object.
|
|
|
|
data, err := json.Marshal(states)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
stateMutex.Lock()
|
|
|
|
defer stateMutex.Unlock()
|
2020-04-10 22:37:10 +00:00
|
|
|
|
2020-04-10 22:33:30 +00:00
|
|
|
// Write the data to the file
|
2020-04-17 21:27:06 +00:00
|
|
|
if err := ioutil.WriteFile(config.Get().System.GetStatesPath(), data, 0644); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 23:55:36 +00:00
|
|
|
return nil
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
2020-04-11 01:22:18 +00:00
|
|
|
|
|
|
|
// Sets the state of the server internally. This function handles crash detection as
|
|
|
|
// well as reporting to event listeners for the server.
|
2020-11-07 05:53:00 +00:00
|
|
|
func (s *Server) OnStateChange() {
|
2020-12-17 06:03:35 +00:00
|
|
|
prevState := s.resources.State.Load()
|
2020-04-11 01:22:18 +00:00
|
|
|
|
2020-11-07 05:53:00 +00:00
|
|
|
st := s.Environment.State()
|
2020-07-19 23:27:55 +00:00
|
|
|
// Update the currently tracked state for the server.
|
2020-12-17 06:03:35 +00:00
|
|
|
s.resources.State.Store(st)
|
2020-04-11 01:22:18 +00:00
|
|
|
|
|
|
|
// Emit the event to any listeners that are currently registered.
|
2020-11-07 05:53:00 +00:00
|
|
|
if prevState != s.Environment.State() {
|
|
|
|
s.Log().WithField("status", st).Debug("saw server status change event")
|
|
|
|
s.Events().Publish(StatusEvent, st)
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
2020-04-11 01:22:18 +00:00
|
|
|
|
|
|
|
// Persist this change to the disk immediately so that should the Daemon be stopped or
|
|
|
|
// crash we can immediately restore the server state.
|
|
|
|
//
|
|
|
|
// This really only makes a difference if all of the Docker containers are also stopped,
|
|
|
|
// but this was a highly requested feature and isn't hard to work with, so lets do it.
|
|
|
|
//
|
|
|
|
// We also get the benefit of server status changes always propagating corrected configurations
|
|
|
|
// to the disk should we forget to do it elsewhere.
|
|
|
|
go func() {
|
|
|
|
if err := saveServerStates(); err != nil {
|
2020-06-13 17:40:26 +00:00
|
|
|
s.Log().WithField("error", err).Warn("failed to write server states to disk")
|
2020-04-11 01:22:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-08-19 04:38:42 +00:00
|
|
|
// Reset the resource usage to 0 when the process fully stops so that all of the UI
|
|
|
|
// views in the Panel correctly display 0.
|
2020-11-07 05:53:00 +00:00
|
|
|
if st == environment.ProcessOfflineState {
|
2020-12-17 06:03:35 +00:00
|
|
|
s.resources.Reset()
|
2020-08-19 04:42:57 +00:00
|
|
|
s.emitProcUsage()
|
2020-08-19 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:22:18 +00:00
|
|
|
// If server was in an online state, and is now in an offline state we should handle
|
|
|
|
// that as a crash event. In that scenario, check the last crash time, and the crash
|
|
|
|
// counter.
|
|
|
|
//
|
|
|
|
// In the event that we have passed the thresholds, don't do anything, otherwise
|
|
|
|
// automatically attempt to start the process back up for the user. This is done in a
|
|
|
|
// separate thread as to not block any actions currently taking place in the flow
|
|
|
|
// that called this function.
|
2020-11-20 21:35:29 +00:00
|
|
|
if (prevState == environment.ProcessStartingState || prevState == environment.ProcessRunningState) && s.Environment.State() == environment.ProcessOfflineState {
|
2020-06-13 17:40:26 +00:00
|
|
|
s.Log().Info("detected server as entering a crashed state; running crash handler")
|
2020-04-11 01:22:18 +00:00
|
|
|
|
|
|
|
go func(server *Server) {
|
|
|
|
if err := server.handleServerCrash(); err != nil {
|
|
|
|
if IsTooFrequentCrashError(err) {
|
2020-06-13 17:40:26 +00:00
|
|
|
server.Log().Info("did not restart server after crash; occurred too soon after the last")
|
2020-04-11 01:22:18 +00:00
|
|
|
} else {
|
2020-12-06 23:34:58 +00:00
|
|
|
s.PublishConsoleOutputFromDaemon("Server crash was detected but an error occurred while handling it.")
|
2020-06-13 17:40:26 +00:00
|
|
|
server.Log().WithField("error", err).Error("failed to handle server crash")
|
2020-04-11 01:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the current state of the server in a race-safe manner.
|
2020-11-07 05:53:00 +00:00
|
|
|
// Deprecated
|
|
|
|
// use Environment.State()
|
2020-04-11 01:22:18 +00:00
|
|
|
func (s *Server) GetState() string {
|
2020-11-07 05:53:00 +00:00
|
|
|
return s.Environment.State()
|
2020-04-11 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determines if the server state is running or not. This is different than the
|
|
|
|
// environment state, it is simply the tracked state from this daemon instance, and
|
|
|
|
// not the response from Docker.
|
|
|
|
func (s *Server) IsRunning() bool {
|
2020-11-20 21:35:29 +00:00
|
|
|
st := s.Environment.State()
|
2020-07-20 00:09:38 +00:00
|
|
|
|
2020-08-20 01:58:48 +00:00
|
|
|
return st == environment.ProcessRunningState || st == environment.ProcessStartingState
|
2020-04-17 21:27:06 +00:00
|
|
|
}
|