provisioner/puppet-masterless: support custom facts

This commit is contained in:
Mitchell Hashimoto 2013-09-08 22:59:43 -07:00
parent 1bad19f58b
commit 4c537105ef
2 changed files with 40 additions and 2 deletions

View File

@ -19,6 +19,9 @@ type Config struct {
// The command used to execute Puppet. // The command used to execute Puppet.
ExecuteCommand string `mapstructure:"execute_command"` ExecuteCommand string `mapstructure:"execute_command"`
// Additional facts to set when executing Puppet
Facter map[string]string
// An array of local paths of modules to upload. // An array of local paths of modules to upload.
ModulePaths []string `mapstructure:"module_paths"` ModulePaths []string `mapstructure:"module_paths"`
@ -38,6 +41,7 @@ type Provisioner struct {
} }
type ExecuteTemplate struct { type ExecuteTemplate struct {
FacterVars string
ModulePath string ModulePath string
ManifestFile string ManifestFile string
Sudo bool Sudo bool
@ -60,7 +64,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
// Set some defaults // Set some defaults
if p.config.ExecuteCommand == "" { if p.config.ExecuteCommand == "" {
p.config.ExecuteCommand = "{{if .Sudo}}sudo {{end}}puppet apply --verbose --modulepath='{{.ModulePath}}' {{.ManifestFile}}" p.config.ExecuteCommand = "{{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply --verbose --modulepath='{{.ModulePath}}' {{.ManifestFile}}"
} }
if p.config.StagingDir == "" { if p.config.StagingDir == "" {
@ -107,6 +111,27 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
} }
} }
newFacts := make(map[string]string)
for k, v := range p.config.Facter {
k, err := p.config.tpl.Process(k, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing facter key %s: %s", k, err))
continue
}
v, err := p.config.tpl.Process(v, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing facter value '%s': %s", v, err))
continue
}
newFacts[k] = v
}
p.config.Facter = newFacts
// Validation // Validation
if p.config.ManifestFile == "" { if p.config.ManifestFile == "" {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
@ -165,8 +190,15 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return fmt.Errorf("Error uploading manifests: %s", err) return fmt.Errorf("Error uploading manifests: %s", err)
} }
// Compile the facter variables
facterVars := make([]string, 0, len(p.config.Facter))
for k, v := range p.config.Facter {
facterVars = append(facterVars, fmt.Sprintf("FACTER_%s='%s'", k, v))
}
// Execute Puppet // Execute Puppet
command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteTemplate{ command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteTemplate{
FacterVars: strings.Join(facterVars, " "),
ManifestFile: remoteManifestFile, ManifestFile: remoteManifestFile,
ModulePath: strings.Join(modulePaths, ":"), ModulePath: strings.Join(modulePaths, ":"),
Sudo: !p.config.PreventSudo, Sudo: !p.config.PreventSudo,

View File

@ -50,6 +50,10 @@ Optional parameters:
various [configuration template variables](/docs/templates/configuration-templates.html) various [configuration template variables](/docs/templates/configuration-templates.html)
available. See below for more information. available. See below for more information.
* `facter` (object, string keys and values) - Additonal
[facts](http://puppetlabs.com/puppet/related-projects/facter) to make
available when Puppet is running.
* `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 remote directories on your local filesystem. These will be uploaded to the remote
machine. By default, this is empty. machine. By default, this is empty.
@ -71,7 +75,7 @@ By default, Packer uses the following command (broken across multiple lines
for readability) to execute Puppet: for readability) to execute Puppet:
``` ```
{{if .Sudo}sudo {{end}}puppet apply \ {{.FacterVars}}{{if .Sudo} sudo -E {{end}}puppet apply \
--verbose \ --verbose \
--modulepath='{{.ModulePath}}' \ --modulepath='{{.ModulePath}}' \
{{.ManifestFile}} {{.ManifestFile}}
@ -81,6 +85,8 @@ 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:
* `FacterVars` - Shell-friendly string of environmental variables used
to set custom facts configured for this provisioner.
* `ManifestFile` - The path on the remote machine to the manifest file * `ManifestFile` - The path on the remote machine to the manifest file
for Puppet to use. for Puppet to use.
* `ModulePath` - The paths to the module directories. * `ModulePath` - The paths to the module directories.