github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/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 upload and execute in
    41      the machine. This path can be absolute or relative. If it is relative, it is
    42      relative to the working directory when Packer is executed.
    43  
    44  -   `scripts` (array of strings) - An array of scripts to execute. The scripts
    45      will be uploaded and executed in the order specified. Each script is
    46      executed in isolation, so state such as variables from one script won't
    47      carry on to the 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}} {{.Artifact}}`.
    58      The value of this is treated as [configuration template](/docs/templates/configuration-templates.html).
    59      There are three available variables: `Script`, which is the path to the script
    60      to run, `Vars`, which is the list of `environment_vars`, if configured and
    61      `Artifact`, which is path to artifact file.
    62  
    63  -   `inline_shebang` (string) - The
    64      [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) value to use when
    65      running commands specified by `inline`. By default, this is `/bin/sh -e`. If
    66      you're not using `inline`, then this configuration has no effect.
    67      **Important:** If you customize this, be sure to include something like the
    68      `-e` flag, otherwise individual steps failing won't fail the provisioner.
    69  
    70  ## Execute Command Example
    71  
    72  To many new users, the `execute_command` is puzzling. However, it provides an
    73  important function: customization of how the command is executed. The most
    74  common use case for this is dealing with **sudo password prompts**. You may also
    75  need to customize this if you use a non-POSIX shell, such as `tcsh` on FreeBSD.
    76  
    77  ## Default Environmental Variables
    78  
    79  In addition to being able to specify custom environmental variables using the
    80  `environment_vars` configuration, the provisioner automatically defines certain
    81  commonly useful environmental variables:
    82  
    83  -   `PACKER_BUILD_NAME` is set to the name of the build that Packer is running.
    84      This is most useful when Packer is making multiple builds and you want to
    85      distinguish them slightly from a common provisioning script.
    86  
    87  -   `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the
    88      machine that the script is running on. This is useful if you want to run
    89      only certain parts of the script on systems built with certain builders.
    90  
    91  ## Safely Writing A Script
    92  
    93  Whether you use the `inline` option, or pass it a direct `script` or `scripts`, it is important to understand a few things about how the shell-local post-processor works to run it safely and easily. This understanding will save you much time in the process.
    94  
    95  ### Once Per Artifact
    96  
    97  The `shell-local` script(s) you pass are run once per artifact output file. That means that if your builder results in 1 output file, your script will be run once. If it results in 3 output files, it will run 3 times, once for each file.
    98  
    99  For example, the virtualbox builders, when configured to provide an `ovf` output format (the default), will provide **two** output files:
   100  
   101  * The actual disk itself, in `.vmdk` format
   102  * The appliance description file, in `.ovf` format
   103  
   104  Each time each shell-local script is run, it is passed the path to the artifact file, relative to the directory in which packer is run, as the first argument to the script. 
   105  
   106  Let's take a simple example. You want to run a post-processor that records the name of every artifact created to `/tmp/artifacts`. (Why? I don't know. For fun.)
   107  
   108  Your post-processor should look like this:
   109  
   110  
   111  ``` {.javascript}
   112  {
   113    "type": "shell-local",
   114    "inline": [
   115      "echo \$1 >> /tmp/artifacts"
   116    ]
   117  }
   118  ```
   119  
   120  The result of the above will be an output line for each artifact.
   121  
   122  The net effect of this is that if you want to post-process only some files, **you must test** `$1` to see if it is the file you want.
   123  
   124  Here is an example script that converts the `.vmdk` artifact of a virtualbox build to a raw img, suitable for converting to a USB.
   125  
   126  
   127  ``` {.bash}
   128      #!/bin/bash -e
   129  
   130      [[ "$1" == *.vmdk ]] && vboxmanage clonemedium disk $1 --format raw output_file.img
   131  
   132  ```
   133  
   134  ### Always Exit Intentionally
   135  
   136  If any post-processor fails, the `packer build` stops and all interim artifacts are cleaned up.
   137  
   138  For a shell script, that means the script **must** exit with a zero code. You *must* be extra careful to `exit 0` when necessary. Using our above conversion script example, if the current artifact is *not* a `.vmdk` file, the test `[[ "$1" == *.vmdk ]]` will fail. Since that is the last command in the script, the script will exit with a non-zero code, the post-processor will fail, the build will fail, and you will have to start over.
   139  
   140  Of course, we didn't mean that! We just meant:
   141  
   142  * If a `.vmdk` file, convert, and that is OK
   143  * If not a `.vmdk` file, ignore, and that is OK
   144  
   145  To make it work correctly, use the following instead:
   146  
   147  ``` {.bash}
   148      #!/bin/bash -e
   149  
   150      [[ "$1" == *.vmdk ]] && vboxmanage clonemedium disk $1 --format raw output_file.img
   151  
   152      # always exit 0 unless a command actually fails
   153      exit 0
   154  
   155  ````
   156