From e708ebc0d610832277de82f87808eb609b24f689 Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:36:40 +0800 Subject: [PATCH] feat: enable setting a snapshot description for EBS surrogate --- .../components/builder/ebssurrogate/README.md | 2 ++ builder/ebssurrogate/builder.go | 13 ++++++---- builder/ebssurrogate/builder.hcl2spec.go | 2 ++ builder/ebssurrogate/step_snapshot_volumes.go | 24 ++++++++++--------- .../ebsvolume/step_snapshot_ebs_volumes.go | 8 +++++-- .../ebssurrogate/Config-not-required.mdx | 2 ++ 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/.web-docs/components/builder/ebssurrogate/README.md b/.web-docs/components/builder/ebssurrogate/README.md index b2f48f486..8abd22a7c 100644 --- a/.web-docs/components/builder/ebssurrogate/README.md +++ b/.web-docs/components/builder/ebssurrogate/README.md @@ -44,6 +44,8 @@ necessary for this build to succeed and can be found further down the page. +- `snapshot_description` (string) - The description for the snapshot. + - `ami_block_device_mappings` (awscommon.BlockDevices) - Add one or more block device mappings to the AMI. These will be attached when booting a new instance from your AMI. To add a block device during the Packer build see `launch_block_device_mappings` below. Your options diff --git a/builder/ebssurrogate/builder.go b/builder/ebssurrogate/builder.go index 7587026e3..a27094613 100644 --- a/builder/ebssurrogate/builder.go +++ b/builder/ebssurrogate/builder.go @@ -35,6 +35,8 @@ type Config struct { awscommon.RunConfig `mapstructure:",squash"` awscommon.AMIConfig `mapstructure:",squash"` + // The description for the snapshot. + SnapshotDescription string `mapstructure:"snapshot_description" required:"false"` // Add one or more block device mappings to the AMI. These will be attached // when booting a new instance from your AMI. To add a block device during // the Packer build see `launch_block_device_mappings` below. Your options @@ -351,11 +353,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) } } else { volumeStep = &StepSnapshotVolumes{ - PollingConfig: b.config.PollingConfig, - LaunchDevices: launchDevices, - SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(), - SnapshotTags: b.config.SnapshotTags, - Ctx: b.config.ctx, + PollingConfig: b.config.PollingConfig, + LaunchDevices: launchDevices, + SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(), + SnapshotTags: b.config.SnapshotTags, + SnapshotDescription: b.config.SnapshotDescription, + Ctx: b.config.ctx, } buildAmiStep = &StepRegisterAMI{ RootDevice: b.config.RootDevice, diff --git a/builder/ebssurrogate/builder.hcl2spec.go b/builder/ebssurrogate/builder.hcl2spec.go index b4b4cf148..345625820 100644 --- a/builder/ebssurrogate/builder.hcl2spec.go +++ b/builder/ebssurrogate/builder.hcl2spec.go @@ -202,6 +202,7 @@ type FlatConfig struct { SnapshotTag []config.FlatKeyValue `mapstructure:"snapshot_tag" required:"false" cty:"snapshot_tag" hcl:"snapshot_tag"` SnapshotUsers []string `mapstructure:"snapshot_users" required:"false" cty:"snapshot_users" hcl:"snapshot_users"` SnapshotGroups []string `mapstructure:"snapshot_groups" required:"false" cty:"snapshot_groups" hcl:"snapshot_groups"` + SnapshotDescription *string `mapstructure:"snapshot_description" required:"false" cty:"snapshot_description" hcl:"snapshot_description"` AMIMappings []common.FlatBlockDevice `mapstructure:"ami_block_device_mappings" required:"false" cty:"ami_block_device_mappings" hcl:"ami_block_device_mappings"` LaunchMappings []FlatBlockDevice `mapstructure:"launch_block_device_mappings" required:"false" cty:"launch_block_device_mappings" hcl:"launch_block_device_mappings"` RootDevice *FlatRootBlockDevice `mapstructure:"ami_root_device" required:"true" cty:"ami_root_device" hcl:"ami_root_device"` @@ -371,6 +372,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "snapshot_tag": &hcldec.BlockListSpec{TypeName: "snapshot_tag", Nested: hcldec.ObjectSpec((*config.FlatKeyValue)(nil).HCL2Spec())}, "snapshot_users": &hcldec.AttrSpec{Name: "snapshot_users", Type: cty.List(cty.String), Required: false}, "snapshot_groups": &hcldec.AttrSpec{Name: "snapshot_groups", Type: cty.List(cty.String), Required: false}, + "snapshot_description": &hcldec.AttrSpec{Name: "snapshot_description", Type: cty.String, Required: false}, "ami_block_device_mappings": &hcldec.BlockListSpec{TypeName: "ami_block_device_mappings", Nested: hcldec.ObjectSpec((*common.FlatBlockDevice)(nil).HCL2Spec())}, "launch_block_device_mappings": &hcldec.BlockListSpec{TypeName: "launch_block_device_mappings", Nested: hcldec.ObjectSpec((*FlatBlockDevice)(nil).HCL2Spec())}, "ami_root_device": &hcldec.BlockSpec{TypeName: "ami_root_device", Nested: hcldec.ObjectSpec((*FlatRootBlockDevice)(nil).HCL2Spec())}, diff --git a/builder/ebssurrogate/step_snapshot_volumes.go b/builder/ebssurrogate/step_snapshot_volumes.go index e4239c12c..b2b76f3ba 100644 --- a/builder/ebssurrogate/step_snapshot_volumes.go +++ b/builder/ebssurrogate/step_snapshot_volumes.go @@ -24,13 +24,14 @@ import ( // // snapshot_ids map[string]string - IDs of the created snapshots type StepSnapshotVolumes struct { - PollingConfig *awscommon.AWSPollingConfig - LaunchDevices []*ec2.BlockDeviceMapping - snapshotIds map[string]string - snapshotMutex sync.Mutex - SnapshotOmitMap map[string]bool - SnapshotTags map[string]string - Ctx interpolate.Context + PollingConfig *awscommon.AWSPollingConfig + LaunchDevices []*ec2.BlockDeviceMapping + snapshotIds map[string]string + snapshotMutex sync.Mutex + SnapshotOmitMap map[string]bool + SnapshotTags map[string]string + SnapshotDescription string + Ctx interpolate.Context } func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error { @@ -58,7 +59,6 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str snapshotTags.Report(ui) ui.Say(fmt.Sprintf("Creating snapshot of EBS Volume %s...", volumeId)) - description := fmt.Sprintf("Packer: %s", time.Now().String()) // Collect tags for tagging on resource creation var tagSpecs []*ec2.TagSpecification @@ -68,13 +68,15 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str ResourceType: aws.String("snapshot"), Tags: snapshotTags, } - tagSpecs = append(tagSpecs, snapTags) } - + description := s.SnapshotDescription + if description == "" { + description = fmt.Sprintf("Packer: %s", time.Now().String()) + } createSnapResp, err := ec2conn.CreateSnapshot(&ec2.CreateSnapshotInput{ VolumeId: &volumeId, - Description: &description, + Description: aws.String(description), TagSpecifications: tagSpecs, }) if err != nil { diff --git a/builder/ebsvolume/step_snapshot_ebs_volumes.go b/builder/ebsvolume/step_snapshot_ebs_volumes.go index 7d2e17a18..8e7b63b92 100644 --- a/builder/ebsvolume/step_snapshot_ebs_volumes.go +++ b/builder/ebsvolume/step_snapshot_ebs_volumes.go @@ -6,6 +6,7 @@ package ebsvolume import ( "context" "fmt" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" @@ -55,11 +56,14 @@ func (s *stepSnapshotEBSVolumes) Run(ctx context.Context, state multistep.StateB ResourceType: aws.String("snapshot"), Tags: tags, } - + description := configVolumeMapping.SnapshotDescription + if description == "" { + description = fmt.Sprintf("Packer: %s", time.Now().String()) + } input := &ec2.CreateSnapshotInput{ VolumeId: aws.String(*instanceBlockDevice.Ebs.VolumeId), TagSpecifications: []*ec2.TagSpecification{tagSpec}, - Description: aws.String(configVolumeMapping.SnapshotDescription), + Description: aws.String(description), } //Dont try to set an empty tag spec diff --git a/docs-partials/builder/ebssurrogate/Config-not-required.mdx b/docs-partials/builder/ebssurrogate/Config-not-required.mdx index 61bb1c6ad..fbc07cec7 100644 --- a/docs-partials/builder/ebssurrogate/Config-not-required.mdx +++ b/docs-partials/builder/ebssurrogate/Config-not-required.mdx @@ -1,5 +1,7 @@ +- `snapshot_description` (string) - The description for the snapshot. + - `ami_block_device_mappings` (awscommon.BlockDevices) - Add one or more block device mappings to the AMI. These will be attached when booting a new instance from your AMI. To add a block device during the Packer build see `launch_block_device_mappings` below. Your options