Merge pull request #8607 from hashicorp/fix_vagrant_crash

Fix a crash in the vagrant driver
This commit is contained in:
Megan Marsh 2020-01-15 13:33:23 -08:00 committed by GitHub
commit ecf0e5794a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package vagrant
import (
"bufio"
"bytes"
"fmt"
"log"
@ -8,7 +9,6 @@ import (
"os/exec"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-version"
)
@ -190,16 +190,47 @@ func (d *Vagrant_2_2_Driver) Version() (string, error) {
return version, nil
}
func (d *Vagrant_2_2_Driver) vagrantCmd(args ...string) (string, string, error) {
var stdout, stderr bytes.Buffer
// Copied and modified from Bufio; this will return data that contains a
// carriage return, not just data that contains a newline.
// This allows us to stream progress output from vagrant that would otherwise
// be smothered. It is a bit noisy, but probably prefereable to suppressing
// the output in a way that looks like Packer has hung.
func ScanLinesInclCR(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0:i], nil
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a CR-terminated line.
return i + 1, data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
func (d *Vagrant_2_2_Driver) vagrantCmd(args ...string) (string, string, error) {
log.Printf("Calling Vagrant CLI: %#v", args)
cmd := exec.Command(d.vagrantBinary, args...)
cmd.Env = append(os.Environ(), fmt.Sprintf("VAGRANT_CWD=%s", d.VagrantCWD))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Start()
stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("error getting err pipe")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("error getting out pipe")
}
err = cmd.Start()
if err != nil {
return "", "", fmt.Errorf("Error starting vagrant command with args: %q",
strings.Join(args, " "))
@ -208,43 +239,24 @@ func (d *Vagrant_2_2_Driver) vagrantCmd(args ...string) (string, string, error)
stdoutString := ""
stderrString := ""
donech := make(chan bool)
scanOut := bufio.NewScanner(stdout)
scanOut.Split(ScanLinesInclCR)
scanErr := bufio.NewScanner(stderr)
scanErr.Split(ScanLinesInclCR)
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 != "" {
for scanErr.Scan() {
line := scanErr.Text()
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)
}
stderrString += line + "\n"
}
}()
err = cmd.Wait()
donech <- true
for scanOut.Scan() {
line := scanOut.Text()
log.Printf("[vagrant driver] stdout: %s", line)
stdoutString += line + "\n"
}
cmd.Wait()
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("Vagrant error: %s", stderrString)

View File

@ -82,7 +82,8 @@ func (s *StepAddBox) Run(ctx context.Context, state multistep.StateBag) multiste
return multistep.ActionContinue
}
ui.Say("Adding box using vagrant box add..")
ui.Say("Adding box using vagrant box add ...")
ui.Message("(this can take some time if we need to download the box)")
addArgs := s.generateAddArgs()
log.Printf("[vagrant] Calling box add with following args %s", strings.Join(addArgs, " "))