package installer import ( "encoding/json" "errors" "github.com/asaskevich/govalidator" "github.com/buger/jsonparser" "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{ Uuid: getString(data, "uuid"), Suspended: false, State: server.ProcessOfflineState, Invocation: "", EnvVars: make(map[string]string), Build: &server.BuildSettings{ MemoryLimit: getInt(data, "build", "memory"), Swap: getInt(data, "build", "swap"), IoWeight: uint16(getInt(data, "build", "io")), CpuLimit: getInt(data, "build", "cpu"), DiskSpace: getInt(data, "build", "disk"), }, Allocations: &server.Allocations{ Mappings: make(map[string][]int), }, } 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") return nil, &Installer{ server: s, } } // Returns the UUID associated with this installer instance. func (i *Installer) Uuid() string { return i.server.Uuid } // Executes the installer process, creating the server and running through the // associated installation process based on the parameters passed through for // the server instance. func (i *Installer) Execute() error { zap.S().Debugw("beginning installation process for server", zap.String("server", i.server.Uuid)) return nil } // 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 }