github.com/timsutton/packer@v1.3.2/builder/parallels/iso/step_create_vm.go (about)

     1  package iso
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  // This step creates the actual virtual machine.
    13  //
    14  // Produces:
    15  //   vmName string - The name of the VM
    16  type stepCreateVM struct {
    17  	vmName string
    18  }
    19  
    20  func (s *stepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    21  
    22  	config := state.Get("config").(*Config)
    23  	driver := state.Get("driver").(parallelscommon.Driver)
    24  	ui := state.Get("ui").(packer.Ui)
    25  	name := config.VMName
    26  
    27  	command := []string{
    28  		"create", name,
    29  		"--distribution", config.GuestOSType,
    30  		"--dst", config.OutputDir,
    31  		"--no-hdd",
    32  	}
    33  
    34  	ui.Say("Creating virtual machine...")
    35  	if err := driver.Prlctl(command...); err != nil {
    36  		err := fmt.Errorf("Error creating VM: %s", err)
    37  		state.Put("error", err)
    38  		ui.Error(err.Error())
    39  		return multistep.ActionHalt
    40  	}
    41  
    42  	ui.Say("Applying default settings...")
    43  	if err := driver.SetDefaultConfiguration(name); err != nil {
    44  		err := fmt.Errorf("Error VM configuration: %s", err)
    45  		state.Put("error", err)
    46  		ui.Error(err.Error())
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	// Set the VM name property on the first command
    51  	if s.vmName == "" {
    52  		s.vmName = name
    53  	}
    54  
    55  	// Set the final name in the state bag so others can use it
    56  	state.Put("vmName", s.vmName)
    57  	return multistep.ActionContinue
    58  }
    59  
    60  func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
    61  	if s.vmName == "" {
    62  		return
    63  	}
    64  
    65  	driver := state.Get("driver").(parallelscommon.Driver)
    66  	ui := state.Get("ui").(packer.Ui)
    67  
    68  	ui.Say("Unregistering virtual machine...")
    69  	if err := driver.Prlctl("unregister", s.vmName); err != nil {
    70  		ui.Error(fmt.Sprintf("Error unregistering virtual machine: %s", err))
    71  	}
    72  }