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.
pull/6451/head
Sean Malloy 8 years ago
parent 8658eaf1ac
commit d8b229b59a

@ -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
}

@ -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

Loading…
Cancel
Save