2020-04-06 02:07:16 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2021-01-10 01:22:39 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-01-19 05:20:58 +00:00
|
|
|
"strings"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2021-01-17 02:06:22 +00:00
|
|
|
"github.com/apex/log"
|
2020-04-06 02:07:16 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-01-17 02:06:22 +00:00
|
|
|
"github.com/pterodactyl/wings/router/middleware"
|
2020-04-06 02:07:16 +00:00
|
|
|
"github.com/pterodactyl/wings/server"
|
2020-04-14 05:01:07 +00:00
|
|
|
"github.com/pterodactyl/wings/server/backup"
|
2020-04-06 02:07:16 +00:00
|
|
|
)
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// postServerBackup performs a backup against a given server instance using the
|
|
|
|
// provided backup adapter.
|
2020-04-06 02:07:16 +00:00
|
|
|
func postServerBackup(c *gin.Context) {
|
2021-01-17 02:06:22 +00:00
|
|
|
s := middleware.ExtractServer(c)
|
|
|
|
logger := middleware.ExtractLogger(c)
|
2021-01-18 05:05:51 +00:00
|
|
|
var data struct {
|
|
|
|
Adapter backup.AdapterType `json:"adapter"`
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
Ignore string `json:"ignore"`
|
|
|
|
}
|
2020-05-29 15:44:49 +00:00
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 02:07:16 +00:00
|
|
|
|
2021-01-18 05:05:51 +00:00
|
|
|
var adapter backup.BackupInterface
|
|
|
|
switch data.Adapter {
|
|
|
|
case backup.LocalBackupAdapter:
|
|
|
|
adapter = backup.NewLocal(data.Uuid, data.Ignore)
|
|
|
|
case backup.S3BackupAdapter:
|
|
|
|
adapter = backup.NewS3(data.Uuid, data.Ignore)
|
|
|
|
default:
|
|
|
|
middleware.CaptureAndAbort(c, errors.New("router/backups: provided adapter is not valid: "+string(data.Adapter)))
|
2020-04-26 23:43:18 +00:00
|
|
|
return
|
2020-04-26 23:21:58 +00:00
|
|
|
}
|
2021-01-18 05:05:51 +00:00
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// Attach the server ID and the request ID to the adapter log context for easier
|
|
|
|
// parsing in the logs.
|
2020-12-28 00:16:40 +00:00
|
|
|
adapter.WithLogContext(map[string]interface{}{
|
2021-01-17 02:06:22 +00:00
|
|
|
"server": s.Id(),
|
|
|
|
"request_id": c.GetString("request_id"),
|
2020-12-28 00:16:40 +00:00
|
|
|
})
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
go func(b backup.BackupInterface, s *server.Server, logger *log.Entry) {
|
|
|
|
if err := s.Backup(b); err != nil {
|
|
|
|
logger.WithField("error", errors.WithStackIf(err)).Error("router: failed to generate server backup")
|
2020-04-26 23:43:18 +00:00
|
|
|
}
|
2021-01-17 02:06:22 +00:00
|
|
|
}(adapter, s, logger)
|
2020-04-26 23:43:18 +00:00
|
|
|
|
2020-04-06 02:07:16 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
|
|
|
}
|
2020-04-10 05:07:48 +00:00
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// postServerRestoreBackup handles restoring a backup for a server by downloading
|
|
|
|
// or finding the given backup on the system and then unpacking the archive into
|
|
|
|
// the server's data directory. If the TruncateDirectory field is provided and
|
|
|
|
// is true all of the files will be deleted for the server.
|
|
|
|
//
|
|
|
|
// This endpoint will block until the backup is fully restored allowing for a
|
|
|
|
// spinner to be displayed in the Panel UI effectively.
|
2021-01-19 05:20:58 +00:00
|
|
|
//
|
|
|
|
// TODO: stop the server if it is running; internally mark it as suspended
|
2021-01-17 02:06:22 +00:00
|
|
|
func postServerRestoreBackup(c *gin.Context) {
|
|
|
|
s := middleware.ExtractServer(c)
|
|
|
|
logger := middleware.ExtractLogger(c)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Adapter backup.AdapterType `binding:"required,oneof=wings s3" json:"adapter"`
|
|
|
|
TruncateDirectory bool `json:"truncate_directory"`
|
|
|
|
// A UUID is always required for this endpoint, however the download URL
|
|
|
|
// is only present when the given adapter type is s3.
|
|
|
|
DownloadUrl string `json:"download_url"`
|
|
|
|
}
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if data.Adapter == backup.S3BackupAdapter && data.DownloadUrl == "" {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "The download_url field is required when the backup adapter is set to S3."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("processing server backup restore request")
|
|
|
|
if data.TruncateDirectory {
|
|
|
|
logger.Info(`recieved "truncate_directory" flag in request: deleting server files`)
|
|
|
|
if err := s.Filesystem().TruncateRootDirectory(); err != nil {
|
|
|
|
middleware.CaptureAndAbort(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've cleaned up the data directory if necessary, grab the backup file
|
|
|
|
// and attempt to restore it into the server directory.
|
|
|
|
if data.Adapter == backup.LocalBackupAdapter {
|
2021-01-18 05:05:51 +00:00
|
|
|
b, _, err := backup.LocateLocal(c.Param("backup"))
|
2021-01-17 02:06:22 +00:00
|
|
|
if err != nil {
|
|
|
|
middleware.CaptureAndAbort(c, err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-31 02:49:07 +00:00
|
|
|
go func(s *server.Server, b backup.BackupInterface, logger *log.Entry) {
|
|
|
|
logger.Info("starting restoration process for server backup using local driver")
|
2021-01-19 05:20:58 +00:00
|
|
|
if err := s.RestoreBackup(b, nil); err != nil {
|
|
|
|
logger.WithField("error", err).Error("failed to restore local backup to server")
|
2021-01-18 05:05:51 +00:00
|
|
|
}
|
2021-01-31 02:49:07 +00:00
|
|
|
s.Events().Publish(server.DaemonMessageEvent, "Completed server restoration from local backup.")
|
|
|
|
s.Events().Publish(server.BackupRestoreCompletedEvent, "")
|
|
|
|
logger.Info("completed server restoration from local backup")
|
|
|
|
}(s, b, logger)
|
2021-01-19 05:20:58 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
2021-01-18 05:05:51 +00:00
|
|
|
return
|
2021-01-17 02:06:22 +00:00
|
|
|
}
|
2021-01-18 05:05:51 +00:00
|
|
|
|
|
|
|
// Since this is not a local backup we need to stream the archive and then
|
|
|
|
// parse over the contents as we go in order to restore it to the server.
|
2021-01-19 05:20:58 +00:00
|
|
|
client := http.Client{}
|
|
|
|
logger.Info("downloading backup from remote location...")
|
2021-01-21 04:03:14 +00:00
|
|
|
// TODO: this will hang if there is an issue. We can't use c.Request.Context() (or really any)
|
|
|
|
// since it will be canceled when the request is closed which happens quickly since we push
|
|
|
|
// this into the background.
|
2021-01-27 03:36:35 +00:00
|
|
|
//
|
|
|
|
// For now I'm just using the server context so at least the request is canceled if
|
|
|
|
// the server gets deleted.
|
|
|
|
req, err := http.NewRequestWithContext(s.Context(), http.MethodGet, data.DownloadUrl, nil)
|
2021-01-19 05:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
middleware.CaptureAndAbort(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
middleware.CaptureAndAbort(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Don't allow content types that we know are going to give us problems.
|
|
|
|
if res.Header.Get("Content-Type") == "" || !strings.Contains("application/x-gzip application/gzip", res.Header.Get("Content-Type")) {
|
2021-01-21 04:03:14 +00:00
|
|
|
res.Body.Close()
|
2021-01-19 05:20:58 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
|
|
|
"error": "The provided backup link is not a supported content type. \"" + res.Header.Get("Content-Type") + "\" is not application/x-gzip.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-01-21 04:03:14 +00:00
|
|
|
|
2021-01-31 02:43:35 +00:00
|
|
|
go func(s *server.Server, uuid string, logger *log.Entry) {
|
|
|
|
logger.Info("starting restoration process for server backup using S3 driver")
|
2021-01-21 04:03:14 +00:00
|
|
|
if err := s.RestoreBackup(backup.NewS3(uuid, ""), res.Body); err != nil {
|
|
|
|
logger.WithField("error", errors.WithStack(err)).Error("failed to restore remote S3 backup to server")
|
2021-01-19 05:20:58 +00:00
|
|
|
}
|
2021-01-31 02:49:07 +00:00
|
|
|
s.Events().Publish(server.DaemonMessageEvent, "Completed server restoration from S3 backup.")
|
2021-01-31 02:43:35 +00:00
|
|
|
s.Events().Publish(server.BackupRestoreCompletedEvent, "")
|
|
|
|
logger.Info("completed server restoration from S3 backup")
|
|
|
|
}(s, c.Param("backup"), logger)
|
2021-01-18 05:05:51 +00:00
|
|
|
|
2021-01-19 05:20:58 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
2021-01-17 02:06:22 +00:00
|
|
|
}
|
2020-04-10 05:07:48 +00:00
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// deleteServerBackup deletes a local backup of a server. If the backup is not
|
|
|
|
// found on the machine just return a 404 error. The service calling this
|
|
|
|
// endpoint can make its own decisions as to how it wants to handle that
|
|
|
|
// response.
|
|
|
|
func deleteServerBackup(c *gin.Context) {
|
2020-04-14 05:01:07 +00:00
|
|
|
b, _, err := backup.LocateLocal(c.Param("backup"))
|
2020-04-10 05:07:48 +00:00
|
|
|
if err != nil {
|
2020-09-01 03:24:07 +00:00
|
|
|
// Just return from the function at this point if the backup was not located.
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The requested backup was not found on this server.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 02:06:22 +00:00
|
|
|
middleware.CaptureAndAbort(c, err)
|
2020-04-10 05:07:48 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-17 02:06:22 +00:00
|
|
|
// I'm not entirely sure how likely this is to happen, however if we did manage to
|
|
|
|
// locate the backup previously and it is now missing when we go to delete, just
|
|
|
|
// treat it as having been successful, rather than returning a 404.
|
|
|
|
if err := b.Remove(); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
middleware.CaptureAndAbort(c, err)
|
|
|
|
return
|
2020-04-10 05:07:48 +00:00
|
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
2021-01-18 05:05:51 +00:00
|
|
|
}
|