wings/router/router_system.go

118 lines
3.5 KiB
Go
Raw Normal View History

2020-04-06 01:00:33 +00:00
package router
import (
"bytes"
2021-01-10 01:22:39 +00:00
"net/http"
"strings"
2020-06-13 17:26:35 +00:00
"github.com/apex/log"
2020-04-06 01:00:33 +00:00
"github.com/gin-gonic/gin"
"github.com/pterodactyl/wings/config"
2020-04-06 01:00:33 +00:00
"github.com/pterodactyl/wings/installer"
2021-01-26 04:28:24 +00:00
"github.com/pterodactyl/wings/router/middleware"
"github.com/pterodactyl/wings/server"
2020-04-06 01:00:33 +00:00
"github.com/pterodactyl/wings/system"
)
// ServerJsonResponse is a type returned when requesting details about a single
// server instance on Wings.
type ServerJsonResponse struct {
*server.Configuration
Resources server.ResourceUsage `json:"resources"`
}
2020-04-06 01:00:33 +00:00
// Returns information about the system that wings is running on.
func getSystemInformation(c *gin.Context) {
i, err := system.GetSystemInformation()
if err != nil {
2020-12-16 05:08:00 +00:00
NewTrackedError(err).Abort(c)
2020-04-06 01:00:33 +00:00
return
}
c.JSON(http.StatusOK, i)
}
// Returns all of the servers that are registered and configured correctly on
// this wings instance.
func getAllServers(c *gin.Context) {
2021-04-02 19:32:30 +00:00
servers := middleware.ExtractManager(c).All()
out := make([]ServerJsonResponse, len(servers), len(servers))
for i, v := range servers {
out[i] = ServerJsonResponse{
Configuration: v.Config(),
Resources: v.Proc(),
}
2021-04-02 19:32:30 +00:00
}
c.JSON(http.StatusOK, out)
2020-04-06 01:00:33 +00:00
}
// Creates a new server on the wings daemon and begins the installation process
// for it.
func postCreateServer(c *gin.Context) {
manager := middleware.ExtractManager(c)
buf := bytes.Buffer{}
buf.ReadFrom(c.Request.Body)
2020-04-06 01:00:33 +00:00
install, err := installer.New(c.Request.Context(), manager, buf.Bytes())
2020-04-06 01:00:33 +00:00
if err != nil {
if installer.IsValidationError(err) {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
"error": "The data provided in the request could not be validated.",
})
return
}
2021-03-04 05:00:58 +00:00
middleware.CaptureAndAbort(c, err)
2020-04-06 01:00:33 +00:00
return
}
// Plop that server instance onto the request so that it can be referenced in
// requests from here-on out.
2021-01-26 04:28:24 +00:00
manager.Add(install.Server())
2020-04-06 01:00:33 +00:00
// Begin the installation process in the background to not block the request
// cycle. If there are any errors they will be logged and communicated back
// to the Panel where a reinstall may take place.
go func(i *installer.Installer) {
err := i.Server().CreateEnvironment()
if err != nil {
i.Server().Log().WithField("error", err).Error("failed to create server environment during install process")
return
}
2020-04-06 01:00:33 +00:00
if err := i.Server().Install(false); err != nil {
2020-06-13 17:26:35 +00:00
log.WithFields(log.Fields{"server": i.Uuid(), "error": err}).Error("failed to run install process for server")
2020-04-06 01:00:33 +00:00
}
}(install)
c.Status(http.StatusAccepted)
}
2021-01-15 04:32:38 +00:00
// Updates the running configuration for this Wings instance.
func postUpdateConfiguration(c *gin.Context) {
2021-01-15 04:32:38 +00:00
cfg := config.Get()
2020-05-29 15:44:49 +00:00
if err := c.BindJSON(&cfg); err != nil {
return
}
// Keep the SSL certificates the same since the Panel will send through Lets Encrypt
// default locations. However, if we picked a different location manually we don't
// want to override that.
//
// If you pass through manual locations in the API call this logic will be skipped.
if strings.HasPrefix(cfg.Api.Ssl.KeyFile, "/etc/letsencrypt/live/") {
2021-01-15 04:32:38 +00:00
cfg.Api.Ssl.KeyFile = strings.ToLower(config.Get().Api.Ssl.KeyFile)
cfg.Api.Ssl.CertificateFile = strings.ToLower(config.Get().Api.Ssl.CertificateFile)
}
2021-01-15 04:32:38 +00:00
// Try to write this new configuration to the disk before updating our global
// state with it.
if err := config.WriteToDisk(cfg); err != nil {
WithError(c, err)
return
}
2021-01-15 04:32:38 +00:00
// Since we wrote it to the disk successfully now update the global configuration
// state to use this new configuration struct.
config.Set(cfg)
c.Status(http.StatusNoContent)
2020-05-29 15:44:49 +00:00
}