Merge pull request #2910 from Rican7/feature/puppet-masterless-provision-execute-options-config

Feature - Adding a new `options` config parameter to Puppet (masterless) provisionining
This commit is contained in:
Chris Bednarski 2015-11-04 11:27:07 -08:00
commit 08f275c9de
3 changed files with 98 additions and 1 deletions

View File

@ -22,6 +22,9 @@ type Config struct {
// The command used to execute Puppet.
ExecuteCommand string `mapstructure:"execute_command"`
// Additional arguments to pass when executing Puppet
ExtraArguments []string `mapstructure:"extra_arguments"`
// Additional facts to set when executing Puppet
Facter map[string]string
@ -62,6 +65,7 @@ type ExecuteTemplate struct {
ManifestFile string
ManifestDir string
Sudo bool
ExtraArguments string
}
func (p *Provisioner) Prepare(raws ...interface{}) error {
@ -86,6 +90,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
"{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" +
"{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" +
"--detailed-exitcodes " +
"{{if ne .ExtraArguments \"\"}}{{.ExtraArguments}} {{end}}" +
"{{.ManifestFile}}"
}
@ -218,6 +223,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ModulePath: strings.Join(modulePaths, ":"),
Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
ExtraArguments: strings.Join(p.config.ExtraArguments, " "),
}
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {

View File

@ -1,10 +1,12 @@
package puppetmasterless
import (
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/mitchellh/packer/packer"
)
func testConfig() map[string]interface{} {
@ -177,3 +179,86 @@ func TestProvisionerPrepare_facterFacts(t *testing.T) {
t.Fatalf("err: Default facts are not set in the Puppet provisioner!")
}
}
func TestProvisionerPrepare_extraArguments(t *testing.T) {
config := testConfig()
// Test with missing parameter
delete(config, "extra_arguments")
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
// Test with malformed value
config["extra_arguments"] = "{{}}"
p = new(Provisioner)
err = p.Prepare(config)
if err == nil {
t.Fatal("should be an error")
}
// Test with valid values
config["extra_arguments"] = []string{
"arg",
}
p = new(Provisioner)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvisionerProvision_extraArguments(t *testing.T) {
config := testConfig()
ui := &packer.MachineReadableUi{
Writer: ioutil.Discard,
}
comm := new(packer.MockCommunicator)
extraArguments := []string{
"--some-arg=yup",
"--some-other-arg",
}
config["extra_arguments"] = extraArguments
// Test with valid values
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Provision(ui, comm)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedArgs := strings.Join(extraArguments, " ")
if !strings.Contains(comm.StartCmd.Command, expectedArgs) {
t.Fatalf("Command %q doesn't contain the expected arguments %q", comm.StartCmd.Command, expectedArgs)
}
// Test with missing parameter
delete(config, "extra_arguments")
p = new(Provisioner)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Provision(ui, comm)
if err != nil {
t.Fatalf("err: %s", err)
}
// Check the expected `extra_arguments` position for an empty value
splitCommand := strings.Split(comm.StartCmd.Command, " ")
if "" == splitCommand[len(splitCommand)-2] {
t.Fatalf("Command %q contains an extra-space which may cause arg parsing issues", comm.StartCmd.Command)
}
}

View File

@ -59,6 +59,12 @@ Optional parameters:
variables](/docs/templates/configuration-templates.html) available. See
below for more information.
- `extra_arguments` (array of strings) - This is an array of additional options to
pass to the puppet command when executing puppet. This allows for
customization of the `execute_command` without having to completely replace
or include it's contents, making forward-compatible customizations much
easier.
- `facter` (object of key/value strings) - Additional
[facts](http://puppetlabs.com/puppet/related-projects/facter) to make
available when Puppet is running.