Make Vagrant driver code pass through logs as it receives them rather than buffering and returning them all at the end of the call.

This commit is contained in:
Megan Marsh 2019-10-24 10:06:46 -07:00
parent ce248c9df4
commit a7be0bed30
2 changed files with 44 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"os/exec"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-version"
)
@ -197,17 +198,53 @@ func (d *Vagrant_2_2_Driver) vagrantCmd(args ...string) (string, string, error)
cmd.Env = append(os.Environ(), fmt.Sprintf("VAGRANT_CWD=%s", d.VagrantCWD))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stdoutString := strings.TrimSpace(stdout.String())
stderrString := strings.TrimSpace(stderr.String())
cmd.Start()
stdoutString := ""
stderrString := ""
donech := make(chan bool)
go func() {
for {
select {
case <-donech:
line := stdout.String()
if line != "" {
log.Printf("[vagrant driver] stdout: %s", line)
}
stdoutString += line
line = stderr.String()
if line != "" {
log.Printf("[vagrant driver] stderr: %s", line)
}
stderrString += line
default:
line, _ := stdout.ReadString('\n')
if line != "" {
log.Printf("[vagrant driver] stdout: %s", line)
stdoutString += line
}
line, _ = stderr.ReadString('\n')
if line != "" {
log.Printf("[vagrant driver] stderr: %s", line)
stderrString += line
}
time.Sleep(500)
}
}
}()
err := cmd.Wait()
donech <- true
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("Vagrant error: %s", stderrString)
}
log.Printf("[vagrant driver] stdout: %s", stdoutString)
log.Printf("[vagrant driver] stderr: %s", stderrString)
return stdoutString, stderrString, err
}

View File

@ -33,7 +33,7 @@ func (s *StepUp) Run(ctx context.Context, state multistep.StateBag) multistep.St
driver := state.Get("driver").(VagrantDriver)
ui := state.Get("ui").(packer.Ui)
ui.Say("Calling Vagrant Up...")
ui.Say("Calling Vagrant Up (this can take some time)...")
_, _, err := driver.Up(s.generateArgs())