packer-cn/builder/parallels/iso/step_create_vm.go

73 lines
1.7 KiB
Go
Raw Normal View History

package iso
import (
"fmt"
"github.com/mitchellh/multistep"
parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
"github.com/mitchellh/packer/packer"
)
// This step creates the actual virtual machine.
//
// Produces:
// vmName string - The name of the VM
type stepCreateVM struct {
vmName string
}
func (s *stepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(parallelscommon.Driver)
ui := state.Get("ui").(packer.Ui)
name := config.VMName
command := []string{
"create", name,
"--distribution", config.GuestOSType,
"--dst", config.OutputDir,
"--vmtype", "vm",
"--no-hdd",
}
ui.Say("Creating virtual machine...")
if err := driver.Prlctl(command...); err != nil {
err := fmt.Errorf("Error creating VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("Applying default settings...")
if err := driver.SetDefaultConfiguration(name); err != nil {
err := fmt.Errorf("Error VM configuration: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Set the VM name property on the first command
if s.vmName == "" {
s.vmName = name
}
// Set the final name in the state bag so others can use it
state.Put("vmName", s.vmName)
return multistep.ActionContinue
}
func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
if s.vmName == "" {
return
}
driver := state.Get("driver").(parallelscommon.Driver)
ui := state.Get("ui").(packer.Ui)
ui.Say("Unregistering virtual machine...")
if err := driver.Prlctl("unregister", s.vmName); err != nil {
ui.Error(fmt.Sprintf("Error unregistering virtual machine: %s", err))
}
}