2013-12-25 01:09:22 -05:00
|
|
|
package common
|
2013-07-01 22:25:33 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-01 22:25:33 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"log"
|
2018-04-24 17:14:37 -04:00
|
|
|
"math"
|
|
|
|
"os"
|
2018-01-22 18:32:33 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-01 22:25:33 -04:00
|
|
|
)
|
|
|
|
|
2013-07-02 20:22:11 -04:00
|
|
|
// This step compacts the virtual disk for the VM unless the "skip_compaction"
|
|
|
|
// boolean is true.
|
2013-07-01 22:25:33 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
2018-04-24 18:23:48 -04:00
|
|
|
// disk_full_paths ([]string) - The full paths to all created disks
|
2013-07-01 22:25:33 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-12-25 01:09:22 -05:00
|
|
|
type StepCompactDisk struct {
|
|
|
|
Skip bool
|
|
|
|
}
|
2013-07-01 22:25:33 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-25 01:09:22 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-04-24 18:23:48 -04:00
|
|
|
diskPaths := state.Get("disk_full_paths").([]string)
|
2013-07-01 22:25:33 -04:00
|
|
|
|
2013-12-25 01:09:22 -05:00
|
|
|
if s.Skip {
|
2013-07-02 20:22:11 -04:00
|
|
|
log.Println("Skipping disk compaction step...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-04-24 17:14:37 -04:00
|
|
|
ui.Say("Compacting all attached virtual disks...")
|
2018-04-24 18:23:48 -04:00
|
|
|
for i, diskPath := range diskPaths {
|
2018-04-24 17:14:37 -04:00
|
|
|
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(diskPath)
|
|
|
|
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
|
2018-04-24 18:23:48 -04:00
|
|
|
if err := driver.CompactDisk(diskPath); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
|
|
|
|
return multistep.ActionHalt
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2018-04-24 17:14:37 -04:00
|
|
|
// Get the file size of the virtual disk post compaction
|
|
|
|
fi, err = os.Stat(diskPath)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2013-07-01 22:25:33 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-25 01:09:22 -05:00
|
|
|
func (StepCompactDisk) Cleanup(multistep.StateBag) {}
|