From 14787fd4cc5332107c5757523cf2229fe9fa8465 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Jun 2015 14:29:12 -0700 Subject: [PATCH 1/2] provisioner/chef-client: run cleanup on node [GH-1295] --- provisioner/chef-client/provisioner.go | 42 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index 527b375c1..20b594562 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -9,7 +9,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" "strings" @@ -336,15 +335,9 @@ func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir stri func (p *Provisioner) cleanNode(ui packer.Ui, comm packer.Communicator, node string) error { ui.Say("Cleaning up chef node...") - app := fmt.Sprintf("knife node delete %s -y", node) - - cmd := exec.Command("sh", "-c", app) - out, err := cmd.Output() - - ui.Message(fmt.Sprintf("%s", out)) - - if err != nil { - return err + args := []string{"node", "delete", node} + if err := p.knifeExec(ui, comm, node, args); err != nil { + return fmt.Errorf("Failed to cleanup node: %s", err) } return nil @@ -352,16 +345,35 @@ func (p *Provisioner) cleanNode(ui packer.Ui, comm packer.Communicator, node str func (p *Provisioner) cleanClient(ui packer.Ui, comm packer.Communicator, node string) error { ui.Say("Cleaning up chef client...") - app := fmt.Sprintf("knife client delete %s -y", node) + args := []string{"client", "delete", node} + if err := p.knifeExec(ui, comm, node, args); err != nil { + return fmt.Errorf("Failed to cleanup client: %s", err) + } - cmd := exec.Command("sh", "-c", app) - out, err := cmd.Output() + return nil +} - ui.Message(fmt.Sprintf("%s", out)) +func (p *Provisioner) knifeExec(ui packer.Ui, comm packer.Communicator, node string, args []string) error { + flags := []string{ + "-y", + "-s", fmt.Sprintf("'%s'", p.config.ServerUrl), + "-k", fmt.Sprintf("'%s'", p.config.ClientKey), + "-u", fmt.Sprintf("'%s'", node), + } - if err != nil { + cmdText := fmt.Sprintf( + "knife %s %s", strings.Join(args, " "), strings.Join(flags, " ")) + if !p.config.PreventSudo { + cmdText = "sudo " + cmdText + } + + cmd := &packer.RemoteCmd{Command: cmdText} + if err := cmd.StartWithUi(comm, ui); err != nil { return err } + if cmd.ExitStatus != 0 { + return fmt.Errorf("Non-zero exit status. See output above for more info.") + } return nil } From 711dfc9d0ad5b8b143290a4e71b5f7a5fa5af8c9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Jun 2015 14:35:54 -0700 Subject: [PATCH 2/2] provisioner/chef: show command in output --- provisioner/chef-client/provisioner.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index 20b594562..2d42d361d 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -372,7 +372,10 @@ func (p *Provisioner) knifeExec(ui packer.Ui, comm packer.Communicator, node str return err } if cmd.ExitStatus != 0 { - return fmt.Errorf("Non-zero exit status. See output above for more info.") + return fmt.Errorf( + "Non-zero exit status. See output above for more info.\n\n"+ + "Command: %s", + cmdText) } return nil