Skip to content

Commit

Permalink
Add an ohai_hints option to upload hint files
Browse files Browse the repository at this point in the history
This option takes a list of hints that will be uploaded to the new node
before starting the initial Chef run.
  • Loading branch information
Sander van Harmelen committed Jun 25, 2015
1 parent 4c66df0 commit 61517f6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 27 deletions.
13 changes: 13 additions & 0 deletions builtin/provisioners/chef/linux_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chef

import (
"fmt"
"path"
"strings"

"github.com/hashicorp/terraform/communicator"
Expand Down Expand Up @@ -60,6 +61,18 @@ func (p *Provisioner) linuxCreateConfigFiles(
return err
}

if len(p.OhaiHints) > 0 {
// Make sure the hits directory exists
hintsDir := path.Join(linuxConfDir, "ohai/hints")
if err := p.runCommand(o, comm, "mkdir -p "+hintsDir); err != nil {
return err
}

if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
return err
}
}

// When done copying the files restore the rights and make sure root is owner
if p.useSudo {
if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
Expand Down
36 changes: 20 additions & 16 deletions builtin/provisioners/chef/linux_provisioner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chef

import (
"path"
"testing"

"github.com/hashicorp/terraform/communicator"
Expand Down Expand Up @@ -133,6 +134,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
}{
"Sudo": {
Config: testConfig(t, map[string]interface{}{
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
"node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
Expand All @@ -141,16 +143,18 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
}),

Commands: map[string]bool{
"sudo mkdir -p " + linuxConfDir: true,
"sudo chmod 777 " + linuxConfDir: true,
"sudo chmod 755 " + linuxConfDir: true,
"sudo chown -R root.root " + linuxConfDir: true,
"sudo mkdir -p " + linuxConfDir: true,
"sudo chmod 777 " + linuxConfDir: true,
"sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"): true,
"sudo chmod 755 " + linuxConfDir: true,
"sudo chown -R root.root " + linuxConfDir: true,
},

Uploads: map[string]string{
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
"/etc/chef/client.rb": defaultLinuxClientConf,
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
linuxConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
linuxConfDir + "/client.rb": defaultLinuxClientConf,
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
},
},

Expand All @@ -169,9 +173,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

Uploads: map[string]string{
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
"/etc/chef/client.rb": defaultLinuxClientConf,
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
linuxConfDir + "/client.rb": defaultLinuxClientConf,
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
},
},

Expand All @@ -193,9 +197,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

Uploads: map[string]string{
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
"/etc/chef/client.rb": proxyLinuxClientConf,
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
linuxConfDir + "/client.rb": proxyLinuxClientConf,
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
},
},

Expand Down Expand Up @@ -235,9 +239,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

Uploads: map[string]string{
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
"/etc/chef/client.rb": defaultLinuxClientConf,
"/etc/chef/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
linuxConfDir + "/client.rb": defaultLinuxClientConf,
linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
},
},
Expand Down
30 changes: 30 additions & 0 deletions builtin/provisioners/chef/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Provisioner struct {
HTTPSProxy string `mapstructure:"https_proxy"`
NOProxy []string `mapstructure:"no_proxy"`
NodeName string `mapstructure:"node_name"`
OhaiHints []string `mapstructure:"ohai_hints"`
OSType string `mapstructure:"os_type"`
PreventSudo bool `mapstructure:"prevent_sudo"`
RunList []string `mapstructure:"run_list"`
Expand Down Expand Up @@ -212,6 +213,14 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
p.Environment = defaultEnv
}

for i, hint := range p.OhaiHints {
hintPath, err := homedir.Expand(hint)
if err != nil {
return nil, fmt.Errorf("Error expanding the path %s: %v", hint, err)
}
p.OhaiHints[i] = hintPath
}

if p.ValidationKeyPath != "" {
keyPath, err := homedir.Expand(p.ValidationKeyPath)
if err != nil {
Expand Down Expand Up @@ -386,6 +395,27 @@ func (p *Provisioner) deployConfigFiles(
return nil
}

func (p *Provisioner) deployOhaiHints(
o terraform.UIOutput,
comm communicator.Communicator,
hintDir string) error {
for _, hint := range p.OhaiHints {
// Open the hint file
f, err := os.Open(hint)
if err != nil {
return err
}
defer f.Close()

// Copy the hint to the new instance
if err := comm.Upload(path.Join(hintDir, path.Base(hint)), f); err != nil {
return fmt.Errorf("Uploading %s failed: %v", path.Base(hint), err)
}
}

return nil
}

// runCommand is used to run already prepared commands
func (p *Provisioner) runCommand(
o terraform.UIOutput,
Expand Down
2 changes: 1 addition & 1 deletion builtin/provisioners/chef/resource_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestResourceProvider_runChefClient(t *testing.T) {
Config: testConfig(t, map[string]interface{}{
"environment": "production",
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand Down
1 change: 1 addition & 0 deletions builtin/provisioners/chef/test-fixtures/ohaihint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OHAI-HINT-FILE
13 changes: 13 additions & 0 deletions builtin/provisioners/chef/windows_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,18 @@ func (p *Provisioner) windowsCreateConfigFiles(
return err
}

if len(p.OhaiHints) > 0 {
// Make sure the hits directory exists
hintsDir := path.Join(windowsConfDir, "ohai/hints")
cmd := fmt.Sprintf("if not exist %q mkdir %q", hintsDir, hintsDir)
if err := p.runCommand(o, comm, cmd); err != nil {
return err
}

if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
return err
}
}

return p.deployConfigFiles(o, comm, windowsConfDir)
}
24 changes: 15 additions & 9 deletions builtin/provisioners/chef/windows_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chef

import (
"fmt"
"path"
"testing"

"github.com/hashicorp/terraform/communicator"
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
}{
"Default": {
Config: testConfig(t, map[string]interface{}{
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
"node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
Expand All @@ -111,12 +113,16 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {

Commands: map[string]bool{
fmt.Sprintf("if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
fmt.Sprintf("if not exist %q mkdir %q",
path.Join(windowsConfDir, "ohai/hints"),
path.Join(windowsConfDir, "ohai/hints")): true,
},

Uploads: map[string]string{
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
"C:/chef/client.rb": defaultWindowsClientConf,
"C:/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
windowsConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
windowsConfDir + "/client.rb": defaultWindowsClientConf,
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
},
},

Expand All @@ -137,9 +143,9 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
},

Uploads: map[string]string{
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
"C:/chef/client.rb": proxyWindowsClientConf,
"C:/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
windowsConfDir + "/client.rb": proxyWindowsClientConf,
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
},
},

Expand Down Expand Up @@ -178,9 +184,9 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
},

Uploads: map[string]string{
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
"C:/chef/client.rb": defaultWindowsClientConf,
"C:/chef/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
windowsConfDir + "/client.rb": defaultWindowsClientConf,
windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
},
},
Expand Down
5 changes: 4 additions & 1 deletion website/source/docs/provisioners/chef.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ The following arguments are supported:

* `node_name (string)` - (Required) The name of the node to register with the Chef Server.

* `ohai_hints (array)` - (Optional) A list with
[Ohai hints](https://docs.chef.io/ohai.html#hints) to upload to the node.

* `os_type (string)` - (Optional) The OS type of the node. Valid options are: `linux` and
`windows`. If not supplied the connection type will be used to determine the OS type (`ssh`
will asume `linux` and `winrm` will assume `windows`).
will asume `linux` and `winrm` will assume `windows`).

* `prevent_sudo (boolean)` - (Optional) Prevent the use of sudo while installing, configuring
and running the initial Chef Client run. This option is only used with `ssh` type
Expand Down

0 comments on commit 61517f6

Please sign in to comment.