From 912fc9c2af5b5e7b587322e1dc5556641ee21cab Mon Sep 17 00:00:00 2001 From: Ian Ellis Date: Sun, 4 Jun 2017 00:11:42 +0100 Subject: [PATCH 1/4] Added grains_file config item to copy file to /etc/salt/grains --- provisioner/salt-masterless/provisioner.go | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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")) From 31f66b6fe945db655d8a185433d91fc5173bd4a8 Mon Sep 17 00:00:00 2001 From: Ian Ellis Date: Sun, 4 Jun 2017 13:03:50 +0100 Subject: [PATCH 2/4] Cloned GrainsFile test from MinionConfig test --- .../salt-masterless/provisioner_test.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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() From d156c41e3ac1a3200add061729557567073b321a Mon Sep 17 00:00:00 2001 From: Ian Ellis Date: Mon, 5 Jun 2017 20:56:42 +0100 Subject: [PATCH 3/4] Added docs for grains_file config item of salt-masterless provisioner --- website/source/docs/provisioners/salt-masterless.html.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/source/docs/provisioners/salt-masterless.html.md b/website/source/docs/provisioners/salt-masterless.html.md index adcc4a517..e8d391617 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. From c4ec705791c23addfbe0f2c4d32aaae4c0ba1ffa Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 6 Jun 2017 12:35:48 -0700 Subject: [PATCH 4/4] fix markdown --- website/source/docs/provisioners/salt-masterless.html.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/source/docs/provisioners/salt-masterless.html.md b/website/source/docs/provisioners/salt-masterless.html.md index e8d391617..fb83fa435 100644 --- a/website/source/docs/provisioners/salt-masterless.html.md +++ b/website/source/docs/provisioners/salt-masterless.html.md @@ -66,8 +66,8 @@ 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 +- `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