diff --git a/command/hcl2_upgrade.go b/command/hcl2_upgrade.go index 3916ee313..c42d503f3 100644 --- a/command/hcl2_upgrade.go +++ b/command/hcl2_upgrade.go @@ -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 { diff --git a/command/hcl2_upgrade_test.go b/command/hcl2_upgrade_test.go index d0a8185cc..0abfaafcd 100644 --- a/command/hcl2_upgrade_test.go +++ b/command/hcl2_upgrade_test.go @@ -19,7 +19,7 @@ func Test_hcl2_upgrade(t *testing.T) { tc := []struct { folder string }{ - {"basic"}, + {"complete"}, {"minimal"}, {"source-name"}, } diff --git a/command/test-fixtures/hcl2_upgrade/basic/expected.pkr.hcl b/command/test-fixtures/hcl2_upgrade/complete/expected.pkr.hcl similarity index 100% rename from command/test-fixtures/hcl2_upgrade/basic/expected.pkr.hcl rename to command/test-fixtures/hcl2_upgrade/complete/expected.pkr.hcl diff --git a/command/test-fixtures/hcl2_upgrade/basic/input.json b/command/test-fixtures/hcl2_upgrade/complete/input.json similarity index 100% rename from command/test-fixtures/hcl2_upgrade/basic/input.json rename to command/test-fixtures/hcl2_upgrade/complete/input.json diff --git a/command/test-fixtures/hcl2_upgrade/minimal/expected.pkr.hcl b/command/test-fixtures/hcl2_upgrade/minimal/expected.pkr.hcl index ebe426587..2802e8e18 100644 --- a/command/test-fixtures/hcl2_upgrade/minimal/expected.pkr.hcl +++ b/command/test-fixtures/hcl2_upgrade/minimal/expected.pkr.hcl @@ -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 diff --git a/command/test-fixtures/hcl2_upgrade/minimal/input.json b/command/test-fixtures/hcl2_upgrade/minimal/input.json index 54e100dcc..71109b978 100644 --- a/command/test-fixtures/hcl2_upgrade/minimal/input.json +++ b/command/test-fixtures/hcl2_upgrade/minimal/input.json @@ -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": [