Move over backup and create transfer logic

This commit is contained in:
Dane Everitt 2020-04-05 19:07:16 -07:00
parent 5bb6dff277
commit 03311ecf03
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 76 additions and 73 deletions

60
http.go
View File

@ -22,7 +22,6 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time"
) )
// Retrieves a server out of the collection by UUID. // Retrieves a server out of the collection by UUID.
@ -96,62 +95,6 @@ func (rt *Router) AuthenticateToken(h httprouter.Handle) httprouter.Handle {
} }
} }
func (rt *Router) routeServerBackup(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s := rt.GetServer(ps.ByName("server"))
defer r.Body.Close()
data := rt.ReaderToBytes(r.Body)
b, err := s.NewBackup(data)
if err != nil {
zap.S().Errorw("failed to create backup struct for server", zap.String("server", s.Uuid), zap.Error(err))
http.Error(w, "failed to update data structure", http.StatusInternalServerError)
return
}
zap.S().Infow("starting backup process for server", zap.String("server", s.Uuid), zap.String("backup", b.Uuid))
go func(bk *server.Backup) {
if err := bk.BackupAndNotify(); err != nil {
zap.S().Errorw("failed to generate backup for server", zap.Error(err))
} else {
zap.S().Infow("completed backup process for server", zap.String("backup", b.Uuid))
}
}(b)
w.WriteHeader(http.StatusAccepted)
}
func (rt *Router) routeRequestServerArchive(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
s := rt.GetServer(ps.ByName("server"))
go func() {
start := time.Now()
if err := s.Archiver.Archive(); err != nil {
zap.S().Errorw("failed to get archive for server", zap.String("server", s.Uuid), zap.Error(err))
return
}
zap.S().Debugw("successfully created archive for server", zap.String("server", s.Uuid), zap.Duration("time", time.Now().Sub(start).Round(time.Microsecond)))
r := api.NewRequester()
rerr, err := r.SendArchiveStatus(s.Uuid, true)
if rerr != nil || err != nil {
if err != nil {
zap.S().Errorw("failed to notify panel with archive status", zap.String("server", s.Uuid), zap.Error(err))
return
}
zap.S().Errorw("panel returned an error when sending the archive status", zap.String("server", s.Uuid), zap.Error(errors.New(rerr.String())))
return
}
zap.S().Debugw("successfully notified panel about archive status", zap.String("server", s.Uuid))
}()
w.WriteHeader(http.StatusAccepted)
}
func (rt *Router) routeGetServerArchive(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (rt *Router) routeGetServerArchive(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
@ -396,9 +339,6 @@ func (rt *Router) ReaderToBytes(r io.Reader) []byte {
func (rt *Router) ConfigureRouter() *httprouter.Router { func (rt *Router) ConfigureRouter() *httprouter.Router {
router := httprouter.New() router := httprouter.New()
router.POST("/api/servers/:server/backup", rt.AuthenticateRequest(rt.routeServerBackup))
router.POST("/api/servers/:server/archive", rt.AuthenticateRequest(rt.routeRequestServerArchive))
router.GET("/api/servers/:server/archive", rt.AuthenticateServer(rt.routeGetServerArchive)) router.GET("/api/servers/:server/archive", rt.AuthenticateServer(rt.routeGetServerArchive))
router.POST("/api/transfer", rt.AuthenticateToken(rt.routeIncomingTransfer)) router.POST("/api/transfer", rt.AuthenticateToken(rt.routeIncomingTransfer))

View File

@ -36,6 +36,10 @@ func Configure() *gin.Engine {
server.POST("/commands", postServerCommands) server.POST("/commands", postServerCommands)
server.POST("/install", postServerInstall) server.POST("/install", postServerInstall)
server.POST("/reinstall", postServerReinstall) server.POST("/reinstall", postServerReinstall)
server.POST("/backup", postServerBackup)
server.GET("/archive", getServerArchive)
server.POST("/archive", postServerArchive)
files := server.Group("/files") files := server.Group("/files")
{ {

View File

@ -0,0 +1,64 @@
package router
import (
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/api"
"github.com/pterodactyl/wings/server"
"go.uber.org/zap"
"net/http"
"time"
)
// Backs up a server.
func postServerBackup(c *gin.Context) {
s := GetServer(c.Param("server"))
var data struct{
Uuid string `json:"uuid"`
IgnoredFiles []string `json:"ignored_files"`
}
c.BindJSON(&data)
go func(backup *server.Backup) {
if err := backup.BackupAndNotify(); err != nil {
zap.S().Errorw("failed to generate backup for server", zap.Error(err))
}
}(s.NewBackup(data.Uuid, data.IgnoredFiles))
c.Status(http.StatusAccepted)
}
func getServerArchive(c *gin.Context) {
}
func postServerArchive(c *gin.Context) {
s := GetServer(c.Param("server"))
go func() {
start := time.Now()
if err := s.Archiver.Archive(); err != nil {
zap.S().Errorw("failed to get archive for server", zap.String("server", s.Uuid), zap.Error(err))
return
}
zap.S().Debugw("successfully created archive for server", zap.String("server", s.Uuid), zap.Duration("time", time.Now().Sub(start).Round(time.Microsecond)))
r := api.NewRequester()
rerr, err := r.SendArchiveStatus(s.Uuid, true)
if rerr != nil || err != nil {
if err != nil {
zap.S().Errorw("failed to notify panel with archive status", zap.String("server", s.Uuid), zap.Error(err))
return
}
zap.S().Errorw("panel returned an error when sending the archive status", zap.String("server", s.Uuid), zap.Error(errors.New(rerr.String())))
return
}
zap.S().Debugw("successfully notified panel about archive status", zap.String("server", s.Uuid))
}()
c.Status(http.StatusAccepted)
}

View File

@ -3,7 +3,6 @@ package server
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json"
"github.com/mholt/archiver/v3" "github.com/mholt/archiver/v3"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/api"
@ -24,17 +23,13 @@ type Backup struct {
} }
// Create a new Backup struct from data passed through in a request. // Create a new Backup struct from data passed through in a request.
func (s *Server) NewBackup(data []byte) (*Backup, error) { func (s *Server) NewBackup(uuid string, ignore []string) *Backup {
backup := &Backup{} return &Backup{
Uuid: uuid,
if err := json.Unmarshal(data, backup); err != nil { IgnoredFiles: ignore,
return nil, errors.WithStack(err) server: s,
localDirectory: path.Join(config.Get().System.BackupDirectory, s.Uuid),
} }
backup.server = s
backup.localDirectory = path.Join(config.Get().System.BackupDirectory, s.Uuid)
return backup, nil
} }
// Locates the backup for a server and returns the local path. This will obviously only // Locates the backup for a server and returns the local path. This will obviously only