Merge pull request #7181 from hashicorp/python_shell_docs

add python example to shell-local docs
This commit is contained in:
Adrien Delorme 2019-01-10 10:43:55 +01:00 committed by GitHub
commit 9f2a3bdfbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 7 deletions

View File

@ -202,8 +202,7 @@ func Validate(config *Config) error {
"the Script or Scripts options instead")) "the Script or Scripts options instead"))
} }
} }
// This is currently undocumented and not a feature users are expected to
// interact with.
if config.EnvVarFormat == "" { if config.EnvVarFormat == "" {
if (runtime.GOOS == "windows") && !config.UseLinuxPathing { if (runtime.GOOS == "windows") && !config.UseLinuxPathing {
config.EnvVarFormat = "set %s=%s && " config.EnvVarFormat = "set %s=%s && "

View File

@ -60,6 +60,14 @@ Optional parameters:
Packer injects some environmental variables by default into the Packer injects some environmental variables by default into the
environment, as well, which are covered in the section below. 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 - `execute_command` (array of strings) - The command used to execute the
script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]` script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]`
on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. This is 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 For a shell script, that means the script **must** exit with a zero code. You
*must* be extra careful to `exit 0` when necessary. *must* be extra careful to `exit 0` when necessary.
## Usage Examples: ## Usage Examples:
### Windows Host
Example of running a .cmd file on windows: 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"] "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", "type": "shell-local",
@ -314,7 +325,7 @@ Example of running a bash script on linux:
"scripts": ["./scripts/example_bash.sh"] "scripts": ["./scripts/example_bash.sh"]
} }
Example of running a bash "inline" on linux: Example of running a bash "inline" on unix:
{ {
"type": "shell-local", "type": "shell-local",
@ -322,3 +333,22 @@ Example of running a bash "inline" on linux:
"inline": ["echo hello", "inline": ["echo hello",
"echo $PROVISIONERTEST"] "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"))
```

View File

@ -74,6 +74,13 @@ Optional parameters:
this as an environment variable. For example: this as an environment variable. For example:
`"environment_vars": "WINRMPASS={{.WinRMPassword}}"` `"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 - `execute_command` (array of strings) - The command used to execute the
script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]` script. By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]`
on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. This is on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. This is
@ -222,6 +229,7 @@ For a shell script, that means the script **must** exit with a zero code. You
## Usage Examples: ## Usage Examples:
### Windows Host
Example of running a .cmd file on windows: Example of running a .cmd file on windows:
{ {
@ -269,6 +277,7 @@ env\_var\_format and execute\_command
"environment_vars": ["SHELLLOCALTEST=ShellTest4"], "environment_vars": ["SHELLLOCALTEST=ShellTest4"],
"execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
"env_var_format": "$env:%s=\"%s\"; ", "env_var_format": "$env:%s=\"%s\"; ",
"script": "./scripts/example_ps.ps1"
} }
Example of running a powershell script on windows as "inline": Required Example of running a powershell script on windows as "inline": Required
@ -283,15 +292,16 @@ customizations: env\_var\_format, tempfile\_extension, and execute\_command
"inline": ["write-output $env:SHELLLOCALTEST"] "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", "type": "shell-local",
"environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
"scripts": ["./scripts/dummy_bash.sh"] "scripts": ["./scripts/example_bash.sh"]
} }
Example of running a bash "inline" on linux: Example of running a bash "inline" on unix:
{ {
"type": "shell-local", "type": "shell-local",
@ -299,3 +309,22 @@ Example of running a bash "inline" on linux:
"inline": ["echo hello", "inline": ["echo hello",
"echo $PROVISIONERTEST"] "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"))
```