Merge pull request #6504 from sharmaansh/6309-amazon-chroot-create-volume-tags

amazon-chroot: Add tags on CreateVolume
This commit is contained in:
Megan Marsh 2018-07-23 14:58:17 -07:00 committed by GitHub
commit 07b6bc0c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -44,6 +44,7 @@ type Config struct {
RootVolumeSize int64 `mapstructure:"root_volume_size"` RootVolumeSize int64 `mapstructure:"root_volume_size"`
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
SourceAmiFilter awscommon.AmiFilterOptions `mapstructure:"source_ami_filter"` SourceAmiFilter awscommon.AmiFilterOptions `mapstructure:"source_ami_filter"`
RootVolumeTags awscommon.TagMap `mapstructure:"root_volume_tags"`
ctx interpolate.Context ctx interpolate.Context
} }
@ -67,6 +68,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
"ami_description", "ami_description",
"snapshot_tags", "snapshot_tags",
"tags", "tags",
"root_volume_tags",
"command_wrapper", "command_wrapper",
"post_mount_commands", "post_mount_commands",
"pre_mount_commands", "pre_mount_commands",
@ -230,6 +232,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepPrepareDevice{}, &StepPrepareDevice{},
&StepCreateVolume{ &StepCreateVolume{
RootVolumeSize: b.config.RootVolumeSize, RootVolumeSize: b.config.RootVolumeSize,
RootVolumeTags: b.config.RootVolumeTags,
Ctx: b.config.ctx,
}, },
&StepAttachVolume{}, &StepAttachVolume{},
&StepEarlyUnflock{}, &StepEarlyUnflock{},

View File

@ -10,6 +10,7 @@ import (
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
) )
// StepCreateVolume creates a new volume from the snapshot of the root // StepCreateVolume creates a new volume from the snapshot of the root
@ -20,6 +21,8 @@ import (
type StepCreateVolume struct { type StepCreateVolume struct {
volumeId string volumeId string
RootVolumeSize int64 RootVolumeSize int64
RootVolumeTags awscommon.TagMap
Ctx interpolate.Context
} }
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@ -28,6 +31,26 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
instance := state.Get("instance").(*ec2.Instance) instance := state.Get("instance").(*ec2.Instance)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
volTags, err := s.RootVolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
if err != nil {
err := fmt.Errorf("Error tagging volumes: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Collect tags for tagging on resource creation
var tagSpecs []*ec2.TagSpecification
if len(volTags) > 0 {
runVolTags := &ec2.TagSpecification{
ResourceType: aws.String("volume"),
Tags: volTags,
}
tagSpecs = append(tagSpecs, runVolTags)
}
var createVolume *ec2.CreateVolumeInput var createVolume *ec2.CreateVolumeInput
if config.FromScratch { if config.FromScratch {
createVolume = &ec2.CreateVolumeInput{ createVolume = &ec2.CreateVolumeInput{
@ -69,6 +92,10 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} }
} }
if len(tagSpecs) > 0 {
createVolume.SetTagSpecifications(tagSpecs)
volTags.Report(ui)
}
log.Printf("Create args: %+v", createVolume) log.Printf("Create args: %+v", createVolume)
createVolumeResp, err := ec2conn.CreateVolume(createVolume) createVolumeResp, err := ec2conn.CreateVolume(createVolume)

View File

@ -254,6 +254,11 @@ each category, the available configuration keys are alphabetized.
of the `source_ami` unless `from_scratch` is `true`, in which case of the `source_ami` unless `from_scratch` is `true`, in which case
this field must be defined. this field must be defined.
- `root_volume_tags` (object of key/value strings) - Tags to apply to the volumes
that are *launched*. This is a
[template engine](/docs/templates/engine.html),
see [Build template data](#build-template-data) for more information.
- `skip_region_validation` (boolean) - Set to true if you want to skip - `skip_region_validation` (boolean) - Set to true if you want to skip
validation of the `ami_regions` configuration option. Default `false`. validation of the `ami_regions` configuration option. Default `false`.