From 8a636519f86cbcf65d7147f805d90614c76870f5 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Tue, 6 Aug 2013 14:54:14 -0700 Subject: [PATCH] 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 --- builder/amazon/common/step_create_tags.go | 42 +++++++++++++++++++++++ builder/amazon/ebs/builder.go | 17 ++------- builder/amazon/ebs/step_create_ami.go | 16 +-------- builder/amazon/instance/builder.go | 2 ++ 4 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 builder/amazon/common/step_create_tags.go diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go new file mode 100644 index 000000000..c2b6371ee --- /dev/null +++ b/builder/amazon/common/step_create_tags.go @@ -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... +} diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 1f85daf24..cd5ba1452 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -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! diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 66b22944a..5f75fb423 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -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 } diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 7fd1d9f0b..3517553c3 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -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!