builder/parallels: Attach ISO images to the separate cdrom device

For each ISO image the individual cdrom device will be added to the VM. During the cleanup these devices will be deleted.
It makes attach steps more clear - there is no doubt what is the name of the device.

Related to: [mitchellh/packer#1502]
This commit is contained in:
Mikhail Zholobov 2014-09-17 15:14:54 +04:00 committed by Rickard von Essen
parent 0cfe58193f
commit 280c3c52de
2 changed files with 31 additions and 34 deletions

View File

@ -17,8 +17,8 @@ import (
// vmName string // vmName string
// //
// Produces: // Produces:
// attachedToolsIso boolean
type StepAttachParallelsTools struct { type StepAttachParallelsTools struct {
cdromDevice string
ParallelsToolsMode string ParallelsToolsMode string
} }
@ -37,27 +37,25 @@ func (s *StepAttachParallelsTools) Run(state multistep.StateBag) multistep.StepA
parallelsToolsPath := state.Get("parallels_tools_path").(string) parallelsToolsPath := state.Get("parallels_tools_path").(string)
// Attach the guest additions to the computer // Attach the guest additions to the computer
ui.Say("Attaching Parallels Tools ISO onto IDE controller...") ui.Say("Attaching Parallels Tools ISO to the new CD/DVD drive...")
command := []string{
"set", vmName, cdrom, err := driver.DeviceAddCdRom(vmName, parallelsToolsPath)
"--device-add", "cdrom",
"--image", parallelsToolsPath, if err != nil {
} err := fmt.Errorf("Error attaching Parallels Tools ISO: %s", err)
if err := driver.Prlctl(command...); err != nil {
err := fmt.Errorf("Error attaching Parallels Tools: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
// Set some state so we know to remove // Track the device name so that we can can delete later
state.Put("attachedToolsIso", true) s.cdromDevice = cdrom
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) { func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) {
if _, ok := state.GetOk("attachedToolsIso"); !ok { if s.cdromDevice == "" {
return return
} }
@ -66,14 +64,10 @@ func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) {
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
log.Println("Detaching Parallels Tools ISO...") log.Println("Detaching Parallels Tools ISO...")
cdDevice := "cdrom0"
if _, ok := state.GetOk("attachedIso"); ok {
cdDevice = "cdrom1"
}
command := []string{ command := []string{
"set", vmName, "set", vmName,
"--device-del", cdDevice, "--device-del", s.cdromDevice,
} }
if err := driver.Prlctl(command...); err != nil { if err := driver.Prlctl(command...); err != nil {

View File

@ -11,10 +11,14 @@ import (
// This step attaches the ISO to the virtual machine. // This step attaches the ISO to the virtual machine.
// //
// Uses: // Uses:
// driver Driver
// isoPath string
// ui packer.Ui
// vmName string
// //
// Produces: // Produces:
type stepAttachISO struct { type stepAttachISO struct {
diskPath string cdromDevice string
} }
func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
@ -24,41 +28,40 @@ func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
// Attach the disk to the controller // Attach the disk to the controller
ui.Say("Attaching ISO onto IDE controller...") ui.Say("Attaching ISO to the new CD/DVD drive...")
command := []string{
"set", vmName, cdrom, err := driver.DeviceAddCdRom(vmName, isoPath)
"--device-set", "cdrom0",
"--image", isoPath, if err != nil {
"--enable", "--connect",
}
if err := driver.Prlctl(command...); err != nil {
err := fmt.Errorf("Error attaching ISO: %s", err) err := fmt.Errorf("Error attaching ISO: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
// Set some state so we know to remove // Track the device name so that we can can delete later
state.Put("attachedIso", true) s.cdromDevice = cdrom
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepAttachISO) Cleanup(state multistep.StateBag) { func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
if _, ok := state.GetOk("attachedIso"); !ok { if s.cdromDevice == "" {
return return
} }
driver := state.Get("driver").(parallelscommon.Driver) driver := state.Get("driver").(parallelscommon.Driver)
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
log.Println("Detaching ISO...")
command := []string{ command := []string{
"set", vmName, "set", vmName,
"--device-set", "cdrom0", "--device-del", s.cdromDevice,
"--enable", "--disconnect",
} }
// Remove the ISO, ignore errors if err := driver.Prlctl(command...); err != nil {
log.Println("Detaching ISO...") ui.Error(fmt.Sprintf("Error detaching ISO: %s", err))
driver.Prlctl(command...) }
} }