Merge pull request #2487 from svanharmelen/f-provisioner-chef-hints
provisioner/chef: add an `ohai_hints` option to upload hint files
This commit is contained in:
commit
73f9d2b5e5
|
@ -2,6 +2,7 @@ package chef
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/communicator"
|
"github.com/hashicorp/terraform/communicator"
|
||||||
|
@ -60,6 +61,18 @@ func (p *Provisioner) linuxCreateConfigFiles(
|
||||||
return err
|
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
|
// When done copying the files restore the rights and make sure root is owner
|
||||||
if p.useSudo {
|
if p.useSudo {
|
||||||
if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
|
if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package chef
|
package chef
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/communicator"
|
"github.com/hashicorp/terraform/communicator"
|
||||||
|
@ -133,6 +134,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
"Sudo": {
|
"Sudo": {
|
||||||
Config: testConfig(t, map[string]interface{}{
|
Config: testConfig(t, map[string]interface{}{
|
||||||
|
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
|
||||||
"node_name": "nodename1",
|
"node_name": "nodename1",
|
||||||
"run_list": []interface{}{"cookbook::recipe"},
|
"run_list": []interface{}{"cookbook::recipe"},
|
||||||
"server_url": "https://chef.local",
|
"server_url": "https://chef.local",
|
||||||
|
@ -141,16 +143,18 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Commands: map[string]bool{
|
Commands: map[string]bool{
|
||||||
"sudo mkdir -p " + linuxConfDir: true,
|
"sudo mkdir -p " + linuxConfDir: true,
|
||||||
"sudo chmod 777 " + linuxConfDir: true,
|
"sudo chmod 777 " + linuxConfDir: true,
|
||||||
"sudo chmod 755 " + linuxConfDir: true,
|
"sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"): true,
|
||||||
"sudo chown -R root.root " + linuxConfDir: true,
|
"sudo chmod 755 " + linuxConfDir: true,
|
||||||
|
"sudo chown -R root.root " + linuxConfDir: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"/etc/chef/client.rb": defaultLinuxClientConf,
|
linuxConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
|
||||||
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
||||||
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -169,9 +173,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"/etc/chef/client.rb": defaultLinuxClientConf,
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
||||||
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -193,9 +197,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"/etc/chef/client.rb": proxyLinuxClientConf,
|
linuxConfDir + "/client.rb": proxyLinuxClientConf,
|
||||||
"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -235,9 +239,9 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"/etc/chef/client.rb": defaultLinuxClientConf,
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
||||||
"/etc/chef/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
||||||
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -63,6 +63,7 @@ type Provisioner struct {
|
||||||
HTTPSProxy string `mapstructure:"https_proxy"`
|
HTTPSProxy string `mapstructure:"https_proxy"`
|
||||||
NOProxy []string `mapstructure:"no_proxy"`
|
NOProxy []string `mapstructure:"no_proxy"`
|
||||||
NodeName string `mapstructure:"node_name"`
|
NodeName string `mapstructure:"node_name"`
|
||||||
|
OhaiHints []string `mapstructure:"ohai_hints"`
|
||||||
OSType string `mapstructure:"os_type"`
|
OSType string `mapstructure:"os_type"`
|
||||||
PreventSudo bool `mapstructure:"prevent_sudo"`
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
||||||
RunList []string `mapstructure:"run_list"`
|
RunList []string `mapstructure:"run_list"`
|
||||||
|
@ -212,6 +213,14 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
|
||||||
p.Environment = defaultEnv
|
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 != "" {
|
if p.ValidationKeyPath != "" {
|
||||||
keyPath, err := homedir.Expand(p.ValidationKeyPath)
|
keyPath, err := homedir.Expand(p.ValidationKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -386,6 +395,27 @@ func (p *Provisioner) deployConfigFiles(
|
||||||
return nil
|
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
|
// runCommand is used to run already prepared commands
|
||||||
func (p *Provisioner) runCommand(
|
func (p *Provisioner) runCommand(
|
||||||
o terraform.UIOutput,
|
o terraform.UIOutput,
|
||||||
|
|
|
@ -98,7 +98,7 @@ func TestResourceProvider_runChefClient(t *testing.T) {
|
||||||
Config: testConfig(t, map[string]interface{}{
|
Config: testConfig(t, map[string]interface{}{
|
||||||
"environment": "production",
|
"environment": "production",
|
||||||
"node_name": "nodename1",
|
"node_name": "nodename1",
|
||||||
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
|
"prevent_sudo": true,
|
||||||
"run_list": []interface{}{"cookbook::recipe"},
|
"run_list": []interface{}{"cookbook::recipe"},
|
||||||
"server_url": "https://chef.local",
|
"server_url": "https://chef.local",
|
||||||
"validation_client_name": "validator",
|
"validation_client_name": "validator",
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
OHAI-HINT-FILE
|
|
@ -71,5 +71,18 @@ func (p *Provisioner) windowsCreateConfigFiles(
|
||||||
return err
|
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)
|
return p.deployConfigFiles(o, comm, windowsConfDir)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package chef
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/communicator"
|
"github.com/hashicorp/terraform/communicator"
|
||||||
|
@ -102,6 +103,7 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
"Default": {
|
"Default": {
|
||||||
Config: testConfig(t, map[string]interface{}{
|
Config: testConfig(t, map[string]interface{}{
|
||||||
|
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
|
||||||
"node_name": "nodename1",
|
"node_name": "nodename1",
|
||||||
"run_list": []interface{}{"cookbook::recipe"},
|
"run_list": []interface{}{"cookbook::recipe"},
|
||||||
"server_url": "https://chef.local",
|
"server_url": "https://chef.local",
|
||||||
|
@ -111,12 +113,16 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
|
||||||
|
|
||||||
Commands: map[string]bool{
|
Commands: map[string]bool{
|
||||||
fmt.Sprintf("if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
|
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{
|
Uploads: map[string]string{
|
||||||
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"C:/chef/client.rb": defaultWindowsClientConf,
|
windowsConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
|
||||||
"C:/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
windowsConfDir + "/client.rb": defaultWindowsClientConf,
|
||||||
|
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -137,9 +143,9 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"C:/chef/client.rb": proxyWindowsClientConf,
|
windowsConfDir + "/client.rb": proxyWindowsClientConf,
|
||||||
"C:/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -178,9 +184,9 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Uploads: map[string]string{
|
Uploads: map[string]string{
|
||||||
"C:/chef/validation.pem": "VALIDATOR-PEM-FILE",
|
windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
||||||
"C:/chef/client.rb": defaultWindowsClientConf,
|
windowsConfDir + "/client.rb": defaultWindowsClientConf,
|
||||||
"C:/chef/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
||||||
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -67,9 +67,12 @@ The following arguments are supported:
|
||||||
|
|
||||||
* `node_name (string)` - (Required) The name of the node to register with the Chef Server.
|
* `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
|
* `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`
|
`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
|
* `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
|
and running the initial Chef Client run. This option is only used with `ssh` type
|
||||||
|
|
Loading…
Reference in New Issue