2019-11-16 23:10:53 +00:00
|
|
|
package installer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/buger/jsonparser"
|
2019-11-17 01:01:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-16 23:48:50 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2019-11-16 23:10:53 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Installer struct {
|
|
|
|
server *server.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validates the received data to ensure that all of the required fields
|
|
|
|
// have been passed along in the request. This should be manually run before
|
|
|
|
// calling Execute().
|
|
|
|
func New(data []byte) (error, *Installer) {
|
|
|
|
if !govalidator.IsUUIDv4(getString(data, "uuid")) {
|
|
|
|
return errors.New("uuid provided was not in a valid format"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.IsUUIDv4(getString(data, "service", "egg")) {
|
|
|
|
return errors.New("service egg provided was not in a valid format"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &server.Server{
|
2019-11-16 23:48:50 +00:00
|
|
|
Uuid: getString(data, "uuid"),
|
|
|
|
Suspended: false,
|
|
|
|
State: server.ProcessOfflineState,
|
|
|
|
Invocation: "",
|
|
|
|
EnvVars: make(map[string]string),
|
|
|
|
Build: &server.BuildSettings{
|
2019-11-16 23:10:53 +00:00
|
|
|
MemoryLimit: getInt(data, "build", "memory"),
|
2019-11-16 23:48:50 +00:00
|
|
|
Swap: getInt(data, "build", "swap"),
|
|
|
|
IoWeight: uint16(getInt(data, "build", "io")),
|
|
|
|
CpuLimit: getInt(data, "build", "cpu"),
|
|
|
|
DiskSpace: getInt(data, "build", "disk"),
|
2019-11-16 23:10:53 +00:00
|
|
|
},
|
|
|
|
Allocations: &server.Allocations{
|
|
|
|
Mappings: make(map[string][]int),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-24 23:08:38 +00:00
|
|
|
s.Init()
|
|
|
|
|
2019-11-16 23:10:53 +00:00
|
|
|
s.Allocations.DefaultMapping.Ip = getString(data, "allocations", "default", "ip")
|
|
|
|
s.Allocations.DefaultMapping.Port = int(getInt(data, "allocations", "default", "port"))
|
|
|
|
|
|
|
|
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
|
|
|
|
s.EnvVars[string(key)] = string(value)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, "environment")
|
|
|
|
|
|
|
|
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
|
|
|
var dat map[string][]int
|
|
|
|
if err := json.Unmarshal(value, &dat); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, k := range dat {
|
|
|
|
s.Allocations.Mappings[i] = k
|
|
|
|
}
|
|
|
|
}, "allocations", "mappings")
|
|
|
|
|
|
|
|
s.Container.Image = getString(data, "container", "image")
|
|
|
|
|
2019-11-24 23:08:38 +00:00
|
|
|
b, err := s.WriteConfigurationToDisk()
|
2019-11-16 23:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the temporary server instance.
|
|
|
|
s = nil
|
|
|
|
|
|
|
|
// Create a new server instance using the configuration we wrote to the disk
|
|
|
|
// so that everything gets instantiated correctly on the struct.
|
|
|
|
s2, err := server.FromConfiguration(b, config.Get().System)
|
|
|
|
|
2019-11-16 23:10:53 +00:00
|
|
|
return nil, &Installer{
|
2019-11-16 23:48:50 +00:00
|
|
|
server: s2,
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the UUID associated with this installer instance.
|
|
|
|
func (i *Installer) Uuid() string {
|
|
|
|
return i.server.Uuid
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:48:50 +00:00
|
|
|
// Return the server instance.
|
|
|
|
func (i *Installer) Server() *server.Server {
|
|
|
|
return i.server
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:10:53 +00:00
|
|
|
// Executes the installer process, creating the server and running through the
|
|
|
|
// associated installation process based on the parameters passed through for
|
|
|
|
// the server instance.
|
2019-11-16 23:48:50 +00:00
|
|
|
func (i *Installer) Execute() {
|
2019-11-16 23:10:53 +00:00
|
|
|
zap.S().Debugw("beginning installation process for server", zap.String("server", i.server.Uuid))
|
|
|
|
|
2019-11-16 23:48:50 +00:00
|
|
|
zap.S().Debugw("creating required environment for server instance", zap.String("server", i.server.Uuid))
|
|
|
|
if err := i.server.Environment.Create(); err != nil {
|
|
|
|
zap.S().Errorw("failed to create environment for server", zap.String("server", i.server.Uuid), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:10:53 +00:00
|
|
|
// Returns a string value from the JSON data provided.
|
|
|
|
func getString(data []byte, key ...string) string {
|
|
|
|
value, _ := jsonparser.GetString(data, key...)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an int value from the JSON data provided.
|
|
|
|
func getInt(data []byte, key ...string) int64 {
|
|
|
|
value, _ := jsonparser.GetInt(data, key...)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|