Commit 684df67c authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

provisioner/shell: Adhere to new communicator API

parent 75074ca9
...@@ -4,10 +4,13 @@ package shell ...@@ -4,10 +4,13 @@ package shell
import ( import (
"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" "log"
"os" "os"
"time"
) )
const DefaultRemotePath = "/tmp/script.sh" const DefaultRemotePath = "/tmp/script.sh"
...@@ -52,34 +55,45 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -52,34 +55,45 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
return return
} }
command := fmt.Sprintf("chmod +x %s && %s", p.config.RemotePath, p.config.RemotePath) // Setup the remote command
log.Printf("Executing command: %s", command) stdout_r, stdout_w := io.Pipe()
cmd, err := comm.Start(command) stderr_r, stderr_w := io.Pipe()
var cmd packer.RemoteCmd
cmd.Command = fmt.Sprintf("chmod +x %s && %s", p.config.RemotePath, p.config.RemotePath)
cmd.Stdout = stdout_w
cmd.Stderr = stderr_w
log.Printf("Executing command: %s", cmd.Command)
err = comm.Start(&cmd)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed executing command: %s", err)) ui.Error(fmt.Sprintf("Failed executing command: %s", err))
return return
} }
exit := cmd.ExitChan() exitChan := make(chan int, 1)
stderr := cmd.StderrChan() stdoutChan := iochan.DelimReader(stdout_r, '\n')
stdout := cmd.StdoutChan() stderrChan := iochan.DelimReader(stderr_r, '\n')
go func() {
defer stdout_w.Close()
defer stderr_w.Close()
for !cmd.Exited {
time.Sleep(50 * time.Millisecond)
}
exitChan <- cmd.ExitStatus
}()
OutputLoop: OutputLoop:
for { for {
select { select {
case output, ok := <-stderr: case output := <-stderrChan:
if !ok { ui.Say(output)
stderr = nil case output := <-stdoutChan:
} else { ui.Say(output)
ui.Say(output) case exitStatus := <-exitChan:
}
case output, ok := <-stdout:
if !ok {
stdout = nil
} else {
ui.Say(output)
}
case exitStatus := <-exit:
log.Printf("shell provisioner exited with status %d", exitStatus) log.Printf("shell provisioner exited with status %d", exitStatus)
break OutputLoop break OutputLoop
} }
...@@ -87,15 +101,11 @@ OutputLoop: ...@@ -87,15 +101,11 @@ OutputLoop:
// Make sure we finish off stdout/stderr because we may have gotten // Make sure we finish off stdout/stderr because we may have gotten
// a message from the exit channel first. // a message from the exit channel first.
if stdout != nil { for output := range stdoutChan {
for output := range stdout { ui.Say(output)
ui.Say(output)
}
} }
if stderr != nil { for output := range stderrChan {
for output := range stderr { ui.Say(output)
ui.Say(output)
}
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment