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