From 7019281ad645d09e58b3d3029af9212fcf52400e Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Sat, 27 Jul 2013 18:12:18 -0700 Subject: [PATCH] provisioner/salt: use cmd.StartWithUi --- provisioner/salt/provisioner.go | 87 ++++++--------------------------- 1 file changed, 15 insertions(+), 72 deletions(-) diff --git a/provisioner/salt/provisioner.go b/provisioner/salt/provisioner.go index 9f641828c..961fe351a 100644 --- a/provisioner/salt/provisioner.go +++ b/provisioner/salt/provisioner.go @@ -5,11 +5,8 @@ package salt import ( "errors" "fmt" - "github.com/mitchellh/iochan" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" - "io" - "log" "os" "path/filepath" "sort" @@ -89,15 +86,18 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { Ui = ui if !p.config.SkipBootstrap { - cmd := fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | sudo sh -s %s", p.config.BootstrapArgs) + cmd := &packer.RemoteCmd{ + Command: fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | sudo sh -s %s", p.config.BootstrapArgs), + } Ui.Say(fmt.Sprintf("Installing Salt with command %s", cmd)) - if err = ExecuteCommand(cmd, comm); err != nil { + if err = cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Unable to install Salt: %d", err) } } Ui.Say(fmt.Sprintf("Creating remote directory: %s", p.config.TempConfigDir)) - if err = ExecuteCommand(fmt.Sprintf("mkdir -p %s", p.config.TempConfigDir), comm); err != nil { + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("mkdir -p %s", p.config.TempConfigDir)} + if err = cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Error creating remote salt state directory: %s", err) } @@ -107,17 +107,20 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } Ui.Say(fmt.Sprintf("Moving %s to /srv/salt", p.config.TempConfigDir)) - if err = ExecuteCommand(fmt.Sprintf("sudo mv %s /srv/salt", p.config.TempConfigDir), comm); err != nil { + cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s /srv/salt", p.config.TempConfigDir)} + if err = cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Unable to move %s to /srv/salt: %d", p.config.TempConfigDir, err) } Ui.Say("Running highstate") - if err = ExecuteCommand("sudo salt-call --local state.highstate -l info", comm); err != nil { + cmd = &packer.RemoteCmd{Command: "sudo salt-call --local state.highstate -l info"} + if err = cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Error executing highstate: %s", err) } Ui.Say("Removing /srv/salt") - if err = ExecuteCommand("sudo rm -r /srv/salt", comm); err != nil { + cmd = &packer.RemoteCmd{Command: "sudo rm -r /srv/salt"} + if err = cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Unable to remove /srv/salt: %d", err) } @@ -133,8 +136,8 @@ func UploadLocalDirectory(localDir string, remoteDir string, comm packer.Communi } if f.IsDir() { // Make remote directory - err = ExecuteCommand(fmt.Sprintf("mkdir -p %s", remotePath), comm) - if err != nil { + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("mkdir -p %s", remotePath)} + if err = cmd.StartWithUi(comm, Ui); err != nil { return err } } else { @@ -146,8 +149,7 @@ func UploadLocalDirectory(localDir string, remoteDir string, comm packer.Communi defer file.Close() Ui.Say(fmt.Sprintf("Uploading file %s: %s", localPath, remotePath)) - err = comm.Upload(remotePath, file) - if err != nil { + if err = comm.Upload(remotePath, file); err != nil { return fmt.Errorf("Error uploading file: %s", err) } } @@ -161,62 +163,3 @@ func UploadLocalDirectory(localDir string, remoteDir string, comm packer.Communi return nil } - -func ExecuteCommand(command string, comm packer.Communicator) (err error) { - // Setup the remote command - stdout_r, stdout_w := io.Pipe() - stderr_r, stderr_w := io.Pipe() - - var cmd packer.RemoteCmd - cmd.Command = command - cmd.Stdout = stdout_w - cmd.Stderr = stderr_w - - log.Printf("Executing command: %s", cmd.Command) - err = comm.Start(&cmd) - if err != nil { - return fmt.Errorf("Failed executing command: %s", err) - } - - exitChan := make(chan int, 1) - stdoutChan := iochan.DelimReader(stdout_r, '\n') - stderrChan := iochan.DelimReader(stderr_r, '\n') - - go func() { - defer stdout_w.Close() - defer stderr_w.Close() - - cmd.Wait() - exitChan <- cmd.ExitStatus - }() - -OutputLoop: - for { - select { - case output := <-stderrChan: - Ui.Message(strings.TrimSpace(output)) - case output := <-stdoutChan: - Ui.Message(strings.TrimSpace(output)) - case exitStatus := <-exitChan: - log.Printf("Salt provisioner exited with status %d", exitStatus) - - if exitStatus != 0 { - return fmt.Errorf("Command exited with non-zero exit status: %d", exitStatus) - } - - break OutputLoop - } - } - - // Make sure we finish off stdout/stderr because we may have gotten - // a message from the exit channel first. - for output := range stdoutChan { - Ui.Message(output) - } - - for output := range stderrChan { - Ui.Message(output) - } - - return nil -}