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

common: move deprecation_time to AMIConfig #482

Merged
merged 1 commit into from
May 24, 2024
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
4 changes: 4 additions & 0 deletions .web-docs/components/builder/chroot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ builders.
[IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
for more information. Defaults to legacy.

- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.

<!-- End of code generated from the comments of the AMIConfig struct in builder/common/ami_config.go; -->


Expand Down
8 changes: 4 additions & 4 deletions .web-docs/components/builder/ebs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ necessary for this build to succeed and can be found further down the page.
make sure you don't set this for *nix guests; behavior may be
unpredictable.

- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.

- `fast_launch` (FastLaunchConfig) - The configuration for fast launch support.

Fast launch is only relevant for Windows AMIs, and should not be used
Expand Down Expand Up @@ -242,6 +238,10 @@ necessary for this build to succeed and can be found further down the page.
[IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
for more information. Defaults to legacy.

- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.

<!-- End of code generated from the comments of the AMIConfig struct in builder/common/ami_config.go; -->


Expand Down
4 changes: 4 additions & 0 deletions .web-docs/components/builder/ebssurrogate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ necessary for this build to succeed and can be found further down the page.
[IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
for more information. Defaults to legacy.

- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.

<!-- End of code generated from the comments of the AMIConfig struct in builder/common/ami_config.go; -->


Expand Down
4 changes: 4 additions & 0 deletions .web-docs/components/builder/instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ necessary for this build to succeed and can be found further down the page.
[IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
for more information. Defaults to legacy.

- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.

<!-- End of code generated from the comments of the AMIConfig struct in builder/common/ami_config.go; -->


Expand Down
4 changes: 4 additions & 0 deletions builder/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Name: b.config.AMIName,
OriginalRegion: *ec2conn.Config.Region,
},
&awscommon.StepEnableDeprecation{
AccessConfig: &b.config.AccessConfig,
DeprecationTime: b.config.DeprecationTime,
},
&awscommon.StepModifyAMIAttributes{
Description: b.config.AMIDescription,
Users: b.config.AMIUsers,
Expand Down
2 changes: 2 additions & 0 deletions builder/chroot/builder.hcl2spec.go

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

13 changes: 13 additions & 0 deletions builder/common/ami_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"regexp"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer-plugin-sdk/template/config"
Expand Down Expand Up @@ -155,6 +156,10 @@ type AMIConfig struct {
// [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
// for more information. Defaults to legacy.
AMIIMDSSupport string `mapstructure:"imds_support" required:"false"`
// The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
// If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
// You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.
DeprecationTime string `mapstructure:"deprecate_at"`

SnapshotConfig `mapstructure:",squash"`
}
Expand Down Expand Up @@ -260,6 +265,14 @@ func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context
)
}

if c.DeprecationTime != "" {
if _, err := time.Parse(time.RFC3339, c.DeprecationTime); err != nil {
errs = append(errs, fmt.Errorf(
"deprecate_at is not a valid time: %q. Expect time format: YYYY-MM-DDTHH:MM:SSZ",
c.DeprecationTime))
}
}

if len(errs) > 0 {
return errs
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ebs
package common

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer-plugin-amazon/builder/common"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

type stepEnableDeprecation struct {
AccessConfig *common.AccessConfig
type StepEnableDeprecation struct {
AccessConfig *AccessConfig
DeprecationTime string
AMISkipCreateImage bool
}

func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
func (s *StepEnableDeprecation) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
if s.AMISkipCreateImage || s.DeprecationTime == "" {
ui.Say("Skipping Enable AMI deprecation...")
Expand All @@ -39,7 +38,7 @@ func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBa
for region, ami := range amis {
ui.Say(fmt.Sprintf("Enabling deprecation on AMI (%s) in region %q ...", ami, region))

conn, err := common.GetRegionConn(s.AccessConfig, region)
conn, err := GetRegionConn(s.AccessConfig, region)
if err != nil {
err := fmt.Errorf("failed to connect to region %s: %s", region, err)
state.Put("error", err.Error())
Expand All @@ -60,6 +59,6 @@ func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBa
}
return multistep.ActionContinue
}
func (s *stepEnableDeprecation) Cleanup(state multistep.StateBag) {
func (s *StepEnableDeprecation) Cleanup(state multistep.StateBag) {
// No cleanup...
}
13 changes: 1 addition & 12 deletions builder/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package ebs
import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
Expand Down Expand Up @@ -78,10 +77,6 @@ type Config struct {
// make sure you don't set this for *nix guests; behavior may be
// unpredictable.
NoEphemeral bool `mapstructure:"no_ephemeral" required:"false"`
// The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ.
// If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute.
// You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now.
DeprecationTime string `mapstructure:"deprecate_at"`
// The configuration for fast launch support.
//
// Fast launch is only relevant for Windows AMIs, and should not be used
Expand Down Expand Up @@ -171,12 +166,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
"you use an AMI that already has either SR-IOV or ENA enabled."))
}

if b.config.DeprecationTime != "" {
if _, err := time.Parse(time.RFC3339, b.config.DeprecationTime); err != nil {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("deprecate_at is not a valid time. Expect time format: YYYY-MM-DDTHH:MM:SSZ"))
}
}

if b.config.RunConfig.SpotPriceAutoProduct != "" {
warns = append(warns, "spot_price_auto_product is deprecated and no "+
"longer necessary for Packer builds. In future versions of "+
Expand Down Expand Up @@ -446,7 +435,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
EnableFastLaunch: b.config.FastLaunch.UseFastLaunch,
MaxInstances: b.config.FastLaunch.MaxParallelLaunches,
},
&stepEnableDeprecation{
&awscommon.StepEnableDeprecation{
AccessConfig: &b.config.AccessConfig,
DeprecationTime: b.config.DeprecationTime,
AMISkipCreateImage: b.config.AMISkipCreateImage,
Expand Down
4 changes: 2 additions & 2 deletions builder/ebs/builder.hcl2spec.go

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

4 changes: 4 additions & 0 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
OriginalRegion: *ec2conn.Config.Region,
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
},
&awscommon.StepEnableDeprecation{
AccessConfig: &b.config.AccessConfig,
DeprecationTime: b.config.DeprecationTime,
},
&awscommon.StepModifyAMIAttributes{
Description: b.config.AMIDescription,
Users: b.config.AMIUsers,
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.

95 changes: 95 additions & 0 deletions builder/ebssurrogate/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer-plugin-amazon/builder/common"
amazon_acc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance"
"github.com/hashicorp/packer-plugin-sdk/acctest"
)

func testEC2Conn(region string) (*ec2.EC2, error) {
access := &common.AccessConfig{RawRegion: region}
session, err := access.Session()
if err != nil {
return nil, err
}

return ec2.New(session), nil
}

func TestAccBuilder_EbssurrogateBasic(t *testing.T) {
ami := amazon_acc.AMIHelper{
Region: "us-east-1",
Expand Down Expand Up @@ -178,6 +191,59 @@ func TestAccBuilder_EbssurrogateUseCreateImageOptional(t *testing.T) {
acctest.TestPlugin(t, testCase)
}

func TestAccBuilder_EbssurrogateWithAMIDeprecate(t *testing.T) {
ami := amazon_acc.AMIHelper{
Region: "us-east-1",
Name: fmt.Sprintf("ebssurrogate-deprecate-at-acctest-%d", time.Now().Unix()),
}
testCase := &acctest.PluginTestCase{
Name: "ebssurrogate - deprecate at set",
Template: fmt.Sprintf(testBuilderAcc_WithDeprecateAt, ami.Name, time.Now().Add(time.Hour).UTC().Format("2006-01-02T15:04:05Z")),
Teardown: func() error {
return ami.CleanUpAmi()
},
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}

conn, err := testEC2Conn("us-east-1")
if err != nil {
return fmt.Errorf("failed to get connection to us-east-1: %s", err)
}

out, err := conn.DescribeImages(&ec2.DescribeImagesInput{
Filters: []*ec2.Filter{{
Name: aws.String("name"),
Values: []*string{&ami.Name},
}},
})
if err != nil {
return fmt.Errorf("unable to describe images: %s", err)
}

if len(out.Images) != 1 {
return fmt.Errorf("got %d images, should have been one", len(out.Images))
}

img := out.Images[0]
if img.DeprecationTime == nil {
return fmt.Errorf("no depreciation time set for image %s", ami.Name)
}

if *img.DeprecationTime == "" {
return fmt.Errorf("no depreciation time set for image %s", ami.Name)
}

return nil
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}

const testBuilderAccBasic = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
Expand Down Expand Up @@ -352,3 +418,32 @@ build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAcc_WithDeprecateAt = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
launch_block_device_mappings {
device_name = "/dev/xvda"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
ami_virtualization_type = "hvm"
ami_root_device {
source_device_name = "/dev/xvda"
device_name = "/dev/sda1"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
deprecate_at = "%s"
}

build {
sources = ["amazon-ebssurrogate.test"]
}
`
Loading