Delete the server's archive when the server is deleted

This commit is contained in:
Matthew Penner 2020-04-04 18:31:18 -06:00
parent 8da9d45c9d
commit 0ca9c8a114
2 changed files with 23 additions and 0 deletions

View File

@ -565,6 +565,12 @@ func (rt *Router) routeServerDelete(w http.ResponseWriter, r *http.Request, ps h
// to start it while this process is running. // to start it while this process is running.
s.Suspended = true s.Suspended = true
// Delete the server's archive if it exists.
if err := s.Archiver.DeleteIfExists(); err != nil {
zap.S().Errorw("failed to delete server's archive", zap.String("server", s.Uuid), zap.Error(err))
// We intentionally don't return here, if the archive fails to delete, the server can still be removed.
}
zap.S().Infow("processing server deletion request", zap.String("server", s.Uuid)) zap.S().Infow("processing server deletion request", zap.String("server", s.Uuid))
// Destroy the environment; in Docker this will handle a running container and // Destroy the environment; in Docker this will handle a running container and
// forcibly terminate it before removing the container, so we do not need to handle // forcibly terminate it before removing the container, so we do not need to handle

View File

@ -70,6 +70,23 @@ func (a *Archiver) Archive() error {
return archiver.NewTarGz().Archive(files, a.ArchivePath()) return archiver.NewTarGz().Archive(files, a.ArchivePath())
} }
// DeleteIfExists deletes the archive if it exists.
func (a *Archiver) DeleteIfExists() error {
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 nil
}
// Checksum computes a SHA256 checksum of the server's archive. // Checksum computes a SHA256 checksum of the server's archive.
func (a *Archiver) Checksum() (string, error) { func (a *Archiver) Checksum() (string, error) {
file, err := os.Open(a.ArchivePath()) file, err := os.Open(a.ArchivePath())