From 71d15d05c06c6537bb7b697eceff2c95549a101b Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 05:16:14 -0600 Subject: [PATCH] Added support for the cpu_count, memory_size, sound, and usb options to the parallels builder. --- builder/parallels/common/driver_10.go | 24 +++++------ builder/parallels/common/driver_11.go | 24 +++++------ builder/parallels/common/driver_9.go | 14 +++--- builder/parallels/common/hw_config.go | 50 ++++++++++++++++++++++ builder/parallels/common/hw_config_test.go | 20 +++++++++ builder/parallels/iso/builder.go | 2 + builder/parallels/iso/step_create_vm.go | 40 ++++++++++++++--- 7 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 builder/parallels/common/hw_config.go create mode 100644 builder/parallels/common/hw_config_test.go diff --git a/builder/parallels/common/driver_10.go b/builder/parallels/common/driver_10.go index be62251d5..cdcf0f4d5 100644 --- a/builder/parallels/common/driver_10.go +++ b/builder/parallels/common/driver_10.go @@ -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...) diff --git a/builder/parallels/common/driver_11.go b/builder/parallels/common/driver_11.go index 09ebdeeb5..e167b7837 100644 --- a/builder/parallels/common/driver_11.go +++ b/builder/parallels/common/driver_11.go @@ -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...) diff --git a/builder/parallels/common/driver_9.go b/builder/parallels/common/driver_9.go index 5634fb123..67600523a 100644 --- a/builder/parallels/common/driver_9.go +++ b/builder/parallels/common/driver_9.go @@ -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...) diff --git a/builder/parallels/common/hw_config.go b/builder/parallels/common/hw_config.go new file mode 100644 index 000000000..1f73f88ab --- /dev/null +++ b/builder/parallels/common/hw_config.go @@ -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 +} diff --git a/builder/parallels/common/hw_config_test.go b/builder/parallels/common/hw_config_test.go new file mode 100644 index 000000000..079868a7e --- /dev/null +++ b/builder/parallels/common/hw_config_test.go @@ -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) + } +} diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index b3609cf7f..6d91b2a86 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -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)...) diff --git a/builder/parallels/iso/step_create_vm.go b/builder/parallels/iso/step_create_vm.go index 8a4e22517..6ec9049eb 100644 --- a/builder/parallels/iso/step_create_vm.go +++ b/builder/parallels/iso/step_create_vm.go @@ -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,19 +25,46 @@ 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...") - 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 + 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...")