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