github.com/sneal/packer@v0.5.2/builder/vmware/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 runs the created virtual machine.
    11  //
    12  // Uses:
    13  //   driver Driver
    14  //   ui     packer.Ui
    15  //   vmx_path string
    16  //
    17  // Produces:
    18  //   <nothing>
    19  type StepRun struct {
    20  	BootWait           time.Duration
    21  	DurationBeforeStop time.Duration
    22  	Headless           bool
    23  
    24  	bootTime time.Time
    25  	vmxPath  string
    26  }
    27  
    28  func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
    29  	driver := state.Get("driver").(Driver)
    30  	ui := state.Get("ui").(packer.Ui)
    31  	vmxPath := state.Get("vmx_path").(string)
    32  
    33  	// Set the VMX path so that we know we started the machine
    34  	s.bootTime = time.Now()
    35  	s.vmxPath = vmxPath
    36  
    37  	ui.Say("Starting virtual machine...")
    38  	if s.Headless {
    39  		vncIpRaw, vncIpOk := state.GetOk("vnc_ip")
    40  		vncPortRaw, vncPortOk := state.GetOk("vnc_port")
    41  
    42  		if vncIpOk && vncPortOk {
    43  			vncIp := vncIpRaw.(string)
    44  			vncPort := vncPortRaw.(uint)
    45  
    46  			ui.Message(fmt.Sprintf(
    47  				"The VM will be run headless, without a GUI. If you want to\n"+
    48  					"view the screen of the VM, connect via VNC without a password to\n"+
    49  					"%s:%d", vncIp, vncPort))
    50  		} else {
    51  			ui.Message("The VM will be run headless, without a GUI, as configured.\n" +
    52  				"If the run isn't succeeding as you expect, please enable the GUI\n" +
    53  				"to inspect the progress of the build.")
    54  		}
    55  	}
    56  
    57  	if err := driver.Start(vmxPath, s.Headless); err != nil {
    58  		err := fmt.Errorf("Error starting VM: %s", err)
    59  		state.Put("error", err)
    60  		ui.Error(err.Error())
    61  		return multistep.ActionHalt
    62  	}
    63  
    64  	// Wait the wait amount
    65  	if int64(s.BootWait) > 0 {
    66  		ui.Say(fmt.Sprintf("Waiting %s for boot...", s.BootWait.String()))
    67  		wait := time.After(s.BootWait)
    68  	WAITLOOP:
    69  		for {
    70  			select {
    71  			case <-wait:
    72  				break WAITLOOP
    73  			case <-time.After(1 * time.Second):
    74  				if _, ok := state.GetOk(multistep.StateCancelled); ok {
    75  					return multistep.ActionHalt
    76  				}
    77  			}
    78  		}
    79  
    80  	}
    81  
    82  	return multistep.ActionContinue
    83  }
    84  
    85  func (s *StepRun) Cleanup(state multistep.StateBag) {
    86  	driver := state.Get("driver").(Driver)
    87  	ui := state.Get("ui").(packer.Ui)
    88  
    89  	// If we started the machine... stop it.
    90  	if s.vmxPath != "" {
    91  		// If we started it less than 5 seconds ago... wait.
    92  		sinceBootTime := time.Since(s.bootTime)
    93  		waitBootTime := s.DurationBeforeStop
    94  		if sinceBootTime < waitBootTime {
    95  			sleepTime := waitBootTime - sinceBootTime
    96  			ui.Say(fmt.Sprintf(
    97  				"Waiting %s to give VMware time to clean up...", sleepTime.String()))
    98  			time.Sleep(sleepTime)
    99  		}
   100  
   101  		// See if it is running
   102  		running, _ := driver.IsRunning(s.vmxPath)
   103  		if running {
   104  			ui.Say("Stopping virtual machine...")
   105  			if err := driver.Stop(s.vmxPath); err != nil {
   106  				ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
   107  			}
   108  		}
   109  	}
   110  }