Skip to content

Commit

Permalink
migrate snapshot file utilities to unified storage func
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Oct 1, 2024
1 parent e232a22 commit 6e4efe5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
23 changes: 3 additions & 20 deletions storage/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package storage

import (
"errors"
"io"
"log"
"os"
"path/filepath"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -35,35 +34,19 @@ func newAWSConfig(backupAWSConfig *util.AWSConfig) (*awsConfig, error) {
}

// snapshot upload to aws s3
func snapshotS3Upload(config *awsConfig, snapshotPath string, cleanup bool) (*s3manager.UploadOutput, error) {
// open snapshot file
snapshotFile, err := os.Open(snapshotPath)
if err != nil {
log.Printf("failed to open snapshot file %q: %v", snapshotPath, err)
return nil, err
}
// defer snapshot close and remove
defer func() {
err = util.SnapshotFileClose(snapshotFile)
if cleanup {
err = util.SnapshotFileRemove(snapshotFile)
}
}()

func snapshotS3Upload(config *awsConfig, snapshotFile io.Reader, snapshotName string) (*s3manager.UploadOutput, error) {
// aws session with configuration populated automatically
awsSession := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))

// initialize an s3 uploader with the session and default options
uploader := s3manager.NewUploader(awsSession)
// determine vault backup base filename for s3 key
snapshotPathBase := filepath.Base(snapshotPath)

// upload the snapshot file to the s3 bucket at specified key
uploadResult, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(config.s3Bucket),
Key: aws.String(config.s3Prefix + "-" + snapshotPathBase),
Key: aws.String(snapshotName),
Body: snapshotFile,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions storage/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func TestNewAWSConfig(test *testing.T) {
func TestSnapshotS3Upload(test *testing.T) {
// test this fails at s3upload
os.Setenv("AWS_REGION", "us-west-1")
_, err := os.Create("./foo")
fooFile, err := os.Create("./foo")
if err != nil {
test.Error("test short-circuited because file could not be created and opened")
}

if _, err := snapshotS3Upload(&expectedConfig, "./foo", true); err == nil || !strings.Contains(err.Error(), "NoCredentialProviders: no valid providers in chain") {
if _, err := snapshotS3Upload(&expectedConfig, fooFile, "prefix-foo"); err == nil || !strings.Contains(err.Error(), "NoCredentialProviders: no valid providers in chain") {
test.Errorf("expected error (contains): NoCredentialProviders: no valid providers in chain, actual: %v", err)
}
}
31 changes: 29 additions & 2 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
package storage

import "github.com/aws/aws-sdk-go/service/s3/s3manager"
import (
"log"
"os"
"path/filepath"

"github.com/aws/aws-sdk-go/service/s3/s3manager"

"github.com/mschuchard/vault-raft-backup/util"
)

func StorageTransfer(config *awsConfig, snapshotPath string, cleanup bool) (*s3manager.UploadOutput, error) {
return snapshotS3Upload(config, snapshotPath, cleanup)
// use supplied prefix and snapshot base filename for full name
snapshotName := config.s3Prefix + "-" + filepath.Base(snapshotPath)

// open snapshot file
snapshotFile, err := os.Open(snapshotPath)
if err != nil {
log.Printf("failed to open snapshot file %q: %v", snapshotPath, err)
return nil, err
}

// defer snapshot close and remove
defer func() {
err = util.SnapshotFileClose(snapshotFile)
if cleanup {
err = util.SnapshotFileRemove(snapshotFile)
}
}()

// TODO: clobbers deferred err from snapshot close and remove
return snapshotS3Upload(config, snapshotFile, snapshotName)
}

0 comments on commit 6e4efe5

Please sign in to comment.