2020-04-04 23:07:25 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-04-20 02:52:19 +00:00
|
|
|
"bufio"
|
2020-04-04 23:07:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-17 20:46:36 +00:00
|
|
|
"github.com/pterodactyl/wings/api"
|
2020-04-14 05:01:07 +00:00
|
|
|
"github.com/pterodactyl/wings/server/backup"
|
2020-04-04 23:07:25 +00:00
|
|
|
"go.uber.org/zap"
|
2020-04-20 02:52:19 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-04 23:07:25 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
// Notifies the panel of a backup's state and returns an error if one is encountered
|
|
|
|
// while performing this action.
|
|
|
|
func (s *Server) notifyPanelOfBackup(uuid string, ad *backup.ArchiveDetails, successful bool) error {
|
|
|
|
r := api.NewRequester()
|
|
|
|
rerr, err := r.SendBackupStatus(uuid, ad.ToRequest(successful))
|
|
|
|
if rerr != nil || err != nil {
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorw(
|
|
|
|
"failed to notify panel of backup status due to internal code error",
|
|
|
|
zap.String("backup", s.Uuid),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.S().Warnw(rerr.String(), zap.String("backup", uuid))
|
|
|
|
|
|
|
|
return errors.New(rerr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-26 23:43:18 +00:00
|
|
|
// Get all of the ignored files for a server based on its .pteroignore file in the root.
|
|
|
|
func (s *Server) getServerwideIgnoredFiles() ([]string, error) {
|
|
|
|
var ignored []string
|
|
|
|
|
|
|
|
f, err := os.Open(path.Join(s.Filesystem.Path(), ".pteroignore"))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
// Only include non-empty lines, for the sake of clarity...
|
|
|
|
if t := scanner.Text(); t != "" {
|
|
|
|
ignored = append(ignored, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ignored, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the backup files to include when generating it.
|
|
|
|
func (s *Server) GetIncludedBackupFiles(ignored []string) (*backup.IncludedFiles, error) {
|
2020-04-20 02:52:19 +00:00
|
|
|
// If no ignored files are present in the request, check for a .pteroignore file in the root
|
|
|
|
// of the server files directory, and use that to generate the backup.
|
2020-04-26 23:43:18 +00:00
|
|
|
if len(ignored) == 0 {
|
|
|
|
if i, err := s.getServerwideIgnoredFiles(); err != nil {
|
|
|
|
zap.S().Warnw("failed to retrieve server ignored files", zap.String("server", s.Uuid), zap.Error(err))
|
2020-04-20 02:52:19 +00:00
|
|
|
} else {
|
2020-04-26 23:43:18 +00:00
|
|
|
ignored = i
|
2020-04-20 02:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the included files based on the root path and the ignored files provided.
|
2020-04-26 23:43:18 +00:00
|
|
|
return s.Filesystem.GetIncludedFiles(s.Filesystem.Path(), ignored)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs a server backup and then emits the event over the server websocket. We
|
|
|
|
// let the actual backup system handle notifying the panel of the status, but that
|
|
|
|
// won't emit a websocket event.
|
|
|
|
func (s *Server) Backup(b backup.Backup) error {
|
|
|
|
// Get the included files based on the root path and the ignored files provided.
|
|
|
|
inc, err := s.GetIncludedBackupFiles(b.Ignored())
|
2020-04-19 06:26:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Backup(inc, s.Filesystem.Path()); err != nil {
|
2020-04-17 20:46:36 +00:00
|
|
|
if notifyError := s.notifyPanelOfBackup(b.Identifier(), &backup.ArchiveDetails{}, false); notifyError != nil {
|
2020-04-26 23:43:18 +00:00
|
|
|
zap.S().Warnw("failed to notify panel of failed backup state", zap.String("backup", b.Identifier()), zap.Error(err))
|
2020-04-04 23:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
// Try to notify the panel about the status of this backup. If for some reason this request
|
|
|
|
// fails, delete the archive from the daemon and return that error up the chain to the caller.
|
2020-04-17 20:46:36 +00:00
|
|
|
ad := b.Details()
|
|
|
|
if notifyError := s.notifyPanelOfBackup(b.Identifier(), ad, true); notifyError != nil {
|
2020-04-14 05:01:07 +00:00
|
|
|
b.Remove()
|
2020-04-04 23:07:25 +00:00
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
return notifyError
|
2020-04-04 23:07:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 04:03:50 +00:00
|
|
|
// Emit an event over the socket so we can update the backup in realtime on
|
|
|
|
// the frontend for the server.
|
2020-04-26 23:43:18 +00:00
|
|
|
s.Events().PublishJson(BackupCompletedEvent+":"+b.Identifier(), map[string]interface{}{
|
|
|
|
"uuid": b.Identifier(),
|
2020-04-17 20:46:36 +00:00
|
|
|
"sha256_hash": ad.Checksum,
|
|
|
|
"file_size": ad.Size,
|
2020-04-07 04:03:50 +00:00
|
|
|
})
|
|
|
|
|
2020-04-04 23:07:25 +00:00
|
|
|
return nil
|
2020-04-26 23:43:18 +00:00
|
|
|
}
|