diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index 41d1e261b..9f4dc9b68 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -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")) diff --git a/provisioner/salt-masterless/provisioner_test.go b/provisioner/salt-masterless/provisioner_test.go index 2b7f3a026..2b40887d3 100644 --- a/provisioner/salt-masterless/provisioner_test.go +++ b/provisioner/salt-masterless/provisioner_test.go @@ -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() diff --git a/website/source/docs/provisioners/salt-masterless.html.md b/website/source/docs/provisioners/salt-masterless.html.md index adcc4a517..fb83fa435 100644 --- a/website/source/docs/provisioners/salt-masterless.html.md +++ b/website/source/docs/provisioners/salt-masterless.html.md @@ -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.