2019-11-24 23:08:38 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-11-25 03:57:20 +00:00
|
|
|
"github.com/buger/jsonparser"
|
2019-11-24 23:08:38 +00:00
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"github.com/pkg/errors"
|
2020-08-20 01:58:48 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2019-11-24 23:08:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Merges data passed through in JSON form into the existing server object.
|
|
|
|
// Any changes to the build settings will apply immediately in the environment
|
|
|
|
// if the environment supports it.
|
|
|
|
//
|
|
|
|
// The server will be marked as requiring a rebuild on the next boot sequence,
|
|
|
|
// it is up to the specific environment to determine what needs to happen when
|
|
|
|
// that is the case.
|
2020-08-28 04:08:33 +00:00
|
|
|
func (s *Server) UpdateDataStructure(data []byte) error {
|
2020-07-19 23:27:55 +00:00
|
|
|
src := new(Configuration)
|
2019-12-22 21:21:21 +00:00
|
|
|
if err := json.Unmarshal(data, src); err != nil {
|
2019-11-24 23:08:38 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:57:20 +00:00
|
|
|
// Don't allow obviously corrupted data to pass through into this function. If the UUID
|
|
|
|
// doesn't match something has gone wrong and the API is attempting to meld this server
|
|
|
|
// instance into a totally different one, which would be bad.
|
2020-07-20 00:53:41 +00:00
|
|
|
if src.Uuid != "" && s.Id() != "" && src.Uuid != s.Id() {
|
2019-11-25 03:57:20 +00:00
|
|
|
return errors.New("attempting to merge a data stack with an invalid UUID")
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Grab a copy of the configuration to work on.
|
2020-07-20 00:46:39 +00:00
|
|
|
c := *s.Config()
|
|
|
|
|
2020-07-31 22:19:09 +00:00
|
|
|
// Lock our copy of the configuration since the deferred unlock will end up acting upon this
|
|
|
|
// new memory address rather than the old one. If we don't lock this, the deferred unlock will
|
2020-07-20 00:46:39 +00:00
|
|
|
// cause a panic when it goes to run. However, since we only update s.cfg at the end, if there
|
|
|
|
// is an error before that point we'll still properly unlock the original configuration for the
|
|
|
|
// server.
|
|
|
|
c.mu.Lock()
|
2020-07-19 23:27:55 +00:00
|
|
|
|
|
|
|
// Lock the server configuration while we're doing this merge to avoid anything
|
|
|
|
// trying to overwrite it or make modifications while we're sorting out what we
|
|
|
|
// need to do.
|
|
|
|
s.cfg.mu.Lock()
|
|
|
|
defer s.cfg.mu.Unlock()
|
|
|
|
|
2019-11-24 23:08:38 +00:00
|
|
|
// Merge the new data object that we have received with the existing server data object
|
|
|
|
// and then save it to the disk so it is persistent.
|
2020-07-20 00:46:39 +00:00
|
|
|
if err := mergo.Merge(&c, src, mergo.WithOverride); err != nil {
|
2019-11-24 23:08:38 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-05-31 19:42:10 +00:00
|
|
|
// Don't explode if we're setting CPU limits to 0. Mergo sees that as an empty value
|
|
|
|
// so it won't override the value we've passed through in the API call. However, we can
|
|
|
|
// safely assume that we're passing through valid data structures here. I foresee this
|
|
|
|
// backfiring at some point, but until then...
|
|
|
|
//
|
|
|
|
// We'll go ahead and do this with swap as well.
|
2020-07-19 23:27:55 +00:00
|
|
|
c.Build.CpuLimit = src.Build.CpuLimit
|
|
|
|
c.Build.Swap = src.Build.Swap
|
|
|
|
c.Build.DiskSpace = src.Build.DiskSpace
|
2020-05-31 19:42:10 +00:00
|
|
|
|
2019-11-25 03:57:20 +00:00
|
|
|
// Mergo can't quite handle this boolean value correctly, so for now we'll just
|
|
|
|
// handle this edge case manually since none of the other data passed through in this
|
|
|
|
// request is going to be boolean. Allegedly.
|
|
|
|
if v, err := jsonparser.GetBoolean(data, "container", "oom_disabled"); err != nil {
|
|
|
|
if err != jsonparser.KeyPathNotFoundError {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-11 04:38:42 +00:00
|
|
|
c.Build.OOMDisabled = v
|
2019-11-25 03:57:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 23:46:28 +00:00
|
|
|
// Mergo also cannot handle this boolean value.
|
|
|
|
if v, err := jsonparser.GetBoolean(data, "suspended"); err != nil {
|
|
|
|
if err != jsonparser.KeyPathNotFoundError {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-19 23:27:55 +00:00
|
|
|
c.Suspended = v
|
2019-11-30 23:46:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 16:41:14 +00:00
|
|
|
if v, err := jsonparser.GetBoolean(data, "skip_egg_scripts"); err != nil {
|
|
|
|
if err != jsonparser.KeyPathNotFoundError {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.SkipEggScripts = v
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:54:03 +00:00
|
|
|
// Environment and Mappings should be treated as a full update at all times, never a
|
|
|
|
// true patch, otherwise we can't know what we're passing along.
|
2019-11-30 23:46:28 +00:00
|
|
|
if src.EnvVars != nil && len(src.EnvVars) > 0 {
|
2020-07-19 23:27:55 +00:00
|
|
|
c.EnvVars = src.EnvVars
|
2019-11-25 04:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 00:37:11 +00:00
|
|
|
if src.Allocations.Mappings != nil && len(src.Allocations.Mappings) > 0 {
|
2020-07-19 23:27:55 +00:00
|
|
|
c.Allocations.Mappings = src.Allocations.Mappings
|
2019-11-25 04:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 21:13:27 +00:00
|
|
|
if src.Mounts != nil && len(src.Mounts) > 0 {
|
2020-07-19 23:27:55 +00:00
|
|
|
c.Mounts = src.Mounts
|
2020-05-21 21:13:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Update the configuration once we have a lock on the configuration object.
|
2020-07-20 00:46:39 +00:00
|
|
|
s.cfg = c
|
2020-07-19 23:27:55 +00:00
|
|
|
|
2019-11-30 23:19:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 04:08:33 +00:00
|
|
|
// Updates the environment for the server to match any of the changed data. This pushes new settings and
|
|
|
|
// environment variables to the environment. In addition, the in-situ update method is called on the
|
|
|
|
// environment which will allow environments that make use of it (such as Docker) to immediately apply
|
|
|
|
// some settings without having to wait on a server to restart.
|
2019-11-30 23:19:08 +00:00
|
|
|
//
|
2020-08-28 04:08:33 +00:00
|
|
|
// This functionality allows a server's resources limits to be modified on the fly and have them apply
|
|
|
|
// right away allowing for dynamic resource allocation and responses to abusive server processes.
|
|
|
|
func (s *Server) SyncWithEnvironment() {
|
|
|
|
s.Log().Debug("syncing server settings with environment")
|
|
|
|
|
|
|
|
// Update the environment settings using the new information from this server.
|
|
|
|
s.Environment.Config().SetSettings(environment.Settings{
|
|
|
|
Mounts: s.Mounts(),
|
|
|
|
Allocations: s.Config().Allocations,
|
|
|
|
Limits: s.Config().Build,
|
|
|
|
})
|
|
|
|
|
|
|
|
// If build limits are changed, environment variables also change. Plus, any modifications to
|
|
|
|
// the startup command also need to be properly propagated to this environment.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2255
|
|
|
|
s.Environment.Config().SetEnvironmentVariables(s.GetEnvironmentVariables())
|
2020-07-19 23:27:55 +00:00
|
|
|
|
|
|
|
if !s.IsSuspended() {
|
|
|
|
// Update the environment in place, allowing memory and CPU usage to be adjusted
|
|
|
|
// on the fly without the user needing to reboot (theoretically).
|
|
|
|
s.Log().Info("performing server limit modification on-the-fly")
|
|
|
|
if err := s.Environment.InSituUpdate(); err != nil {
|
2020-08-28 04:08:33 +00:00
|
|
|
// This is not a failure, the process is still running fine and will fix itself on the
|
|
|
|
// next boot, or fail out entirely in a more logical position.
|
2020-07-19 23:27:55 +00:00
|
|
|
s.Log().WithField("error", err).Warn("failed to perform on-the-fly update of the server environment")
|
|
|
|
}
|
2020-08-28 04:08:33 +00:00
|
|
|
} else {
|
|
|
|
// Checks if the server is now in a suspended state. If so and a server process is currently running it
|
|
|
|
// will be gracefully stopped (and terminated if it refuses to stop).
|
|
|
|
s.Log().Info("server suspended with running process state, terminating now")
|
|
|
|
|
|
|
|
go func (s *Server) {
|
|
|
|
if err := s.Environment.WaitForStop(60, true); err != nil {
|
|
|
|
s.Log().WithField("error", err).Warn("failed to terminate server environment after suspension")
|
|
|
|
}
|
|
|
|
}(s)
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|
2019-11-25 04:54:03 +00:00
|
|
|
}
|