github.com/rothwerx/packer@v0.9.0/builder/virtualbox/common/step_run.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 "time" 8 ) 9 10 // This step starts the virtual machine. 11 // 12 // Uses: 13 // driver Driver 14 // ui packer.Ui 15 // vmName string 16 // 17 // Produces: 18 type StepRun struct { 19 BootWait time.Duration 20 Headless bool 21 22 vmName string 23 } 24 25 func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { 26 driver := state.Get("driver").(Driver) 27 ui := state.Get("ui").(packer.Ui) 28 vmName := state.Get("vmName").(string) 29 30 ui.Say("Starting the virtual machine...") 31 guiArgument := "gui" 32 if s.Headless == true { 33 vrdpIpRaw, vrdpIpOk := state.GetOk("vrdpIp") 34 vrdpPortRaw, vrdpPortOk := state.GetOk("vrdpPort") 35 36 if vrdpIpOk && vrdpPortOk { 37 vrdpIp := vrdpIpRaw.(string) 38 vrdpPort := vrdpPortRaw.(uint) 39 40 ui.Message(fmt.Sprintf( 41 "The VM will be run headless, without a GUI. If you want to\n"+ 42 "view the screen of the VM, connect via VRDP without a password to\n"+ 43 "%s:%d", vrdpIp, vrdpPort)) 44 } else { 45 ui.Message("The VM will be run headless, without a GUI, as configured.\n" + 46 "If the run isn't succeeding as you expect, please enable the GUI\n" + 47 "to inspect the progress of the build.") 48 } 49 guiArgument = "headless" 50 } 51 command := []string{"startvm", vmName, "--type", guiArgument} 52 if err := driver.VBoxManage(command...); err != nil { 53 err := fmt.Errorf("Error starting VM: %s", err) 54 state.Put("error", err) 55 ui.Error(err.Error()) 56 return multistep.ActionHalt 57 } 58 59 s.vmName = vmName 60 61 if int64(s.BootWait) > 0 { 62 ui.Say(fmt.Sprintf("Waiting %s for boot...", s.BootWait)) 63 wait := time.After(s.BootWait) 64 WAITLOOP: 65 for { 66 select { 67 case <-wait: 68 break WAITLOOP 69 case <-time.After(1 * time.Second): 70 if _, ok := state.GetOk(multistep.StateCancelled); ok { 71 return multistep.ActionHalt 72 } 73 } 74 } 75 } 76 77 return multistep.ActionContinue 78 } 79 80 func (s *StepRun) Cleanup(state multistep.StateBag) { 81 if s.vmName == "" { 82 return 83 } 84 85 driver := state.Get("driver").(Driver) 86 ui := state.Get("ui").(packer.Ui) 87 88 if running, _ := driver.IsRunning(s.vmName); running { 89 if err := driver.VBoxManage("controlvm", s.vmName, "poweroff"); err != nil { 90 ui.Error(fmt.Sprintf("Error shutting down VM: %s", err)) 91 } 92 } 93 }