From 5987795a23aa94dfc9151e457ff0b3f3188dab5d Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Sun, 2 Jul 2017 00:50:01 +0300 Subject: [PATCH] Add 'RAM_reserve_all' parameter --- README.md | 1 + config.go | 2 ++ config_test.go | 16 ++++++++++++---- step_hardware.go | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 417b658e9..9362a2797 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Hardware customization: * `CPU_limit` - Upper limit of available CPU resources in MHz. Inherited from source VM by default, set to `-1` for reset. * `RAM` - Amount of RAM in megabytes. Inherited from source VM by default. * `RAM_reservation` - Amount of reserved RAM in MB. Inherited from source VM by default. +* `RAM_reserve_all` - Reserve all available RAM (bool). `false` by default. Cannot be used together with `RAM_reservation`. Provisioning: * `ssh_username` - [**mandatory**] username in guest OS. diff --git a/config.go b/config.go index 80405a5c5..4f1c22968 100644 --- a/config.go +++ b/config.go @@ -81,6 +81,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, fmt.Errorf("vSphere host is required")) } + errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) + if c.RawShutdownTimeout != "" { timeout, err := time.ParseDuration(c.RawShutdownTimeout) if err != nil { diff --git a/config_test.go b/config_test.go index 46f80662b..4281ef086 100644 --- a/config_test.go +++ b/config_test.go @@ -21,6 +21,14 @@ func TestTimeout(t *testing.T) { } } +func TestRAMReservation(t *testing.T) { + raw := minimalConfig() + raw["RAM_reservation"] = 1000 + raw["RAM_reserve_all"] = true + _, warns, err := NewConfig(raw) + testConfigErr(t, warns, err) +} + func minimalConfig() map[string]interface{} { return map[string]interface{}{ "vcenter_server": "vcenter.domain.local", @@ -36,18 +44,18 @@ func minimalConfig() map[string]interface{} { func testConfigOk(t *testing.T, warns []string, err error) { if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) + t.Fatalf("Should be no warnings: %#v", warns) } if err != nil { - t.Fatalf("bad: %s", err) + t.Fatalf("Unexpected error: %s", err) } } func testConfigErr(t *testing.T, warns []string, err error) { if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) + t.Fatalf("Should be no warnings: %#v", warns) } if err == nil { - t.Fatal("should error") + t.Fatal("An error is not raised") } } diff --git a/step_hardware.go b/step_hardware.go index 5675ab4f5..076fa1c73 100644 --- a/step_hardware.go +++ b/step_hardware.go @@ -6,6 +6,7 @@ import ( "github.com/vmware/govmomi/vim25/types" "context" "github.com/vmware/govmomi/object" + "fmt" ) type HardwareConfig struct { @@ -14,6 +15,17 @@ type HardwareConfig struct { CPULimit int64 `mapstructure:"CPU_limit"` RAM int64 `mapstructure:"RAM"` RAMReservation int64 `mapstructure:"RAM_reservation"` + RAMReserveAll bool `mapstructure:"RAM_reserve_all"` +} + +func (c *HardwareConfig) Prepare() []error { + var errs []error + + if c.RAMReservation > 0 && c.RAMReserveAll != false { + errs = append(errs, fmt.Errorf("'RAM_reservation' and 'RAM_reserve_all' cannot be used together")) + } + + return errs } type StepConfigureHardware struct { @@ -41,6 +53,8 @@ func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepActi ramSpec.Reservation = s.config.RAMReservation confSpec.MemoryAllocation = &ramSpec + confSpec.MemoryReservationLockedToMax = &s.config.RAMReserveAll + task, err := vm.Reconfigure(ctx, confSpec) if err != nil { state.Put("error", err)