github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/website/source/docs/extend/post-processor.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  ---
     4  
     5  # Custom Post-Processor Development
     6  
     7  Post-processors are the components of Packer that transform one artifact
     8  into another, for example by compressing files, or uploading them.
     9  
    10  In the compression example, the transformation would be taking an artifact
    11  with a set of files, compressing those files, and returning a new
    12  artifact with only a single file (the compressed archive). For the
    13  upload example, the transformation would be taking an artifact with
    14  some set of files, uploading those files, and returning an artifact
    15  with a single ID: the URL of the upload.
    16  
    17  Prior to reading this page, it is assumed you have read the page on
    18  [plugin development basics](/docs/extend/developing-plugins.html).
    19  
    20  Post-processor plugins implement the `packer.PostProcessor` interface and
    21  are served using the `plugin.ServePostProcessor` function.
    22  
    23  <div class="alert alert-block">
    24    <strong>Warning!</strong> This is an advanced topic. If you're new to Packer,
    25    we recommend getting a bit more comfortable before you dive into writing
    26    plugins.
    27  </div>
    28  
    29  
    30  ## The Interface
    31  
    32  The interface that must be implemented for a post-processor is the
    33  `packer.PostProcessor` interface. It is reproduced below for easy reference.
    34  The actual interface in the source code contains some basic documentation as well explaining
    35  what each method should do.
    36  
    37  
    38  <pre class="prettyprint">
    39  type PostProcessor interface {
    40  	Configure(interface{}) error
    41  	PostProcess(Ui, Artifact) (a Artifact, keep bool, err error)
    42  }
    43  </pre>
    44  
    45  ### The "Configure" Method
    46  
    47  The `Configure` method for each post-processor is called early in the
    48  build process to configure the post-processor. The configuration is passed
    49  in as a raw `interface{]`. The configure method is responsible for translating
    50  this configuration into an internal structure, validating it, and returning
    51  any errors.
    52  
    53  For decoding the `interface{]` into a meaningful structure, the
    54  [mapstructure](https://github.com/mitchellh/mapstructure) library is
    55  recommended. Mapstructure will take an `interface{}` and decode it into an
    56  arbitrarily complex struct. If there are any errors, it generates very
    57  human-friendly errors that can be returned directly from the configure
    58  method.
    59  
    60  While it is not actively enforced, **no side effects** should occur from
    61  running the `Configure` method. Specifically, don't create files, don't
    62  create network connections, etc. Configure's purpose is solely to setup
    63  internal state and validate the configuration as much as possible.
    64  
    65  `Configure` being run is not an indication that `PostProcess` will ever
    66  run. For example, `packer validate` will run `Configure` to verify the
    67  configuration validates, but will never actually run the build.
    68  
    69  ### The "PostProcess" Method
    70  
    71  The `PostProcess` method is where the real work goes. PostProcess is
    72  responsible for taking one `packer.Artifact` implementation, and transforming
    73  it into another.
    74  
    75  When we say "transform," we don't mean actually modifying the existing
    76  `packer.Artifact` value itself. We mean taking the contents of the artifact
    77  and creating a new artifact from that. For example, if we were creating
    78  a "compress" post-processor that is responsible for compressing files,
    79  the transformation would be taking the `Files()` from the original artifact,
    80  compressing them, and creating a new artifact with a single file: the
    81  compressed archive.
    82  
    83  The result signature of this method is `(Artifact, bool, error)`. Each
    84  return value is explained below:
    85  
    86  * `Artifact` - The newly created artifact if no errors occurred.
    87  * `bool` - If true, the input artifact will forcefully be kept. By default,
    88    Packer typically deletes all input artifacts, since the user doesn't generally
    89    want intermediary artifacts. However, some post-processors depend on the
    90    previous artifact existing. If this is `true`, it forces packer to keep the
    91    artifact around.
    92  * `error` - Non-nil if there was an error in any way. If this is the case,
    93    the other two return values are ignored.