Added support for the cpu_count, memory_size, sound, and usb options to the parallels builder.

This commit is contained in:
Ali Rizvi-Santiago 2018-11-17 05:16:14 -06:00
parent 06c2c35e4c
commit 71d15d05c0
7 changed files with 134 additions and 40 deletions

View File

@ -7,19 +7,17 @@ type Parallels10Driver struct {
// SetDefaultConfiguration applies pre-defined default settings to the VM config.
func (d *Parallels10Driver) SetDefaultConfiguration(vmName string) error {
commands := make([][]string, 12)
commands[0] = []string{"set", vmName, "--cpus", "1"}
commands[1] = []string{"set", vmName, "--memsize", "512"}
commands[2] = []string{"set", vmName, "--startup-view", "same"}
commands[3] = []string{"set", vmName, "--on-shutdown", "close"}
commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[5] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[6] = []string{"set", vmName, "--smart-guard", "off"}
commands[7] = []string{"set", vmName, "--shared-cloud", "off"}
commands[8] = []string{"set", vmName, "--shared-profile", "off"}
commands[9] = []string{"set", vmName, "--smart-mount", "off"}
commands[10] = []string{"set", vmName, "--sh-app-guest-to-host", "off"}
commands[11] = []string{"set", vmName, "--sh-app-host-to-guest", "off"}
commands := make([][]string, 10)
commands[0] = []string{"set", vmName, "--startup-view", "same"}
commands[1] = []string{"set", vmName, "--on-shutdown", "close"}
commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[3] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[4] = []string{"set", vmName, "--smart-guard", "off"}
commands[5] = []string{"set", vmName, "--shared-cloud", "off"}
commands[6] = []string{"set", vmName, "--shared-profile", "off"}
commands[7] = []string{"set", vmName, "--smart-mount", "off"}
commands[8] = []string{"set", vmName, "--sh-app-guest-to-host", "off"}
commands[9] = []string{"set", vmName, "--sh-app-host-to-guest", "off"}
for _, command := range commands {
err := d.Prlctl(command...)

View File

@ -38,19 +38,17 @@ func (d *Parallels11Driver) Verify() error {
// SetDefaultConfiguration applies pre-defined default settings to the VM config.
func (d *Parallels11Driver) SetDefaultConfiguration(vmName string) error {
commands := make([][]string, 12)
commands[0] = []string{"set", vmName, "--cpus", "1"}
commands[1] = []string{"set", vmName, "--memsize", "512"}
commands[2] = []string{"set", vmName, "--startup-view", "headless"}
commands[3] = []string{"set", vmName, "--on-shutdown", "close"}
commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[5] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[6] = []string{"set", vmName, "--smart-guard", "off"}
commands[7] = []string{"set", vmName, "--shared-cloud", "off"}
commands[8] = []string{"set", vmName, "--shared-profile", "off"}
commands[9] = []string{"set", vmName, "--smart-mount", "off"}
commands[10] = []string{"set", vmName, "--sh-app-guest-to-host", "off"}
commands[11] = []string{"set", vmName, "--sh-app-host-to-guest", "off"}
commands := make([][]string, 10)
commands[0] = []string{"set", vmName, "--startup-view", "headless"}
commands[1] = []string{"set", vmName, "--on-shutdown", "close"}
commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[3] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[4] = []string{"set", vmName, "--smart-guard", "off"}
commands[5] = []string{"set", vmName, "--shared-cloud", "off"}
commands[6] = []string{"set", vmName, "--shared-profile", "off"}
commands[7] = []string{"set", vmName, "--smart-mount", "off"}
commands[8] = []string{"set", vmName, "--sh-app-guest-to-host", "off"}
commands[9] = []string{"set", vmName, "--sh-app-host-to-guest", "off"}
for _, command := range commands {
err := d.Prlctl(command...)

View File

@ -331,14 +331,12 @@ func prepend(head string, tail []string) []string {
// SetDefaultConfiguration applies pre-defined default settings to the VM config.
func (d *Parallels9Driver) SetDefaultConfiguration(vmName string) error {
commands := make([][]string, 7)
commands[0] = []string{"set", vmName, "--cpus", "1"}
commands[1] = []string{"set", vmName, "--memsize", "512"}
commands[2] = []string{"set", vmName, "--startup-view", "same"}
commands[3] = []string{"set", vmName, "--on-shutdown", "close"}
commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[5] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[6] = []string{"set", vmName, "--smart-guard", "off"}
commands := make([][]string, 5)
commands[0] = []string{"set", vmName, "--startup-view", "same"}
commands[1] = []string{"set", vmName, "--on-shutdown", "close"}
commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"}
commands[3] = []string{"set", vmName, "--auto-share-camera", "off"}
commands[4] = []string{"set", vmName, "--smart-guard", "off"}
for _, command := range commands {
err := d.Prlctl(command...)

View File

@ -0,0 +1,50 @@
package common
import (
"fmt"
"github.com/hashicorp/packer/template/interpolate"
)
type HWConfig struct {
// cpu information
CpuCount int `mapstructure:"cpu_count"`
MemorySize int `mapstructure:"memory_size"`
// device presence
Sound bool `mapstructure:"sound"`
USB bool `mapstructure:"usb"`
}
func (c *HWConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
// Hardware and cpu options
if c.CpuCount < 0 {
errs = append(errs, fmt.Errorf("An invalid cpu_count was specified (cpu_count < 0): %d", c.CpuCount))
c.CpuCount = 0
}
if c.CpuCount == 0 {
c.CpuCount = 1
}
if c.MemorySize < 0 {
errs = append(errs, fmt.Errorf("An invalid memory_size was specified (memory_size < 0): %d", c.MemorySize))
c.MemorySize = 0
}
if c.MemorySize == 0 {
c.MemorySize = 512
}
// Peripherals
if !c.Sound {
c.Sound = false
}
if !c.USB {
c.USB = false
}
return nil
}

View File

@ -0,0 +1,20 @@
package common
import (
"testing"
)
func TestHWConfigPrepare(t *testing.T) {
c := new(HWConfig)
if errs := c.Prepare(testConfigTemplate(t)); len(errs) > 0 {
t.Fatalf("err: %#v", errs)
}
if c.CpuCount < 1 {
t.Errorf("bad cpu count: %d", c.CpuCount)
}
if c.MemorySize < 64 {
t.Errorf("bad memory size: %d", c.MemorySize)
}
}

View File

@ -29,6 +29,7 @@ type Config struct {
common.FloppyConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
parallelscommon.HWConfig `mapstructure:",squash"`
parallelscommon.PrlctlConfig `mapstructure:",squash"`
parallelscommon.PrlctlPostConfig `mapstructure:",squash"`
parallelscommon.PrlctlVersionConfig `mapstructure:",squash"`
@ -76,6 +77,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(
errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.HWConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.PrlctlConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.PrlctlPostConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.PrlctlVersionConfig.Prepare(&b.config.ctx)...)

View File

@ -3,6 +3,7 @@ package iso
import (
"context"
"fmt"
"strconv"
parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
"github.com/hashicorp/packer/helper/multistep"
@ -24,20 +25,47 @@ func (s *stepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
ui := state.Get("ui").(packer.Ui)
name := config.VMName
command := []string{
commands := make([][]string, 3)
commands[0] = []string{
"create", name,
"--distribution", config.GuestOSType,
"--dst", config.OutputDir,
"--no-hdd",
}
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",
})
}
ui.Say("Creating virtual machine...")
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
}
}
ui.Say("Applying default settings...")
if err := driver.SetDefaultConfiguration(name); err != nil {