provisioner/salt: use cmd.StartWithUi

This commit is contained in:
Rafael Garcia 2013-07-27 18:12:18 -07:00
parent 5feadedba2
commit 7019281ad6
1 changed files with 15 additions and 72 deletions

View File

@ -5,11 +5,8 @@ package salt
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/iochan"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io"
"log"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -89,15 +86,18 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
Ui = ui Ui = ui
if !p.config.SkipBootstrap { 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)) 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) return fmt.Errorf("Unable to install Salt: %d", err)
} }
} }
Ui.Say(fmt.Sprintf("Creating remote directory: %s", p.config.TempConfigDir)) 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) 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)) 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) return fmt.Errorf("Unable to move %s to /srv/salt: %d", p.config.TempConfigDir, err)
} }
Ui.Say("Running highstate") 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) return fmt.Errorf("Error executing highstate: %s", err)
} }
Ui.Say("Removing /srv/salt") 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) 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() { if f.IsDir() {
// Make remote directory // Make remote directory
err = ExecuteCommand(fmt.Sprintf("mkdir -p %s", remotePath), comm) cmd := &packer.RemoteCmd{Command: fmt.Sprintf("mkdir -p %s", remotePath)}
if err != nil { if err = cmd.StartWithUi(comm, Ui); err != nil {
return err return err
} }
} else { } else {
@ -146,8 +149,7 @@ func UploadLocalDirectory(localDir string, remoteDir string, comm packer.Communi
defer file.Close() defer file.Close()
Ui.Say(fmt.Sprintf("Uploading file %s: %s", localPath, remotePath)) Ui.Say(fmt.Sprintf("Uploading file %s: %s", localPath, remotePath))
err = comm.Upload(remotePath, file) if err = comm.Upload(remotePath, file); err != nil {
if err != nil {
return fmt.Errorf("Error uploading file: %s", err) return fmt.Errorf("Error uploading file: %s", err)
} }
} }
@ -161,62 +163,3 @@ func UploadLocalDirectory(localDir string, remoteDir string, comm packer.Communi
return nil 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
}