2020-04-06 02:07:16 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-04-26 23:21:58 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-06 02:07:16 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"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
|
|
|
"net/http"
|
2020-09-01 03:24:07 +00:00
|
|
|
"os"
|
2020-04-06 02:07:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Backs up a server.
|
|
|
|
func postServerBackup(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-04-26 23:21:58 +00:00
|
|
|
data := &backup.Request{}
|
2020-05-29 15:44:49 +00:00
|
|
|
// BindJSON sends 400 if the request fails, all we need to do is return
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 02:07:16 +00:00
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
var adapter backup.BackupInterface
|
2020-04-26 23:43:18 +00:00
|
|
|
var err error
|
|
|
|
|
2020-04-26 23:21:58 +00:00
|
|
|
switch data.Adapter {
|
|
|
|
case backup.LocalBackupAdapter:
|
2020-04-26 23:43:18 +00:00
|
|
|
adapter, err = data.NewLocalBackup()
|
2020-04-26 23:21:58 +00:00
|
|
|
case backup.S3BackupAdapter:
|
2020-04-26 23:43:18 +00:00
|
|
|
adapter, err = data.NewS3Backup()
|
2020-04-26 23:21:58 +00:00
|
|
|
default:
|
2020-04-26 23:43:18 +00:00
|
|
|
err = errors.New(fmt.Sprintf("unknown backup adapter [%s] provided", data.Adapter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-16 05:08:00 +00:00
|
|
|
NewServerError(err, s).Abort(c)
|
2020-04-26 23:43:18 +00:00
|
|
|
return
|
2020-04-26 23:21:58 +00:00
|
|
|
}
|
2020-04-06 02:07:16 +00:00
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
go func(b backup.BackupInterface, serv *server.Server) {
|
2020-04-26 23:43:18 +00:00
|
|
|
if err := serv.Backup(b); err != nil {
|
2020-06-13 17:40:26 +00:00
|
|
|
serv.Log().WithField("error", err).Error("failed to generate backup for server")
|
2020-04-26 23:43:18 +00:00
|
|
|
}
|
|
|
|
}(adapter, s)
|
|
|
|
|
2020-04-06 02:07:16 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
|
|
|
}
|
2020-04-10 05:07:48 +00:00
|
|
|
|
2020-09-01 03:24:07 +00:00
|
|
|
// 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.
|
2020-04-10 05:07:48 +00:00
|
|
|
func deleteServerBackup(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
NewServerError(err, s).Abort(c)
|
2020-04-10 05:07:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
if err := b.Remove(); err != nil {
|
2020-09-01 03:24:07 +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 !errors.Is(err, os.ErrNotExist) {
|
2020-12-16 05:08:00 +00:00
|
|
|
NewServerError(err, s).Abort(c)
|
2020-09-01 03:24:07 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-10 05:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
2020-04-26 23:21:58 +00:00
|
|
|
}
|