From 280c3c52de604074595cf63af710c971bc3e9efe Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Wed, 17 Sep 2014 15:14:54 +0400 Subject: [PATCH] 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] --- .../common/step_attach_parallels_tools.go | 28 ++++++-------- builder/parallels/iso/step_attach_iso.go | 37 ++++++++++--------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/builder/parallels/common/step_attach_parallels_tools.go b/builder/parallels/common/step_attach_parallels_tools.go index b80ddd87e..f681a83f1 100644 --- a/builder/parallels/common/step_attach_parallels_tools.go +++ b/builder/parallels/common/step_attach_parallels_tools.go @@ -17,8 +17,8 @@ import ( // vmName string // // Produces: -// attachedToolsIso boolean type StepAttachParallelsTools struct { + cdromDevice string ParallelsToolsMode string } @@ -37,27 +37,25 @@ func (s *StepAttachParallelsTools) Run(state multistep.StateBag) multistep.StepA parallelsToolsPath := state.Get("parallels_tools_path").(string) // Attach the guest additions to the computer - ui.Say("Attaching Parallels Tools ISO onto IDE controller...") - command := []string{ - "set", vmName, - "--device-add", "cdrom", - "--image", parallelsToolsPath, - } - if err := driver.Prlctl(command...); err != nil { - err := fmt.Errorf("Error attaching Parallels Tools: %s", err) + ui.Say("Attaching Parallels Tools ISO to the new CD/DVD drive...") + + cdrom, err := driver.DeviceAddCdRom(vmName, parallelsToolsPath) + + if err != nil { + err := fmt.Errorf("Error attaching Parallels Tools ISO: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - // Set some state so we know to remove - state.Put("attachedToolsIso", true) + // Track the device name so that we can can delete later + s.cdromDevice = cdrom return multistep.ActionContinue } func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) { - if _, ok := state.GetOk("attachedToolsIso"); !ok { + if s.cdromDevice == "" { return } @@ -66,14 +64,10 @@ func (s *StepAttachParallelsTools) Cleanup(state multistep.StateBag) { vmName := state.Get("vmName").(string) log.Println("Detaching Parallels Tools ISO...") - cdDevice := "cdrom0" - if _, ok := state.GetOk("attachedIso"); ok { - cdDevice = "cdrom1" - } command := []string{ "set", vmName, - "--device-del", cdDevice, + "--device-del", s.cdromDevice, } if err := driver.Prlctl(command...); err != nil { diff --git a/builder/parallels/iso/step_attach_iso.go b/builder/parallels/iso/step_attach_iso.go index 7f9f699ad..0541271ea 100644 --- a/builder/parallels/iso/step_attach_iso.go +++ b/builder/parallels/iso/step_attach_iso.go @@ -11,10 +11,14 @@ import ( // This step attaches the ISO to the virtual machine. // // Uses: +// driver Driver +// isoPath string +// ui packer.Ui +// vmName string // // Produces: type stepAttachISO struct { - diskPath string + cdromDevice string } 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) // Attach the disk to the controller - ui.Say("Attaching ISO onto IDE controller...") - command := []string{ - "set", vmName, - "--device-set", "cdrom0", - "--image", isoPath, - "--enable", "--connect", - } - if err := driver.Prlctl(command...); err != nil { + ui.Say("Attaching ISO to the new CD/DVD drive...") + + cdrom, err := driver.DeviceAddCdRom(vmName, isoPath) + + if err != nil { err := fmt.Errorf("Error attaching ISO: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - // Set some state so we know to remove - state.Put("attachedIso", true) + // Track the device name so that we can can delete later + s.cdromDevice = cdrom return multistep.ActionContinue } func (s *stepAttachISO) Cleanup(state multistep.StateBag) { - if _, ok := state.GetOk("attachedIso"); !ok { + if s.cdromDevice == "" { return } driver := state.Get("driver").(parallelscommon.Driver) + ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) + log.Println("Detaching ISO...") + command := []string{ "set", vmName, - "--device-set", "cdrom0", - "--enable", "--disconnect", + "--device-del", s.cdromDevice, } - // Remove the ISO, ignore errors - log.Println("Detaching ISO...") - driver.Prlctl(command...) + if err := driver.Prlctl(command...); err != nil { + ui.Error(fmt.Sprintf("Error detaching ISO: %s", err)) + } }