github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/website/source/docs/post-processors/shell-local.html.md (about)

     1  ---
     2  description: |
     3      The shell-local Packer post processor enables users to do some post processing after artifacts have been built.
     4  layout: docs
     5  page_title: Local Shell Post Processor
     6  ...
     7  
     8  # Local Shell Post Processor
     9  
    10  Type: `shell-local`
    11  
    12  The local shell post processor executes scripts locally during the post processing stage. Shell local provides an easy
    13  way to automate executing some task with the packer outputs.
    14  
    15  ## Basic example
    16  
    17  The example below is fully functional.
    18  
    19  ``` {.javascript}
    20  {
    21    "type": "shell-local",
    22    "inline": ["echo foo"]
    23  }
    24  ```
    25  
    26  ## Configuration Reference
    27  
    28  The reference of available configuration options is listed below. The only
    29  required element is either "inline" or "script". Every other option is optional.
    30  
    31  Exactly *one* of the following is required:
    32  
    33  -   `inline` (array of strings) - This is an array of commands to execute. The
    34      commands are concatenated by newlines and turned into a single file, so they
    35      are all executed within the same context. This allows you to change
    36      directories in one command and use something in the directory in the next
    37      and so on. Inline scripts are the easiest way to pull off simple tasks
    38      within the machine.
    39  
    40  -   `script` (string) - The path to a script to execute. This path can be
    41      absolute or relative. If it is relative, it is relative to the working
    42      directory when Packer is executed.
    43  
    44  -   `scripts` (array of strings) - An array of scripts to execute. The scripts
    45      will be executed in the order specified. Each script is executed in
    46      isolation, so state such as variables from one script won't carry on to the
    47      next.
    48  
    49  Optional parameters:
    50  
    51  -   `environment_vars` (array of strings) - An array of key/value pairs to
    52      inject prior to the execute\_command. The format should be `key=value`.
    53      Packer injects some environmental variables by default into the environment,
    54      as well, which are covered in the section below.
    55  
    56  -   `execute_command` (string) - The command to use to execute the script. By
    57      default this is `chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`.
    58      The value of this is treated as [configuration template](/docs/templates/configuration-templates.html).
    59      There are two available variables: `Script`, which is the path to the script
    60      to run, `Vars`, which is the list of `environment_vars`, if configured.
    61  
    62  -   `inline_shebang` (string) - The
    63      [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) value to use when
    64      running commands specified by `inline`. By default, this is `/bin/sh -e`. If
    65      you're not using `inline`, then this configuration has no effect.
    66      **Important:** If you customize this, be sure to include something like the
    67      `-e` flag, otherwise individual steps failing won't fail the provisioner.
    68  
    69  ## Execute Command Example
    70  
    71  To many new users, the `execute_command` is puzzling. However, it provides an
    72  important function: customization of how the command is executed. The most
    73  common use case for this is dealing with **sudo password prompts**. You may also
    74  need to customize this if you use a non-POSIX shell, such as `tcsh` on FreeBSD.
    75  
    76  ## Default Environmental Variables
    77  
    78  In addition to being able to specify custom environmental variables using the
    79  `environment_vars` configuration, the provisioner automatically defines certain
    80  commonly useful environmental variables:
    81  
    82  -   `PACKER_BUILD_NAME` is set to the name of the build that Packer is running.
    83      This is most useful when Packer is making multiple builds and you want to
    84      distinguish them slightly from a common provisioning script.
    85  
    86  -   `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the
    87      machine that the script is running on. This is useful if you want to run
    88      only certain parts of the script on systems built with certain builders.
    89  
    90  ## Safely Writing A Script
    91  
    92  Whether you use the `inline` option, or pass it a direct `script` or `scripts`,
    93  it is important to understand a few things about how the shell-local
    94  post-processor works to run it safely and easily. This understanding will save
    95  you much time in the process.
    96  
    97  ### Once Per Builder
    98  
    99  The `shell-local` script(s) you pass are run once per builder.  That means that
   100  if you have an `amazon-ebs` builder and a `docker` builder, your script will be
   101  run twice. If you have 3 builders, it will run 3 times, once for each builder.
   102  
   103  ### Interacting with Build Artifacts
   104  
   105  In order to interact with build artifacts, you may want to use the [manifest
   106  post-processor](/docs/post-processors/manifest.html). This will write the list
   107  of files produced by a `builder` to a json file after each `builder` is run.
   108  
   109  For example, if you wanted to package a file from the file builder into
   110  a tarball, you might wright this:
   111  
   112  ```json
   113  {
   114      "builders": [
   115          {
   116              "content": "Lorem ipsum dolor sit amet",
   117              "target": "dummy_artifact",
   118              "type": "file"
   119          }
   120      ],
   121      "post-processors": [
   122          [
   123              {
   124                  "output": "manifest.json",
   125                  "strip_path": true,
   126                  "type": "manifest"
   127              },
   128              {
   129                  "inline": [
   130                      "jq ".builds[].files[].name" manifest.json | xargs tar cfz artifacts.tgz"
   131                  ],
   132                  "type": "shell-local"
   133              }
   134          ]
   135      ]
   136  }
   137  ```
   138  
   139  This uses the [jq](https://stedolan.github.io/jq/) tool to extract all of the
   140  file names from the manifest file and passes them to tar.
   141  
   142  ### Always Exit Intentionally
   143  
   144  If any post-processor fails, the `packer build` stops and all interim artifacts
   145  are cleaned up.
   146  
   147  For a shell script, that means the script **must** exit with a zero code. You
   148  *must* be extra careful to `exit 0` when necessary.
   149