From c096d8802f2f3253fd898f893eda5ecc78b1d7af Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 4 Apr 2020 00:17:32 -0600 Subject: [PATCH] Add configurable ArchiveDirectory --- config/config.go | 3 +++ server/archiver.go | 18 +++++++++++++++--- server/update.go | 11 +++++++++-- wings.go | 5 +++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index ddb905a..e0ffe1e 100644 --- a/config/config.go +++ b/config/config.go @@ -66,6 +66,9 @@ type SystemConfiguration struct { // Directory where the server data is stored at. Data string `default:"/srv/daemon-data" yaml:"data"` + // Directory where server archives for transferring will be stored. + ArchiveDirectory string `default:"/srv/daemon-data/.archives" yaml:"archive_directory"` + // The user that should own all of the server files, and be used for containers. Username string `default:"pterodactyl" yaml:"username"` diff --git a/server/archiver.go b/server/archiver.go index 5e8a88d..61fbc75 100644 --- a/server/archiver.go +++ b/server/archiver.go @@ -18,7 +18,7 @@ type Archiver struct { // ArchivePath returns the path to the server's archive. func (a *Archiver) ArchivePath() string { - return filepath.Join(config.Get().System.Data, ".archives", a.ArchiveName()) + return filepath.Join(config.Get().System.ArchiveDirectory, a.ArchiveName()) } // ArchiveName returns the name of the server's archive. @@ -35,12 +35,12 @@ func (a *Archiver) Exists() bool { return true } -// Stat . +// Stat stats the archive file. func (a *Archiver) Stat() (*Stat, error) { return a.Server.Filesystem.unsafeStat(a.ArchivePath()) } -// Archive creates an archive of the server. +// Archive creates an archive of the server and deletes the previous one. func (a *Archiver) Archive() error { path := a.Server.Filesystem.Path() @@ -55,6 +55,18 @@ func (a *Archiver) Archive() error { files = append(files, filepath.Join(path, file.Name())) } + stat, err := a.Stat() + if err != nil && !os.IsNotExist(err) { + return err + } + + // Check if the file exists. + if stat != nil { + if err := os.Remove(a.ArchivePath()); err != nil { + return err + } + } + return archiver.NewTarGz().Archive(files, a.ArchivePath()) } diff --git a/server/update.go b/server/update.go index 27e3be5..9bc8307 100644 --- a/server/update.go +++ b/server/update.go @@ -6,7 +6,6 @@ import ( "github.com/imdario/mergo" "github.com/pkg/errors" "go.uber.org/zap" - "os" ) // Merges data passed through in JSON form into the existing server object. @@ -101,12 +100,20 @@ func (s *Server) runBackgroundActions() { if server.Suspended && server.State != ProcessOfflineState { zap.S().Infow("server suspended with running process state, terminating now", zap.String("server", server.Uuid)) - if err := server.Environment.Terminate(os.Kill); err != nil { + /*if err := server.Environment.Terminate(os.Kill); err != nil { zap.S().Warnw( "failed to terminate server environment after seeing suspension", zap.String("server", server.Uuid), zap.Error(err), ) + }*/ + + if err := server.Environment.WaitForStop(10, true); err != nil { + zap.S().Warnw( + "failed to stop server environment after seeing suspension", + zap.String("server", server.Uuid), + zap.Error(err), + ) } } }(s) diff --git a/wings.go b/wings.go index 15a71e9..adda06e 100644 --- a/wings.go +++ b/wings.go @@ -144,6 +144,11 @@ func main() { sftp.Initialize(c) } + // Ensure the archive directory exists. + if err := os.MkdirAll(c.System.ArchiveDirectory, 0755); err != nil { + zap.S().Errorw("failed to create archive directory", zap.Error(err)) + } + r := &Router{ token: c.AuthenticationToken, upgrader: websocket.Upgrader{