builder/amazon/all: refactor ami tags [GH-233]

Refactor the EBS ami tag into a common step and add support
for instance-store ami tags.
/cc @jmassara
This commit is contained in:
Mark Peek 2013-08-06 14:54:14 -07:00
parent 3d461195e3
commit 3bcfeafa15
4 changed files with 47 additions and 30 deletions

View File

@ -0,0 +1,42 @@
package common
import (
"fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type StepCreateTags struct {
Tags map[string]string
}
func (s *StepCreateTags) Run(state map[string]interface{}) multistep.StepAction {
ec2conn := state["ec2"].(*ec2.EC2)
ui := state["ui"].(packer.Ui)
amis := state["amis"].(map[string]string)
ami := amis[ec2conn.Region.Name]
if len(s.Tags) > 0 {
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami))
var ec2Tags []ec2.Tag
for key, value := range s.Tags {
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
}
_, err := ec2conn.CreateTags([]string{ami}, ec2Tags)
if err != nil {
err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *StepCreateTags) Cleanup(state map[string]interface{}) {
// No cleanup...
}

View File

@ -30,9 +30,6 @@ type config struct {
// Tags for the AMI
Tags map[string]string
// Unexported fields that are calculated from others
ec2Tags []ec2.Tag
}
type Builder struct {
@ -63,15 +60,6 @@ func (b *Builder) Prepare(raws ...interface{}) error {
}
}
// Convert Tags to ec2.Tag
if b.config.Tags != nil {
var ec2Tags []ec2.Tag
for key, value := range b.config.Tags {
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
}
b.config.ec2Tags = ec2Tags
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
@ -122,9 +110,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
&common.StepProvision{},
&stepStopInstance{},
&stepCreateAMI{
Tags: b.config.ec2Tags,
},
&stepCreateAMI{},
&awscommon.StepCreateTags{b.config.Tags},
}
// Run!

View File

@ -12,9 +12,7 @@ import (
"time"
)
type stepCreateAMI struct {
Tags []ec2.Tag
}
type stepCreateAMI struct{}
type amiNameData struct {
CreateTime string
@ -66,18 +64,6 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
return multistep.ActionHalt
}
// Add tags to AMI
if s.Tags != nil {
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", createResp.ImageId))
_, err := ec2conn.CreateTags([]string{createResp.ImageId}, s.Tags)
if err != nil {
err := fmt.Errorf("Error adding tags to AMI (%s): %s", createResp.ImageId, err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}

View File

@ -33,6 +33,7 @@ type Config struct {
BundleUploadCommand string `mapstructure:"bundle_upload_command"`
BundleVolCommand string `mapstructure:"bundle_vol_command"`
S3Bucket string `mapstructure:"s3_bucket"`
Tags map[string]string
X509CertPath string `mapstructure:"x509_cert_path"`
X509KeyPath string `mapstructure:"x509_key_path"`
X509UploadPath string `mapstructure:"x509_upload_path"`
@ -176,6 +177,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepBundleVolume{},
&StepUploadBundle{},
&StepRegisterAMI{},
&awscommon.StepCreateTags{b.config.Tags},
}
// Run!