2013-08-01 20:05:23 -04:00
|
|
|
// This package implements a provisioner for Packer that executes
|
2013-09-08 02:08:56 -04:00
|
|
|
// Puppet on the remote machine, configured to apply a local manifest
|
|
|
|
// versus connecting to a Puppet master.
|
2013-09-08 01:27:25 -04:00
|
|
|
package puppetmasterless
|
2013-08-01 20:05:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-08 02:08:56 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-08-01 20:05:23 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
tpl *packer.ConfigTemplate
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// The command used to execute Puppet.
|
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2013-08-01 20:05:23 -04:00
|
|
|
|
|
|
|
// An array of local paths of modules to upload.
|
2013-09-08 02:08:56 -04:00
|
|
|
ModulePaths []string `mapstructure:"module_paths"`
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// The main manifest file to apply to kick off the entire thing.
|
2013-08-01 20:05:23 -04:00
|
|
|
ManifestFile string `mapstructure:"manifest_file"`
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// If true, `sudo` will NOT be used to execute Puppet.
|
2013-08-01 20:05:23 -04:00
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
2013-09-08 02:08:56 -04:00
|
|
|
|
|
|
|
// The directory where files will be uploaded. Packer requires write
|
|
|
|
// permissions in this directory.
|
|
|
|
StagingDir string `mapstructure:"staging_directory"`
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2013-09-08 02:08:56 -04:00
|
|
|
config Config
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
type ExecuteTemplate struct {
|
|
|
|
ModulePath string
|
|
|
|
ManifestFile string
|
|
|
|
Sudo bool
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2013-09-08 02:08:56 -04:00
|
|
|
md, err := common.DecodeConfig(&p.config, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
2013-09-08 02:08:56 -04:00
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
|
|
|
// Accumulate any errors
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Set some defaults
|
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
p.config.ExecuteCommand = "{{if .Sudo}}sudo {{end}}puppet apply --verbose --modulepath='{{.ModulePath}}' {{.ManifestFile}}"
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if p.config.StagingDir == "" {
|
|
|
|
p.config.StagingDir = "/tmp/packer-puppet-masterless"
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Templates
|
|
|
|
templates := map[string]*string{
|
|
|
|
"staging_dir": &p.config.StagingDir,
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = p.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceTemplates := map[string][]string{
|
|
|
|
"module_paths": p.config.ModulePaths,
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
for n, slice := range sliceTemplates {
|
|
|
|
for i, elem := range slice {
|
|
|
|
var err error
|
|
|
|
slice[i], err = p.config.tpl.Process(elem, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
validates := map[string]*string{
|
|
|
|
"execute_command": &p.config.ExecuteCommand,
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
for n, ptr := range validates {
|
|
|
|
if err := p.config.tpl.Validate(*ptr); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing %s: %s", n, err))
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Validation
|
|
|
|
if p.config.ManifestFile == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("A manifest_file must be specified."))
|
|
|
|
} else {
|
|
|
|
info, err := os.Stat(p.config.ManifestFile)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("manifest_file is invalid: %s", err))
|
|
|
|
} else if info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("manifest_file must point to a file"))
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:31:28 -04:00
|
|
|
for i, path := range p.config.ModulePaths {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("module_path[%d] is invalid: %s", i, err))
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("module_path[%d] must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-09-08 02:08:56 -04:00
|
|
|
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)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload all modules
|
2013-09-08 02:08:56 -04:00
|
|
|
modulePaths := make([]string, 0, len(p.config.ModulePaths))
|
|
|
|
for i, path := range p.config.ModulePaths {
|
|
|
|
ui.Message(fmt.Sprintf("Upload local modules from: %s", path))
|
|
|
|
targetPath := fmt.Sprintf("%s/module-%d", p.config.StagingDir, i)
|
|
|
|
if err := p.uploadDirectory(ui, comm, targetPath, path); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading modules: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
modulePaths = append(modulePaths, targetPath)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload manifests
|
2013-09-08 02:08:56 -04:00
|
|
|
ui.Message("Uploading manifests...")
|
|
|
|
remoteManifestFile, err := p.uploadManifests(ui, comm)
|
2013-08-01 20:05:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading manifests: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute Puppet
|
2013-09-08 02:08:56 -04:00
|
|
|
command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteTemplate{
|
|
|
|
ManifestFile: remoteManifestFile,
|
|
|
|
ModulePath: strings.Join(modulePaths, ":"),
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
ui.Message("Running Puppet...")
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
func (p *Provisioner) uploadManifests(ui packer.Ui, comm packer.Communicator) (string, error) {
|
|
|
|
// Create the remote manifests directory...
|
|
|
|
ui.Message("Uploading manifests...")
|
|
|
|
remoteManifestsPath := fmt.Sprintf("%s/manifests", p.config.StagingDir)
|
|
|
|
if err := p.createDir(ui, comm, remoteManifestsPath); err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating manifests directory: %s", err)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Upload the main manifest
|
|
|
|
f, err := os.Open(p.config.ManifestFile)
|
2013-08-01 20:05:23 -04:00
|
|
|
if err != nil {
|
2013-09-08 02:08:56 -04:00
|
|
|
return "", err
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
2013-09-08 02:08:56 -04:00
|
|
|
defer f.Close()
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
manifestFilename := filepath.Base(p.config.ManifestFile)
|
|
|
|
remoteManifestFile := fmt.Sprintf("%s/%s", remoteManifestsPath, manifestFilename)
|
|
|
|
if err := comm.Upload(remoteManifestFile, f); err != nil {
|
|
|
|
return "", err
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
return remoteManifestFile, nil
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("mkdir -p '%s'", dir),
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
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
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Make sure there is a trailing "/" so that the directory isn't
|
|
|
|
// created on the other side.
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
src = src + "/"
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
return comm.UploadDir(dst, src, nil)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|