Merge pull request #4961 from magicalbob/grains

Grains
This commit is contained in:
Matthew Hooker 2017-06-06 12:37:18 -07:00 committed by GitHub
commit bc16c13b50
3 changed files with 55 additions and 0 deletions

View File

@ -34,6 +34,9 @@ type Config struct {
// Local path to the minion config
MinionConfig string `mapstructure:"minion_config"`
// Local path to the minion grains
GrainsFile string `mapstructure:"grains_file"`
// Local path to the salt state tree
LocalStateTree string `mapstructure:"local_state_tree"`
@ -107,6 +110,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
errors.New("remote_state_tree and remote_pillar_roots only apply when minion_config is not used"))
}
err = validateFileConfig(p.config.GrainsFile, "grains_file", false)
if err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
// build the command line args to pass onto salt
var cmd_args bytes.Buffer
@ -208,6 +216,26 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
}
if p.config.GrainsFile != "" {
ui.Message(fmt.Sprintf("Uploading grains file: %s", p.config.GrainsFile))
src = p.config.GrainsFile
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "grains"))
if err = p.uploadFile(ui, comm, dst, src); err != nil {
return fmt.Errorf("Error uploading local grains file to remote: %s", err)
}
// move grains file into /etc/salt
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)
}
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "grains"))
dst = "/etc/salt/grains"
if err = p.moveFile(ui, comm, dst, src); err != nil {
return fmt.Errorf("Unable to move %s/grains to /etc/salt/grains: %s", p.config.TempConfigDir, err)
}
}
ui.Message(fmt.Sprintf("Uploading local state tree: %s", p.config.LocalStateTree))
src = p.config.LocalStateTree
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "states"))

View File

@ -119,6 +119,29 @@ func TestProvisionerPrepare_MinionConfig_RemotePillarRoots(t *testing.T) {
}
}
func TestProvisionerPrepare_GrainsFile(t *testing.T) {
var p Provisioner
config := testConfig()
config["grains_file"] = "/i/dont/exist/i/think"
err := p.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
tf, err := ioutil.TempFile("", "grains")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
defer os.Remove(tf.Name())
config["grains_file"] = tf.Name()
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvisionerPrepare_LocalStateTree(t *testing.T) {
var p Provisioner
config := testConfig()

View File

@ -66,6 +66,10 @@ Optional:
uploaded to the `/etc/salt` on the remote. This option overrides the
`remote_state_tree` or `remote_pillar_roots` options.
- `grains_file` (string) - The path to your local [grains file](
https://docs.saltstack.com/en/latest/topics/grains). This will be
uploaded to `/etc/salt/grains` on the remote.
- `skip_bootstrap` (boolean) - By default the salt provisioner runs [salt
bootstrap](https://github.com/saltstack/salt-bootstrap) to install salt. Set
this to true to skip this step.