Skip to content

Commit

Permalink
Merge pull request #364 from hashicorp/f-local-exec-output
Browse files Browse the repository at this point in the history
provisioners/local-exec: mirror output to UI
  • Loading branch information
mitchellh committed Oct 6, 2014
2 parents 28acb7b + 0808236 commit b3fa381
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions builtin/provisioners/local-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package localexec

import (
"fmt"
"io"
"os/exec"
"runtime"

"github.com/armon/circbuf"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/go-linereader"
)

const (
Expand Down Expand Up @@ -44,17 +46,35 @@ func (p *ResourceProvisioner) Apply(
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
cmd := exec.Command(shell, flag, command)
output, _ := circbuf.NewBuffer(maxBufSize)
cmd.Stderr = output
cmd.Stdout = output
cmd.Stderr = io.MultiWriter(output, pw)
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
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",
command, err, output.Bytes())
}

return nil
}

Expand All @@ -64,3 +84,12 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, [
}
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)
}
}

0 comments on commit b3fa381

Please sign in to comment.