Fix import cycle issue

This commit is contained in:
Dane Everitt
2021-01-17 21:05:51 -08:00
parent 7dd0acebc0
commit 66b6f40b61
9 changed files with 193 additions and 199 deletions

View File

@@ -95,6 +95,7 @@ func Configure() *gin.Engine {
backup := server.Group("/backup")
{
backup.POST("", postServerBackup)
backup.POST("/:backup/restore", postServerRestoreBackup)
backup.DELETE("/:backup", deleteServerBackup)
}
}

View File

@@ -7,9 +7,11 @@ import (
"emperror.dev/errors"
"github.com/apex/log"
"github.com/gin-gonic/gin"
"github.com/mholt/archiver/v3"
"github.com/pterodactyl/wings/router/middleware"
"github.com/pterodactyl/wings/server"
"github.com/pterodactyl/wings/server/backup"
"github.com/pterodactyl/wings/system"
)
// postServerBackup performs a backup against a given server instance using the
@@ -17,16 +19,26 @@ import (
func postServerBackup(c *gin.Context) {
s := middleware.ExtractServer(c)
logger := middleware.ExtractLogger(c)
var data backup.Request
var data struct {
Adapter backup.AdapterType `json:"adapter"`
Uuid string `json:"uuid"`
Ignore string `json:"ignore"`
}
if err := c.BindJSON(&data); err != nil {
return
}
adapter, err := data.AsBackup()
if err != nil {
middleware.CaptureAndAbort(c, err)
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)))
return
}
// Attach the server ID and the request ID to the adapter log context for easier
// parsing in the logs.
adapter.WithLogContext(map[string]interface{}{
@@ -55,7 +67,6 @@ func postServerRestoreBackup(c *gin.Context) {
logger := middleware.ExtractLogger(c)
var data struct {
UUID string `binding:"required,uuid" json:"uuid"`
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
@@ -82,16 +93,33 @@ func postServerRestoreBackup(c *gin.Context) {
// 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 {
b, _, err := backup.LocateLocal(data.UUID)
b, _, err := backup.LocateLocal(c.Param("backup"))
if err != nil {
middleware.CaptureAndAbort(c, err)
return
}
if err := b.Restore(s); err != nil {
// Restore restores a backup to the provided server's root data directory.
err = archiver.Walk(b.Path(), func(f archiver.File) error {
if f.IsDir() {
return nil
}
name, err := system.ExtractArchiveSourceName(f, "/")
if err != nil {
return err
}
return s.Filesystem().Writefile(name, f)
})
if err != nil {
middleware.CaptureAndAbort(c, err)
return
}
c.Status(http.StatusNoContent)
return
}
// 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.
c.Status(http.StatusNoContent)
}
@@ -120,4 +148,4 @@ func deleteServerBackup(c *gin.Context) {
return
}
c.Status(http.StatusNoContent)
}
}