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 (
|
2013-07-27 02:35:43 -04:00
|
|
|
"errors"
|
2013-07-22 00:20:39 -04:00
|
|
|
"fmt"
|
2013-08-01 15:11:54 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-07-22 00:20:39 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-27 02:35:43 -04:00
|
|
|
"os"
|
2013-07-22 00:20:39 -04:00
|
|
|
)
|
|
|
|
|
2013-07-27 03:11:04 -04:00
|
|
|
const DefaultTempConfigDir = "/tmp/salt"
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
tpl *packer.ConfigTemplate
|
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 {
|
2013-08-01 02:05:15 -04:00
|
|
|
md, err := common.DecodeConfig(&p.config, raws...)
|
2013-07-22 00:20:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
2013-08-09 17:24:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
if p.config.TempConfigDir == "" {
|
|
|
|
p.config.TempConfigDir = DefaultTempConfigDir
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors
|
2013-08-01 02:05:15 -04:00
|
|
|
errs := common.CheckUnusedConfig(md)
|
2013-07-22 00:20:39 -04:00
|
|
|
|
2013-08-09 17:24:09 -04:00
|
|
|
templates := map[string]*string{
|
2013-08-27 20:37:29 -04:00
|
|
|
"bootstrap_args": &p.config.BootstrapArgs,
|
|
|
|
"minion_config": &p.config.MinionConfig,
|
|
|
|
"local_state_tree": &p.config.LocalStateTree,
|
2013-08-27 19:48:24 -04:00
|
|
|
"local_pillar_roots": &p.config.LocalPillarRoots,
|
2013-08-27 20:37:29 -04:00
|
|
|
"temp_config_dir": &p.config.TempConfigDir,
|
2013-08-09 17:24:09 -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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 11:16:21 -04:00
|
|
|
if p.config.LocalStateTree != "" {
|
|
|
|
if _, err := os.Stat(p.config.LocalStateTree); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("local_state_tree must exist and be accessible"))
|
|
|
|
}
|
2013-08-11 19:17:59 -04:00
|
|
|
}
|
2013-07-27 03:00:01 -04:00
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
if p.config.LocalPillarRoots != "" {
|
|
|
|
if _, err := os.Stat(p.config.LocalPillarRoots); err != nil {
|
2013-08-27 19:37:06 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
2013-08-27 19:48:24 -04:00
|
|
|
errors.New("local_pillar_roots must exist and be accessible"))
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 13:20:02 -04:00
|
|
|
if p.config.MinionConfig != "" {
|
|
|
|
if _, err := os.Stat(p.config.MinionConfig); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("minion_config must exist and be accessible"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
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-05-09 10:08:41 -04:00
|
|
|
Command: fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | %s %s", p.sudo("sh -s"), p.config.BootstrapArgs),
|
2013-07-27 21:12:18 -04:00
|
|
|
}
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message(fmt.Sprintf("Installing Salt with command %s", cmd))
|
2013-07-27 21:12:18 -04:00
|
|
|
if err = cmd.StartWithUi(comm, ui); err != nil {
|
2013-07-26 17:14:41 -04:00
|
|
|
return fmt.Errorf("Unable to install Salt: %d", err)
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message(fmt.Sprintf("Creating remote directory: %s", p.config.TempConfigDir))
|
2013-07-27 21:12:18 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: fmt.Sprintf("mkdir -p %s", p.config.TempConfigDir)}
|
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 creating remote salt state directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-27 18:47:11 -04:00
|
|
|
if p.config.MinionConfig != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading minion config: %s", p.config.MinionConfig))
|
|
|
|
if err = uploadMinionConfig(comm, fmt.Sprintf("%s/minion", p.config.TempConfigDir), p.config.MinionConfig); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading local minion config file to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Moving %s/minion to /etc/salt/minion", p.config.TempConfigDir))
|
2014-05-09 10:08:41 -04:00
|
|
|
cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/minion /etc/salt/minion", p.config.TempConfigDir))}
|
2013-08-27 18:47:11 -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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Unable to move %s/minion to /etc/salt/minion: %d", p.config.TempConfigDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message(fmt.Sprintf("Uploading local state tree: %s", p.config.LocalStateTree))
|
2013-10-09 21:44:12 -04:00
|
|
|
if err = comm.UploadDir(fmt.Sprintf("%s/states", p.config.TempConfigDir),
|
|
|
|
p.config.LocalStateTree, []string{".git"}); err != nil {
|
2013-07-27 02:35:43 -04:00
|
|
|
return fmt.Errorf("Error uploading local state tree to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-10-09 16:06:52 -04:00
|
|
|
ui.Message(fmt.Sprintf("Moving %s/states to /srv/salt", p.config.TempConfigDir))
|
2014-05-09 10:08:41 -04:00
|
|
|
cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/states /srv/salt", p.config.TempConfigDir))}
|
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-08-27 19:37:06 -04:00
|
|
|
return fmt.Errorf("Unable to move %s/states to /srv/salt: %d", p.config.TempConfigDir, err)
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
if p.config.LocalPillarRoots != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading local pillar roots: %s", p.config.LocalPillarRoots))
|
2013-10-09 21:44:12 -04:00
|
|
|
if err = comm.UploadDir(fmt.Sprintf("%s/pillar", p.config.TempConfigDir),
|
|
|
|
p.config.LocalPillarRoots, []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
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Moving %s/pillar to /srv/pillar", p.config.TempConfigDir))
|
2014-05-09 10:08:41 -04:00
|
|
|
cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/pillar /srv/pillar", p.config.TempConfigDir))}
|
2013-08-27 19:37:06 -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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Unable to move %s/pillar to /srv/pillar: %d", p.config.TempConfigDir, err)
|
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message("Running highstate")
|
2014-05-09 10:08:41 -04:00
|
|
|
cmd = &packer.RemoteCmd{Command: p.sudo("salt-call --local state.highstate -l info")}
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-10 13:20:02 -04:00
|
|
|
func uploadMinionConfig(comm packer.Communicator, dst string, src string) error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error opening minion config: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
if err = comm.Upload(dst, f, nil); err != nil {
|
2013-08-10 13:20:02 -04:00
|
|
|
return fmt.Errorf("Error uploading minion config: %s", err)
|
|
|
|
}
|
2013-08-23 18:58:30 -04:00
|
|
|
|
|
|
|
return nil
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|