Skip to content

Commit

Permalink
Delete backups on server delete (#13)
Browse files Browse the repository at this point in the history
Deletes all backups unless the wings config System.Backups.RemoveBackupsOnServerDelete is set to false.
  • Loading branch information
iamkubi authored May 11, 2024
1 parent 6d57db3 commit ad098f9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ type Backups struct {
//
// Defaults to "best_speed" (level 1)
CompressionLevel string `default:"best_speed" yaml:"compression_level"`

// RemoveBackupsOnServerDelete deletes backups associated with a server when the server is deleted
RemoveBackupsOnServerDelete bool `default:"true" yaml:"remove_backups_on_server_delete"`
}

type Transfers struct {
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down Expand Up @@ -674,4 +677,4 @@ nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYm
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
3 changes: 3 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func Configure(m *wserver.Manager, client remote.Client) *gin.Engine {
server.POST("/transfer", postServerTransfer)
server.DELETE("/transfer", deleteServerTransfer)

// Deletes all backups for a server
server.DELETE("deleteAllBackups", deleteAllServerBackups)

files := server.Group("/files")
{
files.GET("/contents", getServerFileContents)
Expand Down
19 changes: 19 additions & 0 deletions router/router_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"context"
"github.com/pelican-dev/wings/config"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -213,6 +214,14 @@ func deleteServer(c *gin.Context) {
dl.Cancel()
}

// Remove all server backups unless config setting is specified
if config.Get().System.Backups.RemoveBackupsOnServerDelete == true {
if err := s.RemoveAllServerBackups(); err != nil {
middleware.CaptureAndAbort(c, err)
return
}
}

// 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
// that here.
Expand Down Expand Up @@ -261,3 +270,13 @@ func postServerDenyWSTokens(c *gin.Context) {

c.Status(http.StatusNoContent)
}

func deleteAllServerBackups(c *gin.Context) {
s := ExtractServer(c)

if err := s.RemoveAllServerBackups(); err != nil {
middleware.CaptureAndAbort(c, err)
} else {
c.Status(http.StatusNoContent)
}
}
10 changes: 10 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -391,3 +392,12 @@ func (s *Server) ToAPIResponse() APIResponse {
Configuration: *s.Config(),
}
}

func (s *Server) RemoveAllServerBackups() error {
sp := path.Join(config.Get().System.BackupDirectory, s.ID())
// This should never be possible, but we'll check it anyway.
if sp == config.Get().System.BackupDirectory {
return errors.New("invalid server, cannot delete backup dir")
}
return os.RemoveAll(sp)
}

0 comments on commit ad098f9

Please sign in to comment.