Commit 65916514 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge branch 'puppet_working_dir' of https://github.com/ColinHebert/packer...

Merge branch 'puppet_working_dir' of https://github.com/ColinHebert/packer into ColinHebert-puppet_working_dir
parents 8dfd553e 34e34d1f
...@@ -44,6 +44,10 @@ type Config struct { ...@@ -44,6 +44,10 @@ type Config struct {
// The directory where files will be uploaded. Packer requires write // The directory where files will be uploaded. Packer requires write
// permissions in this directory. // permissions in this directory.
StagingDir string `mapstructure:"staging_directory"` StagingDir string `mapstructure:"staging_directory"`
// The directory from which the command will be executed.
// Packer requires the directory to exist when running puppet.
WorkingDir string `mapstructure:"working_directory"`
} }
type Provisioner struct { type Provisioner struct {
...@@ -51,6 +55,7 @@ type Provisioner struct { ...@@ -51,6 +55,7 @@ type Provisioner struct {
} }
type ExecuteTemplate struct { type ExecuteTemplate struct {
WorkingDir string
FacterVars string FacterVars string
HieraConfigPath string HieraConfigPath string
ModulePath string ModulePath string
...@@ -74,7 +79,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -74,7 +79,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
// Set some defaults // Set some defaults
if p.config.ExecuteCommand == "" { if p.config.ExecuteCommand == "" {
p.config.ExecuteCommand = "{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" + p.config.ExecuteCommand = "cd {{.WorkingDir}} && " +
"{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" +
"puppet apply --verbose --modulepath='{{.ModulePath}}' " + "puppet apply --verbose --modulepath='{{.ModulePath}}' " +
"{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" + "{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" +
"{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" + "{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" +
...@@ -86,6 +92,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -86,6 +92,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.StagingDir = "/tmp/packer-puppet-masterless" p.config.StagingDir = "/tmp/packer-puppet-masterless"
} }
if p.config.WorkingDir == "" {
p.config.WorkingDir = p.config.StagingDir
}
// Validation // Validation
var errs *packer.MultiError var errs *packer.MultiError
if p.config.HieraConfigPath != "" { if p.config.HieraConfigPath != "" {
...@@ -200,6 +210,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { ...@@ -200,6 +210,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ManifestFile: remoteManifestFile, ManifestFile: remoteManifestFile,
ModulePath: strings.Join(modulePaths, ":"), ModulePath: strings.Join(modulePaths, ":"),
Sudo: !p.config.PreventSudo, Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
} }
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx) command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil { if err != nil {
......
...@@ -79,12 +79,18 @@ Optional parameters: ...@@ -79,12 +79,18 @@ Optional parameters:
this folder. If the permissions are not correct, use a shell provisioner this folder. If the permissions are not correct, use a shell provisioner
prior to this to configure it properly. prior to this to configure it properly.
* `working_directory` (string) - This is the directory from which the puppet command
will be run. When using hiera with a relative path, this option allows to ensure
that the paths are working properly. If not specified, defaults to the value of
specified `staging_directory` (or its default value if not specified either).
## Execute Command ## Execute Command
By default, Packer uses the following command (broken across multiple lines By default, Packer uses the following command (broken across multiple lines
for readability) to execute Puppet: for readability) to execute Puppet:
```liquid ```liquid
cd {{.WorkingDir}} && \
{{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply \ {{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply \
--verbose \ --verbose \
--modulepath='{{.ModulePath}}' \ --modulepath='{{.ModulePath}}' \
...@@ -98,6 +104,7 @@ This command can be customized using the `execute_command` configuration. ...@@ -98,6 +104,7 @@ This command can be customized using the `execute_command` configuration.
As you can see from the default value above, the value of this configuration As you can see from the default value above, the value of this configuration
can contain various template variables, defined below: can contain various template variables, defined below:
* `WorkingDir` - The path from which Puppet will be executed.
* `FacterVars` - Shell-friendly string of environmental variables used * `FacterVars` - Shell-friendly string of environmental variables used
to set custom facts configured for this provisioner. to set custom facts configured for this provisioner.
* `HieraConfigPath` - The path to a hiera configuration file. * `HieraConfigPath` - The path to a hiera configuration file.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment