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

Simplify bucket emptying mechanism #469

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
45 changes: 4 additions & 41 deletions task/aws/resources/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,54 +78,17 @@ func (b *Bucket) Update(ctx context.Context) error {
}

func (b *Bucket) Delete(ctx context.Context) error {
listInput := s3.ListObjectsV2Input{
input := s3.DeleteBucketInput{
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved
Bucket: aws.String(b.Identifier),
}

for paginator := s3.NewListObjectsV2Paginator(b.Client.Services.S3, &listInput); paginator.HasMorePages(); {
page, err := paginator.NextPage(ctx)

if err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() == "NoSuchBucket" {
b.Resource = nil
return nil
}
return err
}

if len(page.Contents) == 0 {
break
}

var objects []types.ObjectIdentifier
for _, object := range page.Contents {
objects = append(objects, types.ObjectIdentifier{
Key: object.Key,
})
}

input := s3.DeleteObjectsInput{
Bucket: aws.String(b.Identifier),
Delete: &types.Delete{
Objects: objects,
},
}

if _, err = b.Client.Services.S3.DeleteObjects(ctx, &input); err != nil {
if _, err := b.Client.Services.S3.DeleteBucket(ctx, &input); err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() != "NoSuchBucket" {
return err
}
}

deleteInput := s3.DeleteBucketInput{
Bucket: aws.String(b.Identifier),
}

_, err := b.Client.Services.S3.DeleteBucket(ctx, &deleteInput)
if err != nil {
return err
}

b.Resource = nil
return nil
}
2 changes: 1 addition & 1 deletion task/aws/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Info("[1/8] Downloading Directory...")
if t.Read(ctx) == nil {
if t.Resources.Bucket.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down
2 changes: 1 addition & 1 deletion task/az/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Info("[1/9] Downloading Directory...")
if t.Read(ctx) == nil {
if t.Resources.BlobContainer.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down
16 changes: 16 additions & 0 deletions task/common/machine/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ func Delete(ctx context.Context, destination string) error {
return err
}

ctx, fi := filter.AddConfig(ctx)
if err := fi.AddRule("+ **"); err != nil {
return err
}

operations.SyncPrintf = func(format string, a ...interface{}) {
logrus.Infof(format, a...)
}
fs.LogPrint = func(level fs.LogLevel, text string) {
logrus.Info(text)
}

ctx, ci := fs.AddConfig(ctx)
ci.LogLevel = fs.LogLevelDebug
ci.StatsLogLevel = fs.LogLevelDebug

actions := []func(context.Context) error{
func(ctx context.Context) error {
return operations.Delete(ctx, destinationFileSystem)
Expand Down
22 changes: 4 additions & 18 deletions task/gcp/resources/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,11 @@ func (b *Bucket) Update(ctx context.Context) error {
}

func (b *Bucket) Delete(ctx context.Context) error {
if b.Read(ctx) == common.NotFoundError {
return nil
}

deletePage := func(objects *storage.Objects) error {
for _, object := range objects.Items {
if err := b.Client.Services.Storage.Objects.Delete(b.Identifier, object.Name).Do(); err != nil {
return err
}
}
return nil
}

if err := b.Client.Services.Storage.Objects.List(b.Identifier).Pages(ctx, deletePage); err != nil {
return err
}

if err := b.Client.Services.Storage.Buckets.Delete(b.Identifier).Do(); err != nil {
return err
var e *googleapi.Error
if errors.As(err, &e) && e.Code != 404 {
return err
}
}

b.Resource = nil
Expand Down
4 changes: 2 additions & 2 deletions task/gcp/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ func (t *Task) Read(ctx context.Context) error {
}

func (t *Task) Delete(ctx context.Context) error {
logrus.Info("[1/11] Downloading Directory...")
if t.Read(ctx) == nil {
logrus.Debug("[1/11] Downloading Directory...")
if t.Resources.Bucket.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down