Skip to content

Commit

Permalink
Merge pull request hashicorp#6274 from DanHam/fix-compaction-esx
Browse files Browse the repository at this point in the history
Revert commits that enabled reporting of disk compaction results for VMware builders
  • Loading branch information
SwampDragons authored May 18, 2018
2 parents 2764058 + 73eb9a6 commit cbbbf55
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 53 deletions.
31 changes: 0 additions & 31 deletions builder/vmware/common/step_compact_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"log"
"math"
"os"

"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
Expand Down Expand Up @@ -38,39 +36,10 @@ func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multis
ui.Say("Compacting all attached virtual disks...")
for i, diskFullPath := range diskFullPaths {
ui.Message(fmt.Sprintf("Compacting virtual disk %d", i+1))
// Get the file size of the virtual disk prior to compaction
fi, err := os.Stat(diskFullPath)
if err != nil {
state.Put("error", fmt.Errorf("Error getting virtual disk file info pre compaction: %s", err))
return multistep.ActionHalt
}
diskFileSizeStart := fi.Size()
// Defragment and compact the disk
if err := driver.CompactDisk(diskFullPath); err != nil {
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
return multistep.ActionHalt
}
// Get the file size of the virtual disk post compaction
fi, err = os.Stat(diskFullPath)
if err != nil {
state.Put("error", fmt.Errorf("Error getting virtual disk file info post compaction: %s", err))
return multistep.ActionHalt
}
diskFileSizeEnd := fi.Size()
// Report compaction results
log.Printf("Before compaction the disk file size was: %d", diskFileSizeStart)
log.Printf("After compaction the disk file size was: %d", diskFileSizeEnd)
if diskFileSizeStart > 0 {
percentChange := ((float64(diskFileSizeEnd) / float64(diskFileSizeStart)) * 100.0) - 100.0
switch {
case percentChange < 0:
ui.Message(fmt.Sprintf("Compacting reduced the disk file size by %.2f%%", math.Abs(percentChange)))
case percentChange == 0:
ui.Message(fmt.Sprintf("The compacting operation left the disk file size unchanged"))
case percentChange > 0:
ui.Message(fmt.Sprintf("WARNING: Compacting increased the disk file size by %.2f%%", percentChange))
}
}
}

return multistep.ActionContinue
Expand Down
25 changes: 3 additions & 22 deletions builder/vmware/common/step_compact_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package common

import (
"context"
"io/ioutil"
"os"
"testing"

"github.com/hashicorp/packer/helper/multistep"
Expand All @@ -17,25 +15,8 @@ func TestStepCompactDisk(t *testing.T) {
state := testState(t)
step := new(StepCompactDisk)

// Create a fake vmdk file for disk file size operations
diskFile, err := ioutil.TempFile("", "disk.vmdk")
if err != nil {
t.Fatalf("Error creating fake vmdk file: %s", err)
}

diskFullPath := diskFile.Name()
defer os.Remove(diskFullPath)

content := []byte("I am the fake vmdk's contents")
if _, err := diskFile.Write(content); err != nil {
t.Fatalf("Error writing to fake vmdk file: %s", err)
}
if err := diskFile.Close(); err != nil {
t.Fatalf("Error closing fake vmdk file: %s", err)
}

// Set up required state
state.Put("disk_full_paths", []string{diskFullPath})
diskFullPaths := []string{"foo"}
state.Put("disk_full_paths", diskFullPaths)

driver := state.Get("driver").(*DriverMock)

Expand All @@ -51,7 +32,7 @@ func TestStepCompactDisk(t *testing.T) {
if !driver.CompactDiskCalled {
t.Fatal("should've called")
}
if driver.CompactDiskPath != diskFullPath {
if driver.CompactDiskPath != "foo" {
t.Fatal("should call with right path")
}
}
Expand Down

0 comments on commit cbbbf55

Please sign in to comment.