github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/parallels/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  		ui.Message("WARNING: The VM will be started in headless mode, as configured.\n" +
    34  			"In headless mode, errors during the boot sequence or OS setup\n" +
    35  			"won't be easily visible. Use at your own discretion.")
    36  		//guiArgument = "headless"
    37  	}
    38  	command := []string{"start", vmName}
    39  	if err := driver.Prlctl(command...); err != nil {
    40  		err := fmt.Errorf("Error starting VM: %s", err)
    41  		state.Put("error", err)
    42  		ui.Error(err.Error())
    43  		return multistep.ActionHalt
    44  	}
    45  
    46  	s.vmName = vmName
    47  
    48  	if int64(s.BootWait) > 0 {
    49  		ui.Say(fmt.Sprintf("Waiting %s for boot...", s.BootWait))
    50  		wait := time.After(s.BootWait)
    51  	WAITLOOP:
    52  		for {
    53  			select {
    54  			case <-wait:
    55  				break WAITLOOP
    56  			case <-time.After(1 * time.Second):
    57  				if _, ok := state.GetOk(multistep.StateCancelled); ok {
    58  					return multistep.ActionHalt
    59  				}
    60  			}
    61  		}
    62  	}
    63  
    64  	return multistep.ActionContinue
    65  }
    66  
    67  func (s *StepRun) Cleanup(state multistep.StateBag) {
    68  	if s.vmName == "" {
    69  		return
    70  	}
    71  
    72  	driver := state.Get("driver").(Driver)
    73  	ui := state.Get("ui").(packer.Ui)
    74  
    75  	if running, _ := driver.IsRunning(s.vmName); running {
    76  		if err := driver.Prlctl("stop", s.vmName); err != nil {
    77  			ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
    78  		}
    79  	}
    80  }