2020-04-04 23:07:25 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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-14 05:01:07 +00:00
|
|
|
// 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.
|
2020-04-17 20:46:36 +00:00
|
|
|
func (s *Server) BackupLocal(b *backup.LocalBackup) error {
|
|
|
|
if err := b.Backup(s.Filesystem.Path()); err != nil {
|
|
|
|
if notifyError := s.notifyPanelOfBackup(b.Identifier(), &backup.ArchiveDetails{}, false); notifyError != nil {
|
2020-04-14 05:01:07 +00:00
|
|
|
zap.S().Warnw("failed to notify panel of failed backup state", zap.String("backup", b.Uuid), 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-14 05:01:07 +00:00
|
|
|
s.Events().PublishJson(BackupCompletedEvent+":"+b.Uuid, map[string]interface{}{
|
2020-04-07 04:22:43 +00:00
|
|
|
"uuid": b.Uuid,
|
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-17 20:46:36 +00:00
|
|
|
}
|