github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_attach_parallels_tools.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 // StepAttachParallelsTools is a step that attaches Parallels Tools ISO image 13 // as an inserted CD onto the virtual machine. 14 // 15 // Uses: 16 // driver Driver 17 // parallels_tools_path string 18 // ui packer.Ui 19 // vmName string 20 // 21 // Produces: 22 type StepAttachParallelsTools struct { 23 cdromDevice string 24 ParallelsToolsMode string 25 } 26 27 // Run adds a virtual CD-ROM device to the VM and attaches Parallels Tools ISO image. 28 // If ISO image is not specified, then this step will be skipped. 29 func (s *StepAttachParallelsTools) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 30 driver := state.Get("driver").(Driver) 31 ui := state.Get("ui").(packer.Ui) 32 vmName := state.Get("vmName").(string) 33 34 // If we're not attaching the guest additions then just return 35 if s.ParallelsToolsMode != ParallelsToolsModeAttach { 36 log.Println("Not attaching parallels tools since we're uploading.") 37 return multistep.ActionContinue 38 } 39 40 // Get the Parallels Tools path on the host machine 41 parallelsToolsPath := state.Get("parallels_tools_path").(string) 42 43 // Attach the guest additions to the computer 44 ui.Say("Attaching Parallels Tools ISO to the new CD/DVD drive...") 45 46 cdrom, err := driver.DeviceAddCDROM(vmName, parallelsToolsPath) 47 48 if err != nil { 49 err = fmt.Errorf("Error attaching Parallels Tools ISO: %s", err) 50 state.Put("error", err) 51 ui.Error(err.Error()) 52 return multistep.ActionHalt 53 } 54 55 // Track the device name so that we can can delete later 56 s.cdromDevice = cdrom 57 58 return multistep.ActionContinue 59 } 60 61 // Cleanup removes the virtual CD-ROM device attached to the VM. 62 func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) { 63 if s.cdromDevice == "" { 64 return 65 } 66 67 driver := state.Get("driver").(Driver) 68 ui := state.Get("ui").(packer.Ui) 69 vmName := state.Get("vmName").(string) 70 71 log.Println("Detaching Parallels Tools ISO...") 72 73 command := []string{ 74 "set", vmName, 75 "--device-del", s.cdromDevice, 76 } 77 78 if err := driver.Prlctl(command...); err != nil { 79 ui.Error(fmt.Sprintf("Error detaching Parallels Tools ISO: %s", err)) 80 } 81 }