From abfe706fc693f80c5bfe450bf4e9d860d69bb1d3 Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Sun, 21 Jun 2015 13:35:52 +0300 Subject: [PATCH] builder/parallels: Add "StepCompactDisk" --- builder/parallels/common/step_compact_disk.go | 51 +++++++++++++ .../common/step_compact_disk_test.go | 73 +++++++++++++++++++ builder/parallels/iso/builder.go | 4 + 3 files changed, 128 insertions(+) create mode 100644 builder/parallels/common/step_compact_disk.go create mode 100644 builder/parallels/common/step_compact_disk_test.go diff --git a/builder/parallels/common/step_compact_disk.go b/builder/parallels/common/step_compact_disk.go new file mode 100644 index 000000000..0ebc7a134 --- /dev/null +++ b/builder/parallels/common/step_compact_disk.go @@ -0,0 +1,51 @@ +package common + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +// This step removes all empty blocks from expanding Parallels virtual disks +// and reduces the result disk size +// +// Uses: +// driver Driver +// vmName string +// ui packer.Ui +// +// Produces: +// +type StepCompactDisk struct { + Skip bool +} + +func (s *StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) + vmName := state.Get("vmName").(string) + ui := state.Get("ui").(packer.Ui) + + if s.Skip { + ui.Say("Skipping disk compaction step...") + return multistep.ActionContinue + } + + ui.Say("Compacting the disk image") + diskPath, err := driver.DiskPath(vmName) + if err != nil { + err := fmt.Errorf("Error detecting virtual disk path: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + if err := driver.CompactDisk(diskPath); err != nil { + state.Put("error", fmt.Errorf("Error compacting disk: %s", err)) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (*StepCompactDisk) Cleanup(multistep.StateBag) {} diff --git a/builder/parallels/common/step_compact_disk_test.go b/builder/parallels/common/step_compact_disk_test.go new file mode 100644 index 000000000..ace932a2d --- /dev/null +++ b/builder/parallels/common/step_compact_disk_test.go @@ -0,0 +1,73 @@ +package common + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/mitchellh/multistep" +) + +func TestStepCompactDisk_impl(t *testing.T) { + var _ multistep.Step = new(StepCompactDisk) +} + +func TestStepCompactDisk(t *testing.T) { + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + tf.Close() + defer os.Remove(tf.Name()) + + state := testState(t) + step := new(StepCompactDisk) + + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + + // Mock results + driver.DiskPathResult = tf.Name() + + // Test the run + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + + // Test the driver + if !driver.CompactDiskCalled { + t.Fatal("should've called") + } + + path, _ := driver.DiskPath("foo") + if path != tf.Name() { + t.Fatal("should call with right path") + } +} + +func TestStepCompactDisk_skip(t *testing.T) { + state := testState(t) + step := new(StepCompactDisk) + step.Skip = true + + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + + // Test the run + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + + // Test the driver + if driver.CompactDiskCalled { + t.Fatal("should not have called") + } +} diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 6b731544d..bfc7c23c4 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -45,6 +45,7 @@ type Config struct { ISOChecksum string `mapstructure:"iso_checksum"` ISOChecksumType string `mapstructure:"iso_checksum_type"` ISOUrls []string `mapstructure:"iso_urls"` + SkipCompaction bool `mapstructure:"skip_compaction"` VMName string `mapstructure:"vm_name"` RawSingleISOUrl string `mapstructure:"iso_url"` @@ -272,6 +273,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Commands: b.config.PrlctlPost, Ctx: b.config.ctx, }, + ¶llelscommon.StepCompactDisk{ + Skip: b.config.SkipCompaction, + }, } // Setup the state bag