github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/builder/parallels/iso/step_create_vm.go (about)

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