provisioners/local-exec: output the output

This commit is contained in:
Mitchell Hashimoto 2014-10-05 23:05:49 -07:00
parent 28acb7baa8
commit 0808236c6e

View File

@ -2,12 +2,14 @@ package localexec
import ( import (
"fmt" "fmt"
"io"
"os/exec" "os/exec"
"runtime" "runtime"
"github.com/armon/circbuf" "github.com/armon/circbuf"
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/go-linereader"
) )
const ( const (
@ -44,17 +46,35 @@ func (p *ResourceProvisioner) Apply(
flag = "-c" flag = "-c"
} }
// Setup the reader that will read the lines from the command
pr, pw := io.Pipe()
copyDoneCh := make(chan struct{})
go p.copyOutput(o, pr, copyDoneCh)
// Setup the command // Setup the command
cmd := exec.Command(shell, flag, command) cmd := exec.Command(shell, flag, command)
output, _ := circbuf.NewBuffer(maxBufSize) output, _ := circbuf.NewBuffer(maxBufSize)
cmd.Stderr = output cmd.Stderr = io.MultiWriter(output, pw)
cmd.Stdout = output cmd.Stdout = io.MultiWriter(output, pw)
// Output what we're about to run
o.Output(fmt.Sprintf(
"Executing: %s %s \"%s\"",
shell, flag, command))
// Run the command to completion // Run the command to completion
if err := cmd.Run(); err != nil { err := cmd.Run()
// Close the write-end of the pipe so that the goroutine mirroring output
// ends properly.
pw.Close()
<-copyDoneCh
if err != nil {
return fmt.Errorf("Error running command '%s': %v. Output: %s", return fmt.Errorf("Error running command '%s': %v. Output: %s",
command, err, output.Bytes()) command, err, output.Bytes())
} }
return nil return nil
} }
@ -64,3 +84,12 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, [
} }
return validator.Validate(c) return validator.Validate(c)
} }
func (p *ResourceProvisioner) copyOutput(
o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
defer close(doneCh)
lr := linereader.New(r)
for line := range lr.Ch {
o.Output(line)
}
}