mirror of
https://github.com/hashicorp/packer.git
synced 2026-07-15 03:57:06 -04:00
builder/parallels: Add "StepCompactDisk"
This commit is contained in:
parent
f7b26e44fe
commit
abfe706fc6
3 changed files with 128 additions and 0 deletions
51
builder/parallels/common/step_compact_disk.go
Normal file
51
builder/parallels/common/step_compact_disk.go
Normal file
|
|
@ -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:
|
||||
// <nothing>
|
||||
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) {}
|
||||
73
builder/parallels/common/step_compact_disk_test.go
Normal file
73
builder/parallels/common/step_compact_disk_test.go
Normal file
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue