github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_compact_disk.go (about)

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