github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/website/source/docs/extend/provisioner.html.markdown (about) 1 --- 2 layout: "docs" 3 --- 4 5 # Custom Provisioner Development 6 7 Provisioners are the components of Packer that install and configure 8 software into a running machine prior to turning that machine into an 9 image. An example of a provisioner is the [shell provisioner](/docs/provisioners/shell.html), 10 which runs shell scripts within the machines. 11 12 Prior to reading this page, it is assumed you have read the page on 13 [plugin development basics](/docs/extend/developing-plugins.html). 14 15 Provisioner plugins implement the `packer.Provisioner` interface and 16 are served using the `plugin.ServeProvisioner` function. 17 18 <div class="alert alert-block"> 19 <strong>Warning!</strong> This is an advanced topic. If you're new to Packer, 20 we recommend getting a bit more comfortable before you dive into writing 21 plugins. 22 </div> 23 24 25 ## The Interface 26 27 The interface that must be implemented for a provisioner is the 28 `packer.Provisioner` interface. It is reproduced below for easy reference. 29 The actual interface in the source code contains some basic documentation as well explaining 30 what each method should do. 31 32 33 <pre class="prettyprint"> 34 type Provisioner interface { 35 Prepare(...interface{}) error 36 Provision(Ui, Communicator) error 37 } 38 </pre> 39 40 ### The "Prepare" Method 41 42 The `Prepare` method for each provisioner is called prior to any runs with 43 the configuration that was given in the template. This is passed in as 44 an array of `interface{}` types, but is generally `map[string]interface{}`. The prepare 45 method is responsible for translating this configuration into an internal 46 structure, validating it, and returning any errors. 47 48 For multiple parameters, they should be merged together into the final 49 configuration, with later parameters overwriting any previous configuration. 50 The exact semantics of the merge are left to the builder author. 51 52 For decoding the `interface{}` into a meaningful structure, the 53 [mapstructure](https://github.com/mitchellh/mapstructure) library is recommended. 54 Mapstructure will take an `interface{}` and decode it into an arbitrarily 55 complex struct. If there are any errors, it generates very human friendly 56 errors that can be returned directly from the prepare method. 57 58 While it is not actively enforced, **no side effects** should occur from 59 running the `Prepare` method. Specifically, don't create files, don't launch 60 virtual machines, etc. Prepare's purpose is solely to configure the builder 61 and validate the configuration. 62 63 The `Prepare` method is called very early in the build process so that 64 errors may be displayed to the user before anything actually happens. 65 66 ### The "Provision" Method 67 68 The `Provision` method is called when a machine is running and ready 69 to be provisioned. The provisioner should do its real work here. 70 71 The method takes two parameters: a `packer.Ui` and a `packer.Communicator`. 72 The UI can be used to communicate with the user what is going on. The 73 communicator is used to communicate with the running machine, and is 74 guaranteed to be connected at this point. 75 76 The provision method should not return until provisioning is complete. 77 78 ## Using the Communicator 79 80 The `packer.Communicator` parameter and interface is used to communicate 81 with running machine. The machine may be local (in a virtual machine or 82 container of some sort) or it may be remote (in a cloud). The communicator 83 interface abstracts this away so that communication is the same overall. 84 85 The documentation around the [code itself](https://github.com/mitchellh/packer/blob/master/packer/communicator.go) 86 is really great as an overview of how to use the interface. You should begin 87 by reading this. Once you have read it, you can see some example usage below: 88 89 <pre class="prettyprint"> 90 // Build the remote command. 91 var cmd packer.RemoteCmd 92 cmd.Command = "echo foo" 93 94 // We care about stdout, so lets collect that into a buffer. Since 95 // we don't set stderr, that will just be discarded. 96 var stdout bytes.Buffer 97 cmd.Stdout = &stdout 98 99 // Start the command 100 if err := comm.Start(&cmd); err != nil { 101 panic(err) 102 } 103 104 // Wait for it to complete 105 cmd.Wait() 106 107 // Read the stdout! 108 fmt.Printf("Command output: %s", stdout.String()) 109 </pre>