github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/parallels/iso/step_create_vm.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  
     6  	parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
     7  	"github.com/hashicorp/packer/packer"
     8  	"github.com/mitchellh/multistep"
     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  		"--no-hdd",
    31  	}
    32  
    33  	ui.Say("Creating virtual machine...")
    34  	if err := driver.Prlctl(command...); err != nil {
    35  		err := fmt.Errorf("Error creating VM: %s", err)
    36  		state.Put("error", err)
    37  		ui.Error(err.Error())
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	ui.Say("Applying default settings...")
    42  	if err := driver.SetDefaultConfiguration(name); err != nil {
    43  		err := fmt.Errorf("Error VM configuration: %s", err)
    44  		state.Put("error", err)
    45  		ui.Error(err.Error())
    46  		return multistep.ActionHalt
    47  	}
    48  
    49  	// Set the VM name property on the first command
    50  	if s.vmName == "" {
    51  		s.vmName = name
    52  	}
    53  
    54  	// Set the final name in the state bag so others can use it
    55  	state.Put("vmName", s.vmName)
    56  	return multistep.ActionContinue
    57  }
    58  
    59  func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
    60  	if s.vmName == "" {
    61  		return
    62  	}
    63  
    64  	driver := state.Get("driver").(parallelscommon.Driver)
    65  	ui := state.Get("ui").(packer.Ui)
    66  
    67  	ui.Say("Unregistering virtual machine...")
    68  	if err := driver.Prlctl("unregister", s.vmName); err != nil {
    69  		ui.Error(fmt.Sprintf("Error unregistering virtual machine: %s", err))
    70  	}
    71  }