Merge pull request #4420 from mitchellh/4419

builder/amazon: fix run volume tagging
This commit is contained in:
Matthew Hooker 2017-01-18 11:40:24 -08:00 committed by GitHub
commit b81b490c0d
4 changed files with 38 additions and 36 deletions

View File

@ -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() {

View File

@ -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),

View File

@ -291,19 +291,21 @@ 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:"))
if _, exists := s.Tags["Name"]; !exists {
s.Tags["Name"] = "Packer Builder"
}
ec2Tags := ConvertToEC2Tags(s.Tags)
_, err = ec2conn.CreateTags(&ec2.CreateTagsInput{
Tags: ec2Tags,
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 {

View File

@ -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"
)
@ -17,37 +18,33 @@ 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 {
ui.Say("Tagging source EBS volumes...")
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
}
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)
_, 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: volumeIds,
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