Skip to content

Commit

Permalink
Add all of the custom AWS template engines to GeneratedData for use b… (
Browse files Browse the repository at this point in the history
  • Loading branch information
SwampDragons authored Aug 12, 2020
1 parent e475db6 commit 54469c4
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 7 deletions.
7 changes: 6 additions & 1 deletion builder/amazon/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}

packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
generatedData := []string{"SourceAMIName", "Device", "MountPath"}
generatedData := awscommon.GetGeneratedDataList()
generatedData = append(generatedData, "Device", "MountPath")

return generatedData, warns, nil
}

Expand Down Expand Up @@ -420,6 +422,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&chroot.StepCopyFiles{
Files: b.config.CopyFiles,
},
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&chroot.StepChrootProvision{},
&chroot.StepEarlyCleanup{},
&StepSnapshot{},
Expand Down
19 changes: 17 additions & 2 deletions builder/amazon/chroot/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain SourceAMIName")
}
if generatedData[1] != "Device" {
if generatedData[1] != "BuildRegion" {
t.Fatalf("Generated data should contain BuildRegion")
}
if generatedData[2] != "SourceAMI" {
t.Fatalf("Generated data should contain SourceAMI")
}
if generatedData[3] != "SourceAMICreationDate" {
t.Fatalf("Generated data should contain SourceAMICreationDate")
}
if generatedData[4] != "SourceAMIOwner" {
t.Fatalf("Generated data should contain SourceAMIOwner")
}
if generatedData[5] != "SourceAMIOwnerName" {
t.Fatalf("Generated data should contain SourceAMIOwnerName")
}
if generatedData[6] != "Device" {
t.Fatalf("Generated data should contain Device")
}
if generatedData[2] != "MountPath" {
if generatedData[7] != "MountPath" {
t.Fatalf("Generated data should contain MountPath")
}
}
18 changes: 18 additions & 0 deletions builder/amazon/common/interpolate_build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ func extractBuildInfo(region string, state multistep.StateBag, generatedData *bu
SourceAMIOwnerName: aws.StringValue(sourceAMI.ImageOwnerAlias),
SourceAMITags: sourceAMITags,
}

generatedData.Put("BuildRegion", buildInfoTemplate.BuildRegion)
generatedData.Put("SourceAMI", buildInfoTemplate.SourceAMI)
generatedData.Put("SourceAMICreationDate", buildInfoTemplate.SourceAMICreationDate)
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
generatedData.Put("SourceAMIOwner", buildInfoTemplate.SourceAMIOwner)
generatedData.Put("SourceAMIOwnerName", buildInfoTemplate.SourceAMIOwnerName)

return buildInfoTemplate
}

func GetGeneratedDataList() []string {
return []string{
"SourceAMIName",
"BuildRegion",
"SourceAMI",
"SourceAMICreationDate",
"SourceAMIOwner",
"SourceAMIOwnerName",
}
}
29 changes: 29 additions & 0 deletions builder/amazon/common/step_set_generated_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package common

import (
"context"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/helper/multistep"
)

// &awscommon.StepSetGeneratedData{
// GeneratedData: generatedData,
// },

type StepSetGeneratedData struct {
GeneratedData *builder.GeneratedData
}

func (s *StepSetGeneratedData) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2)

extractBuildInfo(*ec2conn.Config.Region, state, s.GeneratedData)

return multistep.ActionContinue
}

func (s *StepSetGeneratedData) Cleanup(state multistep.StateBag) {
// No cleanup...
}
5 changes: 4 additions & 1 deletion builder/amazon/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {

packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)

generatedData := []string{"SourceAMIName"}
generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil
}

Expand Down Expand Up @@ -284,6 +284,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm,
Expand Down
18 changes: 18 additions & 0 deletions builder/amazon/ebs/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain SourceAMIName")
}
if generatedData[1] != "BuildRegion" {
t.Fatalf("Generated data should contain BuildRegion")
}
if generatedData[2] != "SourceAMI" {
t.Fatalf("Generated data should contain SourceAMI")
}
if generatedData[3] != "SourceAMICreationDate" {
t.Fatalf("Generated data should contain SourceAMICreationDate")
}
if generatedData[4] != "SourceAMIOwner" {
t.Fatalf("Generated data should contain SourceAMIOwner")
}
if generatedData[5] != "SourceAMIOwnerName" {
t.Fatalf("Generated data should contain SourceAMIOwnerName")
}
}
5 changes: 4 additions & 1 deletion builder/amazon/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {

packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)

generatedData := []string{"SourceAMIName"}
generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil
}

Expand Down Expand Up @@ -308,6 +308,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm,
Expand Down
18 changes: 18 additions & 0 deletions builder/amazon/ebssurrogate/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain SourceAMIName")
}
if generatedData[1] != "BuildRegion" {
t.Fatalf("Generated data should contain BuildRegion")
}
if generatedData[2] != "SourceAMI" {
t.Fatalf("Generated data should contain SourceAMI")
}
if generatedData[3] != "SourceAMICreationDate" {
t.Fatalf("Generated data should contain SourceAMICreationDate")
}
if generatedData[4] != "SourceAMIOwner" {
t.Fatalf("Generated data should contain SourceAMIOwner")
}
if generatedData[5] != "SourceAMIOwnerName" {
t.Fatalf("Generated data should contain SourceAMIOwnerName")
}
}
7 changes: 6 additions & 1 deletion builder/amazon/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder"
awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/hcl2template"
Expand Down Expand Up @@ -146,7 +147,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {

packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)

generatedData := []string{"SourceAMIName"}
generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil
}

Expand All @@ -166,6 +167,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
state.Put("iam", iam)
state.Put("hook", hook)
state.Put("ui", ui)
generatedData := &builder.GeneratedData{State: state}

var instanceStep multistep.Step

Expand Down Expand Up @@ -276,6 +278,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm,
Expand Down
18 changes: 18 additions & 0 deletions builder/amazon/ebsvolume/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain SourceAMIName")
}
if generatedData[1] != "BuildRegion" {
t.Fatalf("Generated data should contain BuildRegion")
}
if generatedData[2] != "SourceAMI" {
t.Fatalf("Generated data should contain SourceAMI")
}
if generatedData[3] != "SourceAMICreationDate" {
t.Fatalf("Generated data should contain SourceAMICreationDate")
}
if generatedData[4] != "SourceAMIOwner" {
t.Fatalf("Generated data should contain SourceAMIOwner")
}
if generatedData[5] != "SourceAMIOwnerName" {
t.Fatalf("Generated data should contain SourceAMIOwnerName")
}
}
5 changes: 4 additions & 1 deletion builder/amazon/instance/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)

generatedData := []string{"SourceAMIName"}
generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil
}

Expand Down Expand Up @@ -360,6 +360,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm,
Expand Down
18 changes: 18 additions & 0 deletions builder/amazon/instance/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain SourceAMIName")
}
if generatedData[1] != "BuildRegion" {
t.Fatalf("Generated data should contain BuildRegion")
}
if generatedData[2] != "SourceAMI" {
t.Fatalf("Generated data should contain SourceAMI")
}
if generatedData[3] != "SourceAMICreationDate" {
t.Fatalf("Generated data should contain SourceAMICreationDate")
}
if generatedData[4] != "SourceAMIOwner" {
t.Fatalf("Generated data should contain SourceAMIOwner")
}
if generatedData[5] != "SourceAMIOwnerName" {
t.Fatalf("Generated data should contain SourceAMIOwnerName")
}
}
7 changes: 7 additions & 0 deletions website/pages/docs/builders/amazon/chroot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,16 @@ This builder generates data that are shared with provisioner and post-processor

The generated variables available for this builder are:

- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMICreationDate` - The source AMI creation date (for example `"2020-05-14T19:26:34.000Z"`).
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
- `Device` - Root device path.
- `MountPath` - Device mounting path.

Expand Down
7 changes: 7 additions & 0 deletions website/pages/docs/builders/amazon/ebs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,16 @@ This builder generates data that are shared with provisioner and post-processor

The generated variables available for this builder are:

- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMICreationDate` - The source AMI creation date (for example `"2020-05-14T19:26:34.000Z"`).
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).

Usage example:

Expand Down
7 changes: 7 additions & 0 deletions website/pages/docs/builders/amazon/ebssurrogate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,16 @@ This builder generates data that are shared with provisioner and post-processor

The generated variables available for this builder are:

- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMICreationDate` - The source AMI creation date (for example `"2020-05-14T19:26:34.000Z"`).
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).

Usage example:

Expand Down
17 changes: 17 additions & 0 deletions website/pages/docs/builders/amazon/ebsvolume.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ variables are available:
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
- `SourceAMITags` - The source AMI Tags, as a `map[string]string` object.

## Build Shared Information Variables

This builder generates data that are shared with provisioner and post-processor via build function of [template engine](/docs/templates/engine) for JSON and [contextual variables](/docs/from-1.5/contextual-variables) for HCL2.

The generated variables available for this builder are:

- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMICreationDate` - The source AMI creation date (for example `"2020-05-14T19:26:34.000Z"`).
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).

-> **Note:** Packer uses pre-built AMIs as the source for building images.
These source AMIs may include volumes that are not flagged to be destroyed on
termination of the instance building the new image. In addition to those
Expand Down
7 changes: 7 additions & 0 deletions website/pages/docs/builders/amazon/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,16 @@ This builder generates data that are shared with provisioner and post-processor

The generated variables available for this builder are:

- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMICreationDate` - The source AMI creation date (for example `"2020-05-14T19:26:34.000Z"`).
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).

Usage example:

Expand Down

0 comments on commit 54469c4

Please sign in to comment.