packer-cn/builder/virtualbox/iso/step_attach_iso.go

67 lines
1.5 KiB
Go
Raw Normal View History

2013-12-21 17:27:00 -05:00
package iso
2013-06-11 23:07:11 -04:00
import (
"fmt"
"github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
2013-06-11 23:07:11 -04:00
"github.com/mitchellh/packer/packer"
)
// This step attaches the ISO to the virtual machine.
//
// Uses:
//
// Produces:
type stepAttachISO struct {
2013-06-11 23:07:11 -04:00
diskPath string
}
2013-08-31 15:44:58 -04:00
func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(vboxcommon.Driver)
2013-08-31 15:44:58 -04:00
isoPath := state.Get("iso_path").(string)
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
2013-06-11 23:07:11 -04:00
// Attach the disk to the controller
command := []string{
"storageattach", vmName,
"--storagectl", "IDE Controller",
"--port", "0",
"--device", "1",
"--type", "dvddrive",
"--medium", isoPath,
}
if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error attaching ISO: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
2013-06-11 23:07:11 -04:00
return multistep.ActionHalt
}
// Track the path so that we can unregister it from VirtualBox later
s.diskPath = isoPath
return multistep.ActionContinue
}
2013-08-31 15:44:58 -04:00
func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
2013-06-11 23:07:11 -04:00
if s.diskPath == "" {
return
}
driver := state.Get("driver").(vboxcommon.Driver)
2013-08-31 15:44:58 -04:00
vmName := state.Get("vmName").(string)
2013-06-11 23:07:11 -04:00
command := []string{
"storageattach", vmName,
"--storagectl", "IDE Controller",
"--port", "0",
"--device", "1",
"--medium", "none",
}
// Remove the ISO. Note that this will probably fail since
// stepRemoveDevices does this as well. No big deal.
driver.VBoxManage(command...)
2013-06-11 23:07:11 -04:00
}