merge acaire/add-ebs-snapshot-tags

This commit is contained in:
Clint Shryock 2015-06-11 10:33:55 -05:00
commit a8155e17aa
4 changed files with 63 additions and 8 deletions

View File

@ -3,7 +3,6 @@ package common
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
@ -20,7 +19,28 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
if len(s.Tags) > 0 {
for region, ami := range amis {
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami))
ui.Say(fmt.Sprintf("Preparing tags for AMI (%s) and related snapshots", ami))
// Declare list of resources to tag
resourceIds := []string{ami}
// Retrieve image list for given AMI
imageResp, err := ec2conn.Images([]string{ami}, ec2.NewFilter())
if err != nil {
err := fmt.Errorf("Error retrieving details for AMI (%s): %s", ami, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
image := &imageResp.Images[0]
// Add only those with a Snapshot ID, i.e. not Ephemeral
for _, device := range image.BlockDevices {
if device.SnapshotId != "" {
ui.Say(fmt.Sprintf("Tagging snapshot: %s", device.SnapshotId))
resourceIds = append(resourceIds, device.SnapshotId)
}
}
var ec2Tags []*ec2.Tag
for key, value := range s.Tags {
@ -28,12 +48,8 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
ec2Tags = append(ec2Tags, &ec2.Tag{Key: &key, Value: &value})
}
regionconn := ec2.New(&aws.Config{
Credentials: ec2conn.Config.Credentials,
Region: region,
})
_, err := regionconn.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{&ami},
Resources: resourceIds,
Tags: ec2Tags,
})
if err != nil {

View File

@ -15,6 +15,18 @@ aws_ami_region_copy_count() {
| wc -l
}
# This verifies AMI tags are correctly applied to relevant snapshots
aws_ami_snapshot_tags_count() {
filter='Name=tag:packer-id,Values=ami_snapshot_tags'
aws ec2 describe-images --region $1 --owners self --output text \
--filters "$filter" \
--query "Images[*].BlockDeviceMappings[*].Ebs.SnapshotId" \
| aws ec2 describe-snapshots --region $1 --owners self --output text \
--filters "$filter" \
--snapshot-ids \
| wc -l
}
teardown() {
aws_ami_cleanup 'us-east-1'
aws_ami_cleanup 'us-west-1'
@ -34,3 +46,9 @@ teardown() {
[ "$(aws_ami_region_copy_count 'us-west-1')" -eq "1" ]
[ "$(aws_ami_region_copy_count 'us-west-2')" -eq "1" ]
}
@test "amazon-ebs: AMI snapshot tags" {
run packer build $FIXTURE_ROOT/ami_snapshot_tags.json
[ "$status" -eq 0 ]
[ "$(aws_ami_snapshot_tags)" -eq "2" ]
}

View File

@ -0,0 +1,20 @@
{
"builders": [{
"type": "amazon-ebs",
"ami_name": "packer-test {{timestamp}}",
"instance_type": "m1.small",
"region": "us-east-1",
"ssh_username": "ubuntu",
"source_ami": "ami-0568456c",
"tags": {
"packer-test": "true",
"packer-id": "ami_snapshot_tags"
},
"ami_block_device_mappings": [
{
"device_name": "/dev/sde",
"volume_type": "standard"
}
]
}]
}

View File

@ -146,7 +146,8 @@ each category, the available configuration keys are alphabetized.
* `subnet_id` (string) - If using VPC, the ID of the subnet, such as
"subnet-12345def", where Packer will launch the EC2 instance.
* `tags` (object of key/value strings) - Tags applied to the AMI.
* `tags` (object of key/value strings) - Tags applied to the AMI and
relevant snapshots.
* `temporary_key_pair_name` (string) - The name of the temporary keypair
to generate. By default, Packer generates a name with a UUID.