2014-01-07 11:32:57 -05:00
|
|
|
// This package implements a provisioner for Packer that executes
|
|
|
|
// Puppet on the remote machine connecting to a Puppet master.
|
|
|
|
package puppetserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2014-01-07 11:32:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2016-06-13 09:12:28 -04:00
|
|
|
// The command used to execute Puppet.
|
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2014-01-07 11:32:57 -05:00
|
|
|
|
|
|
|
// Additional facts to set when executing Puppet
|
|
|
|
Facter map[string]string
|
|
|
|
|
|
|
|
// A path to the client certificate
|
|
|
|
ClientCertPath string `mapstructure:"client_cert_path"`
|
|
|
|
|
|
|
|
// A path to a directory containing the client private keys
|
|
|
|
ClientPrivateKeyPath string `mapstructure:"client_private_key_path"`
|
|
|
|
|
2014-01-08 07:03:03 -05:00
|
|
|
// The hostname of the Puppet node.
|
|
|
|
PuppetNode string `mapstructure:"puppet_node"`
|
|
|
|
|
|
|
|
// The hostname of the Puppet server.
|
2014-01-07 11:32:57 -05:00
|
|
|
PuppetServer string `mapstructure:"puppet_server"`
|
|
|
|
|
2014-01-08 07:03:03 -05:00
|
|
|
// Additional options to be passed to `puppet agent`.
|
|
|
|
Options string `mapstructure:"options"`
|
|
|
|
|
2014-01-09 02:51:48 -05:00
|
|
|
// If true, `sudo` will NOT be used to execute Puppet.
|
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
|
|
|
|
2014-01-08 07:03:03 -05:00
|
|
|
// The directory where files will be uploaded. Packer requires write
|
|
|
|
// permissions in this directory.
|
2014-01-07 11:32:57 -05:00
|
|
|
StagingDir string `mapstructure:"staging_dir"`
|
2015-05-14 18:05:44 -04:00
|
|
|
|
|
|
|
// If true, packer will ignore all exit-codes from a puppet run
|
|
|
|
IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"`
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteTemplate struct {
|
|
|
|
FacterVars string
|
|
|
|
ClientCertPath string
|
|
|
|
ClientPrivateKeyPath string
|
|
|
|
PuppetNode string
|
|
|
|
PuppetServer string
|
|
|
|
Options string
|
2014-01-09 02:51:48 -05:00
|
|
|
Sudo bool
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2015-05-27 17:50:20 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:50:20 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
2016-06-13 09:12:28 -04:00
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
2015-05-27 17:50:20 -04:00
|
|
|
},
|
|
|
|
}, raws...)
|
2014-01-07 11:32:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2016-06-13 09:12:28 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
p.config.ExecuteCommand = p.commandTemplate()
|
|
|
|
}
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
if p.config.StagingDir == "" {
|
|
|
|
p.config.StagingDir = "/tmp/packer-puppet-server"
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
var errs *packer.MultiError
|
2014-01-07 11:32:57 -05:00
|
|
|
if p.config.ClientCertPath != "" {
|
|
|
|
info, err := os.Stat(p.config.ClientCertPath)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("client_cert_dir is invalid: %s", err))
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("client_cert_dir must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ClientPrivateKeyPath != "" {
|
|
|
|
info, err := os.Stat(p.config.ClientPrivateKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("client_private_key_dir is invalid: %s", err))
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("client_private_key_dir must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
ui.Say("Provisioning with Puppet...")
|
|
|
|
ui.Message("Creating Puppet staging directory...")
|
|
|
|
if err := p.createDir(ui, comm, p.config.StagingDir); err != nil {
|
|
|
|
return fmt.Errorf("Error creating staging directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload client cert dir if set
|
|
|
|
remoteClientCertPath := ""
|
|
|
|
if p.config.ClientCertPath != "" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading client cert from: %s", p.config.ClientCertPath))
|
|
|
|
remoteClientCertPath = fmt.Sprintf("%s/certs", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteClientCertPath, p.config.ClientCertPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading client cert: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload client cert dir if set
|
|
|
|
remoteClientPrivateKeyPath := ""
|
|
|
|
if p.config.ClientPrivateKeyPath != "" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading client private keys from: %s", p.config.ClientPrivateKeyPath))
|
|
|
|
remoteClientPrivateKeyPath = fmt.Sprintf("%s/private_keys", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteClientPrivateKeyPath, p.config.ClientPrivateKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading client private keys: %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
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
2014-01-07 11:32:57 -05:00
|
|
|
FacterVars: strings.Join(facterVars, " "),
|
|
|
|
ClientCertPath: remoteClientCertPath,
|
|
|
|
ClientPrivateKeyPath: remoteClientPrivateKeyPath,
|
|
|
|
PuppetNode: p.config.PuppetNode,
|
|
|
|
PuppetServer: p.config.PuppetServer,
|
|
|
|
Options: p.config.Options,
|
2014-01-09 02:51:48 -05:00
|
|
|
Sudo: !p.config.PreventSudo,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
2016-06-13 09:12:28 -04:00
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
2014-01-07 11:32:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Running Puppet: %s", command))
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:05:44 -04:00
|
|
|
if cmd.ExitStatus != 0 && cmd.ExitStatus != 2 && !p.config.IgnoreExitCodes {
|
2014-01-07 11:32:57 -05:00
|
|
|
return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
// Just hard quit. It isn't a big deal if what we're doing keeps
|
|
|
|
// running on the other side.
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("mkdir -p '%s'", dir),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, dst string, src string) error {
|
|
|
|
if err := p.createDir(ui, comm, dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is a trailing "/" so that the directory isn't
|
|
|
|
// created on the other side.
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
src = src + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return comm.UploadDir(dst, src, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) commandTemplate() string {
|
2014-01-09 02:51:48 -05:00
|
|
|
return "{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" +
|
2014-01-07 11:32:57 -05:00
|
|
|
"puppet agent --onetime --no-daemonize " +
|
|
|
|
"{{if ne .PuppetServer \"\"}}--server='{{.PuppetServer}}' {{end}}" +
|
|
|
|
"{{if ne .Options \"\"}}{{.Options}} {{end}}" +
|
|
|
|
"{{if ne .PuppetNode \"\"}}--certname={{.PuppetNode}} {{end}}" +
|
|
|
|
"{{if ne .ClientCertPath \"\"}}--certdir='{{.ClientCertPath}}' {{end}}" +
|
|
|
|
"{{if ne .ClientPrivateKeyPath \"\"}}--privatekeydir='{{.ClientPrivateKeyPath}}' {{end}}" +
|
|
|
|
"--detailed-exitcodes"
|
|
|
|
}
|