2014-04-06 13:21:22 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-08 05:00:59 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
2015-05-27 16:49:31 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2014-04-06 13:21:22 -04:00
|
|
|
driver := state.Get("driver").(parallelscommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
name := config.VMName
|
|
|
|
|
2015-06-18 05:06:49 -04:00
|
|
|
command := []string{
|
2014-04-06 13:21:22 -04:00
|
|
|
"create", name,
|
2014-09-02 08:00:25 -04:00
|
|
|
"--distribution", config.GuestOSType,
|
2014-09-08 05:00:59 -04:00
|
|
|
"--dst", config.OutputDir,
|
2014-10-01 08:04:22 -04:00
|
|
|
"--no-hdd",
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating virtual machine...")
|
2015-06-18 05:06:49 -04:00
|
|
|
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
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
|
2015-06-18 05:06:49 -04:00
|
|
|
// Set the VM name property on the first command
|
|
|
|
if s.vmName == "" {
|
|
|
|
s.vmName = name
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|