Merge pull request #4014 from mexisme/feature/puppet-bin-dir

privisioner/puppet: Add `puppet_bin_dir` option.
This commit is contained in:
Rickard von Essen 2016-10-22 14:48:45 +02:00 committed by GitHub
commit 3b42d28cce
5 changed files with 53 additions and 3 deletions

View File

@ -55,6 +55,10 @@ type Config struct {
// Packer requires the directory to exist when running puppet. // Packer requires the directory to exist when running puppet.
WorkingDir string `mapstructure:"working_directory"` WorkingDir string `mapstructure:"working_directory"`
// The directory that contains the puppet binary.
// E.g. if it can't be found on the standard path.
PuppetBinDir string `mapstructure:"puppet_bin_dir"`
// If true, packer will ignore all exit-codes from a puppet run // If true, packer will ignore all exit-codes from a puppet run
IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"` IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"`
} }
@ -70,6 +74,7 @@ type ExecuteTemplate struct {
ModulePath string ModulePath string
ManifestFile string ManifestFile string
ManifestDir string ManifestDir string
PuppetBinDir string
Sudo bool Sudo bool
ExtraArguments string ExtraArguments string
} }
@ -92,7 +97,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
if p.config.ExecuteCommand == "" { if p.config.ExecuteCommand == "" {
p.config.ExecuteCommand = "cd {{.WorkingDir}} && " + p.config.ExecuteCommand = "cd {{.WorkingDir}} && " +
"{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" + "{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" +
"puppet apply --verbose --modulepath='{{.ModulePath}}' " + "{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}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}}" +
"--detailed-exitcodes " + "--detailed-exitcodes " +
@ -227,6 +233,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ManifestDir: remoteManifestDir, ManifestDir: remoteManifestDir,
ManifestFile: remoteManifestFile, ManifestFile: remoteManifestFile,
ModulePath: strings.Join(modulePaths, ":"), ModulePath: strings.Join(modulePaths, ":"),
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo, Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir, WorkingDir: p.config.WorkingDir,
ExtraArguments: strings.Join(p.config.ExtraArguments, " "), ExtraArguments: strings.Join(p.config.ExtraArguments, " "),

View File

@ -28,6 +28,31 @@ func TestProvisioner_Impl(t *testing.T) {
} }
} }
func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
config := testConfig()
delete(config, "puppet_bin_dir")
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
// Test with a good one
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
defer os.Remove(tf.Name())
config["puppet_bin_dir"] = tf.Name()
p = new(Provisioner)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvisionerPrepare_hieraConfigPath(t *testing.T) { func TestProvisionerPrepare_hieraConfigPath(t *testing.T) {
config := testConfig() config := testConfig()

View File

@ -45,6 +45,10 @@ type Config struct {
// permissions in this directory. // permissions in this directory.
StagingDir string `mapstructure:"staging_dir"` StagingDir string `mapstructure:"staging_dir"`
// The directory that contains the puppet binary.
// E.g. if it can't be found on the standard path.
PuppetBinDir string `mapstructure:"puppet_bin_dir"`
// If true, packer will ignore all exit-codes from a puppet run // If true, packer will ignore all exit-codes from a puppet run
IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"` IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"`
} }
@ -60,6 +64,7 @@ type ExecuteTemplate struct {
PuppetNode string PuppetNode string
PuppetServer string PuppetServer string
Options string Options string
PuppetBinDir string
Sudo bool Sudo bool
} }
@ -160,6 +165,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
PuppetNode: p.config.PuppetNode, PuppetNode: p.config.PuppetNode,
PuppetServer: p.config.PuppetServer, PuppetServer: p.config.PuppetServer,
Options: p.config.Options, Options: p.config.Options,
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo, Sudo: !p.config.PreventSudo,
} }
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx) command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
@ -221,7 +227,8 @@ func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, ds
func (p *Provisioner) commandTemplate() string { func (p *Provisioner) commandTemplate() string {
return "{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" + return "{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" +
"puppet agent --onetime --no-daemonize " + "{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}puppet agent " +
"--onetime --no-daemonize " +
"{{if ne .PuppetServer \"\"}}--server='{{.PuppetServer}}' {{end}}" + "{{if ne .PuppetServer \"\"}}--server='{{.PuppetServer}}' {{end}}" +
"{{if ne .Options \"\"}}{{.Options}} {{end}}" + "{{if ne .Options \"\"}}{{.Options}} {{end}}" +
"{{if ne .PuppetNode \"\"}}--certname={{.PuppetNode}} {{end}}" + "{{if ne .PuppetNode \"\"}}--certname={{.PuppetNode}} {{end}}" +

View File

@ -86,6 +86,11 @@ Optional parameters:
This option was deprecated in puppet 3.6, and removed in puppet 4.0. If you have This option was deprecated in puppet 3.6, and removed in puppet 4.0. If you have
multiple manifests you should use `manifest_file` instead. multiple manifests you should use `manifest_file` instead.
- `puppet_bin_dir` (string) - The path to the binary for running `puppet apply`.
Usually, this would be found via the `$PATH` or `%PATH%` environment variable,
but some builders (notably, the Docker one) do not run profile-setup scripts,
therefore the Path is usually empty.
- `module_paths` (array of strings) - This is an array of paths to module - `module_paths` (array of strings) - This is an array of paths to module
directories on your local filesystem. These will be uploaded to the directories on your local filesystem. These will be uploaded to the
remote machine. By default, this is empty. remote machine. By default, this is empty.
@ -114,7 +119,8 @@ readability) to execute Puppet:
``` {.liquid} ``` {.liquid}
cd {{.WorkingDir}} && \ cd {{.WorkingDir}} && \
{{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply \ {{.FacterVars}}{{if .Sudo}} sudo -E {{end}} \
{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}{{end}}puppet apply \
--verbose \ --verbose \
--modulepath='{{.ModulePath}}' \ --modulepath='{{.ModulePath}}' \
{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}} \ {{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}} \

View File

@ -75,6 +75,11 @@ listed below:
to create directories and write into this folder. If the permissions are not to create directories and write into this folder. If the permissions are not
correct, use a shell provisioner prior to this to configure it properly. correct, use a shell provisioner prior to this to configure it properly.
- `puppet_bin_dir` (string) - The path to the binary for running `puppet apply`.
Usually, this would be found via the `$PATH` or `%PATH%` environment variable,
but some builders (notably, the Docker one) do not run profile-setup scripts,
therefore the Path is usually empty.
- `execute_command` (string) - This is optional. The command used to execute Puppet. This has - `execute_command` (string) - This is optional. The command used to execute Puppet. This has
various [configuration template various [configuration template
variables](/docs/templates/configuration-templates.html) available. See variables](/docs/templates/configuration-templates.html) available. See