2013-07-22 00:20:39 -04:00
|
|
|
// This package implements a provisioner for Packer that executes a
|
|
|
|
// saltstack highstate within the remote machine
|
2013-08-01 02:01:03 -04:00
|
|
|
package saltmasterless
|
2013-07-22 00:20:39 -04:00
|
|
|
|
|
|
|
import (
|
2015-08-23 07:56:16 -04:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2013-07-22 00:20:39 -04:00
|
|
|
"fmt"
|
2013-07-27 02:35:43 -04:00
|
|
|
"os"
|
2014-08-07 01:07:27 -04:00
|
|
|
"path/filepath"
|
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"
|
2013-07-22 00:20:39 -04:00
|
|
|
)
|
|
|
|
|
2013-07-27 03:11:04 -04:00
|
|
|
const DefaultTempConfigDir = "/tmp/salt"
|
2015-07-27 06:36:39 -04:00
|
|
|
const DefaultStateTreeDir = "/srv/salt"
|
|
|
|
const DefaultPillarRootDir = "/srv/pillar"
|
2013-07-27 03:11:04 -04:00
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
type Config struct {
|
2013-08-09 17:24:09 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-07-26 17:14:41 -04:00
|
|
|
// If true, run the salt-bootstrap script
|
|
|
|
SkipBootstrap bool `mapstructure:"skip_bootstrap"`
|
|
|
|
BootstrapArgs string `mapstructure:"bootstrap_args"`
|
2013-07-27 02:35:43 -04:00
|
|
|
|
2014-05-09 10:08:41 -04:00
|
|
|
DisableSudo bool `mapstructure:"disable_sudo"`
|
|
|
|
|
2013-08-10 13:20:02 -04:00
|
|
|
// Local path to the minion config
|
|
|
|
MinionConfig string `mapstructure:"minion_config"`
|
|
|
|
|
2013-07-27 02:35:43 -04:00
|
|
|
// Local path to the salt state tree
|
|
|
|
LocalStateTree string `mapstructure:"local_state_tree"`
|
2013-07-27 03:00:01 -04:00
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
// Local path to the salt pillar roots
|
|
|
|
LocalPillarRoots string `mapstructure:"local_pillar_roots"`
|
2013-08-27 19:37:06 -04:00
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// Remote path to the salt state tree
|
|
|
|
RemoteStateTree string `mapstructure:"remote_state_tree"`
|
|
|
|
|
|
|
|
// Remote path to the salt pillar roots
|
|
|
|
RemotePillarRoots string `mapstructure:"remote_pillar_roots"`
|
|
|
|
|
2013-07-27 03:00:01 -04:00
|
|
|
// Where files will be copied before moving to the /srv/salt directory
|
|
|
|
TempConfigDir string `mapstructure:"temp_config_dir"`
|
2013-08-09 17:24:09 -04:00
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
// Command line args passed onto salt-call
|
|
|
|
CmdArgs string ""
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2013-08-01 02:05:15 -04:00
|
|
|
config Config
|
2013-07-22 00:20:39 -04: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{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-08-09 17:24:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
if p.config.TempConfigDir == "" {
|
|
|
|
p.config.TempConfigDir = DefaultTempConfigDir
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
var errs *packer.MultiError
|
2013-08-09 17:24:09 -04:00
|
|
|
|
2014-08-06 21:23:15 -04:00
|
|
|
// require a salt state tree
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateDirConfig(p.config.LocalStateTree, "local_state_tree", true)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2013-08-11 19:17:59 -04:00
|
|
|
}
|
2013-07-27 03:00:01 -04:00
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateDirConfig(p.config.LocalPillarRoots, "local_pillar_roots", false)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateFileConfig(p.config.MinionConfig, "minion_config", false)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.MinionConfig != "" && (p.config.RemoteStateTree != "" || p.config.RemotePillarRoots != "") {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("minion_config option overrides remote_state_tree and remote_pillar_roots"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the command line args to pass onto salt
|
|
|
|
var cmd_args bytes.Buffer
|
|
|
|
|
|
|
|
if p.config.MinionConfig == "" {
|
|
|
|
// pass --file-root and --pillar-root if no minion_config is supplied
|
|
|
|
if p.config.RemoteStateTree != "" {
|
|
|
|
cmd_args.WriteString(" --file-root=")
|
|
|
|
cmd_args.WriteString(p.config.RemoteStateTree)
|
|
|
|
} else {
|
|
|
|
cmd_args.WriteString(" --file-root=")
|
|
|
|
cmd_args.WriteString(DefaultStateTreeDir)
|
|
|
|
}
|
|
|
|
if p.config.RemotePillarRoots != "" {
|
|
|
|
cmd_args.WriteString(" --pillar-root=")
|
|
|
|
cmd_args.WriteString(p.config.RemotePillarRoots)
|
|
|
|
} else {
|
|
|
|
cmd_args.WriteString(" --pillar-root=")
|
|
|
|
cmd_args.WriteString(DefaultPillarRootDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.config.CmdArgs = cmd_args.String()
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
var err error
|
2014-08-07 01:07:27 -04:00
|
|
|
var src, dst string
|
2013-07-22 00:20:39 -04:00
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Say("Provisioning with Salt...")
|
2013-07-26 17:14:41 -04:00
|
|
|
if !p.config.SkipBootstrap {
|
2013-07-27 21:12:18 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
2014-12-22 15:08:32 -05:00
|
|
|
Command: fmt.Sprintf("curl -L https://bootstrap.saltstack.com -o /tmp/install_salt.sh"),
|
|
|
|
}
|
|
|
|
ui.Message(fmt.Sprintf("Downloading saltstack bootstrap to /tmp/install_salt.sh"))
|
|
|
|
if err = cmd.StartWithUi(comm, ui); err != nil {
|
2014-12-16 01:46:45 -05:00
|
|
|
return fmt.Errorf("Unable to download Salt: %s", err)
|
2014-12-22 15:08:32 -05:00
|
|
|
}
|
|
|
|
cmd = &packer.RemoteCmd{
|
2015-05-13 09:13:38 -04:00
|
|
|
Command: fmt.Sprintf("%s /tmp/install_salt.sh %s", p.sudo("sh"), p.config.BootstrapArgs),
|
2013-07-27 21:12:18 -04:00
|
|
|
}
|
2014-12-16 01:46:45 -05:00
|
|
|
ui.Message(fmt.Sprintf("Installing Salt with command %s", cmd.Command))
|
2013-07-27 21:12:18 -04:00
|
|
|
if err = cmd.StartWithUi(comm, ui); err != nil {
|
2014-12-16 01:46:45 -05:00
|
|
|
return fmt.Errorf("Unable to install Salt: %s", err)
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
|
2015-07-27 07:00:57 -04:00
|
|
|
ui.Message(fmt.Sprintf("Creating remote temporary directory: %s", p.config.TempConfigDir))
|
2014-08-07 01:07:27 -04:00
|
|
|
if err := p.createDir(ui, comm, p.config.TempConfigDir); err != nil {
|
2015-07-27 07:00:57 -04:00
|
|
|
return fmt.Errorf("Error creating remote temporary directory: %s", err)
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 18:47:11 -04:00
|
|
|
if p.config.MinionConfig != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading minion config: %s", p.config.MinionConfig))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.MinionConfig
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "minion"))
|
|
|
|
if err = p.uploadFile(ui, comm, dst, src); err != nil {
|
2013-08-27 18:47:11 -04:00
|
|
|
return fmt.Errorf("Error uploading local minion config file to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
// move minion config into /etc/salt
|
2015-07-27 06:28:19 -04:00
|
|
|
ui.Message(fmt.Sprintf("Make sure directory %s exists", "/etc/salt"))
|
|
|
|
if err := p.createDir(ui, comm, "/etc/salt"); err != nil {
|
|
|
|
return fmt.Errorf("Error creating remote salt configuration directory: %s", err)
|
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "minion"))
|
|
|
|
dst = "/etc/salt/minion"
|
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2014-12-16 01:46:45 -05:00
|
|
|
return fmt.Errorf("Unable to move %s/minion to /etc/salt/minion: %s", p.config.TempConfigDir, err)
|
2013-08-27 18:47:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message(fmt.Sprintf("Uploading local state tree: %s", p.config.LocalStateTree))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.LocalStateTree
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "states"))
|
|
|
|
if err = p.uploadDir(ui, comm, dst, src, []string{".git"}); err != nil {
|
2013-07-27 02:35:43 -04:00
|
|
|
return fmt.Errorf("Error uploading local state tree to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// move state tree from temporary directory
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "states"))
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.RemoteStateTree != "" {
|
|
|
|
dst = p.config.RemoteStateTree
|
|
|
|
} else {
|
|
|
|
dst = DefaultStateTreeDir
|
|
|
|
}
|
2015-07-27 06:36:39 -04:00
|
|
|
if err = p.removeDir(ui, comm, dst); err != nil {
|
|
|
|
return fmt.Errorf("Unable to clear salt tree: %s", err)
|
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2015-07-27 06:36:39 -04:00
|
|
|
return fmt.Errorf("Unable to move %s/states to %s: %s", p.config.TempConfigDir, dst, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
if p.config.LocalPillarRoots != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading local pillar roots: %s", p.config.LocalPillarRoots))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.LocalPillarRoots
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "pillar"))
|
|
|
|
if err = p.uploadDir(ui, comm, dst, src, []string{".git"}); err != nil {
|
2013-08-27 19:48:24 -04:00
|
|
|
return fmt.Errorf("Error uploading local pillar roots to remote: %s", err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// move pillar root from temporary directory
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "pillar"))
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.RemotePillarRoots != "" {
|
|
|
|
dst = p.config.RemotePillarRoots
|
|
|
|
} else {
|
|
|
|
dst = DefaultPillarRootDir
|
|
|
|
}
|
2015-07-27 06:36:39 -04:00
|
|
|
if err = p.removeDir(ui, comm, dst); err != nil {
|
2015-08-21 11:08:48 -04:00
|
|
|
return fmt.Errorf("Unable to clear pillar root: %s", err)
|
2015-07-27 06:36:39 -04:00
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2015-07-27 06:36:39 -04:00
|
|
|
return fmt.Errorf("Unable to move %s/pillar to %s: %s", p.config.TempConfigDir, dst, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message("Running highstate")
|
2015-08-23 07:56:16 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("salt-call --local state.highstate -l info --retcode-passthrough %s", p.config.CmdArgs))}
|
2013-08-09 20:35:57 -04:00
|
|
|
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
|
2013-08-12 12:19:11 -04:00
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
|
2013-07-27 02:35:43 -04:00
|
|
|
return fmt.Errorf("Error executing highstate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:23:36 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-05-09 10:08:41 -04:00
|
|
|
// Prepends sudo to supplied command if config says to
|
|
|
|
func (p *Provisioner) sudo(cmd string) string {
|
|
|
|
if p.config.DisableSudo {
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
return "sudo " + cmd
|
|
|
|
}
|
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
func validateDirConfig(path string, name string, required bool) error {
|
|
|
|
if required == true && path == "" {
|
|
|
|
return fmt.Errorf("%s cannot be empty", name)
|
|
|
|
} else if required == false && path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: path '%s' is invalid: %s", name, path, err)
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: path '%s' must point to a directory", name, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFileConfig(path string, name string, required bool) error {
|
|
|
|
if required == true && path == "" {
|
|
|
|
return fmt.Errorf("%s cannot be empty", name)
|
|
|
|
} else if required == false && path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: path '%s' is invalid: %s", name, path, err)
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: path '%s' must point to a file", name, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst, src string) error {
|
2013-08-10 13:20:02 -04:00
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return fmt.Errorf("Error opening: %s", err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
if err = comm.Upload(dst, f, nil); err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return fmt.Errorf("Error uploading %s: %s", src, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) moveFile(ui packer.Ui, comm packer.Communicator, dst, src string) error {
|
|
|
|
ui.Message(fmt.Sprintf("Moving %s to %s", src, dst))
|
2014-11-05 11:58:25 -05:00
|
|
|
cmd := &packer.RemoteCmd{Command: fmt.Sprintf(p.sudo("mv %s %s"), src, dst)}
|
2014-08-07 01:07:27 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
|
2015-07-27 07:00:57 -04:00
|
|
|
return fmt.Errorf("Unable to move %s to %s: %s", src, dst, err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-23 18:58:30 -04:00
|
|
|
|
2014-08-07 01:07:27 -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),
|
|
|
|
}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
2013-08-23 18:58:30 -04:00
|
|
|
return nil
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
func (p *Provisioner) removeDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
|
|
|
ui.Message(fmt.Sprintf("Removing directory: %s", dir))
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("rm -rf '%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
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
func (p *Provisioner) uploadDir(ui packer.Ui, comm packer.Communicator, dst, src string, ignore []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, ignore)
|
|
|
|
}
|