From a100e9393b9ae963f502c7dcab578cc0dabe4652 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sun, 11 Jan 2015 10:25:48 +1100 Subject: [PATCH 1/3] Add support for custom working directory for puppet --- provisioner/puppet-masterless/provisioner.go | 14 +++++++++++++- .../provisioners/puppet-masterless.html.markdown | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/provisioner/puppet-masterless/provisioner.go b/provisioner/puppet-masterless/provisioner.go index 307ecce38..d96397e41 100644 --- a/provisioner/puppet-masterless/provisioner.go +++ b/provisioner/puppet-masterless/provisioner.go @@ -41,6 +41,10 @@ type Config struct { // The directory where files will be uploaded. Packer requires write // permissions in this 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 { @@ -48,6 +52,7 @@ type Provisioner struct { } type ExecuteTemplate struct { + WorkingDir string FacterVars string HieraConfigPath string ModulePath string @@ -73,7 +78,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // Set some defaults 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}}' " + "{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" + "{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" + @@ -85,12 +91,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.config.StagingDir = "/tmp/packer-puppet-masterless" } + if p.config.WorkingDir == "" { + p.config.StagingDir = p.config.StagingDir + } + // Templates templates := map[string]*string{ "hiera_config_path": &p.config.HieraConfigPath, "manifest_file": &p.config.ManifestFile, "manifest_dir": &p.config.ManifestDir, "staging_dir": &p.config.StagingDir, + "working_dir": &p.config.WorkingDir, } for n, ptr := range templates { @@ -256,6 +267,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { // Execute Puppet command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteTemplate{ + WorkingDir: p.config.WorkingDir, FacterVars: strings.Join(facterVars, " "), HieraConfigPath: remoteHieraConfigPath, ManifestDir: remoteManifestDir, diff --git a/website/source/docs/provisioners/puppet-masterless.html.markdown b/website/source/docs/provisioners/puppet-masterless.html.markdown index 953dca6ea..4ed566bc6 100644 --- a/website/source/docs/provisioners/puppet-masterless.html.markdown +++ b/website/source/docs/provisioners/puppet-masterless.html.markdown @@ -79,12 +79,18 @@ Optional parameters: this folder. If the permissions are not correct, use a shell provisioner 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 he 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 By default, Packer uses the following command (broken across multiple lines for readability) to execute Puppet: ```liquid +cd {{.WorkingDir}} && \ {{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply \ --verbose \ --modulepath='{{.ModulePath}}' \ @@ -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 can contain various template variables, defined below: +* `WorkingDir` - The path from which Puppet will be executed. * `FacterVars` - Shell-friendly string of environmental variables used to set custom facts configured for this provisioner. * `HieraConfigPath` - The path to a hiera configuration file. From b7fccec91c930d67b11b18d5cf6b3afece228f3a Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sun, 11 Jan 2015 10:29:01 +1100 Subject: [PATCH 2/3] Set the working dir to staging dir --- provisioner/puppet-masterless/provisioner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisioner/puppet-masterless/provisioner.go b/provisioner/puppet-masterless/provisioner.go index d96397e41..6bbfc5c1f 100644 --- a/provisioner/puppet-masterless/provisioner.go +++ b/provisioner/puppet-masterless/provisioner.go @@ -92,7 +92,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.WorkingDir == "" { - p.config.StagingDir = p.config.StagingDir + p.config.WorkingDir = p.config.StagingDir } // Templates From 34e34d1f1825c6a1657ddedf4f9304f81df0c52c Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sat, 28 Feb 2015 23:02:49 +1100 Subject: [PATCH 3/3] Fix typo --- .../source/docs/provisioners/puppet-masterless.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/source/docs/provisioners/puppet-masterless.html.markdown b/website/source/docs/provisioners/puppet-masterless.html.markdown index 4ed566bc6..bd239ebe0 100644 --- a/website/source/docs/provisioners/puppet-masterless.html.markdown +++ b/website/source/docs/provisioners/puppet-masterless.html.markdown @@ -81,7 +81,7 @@ Optional parameters: * `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 he paths are working properly. If not specified, defaults to the value of + 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