From bab590c177f7dde893b2e8c38fdd01ea952d05b3 Mon Sep 17 00:00:00 2001 From: Dimitri Rudnev Date: Fri, 10 Feb 2017 10:58:57 -0800 Subject: [PATCH 1/3] Adding OnHostMaintenance option for googlecompue builder --- builder/googlecompute/config.go | 7 +++++++ builder/googlecompute/config_test.go | 15 +++++++++++++++ builder/googlecompute/driver.go | 1 + builder/googlecompute/driver_gce.go | 3 ++- builder/googlecompute/step_create_instance.go | 1 + 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 460437b6e..8fa374306 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -39,6 +39,7 @@ type Config struct { Network string `mapstructure:"network"` NetworkProjectId string `mapstructure:"network_project_id"` OmitExternalIP bool `mapstructure:"omit_external_ip"` + OnHostMaintenance string `mapstructure:"on_host_maintenance"` Preemptible bool `mapstructure:"preemptible"` RawStateTimeout string `mapstructure:"state_timeout"` Region string `mapstructure:"region"` @@ -92,6 +93,12 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { if c.ImageDescription == "" { c.ImageDescription = "Created by Packer" } + // Possible values: + // "MIGRATE" + // "TERMINATE" + if c.OnHostMaintenance == "" { + c.OnHostMaintenance = "MIGRATE" + } if c.ImageName == "" { img, err := interpolate.Render("packer-{{timestamp}}", nil) diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index c2f469c3d..210601948 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -104,6 +104,21 @@ func TestConfigPrepare(t *testing.T) { "SO VERY BAD", true, }, + { + "on_host_maintenance", + nil, + false, + }, + { + "on_host_maintenance", + "TERMINATE", + false, + }, + { + "on_host_maintenance", + "SO VERY BAD", + false, + }, { "preemptible", nil, diff --git a/builder/googlecompute/driver.go b/builder/googlecompute/driver.go index 1452082bc..d55d267b7 100644 --- a/builder/googlecompute/driver.go +++ b/builder/googlecompute/driver.go @@ -69,6 +69,7 @@ type InstanceConfig struct { Network string NetworkProjectId string OmitExternalIP bool + OnHostMaintenance string Preemptible bool Region string Scopes []string diff --git a/builder/googlecompute/driver_gce.go b/builder/googlecompute/driver_gce.go index bce3fb391..c535c139c 100644 --- a/builder/googlecompute/driver_gce.go +++ b/builder/googlecompute/driver_gce.go @@ -386,7 +386,8 @@ func (d *driverGCE) RunInstance(c *InstanceConfig) (<-chan error, error) { }, }, Scheduling: &compute.Scheduling{ - Preemptible: c.Preemptible, + OnHostMaintenance: c.OnHostMaintenance, + Preemptible: c.Preemptible, }, ServiceAccounts: []*compute.ServiceAccount{ { diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index d0dcba18f..3f70d422f 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -110,6 +110,7 @@ func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction Network: c.Network, NetworkProjectId: c.NetworkProjectId, OmitExternalIP: c.OmitExternalIP, + OnHostMaintenance: c.OnHostMaintenance, Preemptible: c.Preemptible, Region: c.Region, ServiceAccountEmail: c.Account.ClientEmail, From 2009fcd9dfe854ec9ebac5355dc5480427f20b19 Mon Sep 17 00:00:00 2001 From: Dimitri Rudnev Date: Fri, 10 Feb 2017 11:57:15 -0800 Subject: [PATCH 2/3] OnHostMaintenance, googlecompute updating documenation , additional option for validation and setting defaults --- builder/googlecompute/config.go | 18 ++++++++++++++---- builder/googlecompute/config_test.go | 2 +- .../source/docs/builders/googlecompute.html.md | 7 +++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 8fa374306..78141bed4 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -93,13 +93,23 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { if c.ImageDescription == "" { c.ImageDescription = "Created by Packer" } - // Possible values: - // "MIGRATE" - // "TERMINATE" - if c.OnHostMaintenance == "" { + // Setting OnHostMaintenance Correct Defaults + // "MIGRATE" : Possible if Preemptible is false + // "TERMINATE": Posssible if Preemptible is true + if c.OnHostMaintenance == "" && c.Preemptible { c.OnHostMaintenance = "MIGRATE" } + if c.OnHostMaintenance == "" && !c.Preemptible { + c.OnHostMaintenance = "TERMINATE" + } + + // Make sure user sets a valid value for on_host_maintenance option + if !(c.OnHostMaintenance == "MIGRATE" || c.OnHostMaintenance == "TERMINATE") { + errs = packer.MultiErrorAppend(errs, + errors.New("on_host_maintenance must be one of MIGRATE or TERMINATE.")) + } + if c.ImageName == "" { img, err := interpolate.Render("packer-{{timestamp}}", nil) if err != nil { diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index 210601948..e65da2794 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -117,7 +117,7 @@ func TestConfigPrepare(t *testing.T) { { "on_host_maintenance", "SO VERY BAD", - false, + true, }, { "preemptible", diff --git a/website/source/docs/builders/googlecompute.html.md b/website/source/docs/builders/googlecompute.html.md index a54099418..571fe1b6c 100644 --- a/website/source/docs/builders/googlecompute.html.md +++ b/website/source/docs/builders/googlecompute.html.md @@ -178,6 +178,13 @@ builder. `use_internal_ip` must be true if this property is true. - `preemptible` (boolean) - If true, launch a preembtible instance. +- 'on_host_maintenance' (string) - Sets Host Maintenance Option + valid strings "MIGRATE" and "TERMINATE" please see + [GCE Instance Scheduling Options](https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options) + Not all machine_type in google support MIGRATE (machines with gpu) + also preemptiblity will impact available options + - preemptible == true , defaults to TERMINATE + - preemptible == false , defaults to MIGRATE - `region` (string) - The region in which to launch the instance. Defaults to to the region hosting the specified `zone`. From 6eaf8f4559aeb62c49348a8b99d202343f128a35 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 10 Feb 2017 12:11:08 -0800 Subject: [PATCH 3/3] tighten up docs --- .../source/docs/builders/googlecompute.html.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/website/source/docs/builders/googlecompute.html.md b/website/source/docs/builders/googlecompute.html.md index 571fe1b6c..97dac1a70 100644 --- a/website/source/docs/builders/googlecompute.html.md +++ b/website/source/docs/builders/googlecompute.html.md @@ -178,13 +178,14 @@ builder. `use_internal_ip` must be true if this property is true. - `preemptible` (boolean) - If true, launch a preembtible instance. -- 'on_host_maintenance' (string) - Sets Host Maintenance Option - valid strings "MIGRATE" and "TERMINATE" please see - [GCE Instance Scheduling Options](https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options) - Not all machine_type in google support MIGRATE (machines with gpu) - also preemptiblity will impact available options - - preemptible == true , defaults to TERMINATE - - preemptible == false , defaults to MIGRATE + +- `on_host_maintenance` (string) - Sets Host Maintenance Option. Valid + choices are `MIGRATE` and `TERMINATE`. Please see [GCE Instance Scheduling + Options](https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options), + as not all machine_types support `MIGRATE` (i.e. machines with GPUs). The + default value depends on preemtability. + - when preemptible == true, defaults to `TERMINATE` + - when preemptible == false, defaults to `MIGRATE` - `region` (string) - The region in which to launch the instance. Defaults to to the region hosting the specified `zone`.