From 096a64ad9216449cc4a1b327235a612da80e2ac6 Mon Sep 17 00:00:00 2001 From: James Massara Date: Thu, 1 Aug 2013 16:31:07 -0700 Subject: [PATCH 1/8] Adds support for adding tags to the AMI --- builder/amazon/common/run_config.go | 2 ++ builder/amazon/ebs/builder.go | 4 +++- builder/amazon/ebs/step_create_ami.go | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 805a34c4b..89c3cf601 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -3,6 +3,7 @@ package common import ( "errors" "fmt" + "github.com/mitchellh/goamz/ec2" "time" ) @@ -18,6 +19,7 @@ type RunConfig struct { SecurityGroupId string `mapstructure:"security_group_id"` SubnetId string `mapstructure:"subnet_id"` VpcId string `mapstructure:"vpc_id"` + Tags []ec2.Tag // Unexported fields that are calculated from others sshTimeout time.Duration diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index da51c264e..7158e22df 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -107,7 +107,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &common.StepProvision{}, &stepStopInstance{}, - &stepCreateAMI{}, + &stepCreateAMI{ + Tags: b.config.Tags, + }, } // Run! diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 5f75fb423..facb38374 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -12,7 +12,9 @@ import ( "time" ) -type stepCreateAMI struct{} +type stepCreateAMI struct { + Tags []ec2.Tag +} type amiNameData struct { CreateTime string @@ -64,6 +66,19 @@ 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("Add tags to AMI (%s)...", createResp.ImageId)) + amiId := []string{createResp.ImageId} + _, err := ec2conn.CreateTags(amiId, 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 } From d6f8411c43bcbf02a34c90776b18e8d347281c72 Mon Sep 17 00:00:00 2001 From: James Massara Date: Thu, 1 Aug 2013 16:38:22 -0700 Subject: [PATCH 2/8] Adds support for adding tags to the AMI --- builder/amazon/ebs/step_create_ami.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index facb38374..fdfaef4f4 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -68,7 +68,7 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction { // Add tags to AMI if s.Tags != nil { - ui.Say(fmt.Sprintf("Add tags to AMI (%s)...", createResp.ImageId)) + ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", createResp.ImageId)) amiId := []string{createResp.ImageId} _, err := ec2conn.CreateTags(amiId, s.Tags) if err != nil { From 1ff7cc28fe45c575f40b9b2a1bdbd8ecf79740b0 Mon Sep 17 00:00:00 2001 From: James Massara Date: Thu, 1 Aug 2013 18:35:43 -0700 Subject: [PATCH 3/8] removed amiId temp variable --- builder/amazon/ebs/step_create_ami.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index fdfaef4f4..66b22944a 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -69,8 +69,7 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction { // Add tags to AMI if s.Tags != nil { ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", createResp.ImageId)) - amiId := []string{createResp.ImageId} - _, err := ec2conn.CreateTags(amiId, s.Tags) + _, 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 From ad44da70706a3a951a214f597ccb9d2e8ef7a202 Mon Sep 17 00:00:00 2001 From: James Massara Date: Thu, 1 Aug 2013 21:28:25 -0700 Subject: [PATCH 4/8] Updated with tags info --- website/source/docs/builders/amazon-ebs.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index 4c47a4de7..6b741aaf6 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -81,6 +81,8 @@ Optional: * `vpc_id` (string) - If launching into a VPC subnet, Packer needs the VPC ID in order to create a temporary security group within the VPC. +* `tags` (array of key/value pairs) - Tags applied to the AMI. + ## Basic Example Here is a basic example. It is completely valid except for the access keys: @@ -94,7 +96,11 @@ Here is a basic example. It is completely valid except for the access keys: "source_ami": "ami-de0d9eb7", "instance_type": "t1.micro", "ssh_username": "ubuntu", - "ami_name": "packer-quick-start {{.CreateTime}}" + "ami_name": "packer-quick-start {{.CreateTime}}", + "tags": [ + { "key": "myTagName1", "value": "myTagValue1" }, + { "key": "myTagName2", "value": "myTagValue2" } + ] } From 9d0fdacedf00ac5635c77210247003fc8a286fa7 Mon Sep 17 00:00:00 2001 From: James Massara Date: Sat, 3 Aug 2013 17:53:53 -0700 Subject: [PATCH 5/8] removed Tags definition from RunConfig --- builder/amazon/common/run_config.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 89c3cf601..805a34c4b 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -3,7 +3,6 @@ package common import ( "errors" "fmt" - "github.com/mitchellh/goamz/ec2" "time" ) @@ -19,7 +18,6 @@ type RunConfig struct { SecurityGroupId string `mapstructure:"security_group_id"` SubnetId string `mapstructure:"subnet_id"` VpcId string `mapstructure:"vpc_id"` - Tags []ec2.Tag // Unexported fields that are calculated from others sshTimeout time.Duration From 8bffb4f17b551a4553456cacff415a367877af73 Mon Sep 17 00:00:00 2001 From: James Massara Date: Sat, 3 Aug 2013 20:09:14 -0700 Subject: [PATCH 6/8] Moved Tags configuration out of common and into ebs. --- builder/amazon/ebs/builder.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 7158e22df..fa9af4a15 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -27,6 +27,12 @@ type config struct { // Configuration of the resulting AMI AMIName string `mapstructure:"ami_name"` + + // Tags for the AMI + Tags []map[string]string + + // Unexported fields that are calculated from others + ec2Tags []ec2.Tag } type Builder struct { @@ -57,6 +63,28 @@ func (b *Builder) Prepare(raws ...interface{}) error { } } + // Accumulate any errors + if b.config.Tags != nil { + var ec2Tags []ec2.Tag + for _, tag := range b.config.Tags { + if _, ok := tag["key"]; !ok { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Unknown tag configuration, expecting 'key'")) + continue + } + if _, ok := tag["value"]; !ok { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Unknown tag configuration, expecting 'value'")) + continue + } + ec2Tags = append(ec2Tags, ec2.Tag{ + Key: tag["key"], + Value: tag["value"], + }) + } + b.config.ec2Tags = ec2Tags + } + if errs != nil && len(errs.Errors) > 0 { return errs } @@ -108,7 +136,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepProvision{}, &stepStopInstance{}, &stepCreateAMI{ - Tags: b.config.Tags, + Tags: b.config.ec2Tags, }, } From 69aa2d32ac4bc053091d56d9364c3e0367f44ff5 Mon Sep 17 00:00:00 2001 From: James Massara Date: Tue, 6 Aug 2013 07:33:00 -0700 Subject: [PATCH 7/8] Simplied Tags configuration --- builder/amazon/ebs/builder.go | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index fa9af4a15..9882b3631 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -29,7 +29,7 @@ type config struct { AMIName string `mapstructure:"ami_name"` // Tags for the AMI - Tags []map[string]string + Tags map[string]string // Unexported fields that are calculated from others ec2Tags []ec2.Tag @@ -63,24 +63,11 @@ func (b *Builder) Prepare(raws ...interface{}) error { } } - // Accumulate any errors + // Convert Tags to ec2.Tag if b.config.Tags != nil { var ec2Tags []ec2.Tag - for _, tag := range b.config.Tags { - if _, ok := tag["key"]; !ok { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Unknown tag configuration, expecting 'key'")) - continue - } - if _, ok := tag["value"]; !ok { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Unknown tag configuration, expecting 'value'")) - continue - } - ec2Tags = append(ec2Tags, ec2.Tag{ - Key: tag["key"], - Value: tag["value"], - }) + for key, value := range b.config.Tags { + ec2Tags = append(ec2Tags, ec2.Tag{key, value}) } b.config.ec2Tags = ec2Tags } From fffce95ca496faf0a07592757f48c327a7d12786 Mon Sep 17 00:00:00 2001 From: James Massara Date: Tue, 6 Aug 2013 07:41:00 -0700 Subject: [PATCH 8/8] Updated tags configuration example --- website/source/docs/builders/amazon-ebs.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index 6b741aaf6..2ea9d4d8d 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -97,10 +97,10 @@ Here is a basic example. It is completely valid except for the access keys: "instance_type": "t1.micro", "ssh_username": "ubuntu", "ami_name": "packer-quick-start {{.CreateTime}}", - "tags": [ - { "key": "myTagName1", "value": "myTagValue1" }, - { "key": "myTagName2", "value": "myTagValue2" } - ] + "tags": { + "myTagName1": "myTagValue1", + "myTagName2": "myTagValue2" + } }