github.com/rothwerx/packer@v0.9.0/builder/vmware/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  	"log"
     8  )
     9  
    10  // This step compacts the virtual disk for the VM unless the "skip_compaction"
    11  // boolean is true.
    12  //
    13  // Uses:
    14  //   driver Driver
    15  //   full_disk_path string
    16  //   ui     packer.Ui
    17  //
    18  // Produces:
    19  //   <nothing>
    20  type StepCompactDisk struct {
    21  	Skip bool
    22  }
    23  
    24  func (s StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction {
    25  	driver := state.Get("driver").(Driver)
    26  	ui := state.Get("ui").(packer.Ui)
    27  	full_disk_path := state.Get("full_disk_path").(string)
    28  
    29  	if s.Skip {
    30  		log.Println("Skipping disk compaction step...")
    31  		return multistep.ActionContinue
    32  	}
    33  
    34  	ui.Say("Compacting the disk image")
    35  	if err := driver.CompactDisk(full_disk_path); err != nil {
    36  		state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
    37  		return multistep.ActionHalt
    38  	}
    39  
    40  	if state.Get("additional_disk_paths") != nil {
    41  		if moreDisks := state.Get("additional_disk_paths").([]string); len(moreDisks) > 0 {
    42  			for i, path := range moreDisks {
    43  				ui.Say(fmt.Sprintf("Compacting additional disk image %d", i+1))
    44  				if err := driver.CompactDisk(path); err != nil {
    45  					state.Put("error", fmt.Errorf("Error compacting additional disk %d: %s", i+1, err))
    46  					return multistep.ActionHalt
    47  				}
    48  			}
    49  		}
    50  	}
    51  
    52  	return multistep.ActionContinue
    53  }
    54  
    55  func (StepCompactDisk) Cleanup(multistep.StateBag) {}