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 := state.Get("ui").(packer.Ui)
ui.Say("Cloning virtual machine...") 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 // Determine if we even have an existing virtual harddrive to attach
harddrivePath := "" harddrivePath := ""

View File

@ -23,16 +23,16 @@ func (s *StepCollateArtifacts) Run(_ context.Context, state multistep.StateBag)
if s.SkipExport { if s.SkipExport {
// Get the path to the main build directory from the statebag // Get the path to the main build directory from the statebag
var packerTempDir string var buildDir string
if v, ok := state.GetOk("packerTempDir"); ok { if v, ok := state.GetOk("build_dir"); ok {
packerTempDir = v.(string) buildDir = v.(string)
} }
// If the user has chosen to skip a full export of the VM the only // 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 // artifacts that they are interested in will be the VHDs. The
// called function searches for all disks under the given source // called function searches for all disks under the given source
// directory and moves them to a 'Virtual Hard Disks' folder under // directory and moves them to a 'Virtual Hard Disks' folder under
// the destination directory // the destination directory
err := driver.MoveCreatedVHDsToOutputDir(packerTempDir, s.OutputDir) err := driver.MoveCreatedVHDsToOutputDir(buildDir, s.OutputDir)
if err != nil { if err != nil {
err = fmt.Errorf("Error moving VHDs from build dir to output dir: %s", err) err = fmt.Errorf("Error moving VHDs from build dir to output dir: %s", err)
state.Put("error", 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 // Needs the path to the main output directory and build directory
step.OutputDir = "foopath" step.OutputDir = "foopath"
packerTempDir := "fooBuildPath" buildDir := "fooBuildPath"
state.Put("packerTempDir", packerTempDir) state.Put("build_dir", buildDir)
// Export has been skipped // Export has been skipped
step.SkipExport = true step.SkipExport = true
@ -77,9 +77,9 @@ func TestStepCollateArtifacts_skipExportArtifacts(t *testing.T) {
if !driver.MoveCreatedVHDsToOutputDir_Called { if !driver.MoveCreatedVHDsToOutputDir_Called {
t.Fatal("Should have called MoveCreatedVHDsToOutputDir") 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", 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 { if driver.MoveCreatedVHDsToOutputDir_DstPath != step.OutputDir {
t.Fatalf("Should call with correct dstPath. Got: %s Wanted: %s", 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 return multistep.ActionContinue
} }
// Get the tmp dir used to store the VMs files during the build process // Get the dir used to store the VMs files during the build process
var tmpPath string var buildDir string
if v, ok := state.GetOk("packerTempDir"); ok { if v, ok := state.GetOk("build_dir"); ok {
tmpPath = v.(string) buildDir = v.(string)
} }
ui.Say("Compacting disks...") 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 // 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 // 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. // 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 { if err != nil {
err := fmt.Errorf("Error compacting disks: %s", err) err := fmt.Errorf("Error compacting disks: %s", err)
state.Put("error", err) state.Put("error", err)

View File

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

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -18,7 +19,7 @@ type StepCreateBuildDir struct {
TempPath string TempPath string
// The full path to the build directory. This is the concatenation of // The full path to the build directory. This is the concatenation of
// TempPath plus a directory uniquely named for the build // 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 // 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() 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 { 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) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
s.dirPath = packerTempDir log.Printf("Created build directory: %s", s.buildDir)
state.Put("packerTempDir", packerTempDir)
// Record the build directory location for later steps
state.Put("build_dir", s.buildDir)
return multistep.ActionContinue return multistep.ActionContinue
} }
// Cleanup removes the build directory // Cleanup removes the build directory
func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) { func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) {
if s.dirPath == "" { if s.buildDir == "" {
return return
} }
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting build directory...") ui.Say("Deleting build directory...")
err := os.RemoveAll(s.dirPath) err := os.RemoveAll(s.buildDir)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error deleting build directory: %s", err)) 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 := state.Get("ui").(packer.Ui)
ui.Say("Creating virtual machine...") 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 // Determine if we even have an existing virtual harddrive to attach
harddrivePath := "" harddrivePath := ""