From aa70c82ec8bc8b0953591c3b8c449883e4b2f4b8 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 11:36:31 +0000 Subject: [PATCH 1/7] use ConvertToEC2Tags to apply tags to source instance, add Say message --- builder/amazon/common/step_run_source_instance.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 4b79131de..774fd94b5 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -291,11 +291,9 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi instance := latestInstance.(*ec2.Instance) - ec2Tags := make([]*ec2.Tag, 1, len(s.Tags)+1) - ec2Tags[0] = &ec2.Tag{Key: aws.String("Name"), Value: aws.String("Packer Builder")} - for k, v := range s.Tags { - ec2Tags = append(ec2Tags, &ec2.Tag{Key: aws.String(k), Value: aws.String(v)}) - } + ui.Say(fmt.Sprintf("Adding tags to source instance:")) + s.Tags["Name"] = "Packer Builder" + ec2Tags := ConvertToEC2Tags(s.Tags, ui) _, err = ec2conn.CreateTags(&ec2.CreateTagsInput{ Tags: ec2Tags, From 36d8a1df1d4a004f9807d3d3343303e12900faf6 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 12:52:59 +0100 Subject: [PATCH 2/7] use common.ConvertToEC2Tags to apply tags to source EBS volumes, change Say message --- builder/amazon/ebs/step_tag_ebs_volumes.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index 2a4f8c047..ecd25e4f0 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" ) @@ -18,7 +19,6 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) if len(s.VolumeRunTags) > 0 { - ui.Say("Tagging source EBS volumes...") volumeIds := make([]*string, 0) for _, v := range instance.BlockDeviceMappings { @@ -31,10 +31,8 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } - tags := make([]*ec2.Tag, len(s.VolumeRunTags)) - for key, value := range s.VolumeRunTags { - tags = append(tags, &ec2.Tag{Key: &key, Value: &value}) - } + ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) + tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{ From a65af51a4bac8c9288ef0866383a50c9a89d2e71 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 12:55:18 +0100 Subject: [PATCH 3/7] apply source instance default "Name" tag only if it's not provided in run_tags --- builder/amazon/common/step_run_source_instance.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 774fd94b5..ab31be980 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -292,7 +292,9 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi instance := latestInstance.(*ec2.Instance) ui.Say(fmt.Sprintf("Adding tags to source instance:")) - s.Tags["Name"] = "Packer Builder" + if _, exists := s.Tags["Name"]; !exists { + s.Tags["Name"] = "Packer Builder" + } ec2Tags := ConvertToEC2Tags(s.Tags, ui) _, err = ec2conn.CreateTags(&ec2.CreateTagsInput{ From ae016a1f250f08f65ae045735f197629492c812a Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 13:03:05 +0100 Subject: [PATCH 4/7] simplify code flow: skip the step if no run_volume_tags provided --- builder/amazon/ebs/step_tag_ebs_volumes.go | 47 +++++++++++----------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index ecd25e4f0..a9ead9ebe 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -18,34 +18,35 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { instance := state.Get("instance").(*ec2.Instance) ui := state.Get("ui").(packer.Ui) - if len(s.VolumeRunTags) > 0 { + if len(s.VolumeRunTags) == 0 { + return multistep.ActionContinue + } - volumeIds := make([]*string, 0) - for _, v := range instance.BlockDeviceMappings { - if ebs := v.Ebs; ebs != nil { - volumeIds = append(volumeIds, ebs.VolumeId) - } + volumeIds := make([]*string, 0) + for _, v := range instance.BlockDeviceMappings { + if ebs := v.Ebs; ebs != nil { + volumeIds = append(volumeIds, ebs.VolumeId) } + } - if len(volumeIds) == 0 { - return multistep.ActionContinue - } + if len(volumeIds) == 0 { + return multistep.ActionContinue + } - ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) - tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) + ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) + tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) - _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ - Resources: []*string{ - instance.BlockDeviceMappings[0].Ebs.VolumeId, - }, - Tags: tags, - }) - if err != nil { - err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ + Resources: []*string{ + instance.BlockDeviceMappings[0].Ebs.VolumeId, + }, + Tags: tags, + }) + if err != nil { + err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } return multistep.ActionContinue From d9186dc8cf7943240842ba38b5acfac1a396a992 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 14:02:10 +0100 Subject: [PATCH 5/7] add proper error handling for tagging source instance --- builder/amazon/common/step_run_source_instance.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index ab31be980..e436e5aff 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -302,8 +302,10 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi Resources: []*string{instance.InstanceId}, }) if err != nil { - ui.Message( - fmt.Sprintf("Failed to tag a Name on the builder instance: %s", err)) + err := fmt.Errorf("Error tagging source instance: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } if s.Debug { From 800351b7ca9c966f3fe16a1135690eaffd000c22 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 18 Jan 2017 11:34:36 -0800 Subject: [PATCH 6/7] fix panic and don't print each tag we add. --- builder/amazon/common/run_config.go | 4 ++++ builder/amazon/common/step_create_tags.go | 7 +++---- builder/amazon/common/step_run_source_instance.go | 2 +- builder/amazon/ebs/step_tag_ebs_volumes.go | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 52289ba50..60124db16 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -69,6 +69,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { c.WindowsPasswordTimeout = 10 * time.Minute } + if c.RunTags == nil { + c.RunTags = make(map[string]string) + } + // Validation errs := c.Comm.Prepare(ctx) if c.SourceAmi == "" && c.SourceAmiFilter.Empty() { diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go index 87ce08840..4de8e50c3 100644 --- a/builder/amazon/common/step_create_tags.go +++ b/builder/amazon/common/step_create_tags.go @@ -31,9 +31,9 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami)) // Convert tags to ec2.Tag format - amiTags := ConvertToEC2Tags(s.Tags, ui) + amiTags := ConvertToEC2Tags(s.Tags) ui.Say(fmt.Sprintf("Snapshot tags:")) - snapshotTags := ConvertToEC2Tags(s.SnapshotTags, ui) + snapshotTags := ConvertToEC2Tags(s.SnapshotTags) // Declare list of resources to tag awsConfig := aws.Config{ @@ -128,10 +128,9 @@ func (s *StepCreateTags) Cleanup(state multistep.StateBag) { // No cleanup... } -func ConvertToEC2Tags(tags map[string]string, ui packer.Ui) []*ec2.Tag { +func ConvertToEC2Tags(tags map[string]string) []*ec2.Tag { var amiTags []*ec2.Tag for key, value := range tags { - ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value)) amiTags = append(amiTags, &ec2.Tag{ Key: aws.String(key), Value: aws.String(value), diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index e436e5aff..649ddfdec 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -295,7 +295,7 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi if _, exists := s.Tags["Name"]; !exists { s.Tags["Name"] = "Packer Builder" } - ec2Tags := ConvertToEC2Tags(s.Tags, ui) + ec2Tags := ConvertToEC2Tags(s.Tags) _, err = ec2conn.CreateTags(&ec2.CreateTagsInput{ Tags: ec2Tags, diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index a9ead9ebe..6db6c3bb6 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -34,7 +34,7 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { } ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) - tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) + tags := common.ConvertToEC2Tags(s.VolumeRunTags) _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{ From f5bf5883931b4881320cc88605bd8641ffda2c88 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 18 Jan 2017 11:34:46 -0800 Subject: [PATCH 7/7] tag all volumes --- builder/amazon/ebs/step_tag_ebs_volumes.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index 6db6c3bb6..ee5a26bb2 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -37,10 +37,8 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { tags := common.ConvertToEC2Tags(s.VolumeRunTags) _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ - Resources: []*string{ - instance.BlockDeviceMappings[0].Ebs.VolumeId, - }, - Tags: tags, + Resources: volumeIds, + Tags: tags, }) if err != nil { err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)