github.com/mitchellh/packer@v1.3.2/builder/vmware/common/step_run.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  // This step runs the created virtual machine.
    13  //
    14  // Uses:
    15  //   driver Driver
    16  //   ui     packer.Ui
    17  //   vmx_path string
    18  //
    19  // Produces:
    20  //   <nothing>
    21  type StepRun struct {
    22  	DurationBeforeStop time.Duration
    23  	Headless           bool
    24  
    25  	bootTime time.Time
    26  	vmxPath  string
    27  }
    28  
    29  func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    30  	driver := state.Get("driver").(Driver)
    31  	ui := state.Get("ui").(packer.Ui)
    32  	vmxPath := state.Get("vmx_path").(string)
    33  
    34  	// Set the VMX path so that we know we started the machine
    35  	s.bootTime = time.Now()
    36  	s.vmxPath = vmxPath
    37  
    38  	ui.Say("Starting virtual machine...")
    39  	if s.Headless {
    40  		vncIpRaw, vncIpOk := state.GetOk("vnc_ip")
    41  		vncPortRaw, vncPortOk := state.GetOk("vnc_port")
    42  		vncPasswordRaw, vncPasswordOk := state.GetOk("vnc_password")
    43  
    44  		if vncIpOk && vncPortOk && vncPasswordOk {
    45  			vncIp := vncIpRaw.(string)
    46  			vncPort := vncPortRaw.(uint)
    47  			vncPassword := vncPasswordRaw.(string)
    48  
    49  			ui.Message(fmt.Sprintf(
    50  				"The VM will be run headless, without a GUI. If you want to\n"+
    51  					"view the screen of the VM, connect via VNC with the password \"%s\" to\n"+
    52  					"vnc://%s:%d", vncPassword, vncIp, vncPort))
    53  		} else {
    54  			ui.Message("The VM will be run headless, without a GUI, as configured.\n" +
    55  				"If the run isn't succeeding as you expect, please enable the GUI\n" +
    56  				"to inspect the progress of the build.")
    57  		}
    58  	}
    59  
    60  	if err := driver.Start(vmxPath, s.Headless); err != nil {
    61  		err := fmt.Errorf("Error starting VM: %s", err)
    62  		state.Put("error", err)
    63  		ui.Error(err.Error())
    64  		return multistep.ActionHalt
    65  	}
    66  
    67  	return multistep.ActionContinue
    68  }
    69  
    70  func (s *StepRun) Cleanup(state multistep.StateBag) {
    71  	driver := state.Get("driver").(Driver)
    72  	ui := state.Get("ui").(packer.Ui)
    73  
    74  	// If we started the machine... stop it.
    75  	if s.vmxPath != "" {
    76  		// If we started it less than 5 seconds ago... wait.
    77  		sinceBootTime := time.Since(s.bootTime)
    78  		waitBootTime := s.DurationBeforeStop
    79  		if sinceBootTime < waitBootTime {
    80  			sleepTime := waitBootTime - sinceBootTime
    81  			ui.Say(fmt.Sprintf(
    82  				"Waiting %s to give VMware time to clean up...", sleepTime.String()))
    83  			time.Sleep(sleepTime)
    84  		}
    85  
    86  		// See if it is running
    87  		running, _ := driver.IsRunning(s.vmxPath)
    88  		if running {
    89  			ui.Say("Stopping virtual machine...")
    90  			if err := driver.Stop(s.vmxPath); err != nil {
    91  				ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
    92  			}
    93  		}
    94  	}
    95  }