Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable setting a snapshot description for EBS surrogate #501

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
gnought marked this conversation as resolved.
Show resolved Hide resolved
}
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())
gnought marked this conversation as resolved.
Show resolved Hide resolved
}
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