Add all of the custom AWS template engines to GeneratedData for use b… (#9751)

This commit is contained in:
Megan Marsh 2020-08-12 01:43:15 -07:00 committed by GitHub
parent e475db6a66
commit 54469c4728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 205 additions and 7 deletions

View File

@ -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) 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 return generatedData, warns, nil
} }
@ -420,6 +422,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&chroot.StepCopyFiles{ &chroot.StepCopyFiles{
Files: b.config.CopyFiles, Files: b.config.CopyFiles,
}, },
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&chroot.StepChrootProvision{}, &chroot.StepChrootProvision{},
&chroot.StepEarlyCleanup{}, &chroot.StepEarlyCleanup{},
&StepSnapshot{}, &StepSnapshot{},

View File

@ -227,10 +227,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if generatedData[0] != "SourceAMIName" { if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain 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") t.Fatalf("Generated data should contain Device")
} }
if generatedData[2] != "MountPath" { if generatedData[7] != "MountPath" {
t.Fatalf("Generated data should contain MountPath") t.Fatalf("Generated data should contain MountPath")
} }
} }

View File

@ -40,6 +40,24 @@ func extractBuildInfo(region string, state multistep.StateBag, generatedData *bu
SourceAMIOwnerName: aws.StringValue(sourceAMI.ImageOwnerAlias), SourceAMIOwnerName: aws.StringValue(sourceAMI.ImageOwnerAlias),
SourceAMITags: sourceAMITags, SourceAMITags: sourceAMITags,
} }
generatedData.Put("BuildRegion", buildInfoTemplate.BuildRegion)
generatedData.Put("SourceAMI", buildInfoTemplate.SourceAMI)
generatedData.Put("SourceAMICreationDate", buildInfoTemplate.SourceAMICreationDate)
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName) generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
generatedData.Put("SourceAMIOwner", buildInfoTemplate.SourceAMIOwner)
generatedData.Put("SourceAMIOwnerName", buildInfoTemplate.SourceAMIOwnerName)
return buildInfoTemplate return buildInfoTemplate
} }
func GetGeneratedDataList() []string {
return []string{
"SourceAMIName",
"BuildRegion",
"SourceAMI",
"SourceAMICreationDate",
"SourceAMIOwner",
"SourceAMIOwnerName",
}
}

View File

@ -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...
}

View File

@ -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) packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
generatedData := []string{"SourceAMIName"} generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil return generatedData, warns, nil
} }
@ -284,6 +284,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
), ),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
}, },
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{}, &common.StepProvision{},
&common.StepCleanupTempKeys{ &common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,

View File

@ -144,7 +144,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 { if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty") t.Fatalf("Generated data should not be empty")
} }
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" { if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain 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")
}
} }

View File

@ -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) packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
generatedData := []string{"SourceAMIName"} generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil return generatedData, warns, nil
} }
@ -308,6 +308,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
), ),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
}, },
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{}, &common.StepProvision{},
&common.StepCleanupTempKeys{ &common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,

View File

@ -86,7 +86,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 { if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty") t.Fatalf("Generated data should not be empty")
} }
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" { if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain 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")
}
} }

View File

@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder"
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/hcl2template" "github.com/hashicorp/packer/hcl2template"
@ -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) packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
generatedData := []string{"SourceAMIName"} generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil return generatedData, warns, nil
} }
@ -166,6 +167,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
state.Put("iam", iam) state.Put("iam", iam)
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
generatedData := &builder.GeneratedData{State: state}
var instanceStep multistep.Step var instanceStep multistep.Step
@ -276,6 +278,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
), ),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
}, },
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{}, &common.StepProvision{},
&common.StepCleanupTempKeys{ &common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,

View File

@ -105,7 +105,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 { if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty") t.Fatalf("Generated data should not be empty")
} }
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" { if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain 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")
}
} }

View File

@ -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) packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
generatedData := []string{"SourceAMIName"} generatedData := awscommon.GetGeneratedDataList()
return generatedData, warns, nil return generatedData, warns, nil
} }
@ -360,6 +360,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
), ),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
}, },
&awscommon.StepSetGeneratedData{
GeneratedData: generatedData,
},
&common.StepProvision{}, &common.StepProvision{},
&common.StepCleanupTempKeys{ &common.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,

View File

@ -319,7 +319,25 @@ func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
if len(generatedData) == 0 { if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty") t.Fatalf("Generated data should not be empty")
} }
if len(generatedData) == 0 {
t.Fatalf("Generated data should not be empty")
}
if generatedData[0] != "SourceAMIName" { if generatedData[0] != "SourceAMIName" {
t.Fatalf("Generated data should contain 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")
}
} }

View File

@ -508,9 +508,16 @@ This builder generates data that are shared with provisioner and post-processor
The generated variables available for this builder are: 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 - `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to `ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI. build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
- `Device` - Root device path. - `Device` - Root device path.
- `MountPath` - Device mounting path. - `MountPath` - Device mounting path.

View File

@ -316,9 +316,16 @@ This builder generates data that are shared with provisioner and post-processor
The generated variables available for this builder are: 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 - `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to `ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI. build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
Usage example: Usage example:

View File

@ -220,9 +220,16 @@ This builder generates data that are shared with provisioner and post-processor
The generated variables available for this builder are: 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 - `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to `ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI. build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
Usage example: Usage example:

View File

@ -245,6 +245,23 @@ variables are available:
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`). - `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
- `SourceAMITags` - The source AMI Tags, as a `map[string]string` object. - `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. -> **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 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 termination of the instance building the new image. In addition to those

View File

@ -213,9 +213,16 @@ This builder generates data that are shared with provisioner and post-processor
The generated variables available for this builder are: 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 - `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to `ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI. build the AMI.
- `SourceAMIOwner` - The source AMI owner ID.
- `SourceAMIOwnerName` - The source AMI owner alias/name (for example `amazon`).
Usage example: Usage example: