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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2019-12-22 21:21:21 +00:00
|
|
|
func (s *Server) UpdateDataStructure(data []byte, background bool) error {
|
|
|
|
src := new(Server)
|
|
|
|
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-04-10 23:55:36 +00:00
|
|
|
if src.Uuid != "" && s.Uuid != "" && src.Uuid != s.Uuid {
|
2019-11-25 03:57:20 +00:00
|
|
|
return errors.New("attempting to merge a data stack with an invalid UUID")
|
|
|
|
}
|
|
|
|
|
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.
|
2019-11-25 03:57:20 +00:00
|
|
|
if err := mergo.Merge(s, src, mergo.WithOverride); err != nil {
|
2019-11-24 23:08:38 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-07-18 17:10:34 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
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.
|
|
|
|
s.Build.CpuLimit = src.Build.CpuLimit
|
|
|
|
s.Build.Swap = src.Build.Swap
|
|
|
|
s.Build.DiskSpace = src.Build.DiskSpace
|
|
|
|
|
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 {
|
|
|
|
s.Container.OomDisabled = v
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
s.Suspended = 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 {
|
2019-11-25 04:54:03 +00:00
|
|
|
s.EnvVars = src.EnvVars
|
|
|
|
}
|
|
|
|
|
2019-12-01 00:37:11 +00:00
|
|
|
if src.Allocations.Mappings != nil && len(src.Allocations.Mappings) > 0 {
|
2019-11-25 04:54:03 +00:00
|
|
|
s.Allocations.Mappings = src.Allocations.Mappings
|
|
|
|
}
|
|
|
|
|
2020-05-21 21:13:27 +00:00
|
|
|
if src.Mounts != nil && len(src.Mounts) > 0 {
|
|
|
|
s.Mounts = src.Mounts
|
|
|
|
}
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
if background {
|
|
|
|
s.runBackgroundActions()
|
|
|
|
}
|
2019-11-30 23:19:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs through different actions once a server's configuration has been persisted
|
|
|
|
// to the disk. This function does not return anything as any failures should be logged
|
|
|
|
// but have no effect on actually updating the server itself.
|
|
|
|
//
|
|
|
|
// These tasks run in independent threads where relevant to speed up any updates
|
|
|
|
// that need to happen.
|
|
|
|
func (s *Server) runBackgroundActions() {
|
|
|
|
// Update the environment in place, allowing memory and CPU usage to be adjusted
|
|
|
|
// on the fly without the user needing to reboot (theoretically).
|
|
|
|
go func(server *Server) {
|
2020-05-31 19:42:10 +00:00
|
|
|
server.Log().Info("performing server limit modification on-the-fly")
|
2019-11-30 23:19:08 +00:00
|
|
|
if err := server.Environment.InSituUpdate(); err != nil {
|
2020-05-31 19:42:10 +00:00
|
|
|
server.Log().WithField("error", err).Warn("failed to perform on-the-fly update of the server environment")
|
2019-11-30 23:19:08 +00:00
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
|
|
|
|
// Check if the server is now suspended, and if so and the process is not terminated
|
|
|
|
// yet, do it immediately.
|
|
|
|
go func(server *Server) {
|
2020-04-11 01:22:18 +00:00
|
|
|
if server.Suspended && server.GetState() != ProcessOfflineState {
|
2020-06-13 17:40:26 +00:00
|
|
|
server.Log().Info("server suspended with running process state, terminating now")
|
2019-11-30 23:46:28 +00:00
|
|
|
|
2020-04-04 06:17:32 +00:00
|
|
|
if err := server.Environment.WaitForStop(10, true); err != nil {
|
2020-06-13 17:40:26 +00:00
|
|
|
server.Log().WithField("error", err).Warn("failed to terminate server environment after suspension")
|
2019-11-30 23:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s)
|
2019-11-25 04:54:03 +00:00
|
|
|
}
|