docs: Add HCL2 code examples to shell-local provisioner and post-processor. (#9602)

* get start on shell-local postprocessor

* HCL2 tabs for shell-local pp

* shell local provisoner hcl examples
pull/9614/head
Megan Marsh 6 years ago committed by GitHub
parent 8964367eb5
commit 6439ebff22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,15 +17,52 @@ some task with packer outputs and variables.
## Basic example ## Basic example
The example below is fully functional. The example below is a fully functional self-contained build.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "builders": [
"inline": ["echo foo"] {
"type": "file",
"name": "example",
"target": "./test_artifact.txt",
"content": "example content"
}
],
"post-processors": [
{
"type": "shell-local",
"inline": ["echo foo"]
}
]
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "file" "example" {
content = "example content"
}
build {
source "source.file.example" {
target = "./test_artifact.txt"
}
post-processor "shell-local" {
inline = ["echo foo"]
}
}
```
</Tab>
</Tabs>
## Configuration Reference ## Configuration Reference
The reference of available configuration options is listed below. The only The reference of available configuration options is listed below. The only
@ -173,6 +210,10 @@ still in beta. There will be some limitations as a result. For example, it will
likely not work unless both Packer and the scripts you want to run are both on likely not work unless both Packer and the scripts you want to run are both on
the C drive. the C drive.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"builders": [ "builders": [
@ -181,7 +222,7 @@ the C drive.
"communicator": "none" "communicator": "none"
} }
], ],
"provisioners": [ "post-processors": [
{ {
"type": "shell-local", "type": "shell-local",
"environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
@ -200,6 +241,37 @@ the C drive.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "null" "example" {
communicator = "none"
}
build {
sources = [
"source.null.example"
]
post-processor "shell-local"{
environment_vars = ["PROVISIONERTEST=ProvisionerTest1"]
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
scripts = ["C:/Users/me/scripts/example_bash.sh"]
}
post-processor "shell-local"{
environment_vars = ["PROVISIONERTEST=ProvisionerTest2"]
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
script = "C:/Users/me/scripts/example_bash.sh"
}
}
```
</Tab>
</Tabs>
## Default Environmental Variables ## Default Environmental Variables
In addition to being able to specify custom environmental variables using the In addition to being able to specify custom environmental variables using the
@ -238,6 +310,9 @@ of files produced by a `builder` to a json file after each `builder` is run.
For example, if you wanted to package a file from the file builder into a For example, if you wanted to package a file from the file builder into a
tarball, you might write this: tarball, you might write this:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"builders": [ "builders": [
@ -265,6 +340,34 @@ tarball, you might write this:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "file" "example" {
content = "Lorem ipsum dolor sit amet"
target = "dummy_artifact.txt"
}
build {
sources = [
"source.file.example"
]
post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
post-processor "shell-local" {
inline = [
"jq \".builds[].files[].name\" manifest.json | xargs tar cfz artifacts.tgz"
]
}
}
```
</Tab>
</Tabs>
This uses the [jq](https://stedolan.github.io/jq/) tool to extract all of the This uses the [jq](https://stedolan.github.io/jq/) tool to extract all of the
file names from the manifest file and passes them to tar. file names from the manifest file and passes them to tar.
@ -282,6 +385,9 @@ _must_ be extra careful to `exit 0` when necessary.
Example of running a .cmd file on windows: Example of running a .cmd file on windows:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -290,6 +396,20 @@ Example of running a .cmd file on windows:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest1"]
scripts = ["./scripts/test_cmd.cmd"]
}
```
</Tab>
</Tabs>
Contents of "test_cmd.cmd": Contents of "test_cmd.cmd":
echo %SHELLLOCALTEST% echo %SHELLLOCALTEST%
@ -297,6 +417,10 @@ Contents of "test_cmd.cmd":
Example of running an inline command on windows: Required customization: Example of running an inline command on windows: Required customization:
tempfile_extension tempfile_extension
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -306,9 +430,26 @@ tempfile_extension
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest2"],
tempfile_extension = ".cmd",
inline = [echo %SHELLLOCALTEST%"]
}
```
</Tab>
</Tabs>
Example of running a bash command on windows using WSL: Required Example of running a bash command on windows using WSL: Required
customizations: use_linux_pathing and execute_command customizations: use_linux_pathing and execute_command
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -319,6 +460,21 @@ customizations: use_linux_pathing and execute_command
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest3"],
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
script = "./scripts/example_bash.sh"
}
```
</Tab>
</Tabs>
Contents of "example_bash.sh": Contents of "example_bash.sh":
#!/bin/bash #!/bin/bash
@ -327,6 +483,9 @@ Contents of "example_bash.sh":
Example of running a powershell script on windows: Required customizations: Example of running a powershell script on windows: Required customizations:
env_var_format and execute_command env_var_format and execute_command
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -337,9 +496,27 @@ env_var_format and execute_command
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest4"]
execute_command = ["powershell.exe", "{{.Vars}} {{.Script}}"]
env_var_format = "$env:%s=\"%s\"; "
script = "./scripts/example_ps.ps1"
}
```
</Tab>
</Tabs>
Example of running a powershell script on windows as "inline": Required Example of running a powershell script on windows as "inline": Required
customizations: env_var_format, tempfile_extension, and execute_command customizations: env_var_format, tempfile_extension, and execute_command
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -351,10 +528,29 @@ customizations: env_var_format, tempfile_extension, and execute_command
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
tempfile_extension = ".ps1"
environment_vars = ["SHELLLOCALTEST=ShellTest5"]
execute_command = ["powershell.exe", "{{.Vars}} {{.Script}}"]
env_var_format = "$env:%s=\"%s\"; "
inline = ["write-output $env:SHELLLOCALTEST"]
}
```
</Tab>
</Tabs>
### Unix Host ### Unix Host
Example of running a bash script on unix: Example of running a bash script on unix:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -363,8 +559,24 @@ Example of running a bash script on unix:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["PROVISIONERTEST=ProvisionerTest1"]
scripts = ["./scripts/example_bash.sh"]
}
```
</Tab>
</Tabs>
Example of running a bash "inline" on unix: Example of running a bash "inline" on unix:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -373,8 +585,25 @@ Example of running a bash "inline" on unix:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
environment_vars = ["PROVISIONERTEST=ProvisionerTest2"]
inline = ["echo hello", "echo $PROVISIONERTEST"]
}
```
</Tab>
</Tabs>
Example of running a python script on unix: Example of running a python script on unix:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "type": "shell-local",
@ -388,6 +617,25 @@ Example of running a python script on unix:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processor "shell-local" {
script = "hello.py"
environment_vars = ["HELLO_USER=packeruser"]
execute_command = [
"/bin/sh",
"-c",
"{{.Vars}} /usr/local/bin/python {{.Script}}"
]
}
```
</Tab>
</Tabs>
Where "hello.py" contains: Where "hello.py" contains:
import os import os

@ -25,13 +25,51 @@ scripts on a remote machine.
The example below is fully functional. The example below is fully functional.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "shell-local", "builders": [
"command": "echo foo" {
"type": "file",
"name": "example",
"target": "./test_artifact.txt",
"content": "example content"
}
],
"provisioners": [
{
"type": "shell-local",
"inline": ["echo foo"]
}
]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
source "file" "example" {
content = "example content"
}
build {
source "source.file.example" {
target = "./test_artifact.txt"
}
provisioner "shell-local" {
inline = ["echo foo"]
}
} }
``` ```
</Tab>
</Tabs>
## Configuration Reference ## Configuration Reference
The reference of available configuration options is listed below. The only The reference of available configuration options is listed below. The only
@ -164,30 +202,66 @@ options instead.
Please note that the WSL is a beta feature, and this tool is not guaranteed to Please note that the WSL is a beta feature, and this tool is not guaranteed to
work as you expect it to. work as you expect it to.
<Tabs>
<Tab heading="JSON">
```json
{
"builders": [
{
"type": "null",
"communicator": "none"
}
],
"provisioners": [
{ {
"builders": [ "type": "shell-local",
{ "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
"type": "null", "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
"communicator": "none" "use_linux_pathing": true,
} "scripts": ["C:/Users/me/scripts/example_bash.sh"]
], },
"provisioners": [ {
{ "type": "shell-local",
"type": "shell-local", "environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
"environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
"execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], "use_linux_pathing": true,
"use_linux_pathing": true, "script": "C:/Users/me/scripts/example_bash.sh"
"scripts": ["C:/Users/me/scripts/example_bash.sh"] }
}, ]
{ }
"type": "shell-local", ```
"environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
"execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], </Tab>
"use_linux_pathing": true, <Tab heading="HCL2">
"script": "C:/Users/me/scripts/example_bash.sh"
} ```hcl
] source "null" "example" {
communicator = "none"
}
build {
sources = [
"source.null.example"
]
provisioner "shell-local"{
environment_vars = ["PROVISIONERTEST=ProvisionerTest1"]
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
scripts = ["C:/Users/me/scripts/example_bash.sh"]
}
provisioner "shell-local"{
environment_vars = ["PROVISIONERTEST=ProvisionerTest2"]
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
script = "C:/Users/me/scripts/example_bash.sh"
} }
}
```
</Tab>
</Tabs>
## Default Environmental Variables ## Default Environmental Variables
@ -234,15 +308,36 @@ _must_ be extra careful to `exit 0` when necessary.
## Usage Examples: ## Usage Examples:
### Windows Host ### Windows Host
Example of running a .cmd file on windows: Example of running a .cmd file on windows:
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"environment_vars": ["SHELLLOCALTEST=ShellTest1"],
"scripts": ["./scripts/test_cmd.cmd"] ```json
} {
"type": "shell-local",
"environment_vars": ["SHELLLOCALTEST=ShellTest1"],
"scripts": ["./scripts/test_cmd.cmd"]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest1"]
scripts = ["./scripts/test_cmd.cmd"]
}
```
</Tab>
</Tabs>
Contents of "test_cmd.cmd": Contents of "test_cmd.cmd":
@ -251,23 +346,63 @@ Contents of "test_cmd.cmd":
Example of running an inline command on windows: Required customization: Example of running an inline command on windows: Required customization:
tempfile_extension tempfile_extension
{
"type": "shell-local", <Tabs>
"environment_vars": ["SHELLLOCALTEST=ShellTest2"], <Tab heading="JSON">
"tempfile_extension": ".cmd",
"inline": ["echo %SHELLLOCALTEST%"] ```json
} {
"type": "shell-local",
"environment_vars": ["SHELLLOCALTEST=ShellTest2"],
"tempfile_extension": ".cmd",
"inline": ["echo %SHELLLOCALTEST%"]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest2"],
tempfile_extension = ".cmd",
inline = [echo %SHELLLOCALTEST%"]
}
```
</Tab>
</Tabs>
Example of running a bash command on windows using WSL: Required Example of running a bash command on windows using WSL: Required
customizations: use_linux_pathing and execute_command customizations: use_linux_pathing and execute_command
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"environment_vars": ["SHELLLOCALTEST=ShellTest3"],
"execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], ```json
"use_linux_pathing": true, {
"script": "./scripts/example_bash.sh" "type": "shell-local",
} "environment_vars": ["SHELLLOCALTEST=ShellTest3"],
"execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
"use_linux_pathing": true,
"script": "./scripts/example_bash.sh"
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest3"],
execute_command = ["bash", "-c", "{{.Vars}} {{.Script}}"]
use_linux_pathing = true
script = "./scripts/example_bash.sh"
}
```
</Tab>
</Tabs>
Contents of "example_bash.sh": Contents of "example_bash.sh":
@ -277,53 +412,158 @@ Contents of "example_bash.sh":
Example of running a powershell script on windows: Required customizations: Example of running a powershell script on windows: Required customizations:
env_var_format and execute_command env_var_format and execute_command
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"environment_vars": ["SHELLLOCALTEST=ShellTest4"],
"execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], ```json
"env_var_format": "$env:%s=\"%s\"; ", {
"script": "./scripts/example_ps.ps1" "type": "shell-local",
} "environment_vars": ["SHELLLOCALTEST=ShellTest4"],
"execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
"env_var_format": "$env:%s=\"%s\"; ",
"script": "./scripts/example_ps.ps1"
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["SHELLLOCALTEST=ShellTest4"]
execute_command = ["powershell.exe", "{{.Vars}} {{.Script}}"]
env_var_format = "$env:%s=\"%s\"; "
script = "./scripts/example_ps.ps1"
}
```
</Tab>
</Tabs>
Example of running a powershell script on windows as "inline": Required Example of running a powershell script on windows as "inline": Required
customizations: env_var_format, tempfile_extension, and execute_command customizations: env_var_format, tempfile_extension, and execute_command
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"tempfile_extension": ".ps1",
"environment_vars": ["SHELLLOCALTEST=ShellTest5"], ```json
"execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], {
"env_var_format": "$env:%s=\"%s\"; ", "type": "shell-local",
"inline": ["write-output $env:SHELLLOCALTEST"] "tempfile_extension": ".ps1",
} "environment_vars": ["SHELLLOCALTEST=ShellTest5"],
"execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
"env_var_format": "$env:%s=\"%s\"; ",
"inline": ["write-output $env:SHELLLOCALTEST"]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
tempfile_extension = ".ps1"
environment_vars = ["SHELLLOCALTEST=ShellTest5"]
execute_command = ["powershell.exe", "{{.Vars}} {{.Script}}"]
env_var_format = "$env:%s=\"%s\"; "
inline = ["write-output $env:SHELLLOCALTEST"]
}
```
</Tab>
</Tabs>
### Unix Host ### Unix Host
Example of running a bash script on unix: Example of running a bash script on unix:
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
"scripts": ["./scripts/example_bash.sh"] ```json
} {
"type": "shell-local",
"environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
"scripts": ["./scripts/example_bash.sh"]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["PROVISIONERTEST=ProvisionerTest1"]
scripts = ["./scripts/example_bash.sh"]
}
```
</Tab>
</Tabs>
Example of running a bash "inline" on unix: Example of running a bash "inline" on unix:
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
"inline": ["echo hello", ```json
"echo $PROVISIONERTEST"] {
} "type": "shell-local",
"environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
"inline": ["echo hello", "echo $PROVISIONERTEST"]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
environment_vars = ["PROVISIONERTEST=ProvisionerTest2"]
inline = ["echo hello", "echo $PROVISIONERTEST"]
}
```
</Tab>
</Tabs>
Example of running a python script on unix: Example of running a python script on unix:
{ <Tabs>
"type": "shell-local", <Tab heading="JSON">
"script": "hello.py",
"environment_vars": ["HELLO_USER=packeruser"], ```json
"execute_command": ["/bin/sh", "-c", "{{.Vars}} /usr/local/bin/python {{.Script}}"] {
} "type": "shell-local",
"script": "hello.py",
"environment_vars": ["HELLO_USER=packeruser"],
"execute_command": [
"/bin/sh",
"-c",
"{{.Vars}} /usr/local/bin/python {{.Script}}"
]
}
```
</Tab>
<Tab heading="HCL2">
```hcl
provisioner "shell-local" {
script = "hello.py"
environment_vars = ["HELLO_USER=packeruser"]
execute_command = [
"/bin/sh",
"-c",
"{{.Vars}} /usr/local/bin/python {{.Script}}"
]
}
```
</Tab>
</Tabs>
Where "hello.py" contains: Where "hello.py" contains:

Loading…
Cancel
Save