github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/step_attach_parallels_tools.go (about)

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