github.com/rothwerx/packer@v0.9.0/builder/parallels/iso/step_create_vm.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // This step creates the actual virtual machine.
    12  //
    13  // Produces:
    14  //   vmName string - The name of the VM
    15  type stepCreateVM struct {
    16  	vmName string
    17  }
    18  
    19  func (s *stepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
    20  
    21  	config := state.Get("config").(*Config)
    22  	driver := state.Get("driver").(parallelscommon.Driver)
    23  	ui := state.Get("ui").(packer.Ui)
    24  	name := config.VMName
    25  
    26  	command := []string{
    27  		"create", name,
    28  		"--distribution", config.GuestOSType,
    29  		"--dst", config.OutputDir,
    30  		"--vmtype", "vm",
    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  }