2013-06-11 19:12:19 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the actual virtual machine.
|
2013-06-11 19:44:43 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// vmName string - The name of the VM
|
2013-06-11 23:07:11 -04:00
|
|
|
type stepCreateVM struct {
|
2013-06-11 19:12:19 -04:00
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-11 19:12:19 -04:00
|
|
|
|
|
|
|
name := config.VMName
|
|
|
|
|
|
|
|
commands := make([][]string, 4)
|
2013-07-07 21:04:30 -04:00
|
|
|
commands[0] = []string{
|
|
|
|
"createvm", "--name", name,
|
|
|
|
"--ostype", config.GuestOSType, "--register",
|
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
commands[1] = []string{
|
|
|
|
"modifyvm", name,
|
|
|
|
"--boot1", "disk", "--boot2", "dvd", "--boot3", "none", "--boot4", "none",
|
|
|
|
}
|
|
|
|
commands[2] = []string{"modifyvm", name, "--cpus", "1"}
|
|
|
|
commands[3] = []string{"modifyvm", name, "--memory", "512"}
|
|
|
|
|
|
|
|
ui.Say("Creating virtual machine...")
|
|
|
|
for _, command := range commands {
|
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error creating VM: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 19:12:19 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-10-24 14:46:54 -04:00
|
|
|
// Set the VM name property on the first command
|
2013-06-11 19:12:19 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
s.vmName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:44:43 -04:00
|
|
|
// Set the final name in the state bag so others can use it
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("vmName", s.vmName)
|
2013-06-11 19:44:43 -04:00
|
|
|
|
2013-06-11 19:12:19 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
|
2013-06-11 19:12:19 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-11 19:12:19 -04:00
|
|
|
|
|
|
|
ui.Say("Unregistering and deleting virtual machine...")
|
|
|
|
if err := driver.VBoxManage("unregistervm", s.vmName, "--delete"); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
|
|
|
|
}
|
|
|
|
}
|