From d8b229b59a3910e52b3e5094f7dba6be50dc6093 Mon Sep 17 00:00:00 2001 From: Sean Malloy Date: Mon, 2 Jul 2018 21:44:30 -0500 Subject: [PATCH] Add feature to googlecompute-import post-processor to delete GCS files New skip_clean config option added to control deleting import tar files from GCS bucket. Defaults to false meaning by default delete import tar files from the GCS bucket. --- .../googlecompute-import/post-processor.go | 41 +++++++++++++++++++ .../googlecompute-import.html.md | 2 + 2 files changed, 43 insertions(+) diff --git a/post-processor/googlecompute-import/post-processor.go b/post-processor/googlecompute-import/post-processor.go index a043a8ade..3e773cbda 100644 --- a/post-processor/googlecompute-import/post-processor.go +++ b/post-processor/googlecompute-import/post-processor.go @@ -34,6 +34,7 @@ type Config struct { ProjectId string `mapstructure:"project_id"` AccountFile string `mapstructure:"account_file"` KeepOriginalImage bool `mapstructure:"keep_input_artifact"` + SkipClean bool `mapstructure:"skip_clean"` ctx interpolate.Context } @@ -115,6 +116,13 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac return nil, p.config.KeepOriginalImage, err } + if !p.config.SkipClean { + err = DeleteFromBucket(p.config.AccountFile, ui, p.config.Bucket, p.config.GCSObjectName) + if err != nil { + return nil, p.config.KeepOriginalImage, err + } + } + return gceImageArtifact, p.config.KeepOriginalImage, nil } @@ -233,3 +241,36 @@ func CreateGceImage(accountFile string, ui packer.Ui, project string, rawImageUR return &Artifact{paths: []string{op.TargetLink}}, nil } + +func DeleteFromBucket(accountFile string, ui packer.Ui, bucket string, gcsObjectName string) error { + var client *http.Client + var account googlecompute.AccountFile + + err := googlecompute.ProcessAccountFile(&account, accountFile) + if err != nil { + return err + } + + var DriverScopes = []string{"https://www.googleapis.com/auth/devstorage.full_control"} + conf := jwt.Config{ + Email: account.ClientEmail, + PrivateKey: []byte(account.PrivateKey), + Scopes: DriverScopes, + TokenURL: "https://accounts.google.com/o/oauth2/token", + } + + client = conf.Client(oauth2.NoContext) + service, err := storage.New(client) + if err != nil { + return err + } + + ui.Say(fmt.Sprintf("Deleting import source from GCS %s/%s...", bucket, gcsObjectName)) + err = service.Objects.Delete(bucket, gcsObjectName).Do() + if err != nil { + ui.Say(fmt.Sprintf("Failed to delete: %v/%v", bucket, gcsObjectName)) + return err + } + + return nil +} diff --git a/website/source/docs/post-processors/googlecompute-import.html.md b/website/source/docs/post-processors/googlecompute-import.html.md index 8f6c4f825..9281c4c5e 100644 --- a/website/source/docs/post-processors/googlecompute-import.html.md +++ b/website/source/docs/post-processors/googlecompute-import.html.md @@ -54,6 +54,8 @@ for details. - `keep_input_artifact` (boolean) - if true, do not delete the compressed RAW disk image. Defaults to false. +- `skip_clean` (boolean) - Skip removing the TAR file uploaded to the GCS bucket after the import process has completed. "true" means that we should leave it in the GCS bucket, "false" means to clean it out. Defaults to `false`. + ## Basic Example