Skip to content

Commit

Permalink
Tweaking a few minor things according to the feedback on GH
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander van Harmelen committed May 12, 2015
1 parent a8daced commit 11314a3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
7 changes: 4 additions & 3 deletions builtin/provisioners/chef/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Provisioner struct {
installChefClient func(terraform.UIOutput, communicator.Communicator) error
createConfigFiles func(terraform.UIOutput, communicator.Communicator) error
runChefClient func(terraform.UIOutput, communicator.Communicator) error
useSudo bool
}

// ResourceProvisioner represents a generic chef provisioner
Expand All @@ -93,15 +94,15 @@ func (r *ResourceProvisioner) Apply(
// Set some values based on the targeted OS
switch s.Ephemeral.ConnInfo["type"] {
case "ssh", "": // The default connection type is ssh, so if the type is empty use ssh
p.PreventSudo = p.PreventSudo || s.Ephemeral.ConnInfo["user"] == "root"
p.installChefClient = p.sshInstallChefClient
p.createConfigFiles = p.sshCreateConfigFiles
p.runChefClient = p.runChefClientFunc(linuxConfDir)
p.useSudo = !p.PreventSudo && s.Ephemeral.ConnInfo["user"] != "root"
case "winrm":
p.PreventSudo = true
p.installChefClient = p.winrmInstallChefClient
p.createConfigFiles = p.winrmCreateConfigFiles
p.runChefClient = p.runChefClientFunc(windowsConfDir)
p.useSudo = false
default:
return fmt.Errorf("Unsupported connection type: %s", s.Ephemeral.ConnInfo["type"])
}
Expand Down Expand Up @@ -363,7 +364,7 @@ func (p *Provisioner) runCommand(
var err error

// Unless prevented, prefix the command with sudo
if !p.PreventSudo {
if p.useSudo {
command = "sudo " + command
}

Expand Down
1 change: 1 addition & 0 deletions builtin/provisioners/chef/resource_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestResourceProvider_runChefClient(t *testing.T) {
}

p.runChefClient = p.runChefClientFunc(tc.ConfDir)
p.useSudo = !p.PreventSudo

err = p.runChefClient(o, c)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions builtin/provisioners/chef/ssh_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *Provisioner) sshCreateConfigFiles(
}

// Make sure we have enough rights to upload the files if using sudo
if !p.PreventSudo {
if p.useSudo {
if err := p.runCommand(o, comm, "chmod 777 "+linuxConfDir); err != nil {
return err
}
Expand All @@ -61,7 +61,7 @@ func (p *Provisioner) sshCreateConfigFiles(
}

// When done copying the files restore the rights and make sure root is owner
if !p.PreventSudo {
if p.useSudo {
if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions builtin/provisioners/chef/ssh_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func TestResourceProvider_sshInstallChefClient(t *testing.T) {
t.Fatalf("Error: %v", err)
}

p.useSudo = !p.PreventSudo

err = p.sshInstallChefClient(o, c)
if err != nil {
t.Fatalf("Test %q failed: %v", k, err)
Expand Down Expand Up @@ -254,6 +256,8 @@ func TestResourceProvider_sshCreateConfigFiles(t *testing.T) {
t.Fatalf("Error: %v", err)
}

p.useSudo = !p.PreventSudo

err = p.sshCreateConfigFiles(o, c)
if err != nil {
t.Fatalf("Test %q failed: %v", k, err)
Expand Down
10 changes: 4 additions & 6 deletions builtin/provisioners/chef/winrm_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestResourceProvider_winrmInstallChefClient(t *testing.T) {
"Default": {
Config: testConfig(t, map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand All @@ -38,7 +37,6 @@ func TestResourceProvider_winrmInstallChefClient(t *testing.T) {
"http_proxy": "http://proxy.local",
"no_proxy": []interface{}{"http://local.local", "http://local.org"},
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand All @@ -57,7 +55,6 @@ func TestResourceProvider_winrmInstallChefClient(t *testing.T) {
"Version": {
Config: testConfig(t, map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand Down Expand Up @@ -88,6 +85,8 @@ func TestResourceProvider_winrmInstallChefClient(t *testing.T) {
t.Fatalf("Error: %v", err)
}

p.useSudo = false

err = p.winrmInstallChefClient(o, c)
if err != nil {
t.Fatalf("Test %q failed: %v", k, err)
Expand All @@ -104,7 +103,6 @@ func TestResourceProvider_winrmCreateConfigFiles(t *testing.T) {
"Default": {
Config: testConfig(t, map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand All @@ -128,7 +126,6 @@ func TestResourceProvider_winrmCreateConfigFiles(t *testing.T) {
"https_proxy": "https://proxy.local",
"no_proxy": []interface{}{"http://local.local", "https://local.local"},
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand Down Expand Up @@ -170,7 +167,6 @@ func TestResourceProvider_winrmCreateConfigFiles(t *testing.T) {
},
},
"node_name": "nodename1",
"prevent_sudo": true, // Needs to be set for ALL WinRM tests!
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"validation_client_name": "validator",
Expand Down Expand Up @@ -203,6 +199,8 @@ func TestResourceProvider_winrmCreateConfigFiles(t *testing.T) {
t.Fatalf("Error: %v", err)
}

p.useSudo = false

err = p.winrmCreateConfigFiles(o, c)
if err != nil {
t.Fatalf("Test %q failed: %v", k, err)
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/provisioners/chef.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ resource "aws_instance" "web" {

The following arguments are supported:

* `attributes (hash)` - (Optional) A hash with initial node attributes for the new node.
* `attributes (map)` - (Optional) A map with initial node attributes for the new node.
See example.

* `environment (string)` - (Optional) The Chef environment the new node will be joining
Expand Down

0 comments on commit 11314a3

Please sign in to comment.