diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 460437b6e..78141bed4 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,22 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { if c.ImageDescription == "" { c.ImageDescription = "Created by Packer" } + // 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) diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index c2f469c3d..e65da2794 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", + true, + }, { "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, diff --git a/website/source/docs/builders/googlecompute.html.md b/website/source/docs/builders/googlecompute.html.md index a54099418..97dac1a70 100644 --- a/website/source/docs/builders/googlecompute.html.md +++ b/website/source/docs/builders/googlecompute.html.md @@ -179,6 +179,14 @@ builder. - `preemptible` (boolean) - If true, launch a preembtible instance. +- `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`.