2019-11-16 23:10:53 +00:00
|
|
|
package installer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-06-13 17:26:35 +00:00
|
|
|
"github.com/apex/log"
|
2019-11-16 23:10:53 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/buger/jsonparser"
|
2019-11-17 01:01:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-10 22:33:30 +00:00
|
|
|
"github.com/pterodactyl/wings/api"
|
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"
|
2019-12-25 01:13:03 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-11-16 23:10:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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().
|
2019-11-25 04:40:13 +00:00
|
|
|
func New(data []byte) (*Installer, error) {
|
2019-11-16 23:10:53 +00:00
|
|
|
if !govalidator.IsUUIDv4(getString(data, "uuid")) {
|
2020-04-12 00:55:00 +00:00
|
|
|
return nil, NewValidationError("uuid provided was not in a valid format")
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.IsUUIDv4(getString(data, "service", "egg")) {
|
2020-04-12 00:55:00 +00:00
|
|
|
return nil, NewValidationError("service egg provided was not in a valid format")
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg := &server.Configuration{
|
2019-11-16 23:48:50 +00:00
|
|
|
Uuid: getString(data, "uuid"),
|
|
|
|
Suspended: false,
|
2019-12-22 05:02:02 +00:00
|
|
|
Invocation: getString(data, "invocation"),
|
2019-12-01 00:37:11 +00:00
|
|
|
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"),
|
2020-03-29 19:31:17 +00:00
|
|
|
Threads: getString(data, "build", "threads"),
|
2019-11-16 23:10:53 +00:00
|
|
|
},
|
2020-07-19 23:27:55 +00:00
|
|
|
CrashDetectionEnabled: true,
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg.Allocations.DefaultMapping.Ip = getString(data, "allocations", "default", "ip")
|
|
|
|
cfg.Allocations.DefaultMapping.Port = int(getInt(data, "allocations", "default", "port"))
|
2019-11-16 23:10:53 +00:00
|
|
|
|
2019-11-25 04:40:13 +00:00
|
|
|
// Unmarshal the environment variables from the request into the server struct.
|
|
|
|
if b, _, _, err := jsonparser.Get(data, "environment"); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
} else {
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg.EnvVars = make(server.EnvironmentVariables)
|
|
|
|
if err := json.Unmarshal(b, &cfg.EnvVars); err != nil {
|
2019-11-25 04:40:13 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
2019-11-25 04:40:13 +00:00
|
|
|
}
|
2019-11-16 23:10:53 +00:00
|
|
|
|
2019-11-25 04:40:13 +00:00
|
|
|
// Unmarshal the allocation mappings from the request into the server struct.
|
|
|
|
if b, _, _, err := jsonparser.Get(data, "allocations", "mappings"); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
} else {
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg.Allocations.Mappings = make(map[string][]int)
|
|
|
|
if err := json.Unmarshal(b, &cfg.Allocations.Mappings); err != nil {
|
2019-11-25 04:40:13 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
2019-11-25 04:40:13 +00:00
|
|
|
}
|
2019-11-16 23:10:53 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
cfg.Container.Image = getString(data, "container", "image")
|
2019-11-16 23:10:53 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
c, rerr, err := api.NewRequester().GetServerConfiguration(cfg.Uuid)
|
2020-04-10 22:33:30 +00:00
|
|
|
if err != nil || rerr != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New(rerr.String())
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:48:50 +00:00
|
|
|
// Create a new server instance using the configuration we wrote to the disk
|
|
|
|
// so that everything gets instantiated correctly on the struct.
|
2020-07-20 00:26:53 +00:00
|
|
|
s, err := server.FromConfiguration(c)
|
2019-11-16 23:48:50 +00:00
|
|
|
|
2019-11-25 04:40:13 +00:00
|
|
|
return &Installer{
|
2020-07-19 23:27:55 +00:00
|
|
|
server: s,
|
2019-12-17 04:55:56 +00:00
|
|
|
}, err
|
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() {
|
2020-06-13 17:26:35 +00:00
|
|
|
p := path.Join(config.Get().System.Data, i.Uuid())
|
|
|
|
l := log.WithFields(log.Fields{"server": i.Uuid(), "process": "installer"})
|
|
|
|
|
|
|
|
l.WithField("path", p).Debug("creating required server data directory")
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
l.WithFields(log.Fields{"path": p, "error": errors.WithStack(err)}).Error("failed to create server data directory")
|
2019-12-25 01:13:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
if err := os.Chown(p, config.Get().System.User.Uid, config.Get().System.User.Gid); err != nil {
|
|
|
|
l.WithField("error", errors.WithStack(err)).Error("failed to chown server data directory")
|
2019-12-25 01:13:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
l.Debug("creating required environment for server instance")
|
2019-11-16 23:48:50 +00:00
|
|
|
if err := i.server.Environment.Create(); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
l.WithField("error", err).Error("failed to create environment for server")
|
2019-11-16 23:48:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-22 05:03:57 +00:00
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
l.Info("successfully created environment for server during install process")
|
2019-11-16 23:48:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|