Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all shells: Pass env vars through a key value store #11569

Merged
merged 13 commits into from Feb 16, 2022
14 changes: 14 additions & 0 deletions command/build_test.go
Expand Up @@ -465,6 +465,20 @@ func TestBuild(t *testing.T) {
},
},
},
{
name: "hcl - unknown ",
args: []string{
testFixture("hcl", "data-source-validation.pkr.hcl"),
},
fileCheck: fileCheck{
expectedContent: map[string]string{
"foo.txt": "foo",
},
expected: []string{
"s3cr3t",
},
},
},
}

for _, tt := range tc {
Expand Down
39 changes: 39 additions & 0 deletions command/test-fixtures/hcl/data-source-validation.pkr.hcl
@@ -0,0 +1,39 @@

data "null" "secret" {
input = "s3cr3t"
}

locals {
secret = data.null.secret.output
}

source "file" "foo" {
content = "foo"
target = "foo.txt"
}

build {
sources = ["file.foo"]
provisioner "shell-local" {
only_on = ["darwin", "freebsd", "linux", "openbsd", "solaris"]
# original bug in :
# environment_vars = ["MY_SECRET=${local.secret}"]
env = {
"MY_SECRET":"${local.secret}",
}
inline = [
"echo yo, my secret is $MY_SECRET",
"echo '' > $MY_SECRET",
]
}
provisioner "shell-local" {
only_on = ["windows"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a neat feature for tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little gem.

env = {
"MY_SECRET":"${local.secret}",
}
inline = [
"echo yo, my secret is %MY_SECRET%",
"echo '' > %MY_SECRET%",
]
}
}
2 changes: 2 additions & 0 deletions command/test_utils.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/packer/builder/null"
hcppackerimagedatasource "github.com/hashicorp/packer/datasource/hcp-packer-image"
hcppackeriterationdatasource "github.com/hashicorp/packer/datasource/hcp-packer-iteration"
nulldatasource "github.com/hashicorp/packer/datasource/null"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/post-processor/manifest"
shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local"
Expand Down Expand Up @@ -51,6 +52,7 @@ func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
},
DataSources: packer.MapOfDatasource{
"mock": func() (packersdk.Datasource, error) { return &packersdk.MockDatasource{}, nil },
"null": func() (packersdk.Datasource, error) { return &nulldatasource.Datasource{}, nil },
"hcp-packer-image": func() (packersdk.Datasource, error) { return &hcppackerimagedatasource.Datasource{}, nil },
"hcp-packer-iteration": func() (packersdk.Datasource, error) { return &hcppackeriterationdatasource.Datasource{}, nil },
},
Expand Down
3 changes: 3 additions & 0 deletions command/validate_test.go
Expand Up @@ -36,6 +36,9 @@ func TestValidateCommand(t *testing.T) {

// Should return multiple errors,
{path: filepath.Join(testFixture("validate", "circular_error.pkr.hcl")), exitCode: 1},

// datasource could be unknown at that moment
{path: filepath.Join(testFixture("hcl", "data-source-validation.pkr.hcl")), exitCode: 0},
}

for _, tc := range tt {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -24,7 +24,7 @@ require (
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/hcp-sdk-go v0.15.1-0.20220112153249-f565607d7cc4
github.com/hashicorp/packer-plugin-amazon v1.0.6
github.com/hashicorp/packer-plugin-sdk v0.2.11
github.com/hashicorp/packer-plugin-sdk v0.2.12-0.20220215104433-24404dc7a52e
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869
github.com/klauspost/compress v1.13.5 // indirect
github.com/klauspost/pgzip v1.2.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -768,6 +768,8 @@ github.com/hashicorp/packer-plugin-sdk v0.2.7/go.mod h1:ii9ub5UNAp30RGod3i3W8qj7
github.com/hashicorp/packer-plugin-sdk v0.2.9/go.mod h1:ii9ub5UNAp30RGod3i3W8qj7wA+H7kpURnD+Jt7oDkQ=
github.com/hashicorp/packer-plugin-sdk v0.2.11 h1:FsL2oOfLJmXC9F6W9eMFe0eUi3+ggFuqkLGX41fyPE0=
github.com/hashicorp/packer-plugin-sdk v0.2.11/go.mod h1:DI8REf9TEIcVkYPErI/nedo6zS2h81ze7sKuc/mIlTE=
github.com/hashicorp/packer-plugin-sdk v0.2.12-0.20220215104433-24404dc7a52e h1:bWsX98L1C0rFA1q3YVYNqUltygRxy7y7QKZ6upg2bO4=
github.com/hashicorp/packer-plugin-sdk v0.2.12-0.20220215104433-24404dc7a52e/go.mod h1:DI8REf9TEIcVkYPErI/nedo6zS2h81ze7sKuc/mIlTE=
github.com/hashicorp/packer-plugin-tencentcloud v1.0.3 h1:8U2vMFyIE+pizoMDsSLyMBsbNdB1HzRAIaTJNIELyi4=
github.com/hashicorp/packer-plugin-tencentcloud v1.0.3/go.mod h1:TCSlq9lCFU8H8eMMWNtwdnCeyHmaQ1A13PR11EJoJh0=
github.com/hashicorp/packer-plugin-triton v1.0.0 h1:Uvh8fjEKqlii61BzIt1VEgSyJXL+UYfuMHCj44aVpU8=
Expand Down
2 changes: 2 additions & 0 deletions provisioner/powershell/provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions provisioner/shell/provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions provisioner/windows-shell/provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions website/content/docs/post-processors/shell-local.mdx
Expand Up @@ -94,6 +94,11 @@ Exactly _one_ of the following is required:

Optional parameters:

- `env` (map of strings) - A map of key/value pairs to inject prior to the
execute_command. Packer injects some environmental variables by default into
the environment, as well, which are covered in the section below. Duplciate
`env` settings override `environment_vars` settings.

- `environment_vars` (array of strings) - An array of key/value pairs to
inject prior to the `execute_command`. The format should be `key=value`.
Packer injects some environmental variables by default into the
Expand Down
6 changes: 6 additions & 0 deletions website/content/docs/provisioners/powershell.mdx
Expand Up @@ -75,6 +75,12 @@ provisioner "powershell" {
- `Vars`: The location of a temp file containing the list of
`environment_vars`, if configured.

- `env` (map of strings) - A map of key/value pairs to inject prior to the
execute_command. Packer injects some environmental variables by default into
the environment, as well, which are covered in the section below. Duplciate
`env` settings override `environment_vars` settings. This is not a JSON
template engine enabled function. HCL interpolation works as usual.

- `environment_vars` (array of strings) - An array of key/value pairs to
inject prior to the execute_command. The format should be `key=value`.
Packer injects some environmental variables by default into the
Expand Down
5 changes: 5 additions & 0 deletions website/content/docs/provisioners/shell-local.mdx
Expand Up @@ -103,6 +103,11 @@ Exactly _one_ of the following is required:

Optional parameters:

- `env` (map of strings) - A map of key/value pairs to inject prior to the
execute_command. Packer injects some environmental variables by default into
the environment, as well, which are covered in the section below. Duplciate
`env` settings override `environment_vars` settings.

- `environment_vars` (array of strings) - An array of key/value pairs to
inject prior to the `execute_command`. The format should be `key=value`.
Packer injects some environmental variables by default into the
Expand Down
5 changes: 5 additions & 0 deletions website/content/docs/provisioners/shell.mdx
Expand Up @@ -52,6 +52,11 @@ provisioner "shell" {

@include 'provisioners/shell-config.mdx'

- `env` (map of strings) - A map of key/value pairs to inject prior to the
execute_command. Packer injects some environmental variables by default into
the environment, as well, which are covered in the section below. Duplciate
`env` settings override `environment_vars` settings.

- `environment_vars` (array of strings) - An array of key/value pairs to
inject prior to the execute_command. The format should be `key=value`.
Packer injects some environmental variables by default into the
Expand Down
5 changes: 5 additions & 0 deletions website/content/docs/provisioners/windows-shell.mdx
Expand Up @@ -46,6 +46,11 @@ provisioner "windows-shell" {

@include 'provisioners/shell-config.mdx'

- `env` (map of strings) - A map of key/value pairs to inject prior to the
execute_command. Packer injects some environmental variables by default into
the environment, as well, which are covered in the section below. Duplciate
`env` settings override `environment_vars` settings.

- `environment_vars` (array of strings) - An array of key/value pairs to
inject prior to the execute_command. The format should be `key=value`.
Packer injects some environmental variables by default into the
Expand Down