2019-11-16 23:10:53 +00:00
|
|
|
package installer
|
|
|
|
|
|
|
|
import (
|
2021-02-02 05:28:46 +00:00
|
|
|
"context"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2019-11-16 23:10:53 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
2022-10-06 15:58:42 +00:00
|
|
|
|
2021-02-02 05:28:46 +00:00
|
|
|
"github.com/pterodactyl/wings/remote"
|
2019-11-16 23:10:53 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Installer struct {
|
2021-08-29 21:08:01 +00:00
|
|
|
server *server.Server
|
|
|
|
StartOnCompletion bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerDetails struct {
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
StartOnCompletion bool `json:"start_on_completion"`
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// New validates the received data to ensure that all the required fields
|
2019-11-16 23:10:53 +00:00
|
|
|
// have been passed along in the request. This should be manually run before
|
|
|
|
// calling Execute().
|
2021-08-29 21:08:01 +00:00
|
|
|
func New(ctx context.Context, manager *server.Manager, details ServerDetails) (*Installer, error) {
|
|
|
|
if !govalidator.IsUUIDv4(details.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
|
|
|
}
|
|
|
|
|
2021-08-29 21:08:01 +00:00
|
|
|
c, err := manager.Client().GetServerConfiguration(ctx, details.UUID)
|
2020-10-31 17:04:20 +00:00
|
|
|
if err != nil {
|
2021-02-02 05:28:46 +00:00
|
|
|
if !remote.IsRequestError(err) {
|
2021-03-04 04:56:18 +00:00
|
|
|
return nil, errors.WithStackIf(err)
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
2021-03-04 04:56:18 +00:00
|
|
|
return nil, errors.WrapIf(err, "installer: could not get server configuration from remote API")
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2021-07-04 19:12:32 +00:00
|
|
|
s, err := manager.InitServer(ctx, c)
|
2021-03-04 04:56:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WrapIf(err, "installer: could not init server instance")
|
|
|
|
}
|
2021-08-29 21:08:01 +00:00
|
|
|
i := Installer{server: s, StartOnCompletion: details.StartOnCompletion}
|
|
|
|
return &i, nil
|
2019-11-16 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 05:28:46 +00:00
|
|
|
// Server returns the server instance.
|
2019-11-16 23:48:50 +00:00
|
|
|
func (i *Installer) Server() *server.Server {
|
|
|
|
return i.server
|
|
|
|
}
|