Skip to content

Commit

Permalink
Merge pull request #177 from jim-raney-physiq/add-image-family
Browse files Browse the repository at this point in the history
Added GCE image family handling.

Thank you @jim-raney-physiq - LGTM. And thank you (as always) @Temikus!
  • Loading branch information
erjohnso authored Oct 27, 2017
2 parents 55846cb + 98b4ac3 commit 203a6c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/vagrant-google/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
unless 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}")
Expand All @@ -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
Expand Down
24 changes: 22 additions & 2 deletions lib/vagrant-google/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -255,7 +261,16 @@ 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
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

# Default instance group name is nil
@instance_group = nil if @instance_group == UNSET_VALUE
Expand Down Expand Up @@ -365,9 +380,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 }
Expand Down
2 changes: 2 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
#-------------------------------------------------------------------------------
Expand Down

0 comments on commit 203a6c2

Please sign in to comment.