github.com/rothwerx/packer@v0.9.0/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  
    21  	vmName string
    22  }
    23  
    24  func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
    25  	driver := state.Get("driver").(Driver)
    26  	ui := state.Get("ui").(packer.Ui)
    27  	vmName := state.Get("vmName").(string)
    28  
    29  	ui.Say("Starting the virtual machine...")
    30  	command := []string{"start", vmName}
    31  	if err := driver.Prlctl(command...); err != nil {
    32  		err := fmt.Errorf("Error starting VM: %s", err)
    33  		state.Put("error", err)
    34  		ui.Error(err.Error())
    35  		return multistep.ActionHalt
    36  	}
    37  
    38  	s.vmName = vmName
    39  
    40  	if int64(s.BootWait) > 0 {
    41  		ui.Say(fmt.Sprintf("Waiting %s for boot...", s.BootWait))
    42  		wait := time.After(s.BootWait)
    43  	WAITLOOP:
    44  		for {
    45  			select {
    46  			case <-wait:
    47  				break WAITLOOP
    48  			case <-time.After(1 * time.Second):
    49  				if _, ok := state.GetOk(multistep.StateCancelled); ok {
    50  					return multistep.ActionHalt
    51  				}
    52  			}
    53  		}
    54  	}
    55  
    56  	return multistep.ActionContinue
    57  }
    58  
    59  func (s *StepRun) Cleanup(state multistep.StateBag) {
    60  	if s.vmName == "" {
    61  		return
    62  	}
    63  
    64  	driver := state.Get("driver").(Driver)
    65  	ui := state.Get("ui").(packer.Ui)
    66  
    67  	if running, _ := driver.IsRunning(s.vmName); running {
    68  		if err := driver.Prlctl("stop", s.vmName); err != nil {
    69  			ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
    70  		}
    71  	}
    72  }