Skip to content

Commit

Permalink
feat: enable setting a snapshot description for EBS surrogate
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought authored and lbajolet-hashicorp committed Aug 13, 2024
1 parent 3dc75aa commit e708ebc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .web-docs/components/builder/ebssurrogate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ necessary for this build to succeed and can be found further down the page.

<!-- Code generated from the comments of the Config struct in builder/ebssurrogate/builder.go; DO NOT EDIT MANUALLY -->

- `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
Expand Down
13 changes: 8 additions & 5 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions builder/ebssurrogate/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions builder/ebssurrogate/step_snapshot_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions builder/ebsvolume/step_snapshot_ebs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs-partials/builder/ebssurrogate/Config-not-required.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!-- Code generated from the comments of the Config struct in builder/ebssurrogate/builder.go; DO NOT EDIT MANUALLY -->

- `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
Expand Down

0 comments on commit e708ebc

Please sign in to comment.