From 641ae2d837045fc143848bd61633d32b8c4dc3ab Mon Sep 17 00:00:00 2001 From: Ian Duffy Date: Thu, 29 Sep 2016 16:06:28 +0100 Subject: [PATCH] Allow naming of vm imported AMIs Signed-off-by: Ian Duffy --- .../amazon-import/post-processor.go | 42 ++++++++++++++++++- .../post-processors/amazon-import.html.md | 2 + 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/post-processor/amazon-import/post-processor.go b/post-processor/amazon-import/post-processor.go index 7176c28e5..45019ffaf 100644 --- a/post-processor/amazon-import/post-processor.go +++ b/post-processor/amazon-import/post-processor.go @@ -30,8 +30,9 @@ type Config struct { S3Key string `mapstructure:"s3_key_name"` SkipClean bool `mapstructure:"skip_clean"` Tags map[string]string `mapstructure:"tags"` + Name string `mapstructure:"ami_name"` - ctx interpolate.Context + ctx interpolate.Context } type PostProcessor struct { @@ -206,6 +207,45 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Pull AMI ID out of the completed job createdami := *import_result.ImportImageTasks[0].ImageId + if p.config.Name != "" { + + ui.Message(fmt.Sprintf("Starting rename of AMI (%s)", createdami)) + + resp, err := ec2conn.CopyImage(&ec2.CopyImageInput{ + Name: &p.config.Name, + SourceImageId: &createdami, + SourceRegion: config.Region, + }) + + if err != nil { + return nil, false, fmt.Errorf("Error Copying AMI (%s): %s", createdami, err) + } + + ui.Message(fmt.Sprintf("Waiting for AMI rename to complete (may take a while)")) + + stateChange := awscommon.StateChangeConf{ + Pending: []string{"pending"}, + Target: "available", + Refresh: awscommon.AMIStateRefreshFunc(ec2conn, *resp.ImageId), + } + + if _, err := awscommon.WaitForState(&stateChange); err != nil { + return nil, false, fmt.Errorf("Error waiting for AMI (%s): %s", *resp.ImageId, err) + } + + ec2conn.DeregisterImage(&ec2.DeregisterImageInput{ + ImageId: &createdami, + }) + + if err != nil { + return nil, false, fmt.Errorf("Error deregistering existing AMI: %s", err) + } + + ui.Message(fmt.Sprintf("AMI rename completed")) + + createdami = *resp.ImageId + } + // If we have tags, then apply them now to both the AMI and snaps // created by the import if len(p.config.Tags) > 0 { diff --git a/website/source/docs/post-processors/amazon-import.html.md b/website/source/docs/post-processors/amazon-import.html.md index 2bd040f06..c41b1aa4c 100644 --- a/website/source/docs/post-processors/amazon-import.html.md +++ b/website/source/docs/post-processors/amazon-import.html.md @@ -46,6 +46,8 @@ Optional: - `skip_clean` (boolean) - Whether we should skip removing the OVA file uploaded to S3 after the import process has completed. "true" means that we should leave it in the S3 bucket, "false" means to clean it out. Defaults to "false". +- `ami_name` (string) - The name of the ami within the console. If not specified, this will default to something like `ami-import-sfwerwf`. Please note, specifying this option will result in a slightly longer execution time. + - `tags` (object of key/value strings) - Tags applied to the created AMI and relevant snapshots.