From cdeb6db3df3cf24241537a86ee30934750ab720e Mon Sep 17 00:00:00 2001 From: Jim Raney Date: Sun, 17 Sep 2017 13:30:32 -0500 Subject: [PATCH 1/4] Added GCE image family handling. --- lib/vagrant-google/action/run_instance.rb | 7 +++++++ lib/vagrant-google/config.rb | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/vagrant-google/action/run_instance.rb b/lib/vagrant-google/action/run_instance.rb index 1e823bc..c045304 100644 --- a/lib/vagrant-google/action/run_instance.rb +++ b/lib/vagrant-google/action/run_instance.rb @@ -43,6 +43,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize # Get the configs zone_config = env[:machine].provider_config.get_zone_config(zone) image = zone_config.image + image_family = zone_config.image_family instance_group = zone_config.instance_group name = zone_config.name machine_type = zone_config.machine_type @@ -62,6 +63,11 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize autodelete_disk = zone_config.autodelete_disk service_accounts = zone_config.service_accounts + # If image_family is set, get the latest image image from the family. + if !image_family.nil? + image = env[:google_compute].images.get_from_family(image_family).name + end + # Launch! env[:ui].info(I18n.t("vagrant_google.launching_instance")) env[:ui].info(" -- Name: #{name}") @@ -70,6 +76,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize env[:ui].info(" -- Disk size: #{disk_size} GB") env[:ui].info(" -- Disk name: #{disk_name}") env[:ui].info(" -- Image: #{image}") + env[:ui].info(" -- Image family: #{image_family}") env[:ui].info(" -- Instance Group: #{instance_group}") env[:ui].info(" -- Zone: #{zone}") if zone env[:ui].info(" -- Network: #{network}") if network diff --git a/lib/vagrant-google/config.rb b/lib/vagrant-google/config.rb index b383580..b9373f8 100644 --- a/lib/vagrant-google/config.rb +++ b/lib/vagrant-google/config.rb @@ -42,6 +42,11 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt # @return [String] attr_accessor :image + # The image family of the instance to use. + # + # @return [String] + attr_accessor :image_family + # The instance group name to put the instance in. # # @return [String] @@ -156,6 +161,7 @@ def initialize(zone_specific=false) @google_json_key_location = UNSET_VALUE @google_project_id = UNSET_VALUE @image = UNSET_VALUE + @image_family = UNSET_VALUE @instance_group = UNSET_VALUE @machine_type = UNSET_VALUE @disk_size = UNSET_VALUE @@ -262,6 +268,9 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC # Image must be nil, since we can't default that @image = "debian-8-jessie-v20160511" if @image == UNSET_VALUE + # Default image family is nil + @image_family = nil if @image_family == UNSET_VALUE + # Default instance group name is nil @instance_group = nil if @instance_group == UNSET_VALUE From b6bbc3f325cb1bb11e26c124716ad9d2b27f5a39 Mon Sep 17 00:00:00 2001 From: Jim Raney Date: Sat, 23 Sep 2017 17:09:21 -0500 Subject: [PATCH 2/4] Changed "image_family" check to use "unless" instead of "if not" logic. --- lib/vagrant-google/action/run_instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-google/action/run_instance.rb b/lib/vagrant-google/action/run_instance.rb index c045304..19fc27d 100644 --- a/lib/vagrant-google/action/run_instance.rb +++ b/lib/vagrant-google/action/run_instance.rb @@ -64,7 +64,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize service_accounts = zone_config.service_accounts # If image_family is set, get the latest image image from the family. - if !image_family.nil? + unless image_family.nil? image = env[:google_compute].images.get_from_family(image_family).name end From f01db0aaafc3d3ee5d89a6473345d7757b9c0dea Mon Sep 17 00:00:00 2001 From: Jim Raney Date: Sat, 23 Sep 2017 18:14:39 -0500 Subject: [PATCH 3/4] Set error condition if both image and image_family are set. --- lib/vagrant-google/config.rb | 19 +++++++++++++++++-- locales/en.yml | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-google/config.rb b/lib/vagrant-google/config.rb index b9373f8..4c434b6 100644 --- a/lib/vagrant-google/config.rb +++ b/lib/vagrant-google/config.rb @@ -266,7 +266,17 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC @google_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE # Image must be nil, since we can't default that - @image = "debian-8-jessie-v20160511" if @image == UNSET_VALUE + if (@image == UNSET_VALUE) && (@image_family == UNSET_VALUE) + @image = "debian-8-jessie-v20160511" + end + + if @image == UNSET_VALUE + if @image_family == UNSET_VALUE + @image = "debian-8-jessie-v20160511" + else + @image = nil + end + end # Default image family is nil @image_family = nil if @image_family == UNSET_VALUE @@ -382,9 +392,14 @@ def validate(machine) errors << I18n.t("vagrant_google.config.on_host_maintenance_invalid_on_preemptible") unless \ config.on_host_maintenance == "TERMINATE" end + + if config.image_family + errors << I18n.t("vagrant_google.config.image_and_image_family_set") if \ + config.image + end end - errors << I18n.t("vagrant_google.config.image_required") if config.image.nil? + errors << I18n.t("vagrant_google.config.image_required") if config.image.nil? && config.image_family.nil? errors << I18n.t("vagrant_google.config.name_required") if @name.nil? { "Google Provider" => errors } diff --git a/locales/en.yml b/locales/en.yml index 3c60c5e..7f4df6c 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -78,6 +78,8 @@ en: on_host_maintenance_invalid_on_preemptible: |- "on_host_maintenance" option must be set to "TERMINATE" for preemptible instances. + image_and_image_family_set: |- + "image" must be unset when setting "image_family" #------------------------------------------------------------------------------- # Translations for exception classes #------------------------------------------------------------------------------- From 98b4ac36ec00bd79b649512bee725236c142c106 Mon Sep 17 00:00:00 2001 From: Jim Raney Date: Sat, 23 Sep 2017 18:23:17 -0500 Subject: [PATCH 4/4] Pulled leftover test code out. --- lib/vagrant-google/config.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/vagrant-google/config.rb b/lib/vagrant-google/config.rb index 4c434b6..66fd388 100644 --- a/lib/vagrant-google/config.rb +++ b/lib/vagrant-google/config.rb @@ -266,10 +266,6 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC @google_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE # Image must be nil, since we can't default that - if (@image == UNSET_VALUE) && (@image_family == UNSET_VALUE) - @image = "debian-8-jessie-v20160511" - end - if @image == UNSET_VALUE if @image_family == UNSET_VALUE @image = "debian-8-jessie-v20160511"