From 09c6d2880fbf386811ee577dd44fdfd8e1778753 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 15 Oct 2020 16:30:46 -0700 Subject: [PATCH 1/2] implement packer_version variable in hcl --- hcl2template/types.packer_config.go | 5 ++ .../docs/from-1.5/contextual-variables.mdx | 46 ++++++++++++++++++- .../from-1.5/functions/contextual/index.mdx | 6 +++ .../functions/{vault => contextual}/vault.mdx | 0 .../docs/from-1.5/functions/vault/index.mdx | 6 --- 5 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 website/pages/docs/from-1.5/functions/contextual/index.mdx rename website/pages/docs/from-1.5/functions/{vault => contextual}/vault.mdx (100%) delete mode 100644 website/pages/docs/from-1.5/functions/vault/index.mdx diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index c7f0d0a7f..a0d0fca59 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/version" "github.com/zclconf/go-cty/cty" ) @@ -60,6 +61,7 @@ const ( pathVariablesAccessor = "path" sourcesAccessor = "source" buildAccessor = "build" + packerAccessor = "packer" ) // EvalContext returns the *hcl.EvalContext that will be passed to an hcl @@ -78,6 +80,9 @@ func (cfg *PackerConfig) EvalContext(variables map[string]cty.Value) *hcl.EvalCo "name": cty.UnknownVal(cty.String), }), buildAccessor: cty.UnknownVal(cty.EmptyObject), + packerAccessor: cty.ObjectVal(map[string]cty.Value{ + "version": cty.StringVal(version.FormattedVersion()), + }), pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{ "cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)), "root": cty.StringVal(strings.ReplaceAll(cfg.Basedir, `\`, `/`)), diff --git a/website/pages/docs/from-1.5/contextual-variables.mdx b/website/pages/docs/from-1.5/contextual-variables.mdx index 098588bcc..13cbd8e77 100644 --- a/website/pages/docs/from-1.5/contextual-variables.mdx +++ b/website/pages/docs/from-1.5/contextual-variables.mdx @@ -13,7 +13,6 @@ description: |- # Build Variables - Build variables will allow you to access connection information and basic instance state information for a builder. All special build variables are stored in the `build` variable: @@ -72,3 +71,48 @@ For builder-specific builder variables, please also refer to the builder docs: The HCL2 Special Build Variables is in beta; please report any issues or requests on the Packer issue tracker on GitHub. + + +# Packer Version + +This variable is set to the Packer version currently running. + +```hcl +source "null" "first-example" { + communicator = "none" +} + +build { + sources = ["null.first-example"] + + provisioner "shell-local" { + inline = ["echo packer_version is '${packer.version}'"] + } +} +``` + +If you are running a development version of Packer, the version variable will +contain the released version number, dev flag, and current commit. + +```shell-session +PACKER_LOG=0 packer build packer_version_demo.pkr.hcl +null.first-example: output will be in this color. + +==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell083160352 + null.first-example: packer_version is 1.6.5-dev (a69392129+CHANGES) +``` + +If you are running a released version of Packer, the version variable will +contain the released version number only: + +```shell-session +PACKER_LOG=0 packer build packer_version_demo.pkr.hcl +null.first-example: output will be in this color. + +==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell718995312 + null.first-example: packer_version is 1.6.5 +``` + +Make sure to wrap your variable in single quotes in order to escape the +string that is returned; if you are running a dev version of packer the +parenthesis may through off your shell escaping otherwise. diff --git a/website/pages/docs/from-1.5/functions/contextual/index.mdx b/website/pages/docs/from-1.5/functions/contextual/index.mdx new file mode 100644 index 000000000..5e4c9da8d --- /dev/null +++ b/website/pages/docs/from-1.5/functions/contextual/index.mdx @@ -0,0 +1,6 @@ +--- +layout: docs +page_title: Contextual - Functions - Configuration Language +sidebar_title: Contextual Functions +description: Overview of available Contextual functions +--- diff --git a/website/pages/docs/from-1.5/functions/vault/vault.mdx b/website/pages/docs/from-1.5/functions/contextual/vault.mdx similarity index 100% rename from website/pages/docs/from-1.5/functions/vault/vault.mdx rename to website/pages/docs/from-1.5/functions/contextual/vault.mdx diff --git a/website/pages/docs/from-1.5/functions/vault/index.mdx b/website/pages/docs/from-1.5/functions/vault/index.mdx deleted file mode 100644 index 59fb78ef5..000000000 --- a/website/pages/docs/from-1.5/functions/vault/index.mdx +++ /dev/null @@ -1,6 +0,0 @@ ---- -layout: docs -page_title: vault - Functions - Configuration Language -sidebar_title: vault Functions -description: Overview of available vault functions ---- \ No newline at end of file From f696e8286eeb88fbaebb2d1bbf6dbdab781ae585 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 16 Oct 2020 14:09:47 -0700 Subject: [PATCH 2/2] contextual functions menu section for vault func --- website/data/docs-navigation.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/data/docs-navigation.js b/website/data/docs-navigation.js index 27b4feb97..922c47855 100644 --- a/website/data/docs-navigation.js +++ b/website/data/docs-navigation.js @@ -29,6 +29,12 @@ export default [ { category: 'functions', content: [ + { + category: 'contextual', + content: [ + 'vault', + ], + }, { category: 'numeric', content: [