github.phpd.cn/hashicorp/packer@v1.3.2/builder/vmware/common/step_compact_disk.go (about) 1 package common 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 ) 11 12 // This step compacts the virtual disk for the VM unless the "skip_compaction" 13 // boolean is true. 14 // 15 // Uses: 16 // driver Driver 17 // disk_full_paths ([]string) - The full paths to all created disks 18 // ui packer.Ui 19 // 20 // Produces: 21 // <nothing> 22 type StepCompactDisk struct { 23 Skip bool 24 } 25 26 func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 27 driver := state.Get("driver").(Driver) 28 ui := state.Get("ui").(packer.Ui) 29 diskFullPaths := state.Get("disk_full_paths").([]string) 30 31 if s.Skip { 32 log.Println("Skipping disk compaction step...") 33 return multistep.ActionContinue 34 } 35 36 ui.Say("Compacting all attached virtual disks...") 37 for i, diskFullPath := range diskFullPaths { 38 ui.Message(fmt.Sprintf("Compacting virtual disk %d", i+1)) 39 if err := driver.CompactDisk(diskFullPath); err != nil { 40 state.Put("error", fmt.Errorf("Error compacting disk: %s", err)) 41 return multistep.ActionHalt 42 } 43 } 44 45 return multistep.ActionContinue 46 } 47 48 func (StepCompactDisk) Cleanup(multistep.StateBag) {}