2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-11 13:49:54 -05:00
|
|
|
"log"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
2016-12-11 17:37:41 -05:00
|
|
|
// StepAttachParallelsTools is a step that attaches Parallels Tools ISO image
|
|
|
|
// as an inserted CD onto the virtual machine.
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
2014-09-02 05:28:04 -04:00
|
|
|
// parallels_tools_path string
|
2014-04-06 13:21:22 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type StepAttachParallelsTools struct {
|
2014-09-17 07:14:54 -04:00
|
|
|
cdromDevice string
|
2014-09-02 05:28:04 -04:00
|
|
|
ParallelsToolsMode string
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run adds a virtual CD-ROM device to the VM and attaches Parallels Tools ISO image.
|
|
|
|
// If ISO image is not specified, then this step will be skipped.
|
2014-04-06 13:21:22 -04:00
|
|
|
func (s *StepAttachParallelsTools) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
// If we're not attaching the guest additions then just return
|
|
|
|
if s.ParallelsToolsMode != ParallelsToolsModeAttach {
|
|
|
|
log.Println("Not attaching parallels tools since we're uploading.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
// Get the Parallels Tools path on the host machine
|
2014-09-02 05:28:04 -04:00
|
|
|
parallelsToolsPath := state.Get("parallels_tools_path").(string)
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
// Attach the guest additions to the computer
|
2014-09-17 07:14:54 -04:00
|
|
|
ui.Say("Attaching Parallels Tools ISO to the new CD/DVD drive...")
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
cdrom, err := driver.DeviceAddCDROM(vmName, parallelsToolsPath)
|
2014-09-17 07:14:54 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error attaching Parallels Tools ISO: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-09-17 07:14:54 -04:00
|
|
|
// Track the device name so that we can can delete later
|
|
|
|
s.cdromDevice = cdrom
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup removes the virtual CD-ROM device attached to the VM.
|
2014-08-08 02:12:48 -04:00
|
|
|
func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) {
|
2014-09-17 07:14:54 -04:00
|
|
|
if s.cdromDevice == "" {
|
2014-08-08 02:12:48 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(Driver)
|
2014-09-02 05:28:04 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2014-08-08 02:12:48 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
log.Println("Detaching Parallels Tools ISO...")
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"set", vmName,
|
2014-09-17 07:14:54 -04:00
|
|
|
"--device-del", s.cdromDevice,
|
2014-08-08 02:12:48 -04:00
|
|
|
}
|
2014-09-02 05:28:04 -04:00
|
|
|
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error detaching Parallels Tools ISO: %s", err))
|
|
|
|
}
|
2014-08-08 02:12:48 -04:00
|
|
|
}
|