From b44960718155aa744d0eeafe5bbb9e9da0f52119 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 9 Oct 2020 15:31:39 +0300 Subject: [PATCH] lib/backup: add MustStop() method for all remote filesystems --- app/vmbackup/main.go | 2 ++ app/vmrestore/main.go | 1 + lib/backup/common/fs.go | 6 ++++++ lib/backup/fsnil/fsnil.go | 5 +++++ lib/backup/fsremote/fsremote.go | 5 +++++ lib/backup/gcsremote/gcs.go | 7 +++++++ lib/backup/s3remote/s3.go | 8 ++++++++ 7 files changed, 34 insertions(+) diff --git a/app/vmbackup/main.go b/app/vmbackup/main.go index 357e4c5ac..49bb72874 100644 --- a/app/vmbackup/main.go +++ b/app/vmbackup/main.go @@ -91,6 +91,8 @@ func main() { logger.Fatalf("cannot create backup: %s", err) } srcFS.MustStop() + dstFS.MustStop() + originFS.MustStop() } func usage() { diff --git a/app/vmrestore/main.go b/app/vmrestore/main.go index 1c0d225f3..0f0515bb4 100644 --- a/app/vmrestore/main.go +++ b/app/vmrestore/main.go @@ -52,6 +52,7 @@ func main() { if err := a.Run(); err != nil { logger.Fatalf("cannot restore from backup: %s", err) } + srcFS.MustStop() dstFS.MustStop() } diff --git a/lib/backup/common/fs.go b/lib/backup/common/fs.go index 94d497d5e..1282fd583 100644 --- a/lib/backup/common/fs.go +++ b/lib/backup/common/fs.go @@ -9,6 +9,9 @@ import ( // This filesystem is used for performing server-side file copies // instead of uploading data from local filesystem. type OriginFS interface { + // MustStop must be called when the RemoteFS is no longer needed. + MustStop() + // String must return human-readable representation of OriginFS. String() string @@ -18,6 +21,9 @@ type OriginFS interface { // RemoteFS is a filesystem where backups are stored. type RemoteFS interface { + // MustStop must be called when the RemoteFS is no longer needed. + MustStop() + // String must return human-readable representation of RemoteFS. String() string diff --git a/lib/backup/fsnil/fsnil.go b/lib/backup/fsnil/fsnil.go index 456a1e0c3..6ef466cdb 100644 --- a/lib/backup/fsnil/fsnil.go +++ b/lib/backup/fsnil/fsnil.go @@ -7,6 +7,11 @@ import ( // FS represents nil remote filesystem. type FS struct{} +// MustStop stops fs. +func (fs *FS) MustStop() { + // Nothing to do +} + // String returns human-readable string representation for fs. func (fs *FS) String() string { return "fsnil" diff --git a/lib/backup/fsremote/fsremote.go b/lib/backup/fsremote/fsremote.go index 4055ac9ea..3e7c65b12 100644 --- a/lib/backup/fsremote/fsremote.go +++ b/lib/backup/fsremote/fsremote.go @@ -22,6 +22,11 @@ type FS struct { Dir string } +// MustStop stops fs. +func (fs *FS) MustStop() { + // Nothing to do +} + // String returns human-readable string representation for fs. func (fs *FS) String() string { return fmt.Sprintf("fsremote %q", fs.Dir) diff --git a/lib/backup/gcsremote/gcs.go b/lib/backup/gcsremote/gcs.go index 2857c5e58..2e28da9f6 100644 --- a/lib/backup/gcsremote/gcs.go +++ b/lib/backup/gcsremote/gcs.go @@ -33,6 +33,8 @@ type FS struct { } // Init initializes fs. +// +// The returned fs must be stopped when no long needed with MustStop call. func (fs *FS) Init() error { if fs.bkt != nil { logger.Panicf("BUG: fs.Init has been already called") @@ -63,6 +65,11 @@ func (fs *FS) Init() error { return nil } +// MustStop stops fs. +func (fs *FS) MustStop() { + fs.bkt = nil +} + // String returns human-readable description for fs. func (fs *FS) String() string { return fmt.Sprintf("GCS{bucket: %q, dir: %q}", fs.Bucket, fs.Dir) diff --git a/lib/backup/s3remote/s3.go b/lib/backup/s3remote/s3.go index b6e31a6e7..9bfc1ecfb 100644 --- a/lib/backup/s3remote/s3.go +++ b/lib/backup/s3remote/s3.go @@ -45,6 +45,8 @@ type FS struct { } // Init initializes fs. +// +// The returned fs must be stopped when no long needed with MustStop call. func (fs *FS) Init() error { if fs.s3 != nil { logger.Panicf("BUG: Init is already called") @@ -96,6 +98,12 @@ func (fs *FS) Init() error { return nil } +// MustStop stops fs. +func (fs *FS) MustStop() { + fs.s3 = nil + fs.uploader = nil +} + // String returns human-readable description for fs. func (fs *FS) String() string { return fmt.Sprintf("S3{bucket: %q, dir: %q}", fs.Bucket, fs.Dir)