Allow naming of vm imported AMIs

Signed-off-by: Ian Duffy <ian@ianduffy.ie>
This commit is contained in:
Ian Duffy 2016-09-29 16:06:28 +01:00
parent 78cb8e7193
commit 641ae2d837
No known key found for this signature in database
GPG Key ID: 62E8225761317390
2 changed files with 43 additions and 1 deletions

View File

@ -30,6 +30,7 @@ type Config struct {
S3Key string `mapstructure:"s3_key_name"` S3Key string `mapstructure:"s3_key_name"`
SkipClean bool `mapstructure:"skip_clean"` SkipClean bool `mapstructure:"skip_clean"`
Tags map[string]string `mapstructure:"tags"` Tags map[string]string `mapstructure:"tags"`
Name string `mapstructure:"ami_name"`
ctx interpolate.Context ctx interpolate.Context
} }
@ -206,6 +207,45 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
// Pull AMI ID out of the completed job // Pull AMI ID out of the completed job
createdami := *import_result.ImportImageTasks[0].ImageId 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 // If we have tags, then apply them now to both the AMI and snaps
// created by the import // created by the import
if len(p.config.Tags) > 0 { if len(p.config.Tags) > 0 {

View File

@ -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". - `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 - `tags` (object of key/value strings) - Tags applied to the created AMI and
relevant snapshots. relevant snapshots.