github.com/rothwerx/packer@v0.9.0/builder/parallels/common/step_compact_disk.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  // This step removes all empty blocks from expanding Parallels virtual disks
    10  // and reduces the result disk size
    11  //
    12  // Uses:
    13  //   driver Driver
    14  //   vmName string
    15  //   ui     packer.Ui
    16  //
    17  // Produces:
    18  //   <nothing>
    19  type StepCompactDisk struct {
    20  	Skip bool
    21  }
    22  
    23  func (s *StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction {
    24  	driver := state.Get("driver").(Driver)
    25  	vmName := state.Get("vmName").(string)
    26  	ui := state.Get("ui").(packer.Ui)
    27  
    28  	if s.Skip {
    29  		ui.Say("Skipping disk compaction step...")
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	ui.Say("Compacting the disk image")
    34  	diskPath, err := driver.DiskPath(vmName)
    35  	if err != nil {
    36  		err := fmt.Errorf("Error detecting virtual disk path: %s", err)
    37  		state.Put("error", err)
    38  		ui.Error(err.Error())
    39  		return multistep.ActionHalt
    40  	}
    41  
    42  	if err := driver.CompactDisk(diskPath); err != nil {
    43  		state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
    44  		ui.Error(err.Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	return multistep.ActionContinue
    49  }
    50  
    51  func (*StepCompactDisk) Cleanup(multistep.StateBag) {}