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