From a16d991cf4f04318b785f65ee9d7c0613dc510ea Mon Sep 17 00:00:00 2001 From: Devashish Date: Fri, 26 Jan 2024 14:24:59 -0500 Subject: [PATCH] Add PR suggestions --- hcl2template/types.packer_config.go | 24 +++--- .../hcl_templates/contextual-variables.mdx | 81 ++++++++++++++----- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index f8343cfd697..0f547b22413 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -5,7 +5,6 @@ package hcl2template import ( "fmt" - "log" "sort" "strings" @@ -122,24 +121,23 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty. }, } + packerVars := map[string]cty.Value{ + "version": cty.StringVal(cfg.CorePackerVersionString), + "iterationID": cty.UnknownVal(cty.String), + "versionFingerprint": cty.UnknownVal(cty.String), + } + iterID, ok := cfg.HCPVars["iterationID"] if ok { - log.Printf("[WARN] Deprecation: Contextual Variable `iterationID` has been deprecated packer context. " + - "Please use `versionFingerprint` variable instead.") - ectx.Variables[packerAccessor] = cty.ObjectVal(map[string]cty.Value{ - "version": cty.StringVal(cfg.CorePackerVersionString), - "iterationID": iterID, - }) + packerVars["iterationID"] = iterID } - - versionFingerprint, ok := cfg.HCPVars["versionFingerprint"] + versionFP, ok := cfg.HCPVars["versionFingerprint"] if ok { - ectx.Variables[packerAccessor] = cty.ObjectVal(map[string]cty.Value{ - "version": cty.StringVal(cfg.CorePackerVersionString), - "versionFingerprint": versionFingerprint, - }) + packerVars["versionFingerprint"] = versionFP } + ectx.Variables[packerAccessor] = cty.ObjectVal(packerVars) + // In the future we'd like to load and execute HCL blocks using a graph // dependency tree, so that any block can use any block whatever the // order. diff --git a/website/content/docs/templates/hcl_templates/contextual-variables.mdx b/website/content/docs/templates/hcl_templates/contextual-variables.mdx index fcf9f9855ec..03c04d82c86 100644 --- a/website/content/docs/templates/hcl_templates/contextual-variables.mdx +++ b/website/content/docs/templates/hcl_templates/contextual-variables.mdx @@ -132,25 +132,48 @@ parenthesis may through off your shell escaping otherwise. # HCP Packer Iteration ID -~> **Note**: Deprecation: Contextual Variable `iterationID` has been deprecated packer context. Please use `versionFingerprint` variable instead. +~> **Note**: The `packer.iterationID` variable is now deprecated and will be removed in a future version of Packer. HCP Packer Versions should be accessed with their fingerprint instead. The `packer.versionFingerprint` variable is now exposed to be used in its stead with the new HCP Packer data sources. If your build is pushing metadata to the HCP Packer registry, this variable is set to the value of the Iteration ID associated with this run. ```hcl -source "amazon-ebs" "cannonical-ubuntu-server" { - ami_name = "packer-example" - // ... - run_volume_tags = { - hcp_iteration_id = packer.iterationID +source "null" "example" { + communicator = "none" +} + +data "hcp-packer-version" "hardened-source" { + bucket_name = "example" + channel_name = "latest" +} + +data "hcp-packer-artifact" "file" { + bucket_name = "example" + version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" + platform = "aws" + region = "us-east-1" +} + +build { + hcp_packer_registry { + bucket_name = "simple" + } + sources = [ + "source.null.example" + ] + + provisioner "shell-local" { + inline = [ + "echo data is ${packer.iterationID}" + ] } } + ``` ```shell-session -==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder" - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "hcp_iteration_id": "01FHGF3M2AK4TS6PCZES4VX5E7" +==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 + mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 ``` # HCP Packer Version Fingerprint @@ -159,19 +182,42 @@ If your build is pushing metadata to the HCP Packer registry, this variable is set to the value of the Version Fingerprint associated with this run. ```hcl -source "amazon-ebs" "cannonical-ubuntu-server" { - ami_name = "packer-example" - // ... - run_volume_tags = { - hcp_version_fingerprint = packer.versionFingerprint +source "null" "example" { + communicator = "none" +} + +data "hcp-packer-version" "hardened-source" { + bucket_name = "example" + channel_name = "latest" +} + +data "hcp-packer-artifact" "file" { + bucket_name = "example" + version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" + platform = "aws" + region = "us-east-1" +} + +build { + hcp_packer_registry { + bucket_name = "simple" + } + sources = [ + "source.null.example" + ] + + provisioner "shell-local" { + inline = [ + "echo data is ${packer.versionFingerprint}" + ] } } + ``` ```shell-session -==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder" - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "hcp_version_fingerprint": "01FHGF3M2AK4TS6PCZES4VX5E74tf" +==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 + mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 ``` You can also add this value to post-processors, for example to add to a manifest file: @@ -182,7 +228,6 @@ You can also add this value to post-processors, for example to add to a manifest strip_path = true custom_data = { version_fingerprint = "${packer.versionFingerprint}" - // `packer.iterationID` has been deprecated. iteration = "${packer.iterationID}" } }