write timestamp local only when necessary (#10602)

This commit is contained in:
Sylvia Moss 2021-02-10 14:54:19 +01:00 committed by GitHub
parent 1e889078fd
commit d28e6fe009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 13 deletions

View File

@ -114,10 +114,13 @@ build {
# https://www.packer.io/docs/datasources/amazon/secretsmanager`
)
var amazonSecretsManagerMap = map[string]map[string]interface{}{}
var localsVariableMap = map[string]string{}
var (
amazonSecretsManagerMap = map[string]map[string]interface{}{}
localsVariableMap = map[string]string{}
timestamp = false
)
func (c *HCL2UpgradeCommand) RunContext(buildCtx context.Context, cla *HCL2UpgradeArgs) int {
func (c *HCL2UpgradeCommand) RunContext(_ context.Context, cla *HCL2UpgradeArgs) int {
var output io.Writer
if err := os.MkdirAll(filepath.Dir(cla.OutputFile), 0); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to create output directory: %v", err))
@ -160,7 +163,6 @@ func (c *HCL2UpgradeCommand) RunContext(buildCtx context.Context, cla *HCL2Upgra
variables := []*template.Variable{}
{
// sort variables to avoid map's randomness
for _, variable := range tpl.Variables {
variables = append(variables, variable)
}
@ -371,9 +373,11 @@ func (c *HCL2UpgradeCommand) RunContext(buildCtx context.Context, cla *HCL2Upgra
out.Write(amazonAmiOut)
}
_, _ = out.Write([]byte("\n"))
fmt.Fprintln(out, `# "timestamp" template function replacement`)
fmt.Fprintln(out, `locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }`)
if timestamp {
_, _ = out.Write([]byte("\n"))
fmt.Fprintln(out, `# "timestamp" template function replacement`)
fmt.Fprintln(out, `locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }`)
}
if len(localsOut) > 0 {
out.Write([]byte(localsVarHeader))
@ -527,9 +531,11 @@ func templateCommonFunctionMap() texttemplate.FuncMap {
}
return fmt.Sprintf("${data.amazon-secretsmanager.%s.value}", id)
}, "timestamp": func() string {
timestamp = true
return "${local.timestamp}"
},
"isotime": func() string {
timestamp = true
return "${local.timestamp}"
},
"user": func(in string) string {

View File

@ -19,7 +19,7 @@ func Test_hcl2_upgrade(t *testing.T) {
tc := []struct {
folder string
}{
{"basic"},
{"complete"},
{"minimal"},
{"source-name"},
}

View File

@ -34,9 +34,6 @@ variable "aws_secret_key" {
default = ""
}
# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors on a
# source. Read the documentation for source blocks here:
@ -44,7 +41,7 @@ locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
source "amazon-ebs" "autogenerated_1" {
access_key = "${var.aws_access_key}"
ami_description = "Ubuntu 16.04 LTS - expand root partition"
ami_name = "ubuntu-16-04-test-${local.timestamp}"
ami_name = "ubuntu-16-04-test"
encrypt_boot = true
launch_block_device_mappings {
delete_on_termination = true

View File

@ -11,7 +11,7 @@
"region": "{{ user `aws_region` }}",
"secret_key": "{{ user `aws_secret_key` }}",
"access_key": "{{ user `aws_access_key` }}",
"ami_name": "ubuntu-16-04-test-{{ timestamp }}",
"ami_name": "ubuntu-16-04-test",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami": "ami1234567",
"launch_block_device_mappings": [