Issue-6309 Amazon Chroot Provider

-  Add tags on CreateVolume
This commit is contained in:
Anshul Sharma 2018-07-18 13:01:15 +03:00
parent b28098e0f7
commit 885ecb0790
No known key found for this signature in database
GPG Key ID: 184D0287671243D1
2 changed files with 31 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)