2014-04-06 13:21:22 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-04-06 13:21:22 -04:00
|
|
|
"fmt"
|
2018-11-17 06:16:14 -05:00
|
|
|
"strconv"
|
2014-09-08 05:00:59 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the actual virtual machine.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// vmName string - The name of the VM
|
|
|
|
type stepCreateVM struct {
|
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-04-06 13:21:22 -04:00
|
|
|
|
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
|
|
|
|
|
2018-11-17 06:16:14 -05:00
|
|
|
commands := make([][]string, 3)
|
|
|
|
|
|
|
|
commands[0] = []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
|
|
|
}
|
2018-11-17 06:16:14 -05:00
|
|
|
commands[1] = []string{
|
|
|
|
"set", name,
|
|
|
|
"--cpus", strconv.Itoa(config.HWConfig.CpuCount),
|
|
|
|
}
|
|
|
|
commands[2] = []string{
|
|
|
|
"set", name,
|
|
|
|
"--memsize", strconv.Itoa(config.HWConfig.MemorySize),
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HWConfig.Sound {
|
|
|
|
commands = append(commands, []string{
|
|
|
|
"set", name,
|
|
|
|
"--device-add-sound",
|
|
|
|
"--connect",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HWConfig.USB {
|
|
|
|
commands = append(commands, []string{
|
|
|
|
"set", name,
|
|
|
|
"--device-add-usb",
|
|
|
|
})
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
ui.Say("Creating virtual machine...")
|
2018-11-17 06:16:14 -05:00
|
|
|
for _, command := range commands {
|
|
|
|
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
|
|
|
|
}
|
2015-06-18 05:06:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|