Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to supply a glob for matching files to remove from S3 #61

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/drone-plugins/drone-s3

require (
github.com/aws/aws-sdk-go v1.16.17
github.com/bmatcuk/doublestar v1.1.1
github.com/joho/godotenv v1.3.0
github.com/mattn/go-zglob v0.0.1
github.com/sirupsen/logrus v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/aws/aws-sdk-go v1.16.17 h1:hHRKZhoB4qEY17aGNp71UxQFyYpx6WZXGMUzx9y/A4w=
github.com/aws/aws-sdk-go v1.16.17/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/bmatcuk/doublestar v1.1.1 h1:YroD6BJCZBYx06yYFEWvUuKVWQn3vLLQAVmDmvTSaiQ=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func main() {
Name: "env-file",
Usage: "source env file",
},
cli.StringFlag{
Name: "target-remove",
Usage: "glob for which files to remove from target",
EnvVar: "PLUGIN_TARGET_REMOVE",
},
}

if err := app.Run(os.Args); err != nil {
Expand Down Expand Up @@ -125,6 +130,7 @@ func run(c *cli.Context) error {
CacheControl: c.String("cache-control"),
PathStyle: c.Bool("path-style"),
DryRun: c.Bool("dry-run"),
TargetRemove: c.String("target-remove"),
}

return plugin.Exec()
Expand Down
80 changes: 80 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/bmatcuk/doublestar"
"github.com/mattn/go-zglob"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -74,6 +75,8 @@ type Plugin struct {
PathStyle bool
// Dry run without uploading/
DryRun bool
// Glob for which files to remove from target
TargetRemove string
}

// Exec runs the plugin
Expand Down Expand Up @@ -114,6 +117,83 @@ func (p *Plugin) Exec() error {
return err
}

if len(p.TargetRemove) != 0 {

log.WithFields(log.Fields{
"glob": p.TargetRemove,
}).Info("Deleting files according to glob")

log.Info("Listing files in bucket")
list_input := &s3.ListObjectsInput{
tboerger marked this conversation as resolved.
Show resolved Hide resolved
Bucket: &p.Bucket,
}

s3_objects, list_err := client.ListObjects(list_input)
tboerger marked this conversation as resolved.
Show resolved Hide resolved
if list_err != nil {
log.WithFields(log.Fields{
"error": list_err,
}).Error("Error listing objects from bucket")
return list_err
}

var to_remove []string
tboerger marked this conversation as resolved.
Show resolved Hide resolved
for _, object := range s3_objects.Contents {
filename := object.Key

globmatch, globerr := doublestar.PathMatch(p.TargetRemove, *filename)
tboerger marked this conversation as resolved.
Show resolved Hide resolved

if globerr != nil {
log.WithFields(log.Fields{
"error": globerr,
"glob": p.TargetRemove,
}).Error("Error with provided glob")
return globerr
}

if globmatch {
to_remove = append(to_remove, *filename)
}
}

if len(to_remove) > 0 {
log.WithFields(log.Fields{
"num_files": len(to_remove),
}).Info("Deleting files from bucket")

var remove_identifiers []*s3.ObjectIdentifier
tboerger marked this conversation as resolved.
Show resolved Hide resolved
for _, key := range to_remove {
id := s3.ObjectIdentifier{
Key: aws.String(key),
}
remove_identifiers = append(remove_identifiers, &id)
}

delete_input := &s3.DeleteObjectsInput{
tboerger marked this conversation as resolved.
Show resolved Hide resolved
Bucket: &p.Bucket,
Delete: &s3.Delete{
Objects: remove_identifiers,
Quiet: aws.Bool(false),
},
}

// when executing a dry-run we skip this step because we don't actually
// want to remove files from S3.
if !p.DryRun {
log.WithFields(log.Fields{
"num_files": len(remove_identifiers),
}).Info("Attempting to delete files")
_, delete_err := client.DeleteObjects(delete_input)
tboerger marked this conversation as resolved.
Show resolved Hide resolved

if delete_err != nil {
log.WithFields(log.Fields{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to add log in err != nil condition since main.go can handle this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the file currently does this for other code, i.e. here:

drone-s3/plugin.go

Lines 110 to 113 in 758ad2d

if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Could not match files")

I'm fine with removing my additions which do this, though. The only concern is that maybe the error messages from S3 are more obscure than the ones provided by the logs.

"error": delete_err,
}).Error("Error deleting objects from S3")
return delete_err
}
}
}
}

for _, match := range matches {

stat, err := os.Stat(match)
Expand Down