2013-12-22 13:30:12 -05:00
|
|
|
package common
|
2013-06-11 23:46:32 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 23:46:32 -04:00
|
|
|
"fmt"
|
2017-03-28 21:29:55 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-11 23:46:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step starts the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 13:30:12 -05:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2013-06-11 23:46:32 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2013-12-22 13:30:12 -05:00
|
|
|
type StepRun struct {
|
|
|
|
Headless bool
|
|
|
|
|
2013-06-11 23:46:32 -04:00
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 13:30:12 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:46:32 -04:00
|
|
|
|
|
|
|
ui.Say("Starting the virtual machine...")
|
2013-07-02 00:13:24 -04:00
|
|
|
guiArgument := "gui"
|
2017-03-28 21:29:55 -04:00
|
|
|
if s.Headless {
|
2015-08-22 18:41:08 -04:00
|
|
|
vrdpIpRaw, vrdpIpOk := state.GetOk("vrdpIp")
|
|
|
|
vrdpPortRaw, vrdpPortOk := state.GetOk("vrdpPort")
|
|
|
|
|
|
|
|
if vrdpIpOk && vrdpPortOk {
|
|
|
|
vrdpIp := vrdpIpRaw.(string)
|
|
|
|
vrdpPort := vrdpPortRaw.(uint)
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"The VM will be run headless, without a GUI. If you want to\n"+
|
2016-02-12 02:53:40 -05:00
|
|
|
"view the screen of the VM, connect via VRDP without a password to\n"+
|
2017-02-02 04:55:28 -05:00
|
|
|
"rdp://%s:%d", vrdpIp, vrdpPort))
|
2015-08-22 18:41:08 -04:00
|
|
|
} else {
|
|
|
|
ui.Message("The VM will be run headless, without a GUI, as configured.\n" +
|
2016-02-12 02:53:40 -05:00
|
|
|
"If the run isn't succeeding as you expect, please enable the GUI\n" +
|
|
|
|
"to inspect the progress of the build.")
|
2015-08-22 18:41:08 -04:00
|
|
|
}
|
2013-07-02 00:13:24 -04:00
|
|
|
guiArgument = "headless"
|
|
|
|
}
|
|
|
|
command := []string{"startvm", vmName, "--type", guiArgument}
|
2013-06-11 23:46:32 -04:00
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error starting 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 23:46:32 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = vmName
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 13:30:12 -05:00
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
2013-06-11 23:46:32 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-22 13:30:12 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-24 01:00:40 -04:00
|
|
|
|
|
|
|
if running, _ := driver.IsRunning(s.vmName); running {
|
|
|
|
if err := driver.VBoxManage("controlvm", s.vmName, "poweroff"); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error shutting down VM: %s", err))
|
|
|
|
}
|
2013-06-11 23:46:32 -04:00
|
|
|
}
|
|
|
|
}
|