github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_run.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  // StepRun is a step that 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  	vmName string
    21  }
    22  
    23  // Run starts the VM.
    24  func (s *StepRun) Run(_ context.Context, 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  	return multistep.ActionContinue
    41  }
    42  
    43  // Cleanup stops the VM.
    44  func (s *StepRun) Cleanup(state multistep.StateBag) {
    45  	if s.vmName == "" {
    46  		return
    47  	}
    48  
    49  	driver := state.Get("driver").(Driver)
    50  	ui := state.Get("ui").(packer.Ui)
    51  
    52  	if running, _ := driver.IsRunning(s.vmName); running {
    53  		if err := driver.Stop(s.vmName); err != nil {
    54  			ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
    55  		}
    56  	}
    57  }