2014-04-06 13:21:22 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
|
2014-05-01 17:24:19 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-08-08 02:12:48 -04:00
|
|
|
"log"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step attaches the ISO to the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2014-09-17 07:14:54 -04:00
|
|
|
// driver Driver
|
|
|
|
// isoPath string
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type stepAttachISO struct {
|
2014-09-17 07:14:54 -04:00
|
|
|
cdromDevice string
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(parallelscommon.Driver)
|
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
// Attach the disk to the controller
|
2014-09-17 07:14:54 -04:00
|
|
|
ui.Say("Attaching ISO to the new CD/DVD drive...")
|
|
|
|
cdrom, err := driver.DeviceAddCdRom(vmName, isoPath)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-04-06 13:21:22 -04:00
|
|
|
err := fmt.Errorf("Error attaching ISO: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-09-30 02:44:20 -04:00
|
|
|
// Set new boot order
|
2014-09-17 09:42:01 -04:00
|
|
|
ui.Say("Setting the boot order...")
|
|
|
|
command := []string{
|
|
|
|
"set", vmName,
|
|
|
|
"--device-bootorder", fmt.Sprintf("hdd0 %s cdrom0 net0", cdrom),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error setting the boot order: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-09-30 02:44:20 -04:00
|
|
|
// Disable 'cdrom0' device
|
|
|
|
ui.Say("Disabling default CD/DVD drive...")
|
|
|
|
command = []string{
|
|
|
|
"set", vmName,
|
|
|
|
"--device-set", "cdrom0", "--disable",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error disabling default CD/DVD drive: %s", err)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-08 02:12:48 -04:00
|
|
|
func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(parallelscommon.Driver)
|
2014-09-17 07:14:54 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2014-08-08 02:12:48 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
2014-09-30 02:44:20 -04:00
|
|
|
// Enable 'cdrom0' device back
|
|
|
|
log.Println("Enabling default CD/DVD drive...")
|
2014-08-08 02:12:48 -04:00
|
|
|
command := []string{
|
2014-09-30 02:44:20 -04:00
|
|
|
"set", vmName,
|
2014-10-21 08:44:43 -04:00
|
|
|
"--device-set", "cdrom0", "--enable", "--disconnect",
|
2014-09-30 02:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error enabling default CD/DVD drive: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detach ISO
|
|
|
|
if s.cdromDevice == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Detaching ISO...")
|
|
|
|
command = []string{
|
2014-08-08 02:12:48 -04:00
|
|
|
"set", vmName,
|
2014-09-17 07:14:54 -04:00
|
|
|
"--device-del", s.cdromDevice,
|
2014-08-08 02:12:48 -04:00
|
|
|
}
|
|
|
|
|
2014-09-17 07:14:54 -04:00
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error detaching ISO: %s", err))
|
|
|
|
}
|
2014-08-08 02:12:48 -04:00
|
|
|
}
|