Change variable/statebag key name to better convey purpose

This commit is contained in:
DanHam 2018-07-08 23:27:37 +01:00
parent 00276f2f64
commit ed5bebfa8c
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
7 changed files with 36 additions and 29 deletions

View File

@ -37,7 +37,7 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep
ui := state.Get("ui").(packer.Ui)
ui.Say("Cloning virtual machine...")
path := state.Get("packerTempDir").(string)
path := state.Get("build_dir").(string)
// Determine if we even have an existing virtual harddrive to attach
harddrivePath := ""

View File

@ -23,16 +23,16 @@ func (s *StepCollateArtifacts) Run(_ context.Context, state multistep.StateBag)
if s.SkipExport {
// Get the path to the main build directory from the statebag
var packerTempDir string
if v, ok := state.GetOk("packerTempDir"); ok {
packerTempDir = v.(string)
var buildDir string
if v, ok := state.GetOk("build_dir"); ok {
buildDir = v.(string)
}
// If the user has chosen to skip a full export of the VM the only
// artifacts that they are interested in will be the VHDs. The
// called function searches for all disks under the given source
// directory and moves them to a 'Virtual Hard Disks' folder under
// the destination directory
err := driver.MoveCreatedVHDsToOutputDir(packerTempDir, s.OutputDir)
err := driver.MoveCreatedVHDsToOutputDir(buildDir, s.OutputDir)
if err != nil {
err = fmt.Errorf("Error moving VHDs from build dir to output dir: %s", err)
state.Put("error", err)

View File

@ -58,8 +58,8 @@ func TestStepCollateArtifacts_skipExportArtifacts(t *testing.T) {
// Needs the path to the main output directory and build directory
step.OutputDir = "foopath"
packerTempDir := "fooBuildPath"
state.Put("packerTempDir", packerTempDir)
buildDir := "fooBuildPath"
state.Put("build_dir", buildDir)
// Export has been skipped
step.SkipExport = true
@ -77,9 +77,9 @@ func TestStepCollateArtifacts_skipExportArtifacts(t *testing.T) {
if !driver.MoveCreatedVHDsToOutputDir_Called {
t.Fatal("Should have called MoveCreatedVHDsToOutputDir")
}
if driver.MoveCreatedVHDsToOutputDir_SrcPath != packerTempDir {
if driver.MoveCreatedVHDsToOutputDir_SrcPath != buildDir {
t.Fatalf("Should call with correct srcPath. Got: %s Wanted: %s",
driver.MoveCreatedVHDsToOutputDir_SrcPath, packerTempDir)
driver.MoveCreatedVHDsToOutputDir_SrcPath, buildDir)
}
if driver.MoveCreatedVHDsToOutputDir_DstPath != step.OutputDir {
t.Fatalf("Should call with correct dstPath. Got: %s Wanted: %s",

View File

@ -22,10 +22,10 @@ func (s *StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multi
return multistep.ActionContinue
}
// Get the tmp dir used to store the VMs files during the build process
var tmpPath string
if v, ok := state.GetOk("packerTempDir"); ok {
tmpPath = v.(string)
// Get the dir used to store the VMs files during the build process
var buildDir string
if v, ok := state.GetOk("build_dir"); ok {
buildDir = v.(string)
}
ui.Say("Compacting disks...")
@ -33,7 +33,7 @@ func (s *StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multi
// path and runs the compacting process on each of them. If no disks
// are found under the supplied path this is treated as a 'soft' error
// and a warning message is printed. All other errors halt the build.
result, err := driver.CompactDisks(tmpPath)
result, err := driver.CompactDisks(buildDir)
if err != nil {
err := fmt.Errorf("Error compacting disks: %s", err)
state.Put("error", err)

View File

@ -15,9 +15,9 @@ func TestStepCompactDisk(t *testing.T) {
state := testState(t)
step := new(StepCompactDisk)
// Set up the path to the tmp directory used to store VM files
tmpPath := "foopath"
state.Put("packerTempDir", tmpPath)
// Set up the path to the build directory
buildDir := "foopath"
state.Put("build_dir", buildDir)
driver := state.Get("driver").(*DriverMock)
@ -33,8 +33,8 @@ func TestStepCompactDisk(t *testing.T) {
if !driver.CompactDisks_Called {
t.Fatal("Should have called CompactDisks")
}
if driver.CompactDisks_Path != tmpPath {
t.Fatalf("Should call with correct path. Got: %s Wanted: %s", driver.CompactDisks_Path, tmpPath)
if driver.CompactDisks_Path != buildDir {
t.Fatalf("Should call with correct path. Got: %s Wanted: %s", driver.CompactDisks_Path, buildDir)
}
}
@ -43,8 +43,8 @@ func TestStepCompactDisk_skip(t *testing.T) {
step := new(StepCompactDisk)
step.SkipCompaction = true
// Set up the path to the tmp directory used to store VM files
state.Put("packerTempDir", "foopath")
// Set up the path to the build directory
state.Put("build_dir", "foopath")
driver := state.Get("driver").(*DriverMock)

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/hashicorp/packer/helper/multistep"
@ -18,7 +19,7 @@ type StepCreateBuildDir struct {
TempPath string
// The full path to the build directory. This is the concatenation of
// TempPath plus a directory uniquely named for the build
dirPath string
buildDir string
}
// Creates the main directory used to house the VMs files and folders
@ -32,30 +33,33 @@ func (s *StepCreateBuildDir) Run(_ context.Context, state multistep.StateBag) mu
s.TempPath = os.TempDir()
}
packerTempDir, err := ioutil.TempDir(s.TempPath, "packerhv")
var err error
s.buildDir, err = ioutil.TempDir(s.TempPath, "packerhv")
if err != nil {
err := fmt.Errorf("Error creating build directory: %s", err)
err = fmt.Errorf("Error creating build directory: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.dirPath = packerTempDir
state.Put("packerTempDir", packerTempDir)
log.Printf("Created build directory: %s", s.buildDir)
// Record the build directory location for later steps
state.Put("build_dir", s.buildDir)
return multistep.ActionContinue
}
// Cleanup removes the build directory
func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) {
if s.dirPath == "" {
if s.buildDir == "" {
return
}
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting build directory...")
err := os.RemoveAll(s.dirPath)
err := os.RemoveAll(s.buildDir)
if err != nil {
ui.Error(fmt.Sprintf("Error deleting build directory: %s", err))
}

View File

@ -40,7 +40,10 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating virtual machine...")
path := state.Get("packerTempDir").(string)
var path string
if v, ok := state.GetOk("build_dir"); ok {
path = v.(string)
}
// Determine if we even have an existing virtual harddrive to attach
harddrivePath := ""