From ed5bebfa8c8bf310ef27491405e26613d029f4af Mon Sep 17 00:00:00 2001 From: DanHam Date: Sun, 8 Jul 2018 23:27:37 +0100 Subject: [PATCH] Change variable/statebag key name to better convey purpose --- builder/hyperv/common/step_clone_vm.go | 2 +- .../hyperv/common/step_collate_artifacts.go | 8 ++++---- .../common/step_collate_artifacts_test.go | 8 ++++---- builder/hyperv/common/step_compact_disk.go | 10 +++++----- .../hyperv/common/step_compact_disk_test.go | 14 +++++++------- builder/hyperv/common/step_create_build_dir.go | 18 +++++++++++------- builder/hyperv/common/step_create_vm.go | 5 ++++- 7 files changed, 36 insertions(+), 29 deletions(-) diff --git a/builder/hyperv/common/step_clone_vm.go b/builder/hyperv/common/step_clone_vm.go index ac28f9cea..6a3d434a8 100644 --- a/builder/hyperv/common/step_clone_vm.go +++ b/builder/hyperv/common/step_clone_vm.go @@ -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 := "" diff --git a/builder/hyperv/common/step_collate_artifacts.go b/builder/hyperv/common/step_collate_artifacts.go index d98b42b38..2e40a532f 100644 --- a/builder/hyperv/common/step_collate_artifacts.go +++ b/builder/hyperv/common/step_collate_artifacts.go @@ -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) diff --git a/builder/hyperv/common/step_collate_artifacts_test.go b/builder/hyperv/common/step_collate_artifacts_test.go index 4892e76e8..d88ca59bc 100644 --- a/builder/hyperv/common/step_collate_artifacts_test.go +++ b/builder/hyperv/common/step_collate_artifacts_test.go @@ -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", diff --git a/builder/hyperv/common/step_compact_disk.go b/builder/hyperv/common/step_compact_disk.go index 628703c7a..ebaa9978e 100644 --- a/builder/hyperv/common/step_compact_disk.go +++ b/builder/hyperv/common/step_compact_disk.go @@ -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) diff --git a/builder/hyperv/common/step_compact_disk_test.go b/builder/hyperv/common/step_compact_disk_test.go index 75f3aa19d..f8943d25f 100644 --- a/builder/hyperv/common/step_compact_disk_test.go +++ b/builder/hyperv/common/step_compact_disk_test.go @@ -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) diff --git a/builder/hyperv/common/step_create_build_dir.go b/builder/hyperv/common/step_create_build_dir.go index 8641ddca4..405a3407c 100644 --- a/builder/hyperv/common/step_create_build_dir.go +++ b/builder/hyperv/common/step_create_build_dir.go @@ -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)) } diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index 9987c50b4..1c655becf 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -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 := ""