github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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  	commands := make([][]string, 8)
    27  	commands[0] = []string{
    28  		"create", name,
    29  		"--distribution", config.GuestOSType,
    30  		"--dst", config.OutputDir,
    31  		"--vmtype", "vm",
    32  	}
    33  	commands[1] = []string{"set", name, "--cpus", "1"}
    34  	commands[2] = []string{"set", name, "--memsize", "512"}
    35  	commands[3] = []string{"set", name, "--startup-view", "same"}
    36  	commands[4] = []string{"set", name, "--on-shutdown", "close"}
    37  	commands[5] = []string{"set", name, "--on-window-close", "keep-running"}
    38  	commands[6] = []string{"set", name, "--auto-share-camera", "off"}
    39  	commands[7] = []string{"set", name, "--smart-guard", "off"}
    40  
    41  	ui.Say("Creating virtual machine...")
    42  	for _, command := range commands {
    43  		err := driver.Prlctl(command...)
    44  		ui.Say(fmt.Sprintf("Executing: prlctl %s", command))
    45  		if err != nil {
    46  			err := fmt.Errorf("Error creating VM: %s", err)
    47  			state.Put("error", err)
    48  			ui.Error(err.Error())
    49  			return multistep.ActionHalt
    50  		}
    51  
    52  		// Set the VM name property on the first command
    53  		if s.vmName == "" {
    54  			s.vmName = name
    55  		}
    56  	}
    57  
    58  	// Set the final name in the state bag so others can use it
    59  	state.Put("vmName", s.vmName)
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
    64  	if s.vmName == "" {
    65  		return
    66  	}
    67  
    68  	driver := state.Get("driver").(parallelscommon.Driver)
    69  	ui := state.Get("ui").(packer.Ui)
    70  
    71  	ui.Say("Unregistering virtual machine...")
    72  	if err := driver.Prlctl("unregister", s.vmName); err != nil {
    73  		ui.Error(fmt.Sprintf("Error unregistering virtual machine: %s", err))
    74  	}
    75  }