Simplify logic when creating a new installer; no longer requires an entire server object be passed.
This commit is contained in:
parent
7321c6aa45
commit
3b5e042ccc
|
@ -12,6 +12,7 @@
|
||||||
### Changed
|
### Changed
|
||||||
* Releases are now built using `Go 1.17` — the minimum version required to build Wings remains `Go 1.16`.
|
* Releases are now built using `Go 1.17` — the minimum version required to build Wings remains `Go 1.16`.
|
||||||
* Simplifed the logic powering server updates to only pull information from the Panel rather than trying to accept updated values. All parts of Wings needing the most up-to-date server details should call `Server#Sync()` to fetch the latest stored build information.
|
* Simplifed the logic powering server updates to only pull information from the Panel rather than trying to accept updated values. All parts of Wings needing the most up-to-date server details should call `Server#Sync()` to fetch the latest stored build information.
|
||||||
|
* `Installer#New()` no longer requires passing all of the server data as a byte slice, rather a new `Installer#ServerDetails` struct is exposed which can be passed and accepts a UUID and if the server should be started after the installer finishes.
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* Removes complicated (and unused) logic during the server installation process that was a hold-over from legacy Wings architectures.
|
* Removes complicated (and unused) logic during the server installation process that was a hold-over from legacy Wings architectures.
|
||||||
|
|
|
@ -5,26 +5,29 @@ import (
|
||||||
|
|
||||||
"emperror.dev/errors"
|
"emperror.dev/errors"
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
"github.com/buger/jsonparser"
|
|
||||||
|
|
||||||
"github.com/pterodactyl/wings/remote"
|
"github.com/pterodactyl/wings/remote"
|
||||||
"github.com/pterodactyl/wings/server"
|
"github.com/pterodactyl/wings/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Installer struct {
|
type Installer struct {
|
||||||
server *server.Server
|
server *server.Server
|
||||||
|
StartOnCompletion bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerDetails struct {
|
||||||
|
UUID string `json:"uuid"`
|
||||||
|
StartOnCompletion bool `json:"start_on_completion"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// New validates the received data to ensure that all the required fields
|
// New validates the received data to ensure that all the required fields
|
||||||
// have been passed along in the request. This should be manually run before
|
// have been passed along in the request. This should be manually run before
|
||||||
// calling Execute().
|
// calling Execute().
|
||||||
func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer, error) {
|
func New(ctx context.Context, manager *server.Manager, details ServerDetails) (*Installer, error) {
|
||||||
uuid := getString(data, "uuid")
|
if !govalidator.IsUUIDv4(details.UUID) {
|
||||||
if !govalidator.IsUUIDv4(uuid) {
|
|
||||||
return nil, NewValidationError("uuid provided was not in a valid format")
|
return nil, NewValidationError("uuid provided was not in a valid format")
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := manager.Client().GetServerConfiguration(ctx, uuid)
|
c, err := manager.Client().GetServerConfiguration(ctx, details.UUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !remote.IsRequestError(err) {
|
if !remote.IsRequestError(err) {
|
||||||
return nil, errors.WithStackIf(err)
|
return nil, errors.WithStackIf(err)
|
||||||
|
@ -38,35 +41,11 @@ func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WrapIf(err, "installer: could not init server instance")
|
return nil, errors.WrapIf(err, "installer: could not init server instance")
|
||||||
}
|
}
|
||||||
return &Installer{server: s}, nil
|
i := Installer{server: s, StartOnCompletion: details.StartOnCompletion}
|
||||||
}
|
return &i, nil
|
||||||
|
|
||||||
// Uuid returns the UUID associated with this installer instance.
|
|
||||||
func (i *Installer) Uuid() string {
|
|
||||||
return i.server.ID()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server returns the server instance.
|
// Server returns the server instance.
|
||||||
func (i *Installer) Server() *server.Server {
|
func (i *Installer) Server() *server.Server {
|
||||||
return i.server
|
return i.server
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
func getBoolean(data []byte, key ...string) bool {
|
|
||||||
value, _ := jsonparser.GetBoolean(data, key...)
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -44,10 +43,13 @@ func getAllServers(c *gin.Context) {
|
||||||
// for it.
|
// for it.
|
||||||
func postCreateServer(c *gin.Context) {
|
func postCreateServer(c *gin.Context) {
|
||||||
manager := middleware.ExtractManager(c)
|
manager := middleware.ExtractManager(c)
|
||||||
buf := bytes.Buffer{}
|
|
||||||
buf.ReadFrom(c.Request.Body)
|
|
||||||
|
|
||||||
install, err := installer.New(c.Request.Context(), manager, buf.Bytes())
|
details := installer.ServerDetails{}
|
||||||
|
if err := c.BindJSON(&details); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
install, err := installer.New(c.Request.Context(), manager, details)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if installer.IsValidationError(err) {
|
if installer.IsValidationError(err) {
|
||||||
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
@ -74,24 +76,21 @@ func postCreateServer(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := i.Server().Install(false); err != nil {
|
if err := i.Server().Install(false); err != nil {
|
||||||
log.WithFields(log.Fields{"server": i.Uuid(), "error": err}).Error("failed to run install process for server")
|
log.WithFields(log.Fields{"server": i.Server().ID(), "error": err}).Error("failed to run install process for server")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.Server().Config().StartOnCompletion {
|
if i.StartOnCompletion {
|
||||||
log.WithField("server_id", i.Server().ID()).Debug("starting server after successful installation")
|
log.WithField("server_id", i.Server().ID()).Debug("starting server after successful installation")
|
||||||
if err := i.Server().HandlePowerAction(server.PowerActionStart, 30); err != nil {
|
if err := i.Server().HandlePowerAction(server.PowerActionStart, 30); err != nil {
|
||||||
if errors.Is(err, context.DeadlineExceeded) {
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
log.WithFields(log.Fields{"server_id": i.Server().ID(), "action": "start"}).
|
log.WithFields(log.Fields{"server_id": i.Server().ID(), "action": "start"}).Warn("could not acquire a lock while attempting to perform a power action")
|
||||||
Warn("could not acquire a lock while attempting to perform a power action")
|
|
||||||
} else {
|
} else {
|
||||||
log.WithFields(log.Fields{"server_id": i.Server().ID(), "action": "start", "error": err}).
|
log.WithFields(log.Fields{"server_id": i.Server().ID(), "action": "start", "error": err}).Error("encountered error processing a server power action in the background")
|
||||||
Error("encountered error processing a server power action in the background")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.WithField("server_id", i.Server().ID()).
|
log.WithField("server_id", i.Server().ID()).Debug("skipping automatic start after successful server installation")
|
||||||
Debug("skipping automatic start after successful server installation")
|
|
||||||
}
|
}
|
||||||
}(install)
|
}(install)
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -50,7 +49,7 @@ type serverTransferRequest struct {
|
||||||
ServerID string `binding:"required" json:"server_id"`
|
ServerID string `binding:"required" json:"server_id"`
|
||||||
URL string `binding:"required" json:"url"`
|
URL string `binding:"required" json:"url"`
|
||||||
Token string `binding:"required" json:"token"`
|
Token string `binding:"required" json:"token"`
|
||||||
Server json.RawMessage `json:"server"`
|
Server installer.ServerDetails `json:"server"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArchivePath(sID string) string {
|
func getArchivePath(sID string) string {
|
||||||
|
|
|
@ -35,8 +35,6 @@ type Configuration struct {
|
||||||
// server, specific installation scripts will be skipped for the server process.
|
// server, specific installation scripts will be skipped for the server process.
|
||||||
SkipEggScripts bool `json:"skip_egg_scripts"`
|
SkipEggScripts bool `json:"skip_egg_scripts"`
|
||||||
|
|
||||||
StartOnCompletion bool `json:"start_on_completion"`
|
|
||||||
|
|
||||||
// An array of environment variables that should be passed along to the running
|
// An array of environment variables that should be passed along to the running
|
||||||
// server process.
|
// server process.
|
||||||
EnvVars environment.Variables `json:"environment"`
|
EnvVars environment.Variables `json:"environment"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user