From 4581b3badb7c9fc0f1ce677d2fe0894027ec9499 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 9 Jan 2019 09:48:10 -0800 Subject: [PATCH 1/3] add python example to shell-local docs use abs path for python --- .../docs/provisioners/shell-local.html.md | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/website/source/docs/provisioners/shell-local.html.md b/website/source/docs/provisioners/shell-local.html.md index e6641434a..eb0ecb6fa 100644 --- a/website/source/docs/provisioners/shell-local.html.md +++ b/website/source/docs/provisioners/shell-local.html.md @@ -222,6 +222,7 @@ For a shell script, that means the script **must** exit with a zero code. You ## Usage Examples: +### Windows Host Example of running a .cmd file on windows: { @@ -283,7 +284,8 @@ customizations: env\_var\_format, tempfile\_extension, and execute\_command "inline": ["write-output $env:SHELLLOCALTEST"] } -Example of running a bash script on linux: +### Unix Host +Example of running a bash script on unix: { "type": "shell-local", @@ -291,7 +293,7 @@ Example of running a bash script on linux: "scripts": ["./scripts/dummy_bash.sh"] } -Example of running a bash "inline" on linux: +Example of running a bash "inline" on unix: { "type": "shell-local", @@ -299,3 +301,22 @@ Example of running a bash "inline" on linux: "inline": ["echo hello", "echo $PROVISIONERTEST"] } + +Example of running a python script on unix: + +``` + { + "type": "shell-local", + "script": "hello.py", + "environment_vars": ["HELLO_USER=packeruser"], + "execute_command": ["/bin/sh", "-c", "{{.Vars}} /usr/local/bin/python {{.Script}}"] + } +``` + +Where "hello.py" contains: + +``` +import os + +print('Hello, %s!' % os.getenv("HELLO_USER")) +``` \ No newline at end of file From 56725df3a2cd5c3e3f02b03fefa5ef9e401b71b3 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 9 Jan 2019 11:16:19 -0800 Subject: [PATCH 2/3] add env_var_format to docs since we reference it in the examples --- .../docs/post-processors/shell-local.html.md | 34 +++++++++++++++++-- .../docs/provisioners/shell-local.html.md | 10 +++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/website/source/docs/post-processors/shell-local.html.md b/website/source/docs/post-processors/shell-local.html.md index 0e4fc0734..279548b10 100644 --- a/website/source/docs/post-processors/shell-local.html.md +++ b/website/source/docs/post-processors/shell-local.html.md @@ -60,6 +60,14 @@ Optional parameters: Packer injects some environmental variables by default into the environment, as well, which are covered in the section below. +- `env_var_format` (string) - When we parse the environment_vars that you + provide, this gives us a string template to use in order to make sure that + we are setting the environment vars correctly. By default on Windows hosts + this format is `set %s=%s && ` and on Unix, it is `%s='%s' `. You probably + won't need to change this format, but you can see usage examples for where + it is necessary below. + + - `execute_command` (array of strings) - The command used to execute the script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]` on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. This is @@ -242,8 +250,10 @@ are cleaned up. For a shell script, that means the script **must** exit with a zero code. You *must* be extra careful to `exit 0` when necessary. + ## Usage Examples: +### Windows Host Example of running a .cmd file on windows: { @@ -306,7 +316,8 @@ customizations: env\_var\_format, tempfile\_extension, and execute\_command "inline": ["write-output $env:SHELLLOCALTEST"] } -Example of running a bash script on linux: +### Unix Host +Example of running a bash script on unix: { "type": "shell-local", @@ -314,7 +325,7 @@ Example of running a bash script on linux: "scripts": ["./scripts/example_bash.sh"] } -Example of running a bash "inline" on linux: +Example of running a bash "inline" on unix: { "type": "shell-local", @@ -322,3 +333,22 @@ Example of running a bash "inline" on linux: "inline": ["echo hello", "echo $PROVISIONERTEST"] } + +Example of running a python script on unix: + +``` + { + "type": "shell-local", + "script": "hello.py", + "environment_vars": ["HELLO_USER=packeruser"], + "execute_command": ["/bin/sh", "-c", "{{.Vars}} /usr/local/bin/python {{.Script}}"] + } +``` + +Where "hello.py" contains: + +``` +import os + +print('Hello, %s!' % os.getenv("HELLO_USER")) +``` \ No newline at end of file diff --git a/website/source/docs/provisioners/shell-local.html.md b/website/source/docs/provisioners/shell-local.html.md index eb0ecb6fa..cf9b6cc60 100644 --- a/website/source/docs/provisioners/shell-local.html.md +++ b/website/source/docs/provisioners/shell-local.html.md @@ -74,6 +74,13 @@ Optional parameters: this as an environment variable. For example: `"environment_vars": "WINRMPASS={{.WinRMPassword}}"` +- `env_var_format` (string) - When we parse the environment_vars that you + provide, this gives us a string template to use in order to make sure that + we are setting the environment vars correctly. By default on Windows hosts + this format is `set %s=%s && ` and on Unix, it is `%s='%s' `. You probably + won't need to change this format, but you can see usage examples for where + it is necessary below. + - `execute_command` (array of strings) - The command used to execute the script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]` on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. This is @@ -270,6 +277,7 @@ env\_var\_format and execute\_command "environment_vars": ["SHELLLOCALTEST=ShellTest4"], "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], "env_var_format": "$env:%s=\"%s\"; ", + "script": "./scripts/example_ps.ps1" } Example of running a powershell script on windows as "inline": Required @@ -290,7 +298,7 @@ Example of running a bash script on unix: { "type": "shell-local", "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], - "scripts": ["./scripts/dummy_bash.sh"] + "scripts": ["./scripts/example_bash.sh"] } Example of running a bash "inline" on unix: From a2c3de939661d47ad7c19bb59ba85bd04e10ccda Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 9 Jan 2019 11:16:48 -0800 Subject: [PATCH 3/3] remove outdated logline --- common/shell-local/config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/shell-local/config.go b/common/shell-local/config.go index 030e67ab4..d9c9408b4 100644 --- a/common/shell-local/config.go +++ b/common/shell-local/config.go @@ -202,8 +202,7 @@ func Validate(config *Config) error { "the Script or Scripts options instead")) } } - // This is currently undocumented and not a feature users are expected to - // interact with. + if config.EnvVarFormat == "" { if (runtime.GOOS == "windows") && !config.UseLinuxPathing { config.EnvVarFormat = "set %s=%s && "