-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2731 from legal90/prl-compact-disk
Parallels: Add "CompactDisk" build step
- Loading branch information
Showing
8 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters