use VAGRANT_CWD rather than changing packer run directories

This commit is contained in:
Megan Marsh 2019-02-07 14:35:01 -08:00
parent dc848ea5d7
commit 5057220ad2
4 changed files with 12 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"path/filepath"
"strings"
"time"
@ -167,7 +168,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
// a VirtualBox appliance.
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Create the driver that we'll use to communicate with VirtualBox
driver, err := NewDriver()
VagrantCWD, err := filepath.Abs(b.config.OutputDir)
if err != nil {
return nil, err
}
driver, err := NewDriver(VagrantCWD)
if err != nil {
return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err)
}

View File

@ -42,7 +42,7 @@ type VagrantDriver interface {
Version() (string, error)
}
func NewDriver() (VagrantDriver, error) {
func NewDriver(outputDir string) (VagrantDriver, error) {
// Hardcode path for now while I'm developing. Obviously this path needs
// to be discovered based on OS.
vagrantBinary := "vagrant"
@ -56,6 +56,7 @@ func NewDriver() (VagrantDriver, error) {
driver := &Vagrant_2_2_Driver{
vagrantBinary: vagrantBinary,
VagrantCWD: outputDir,
}
if err := driver.Verify(); err != nil {

View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
@ -16,6 +17,7 @@ const VAGRANT_MIN_VERSION = ">= 2.0.2"
type Vagrant_2_2_Driver struct {
vagrantBinary string
VagrantCWD string
}
// Calls "vagrant init"
@ -69,6 +71,7 @@ func (d *Vagrant_2_2_Driver) Destroy(id string) error {
// Calls "vagrant package"
func (d *Vagrant_2_2_Driver) Package(args []string) error {
args = append(args, "--output", filepath.Join(d.VagrantCWD, "package.box"))
_, _, err := d.vagrantCmd(append([]string{"package"}, args...)...)
return err
}
@ -177,6 +180,7 @@ 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.Run()

View File

@ -89,8 +89,6 @@ func (s *StepCreateVagrantfile) Run(_ context.Context, state multistep.StateBag)
}
log.Printf("Created vagrantfile at %s", vagrantfilePath)
os.Chdir(s.OutputDir)
return multistep.ActionContinue
}