Skip to content

Commit

Permalink
Merge pull request #180 from KohlsTechnology/update-fog-google
Browse files Browse the repository at this point in the history
Update to use fog-google v1
  • Loading branch information
erjohnso authored Mar 20, 2018
2 parents c73200b + 72c4579 commit a86df95
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ pkg/*
tags
Gemfile.lock
/vagrant-google/*
vendor/*

# Vagrant
.vagrant
/Vagrantfile*

# Intellij projects folder
.idea
.idea
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 2.0.0 (Release Date TBD)

* Update to use fog-google gem v1
* Drop support for configuration option `google_key_location`(GCP P12 key)
* Add new configuration option `labels` for setting [labels](https://cloud.google.com/compute/docs/labeling-resources) on GCE instances

# 1.0.0 (July 2017)
# 0.2.5 (October 2016)
# 0.2.4 (April 2016)
# 0.2.3 (January 2016)

# 0.2.2 (October 2015)

* Cleanup instance and disks on backend failures [p0deje]
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ This provider exposes quite a few provider-specific configuration options:

* `google_client_email` - The Client Email address for your Service Account.
(Can also be configured with `GOOGLE_CLIENT_EMAIL` environment variable.)
* `google_key_location` - The location of the P12 private key file matching your
Service Account.
(Can also be configured with `GOOGLE_KEY_LOCATION` environment variable.)
* `google_json_key_location` - The location of the JSON private key file matching your
Service Account.
(Can also be configured with `GOOGLE_JSON_KEY_LOCATION` environment variable.)
Expand All @@ -182,20 +179,22 @@ This provider exposes quite a few provider-specific configuration options:
* `disk_size` - The disk size in GB. The default is 10.
* `disk_name` - The disk name to use. If the disk exists, it will be reused, otherwise created.
* `disk_type` - Whether to use Standard disk or SSD disk. Use either `pd-ssd` or `pd-standard`.
* `autodelete_disk` - Boolean whether to delete the disk when the instance is deleted or not. Default is true.
* `metadata` - Custom key/value pairs of metadata to add to the instance.
* `name` - The name of your instance. The default is "i-yyyymmddhh-randomsd",
e.g. 10/08/2015 13:15:15 is "i-2015081013-15637fda".
* `network` - The name of the network to use for the instance. Default is
"default".
* `subnetwork` - The name of the subnetwork to use for the instance.
* `tags` - An array of tags to apply to this instance.
* `labels` - Custom key/value pairs of labels to add to the instance.
* `zone` - The zone name where the instance will be created.
* `can_ip_forward` - Boolean whether to enable IP Forwarding.
* `external_ip` - The external IP address to use (supports names). Set to `false` to not assign an external address.
* `use_private_ip` - Boolean whether to use private IP for SSH/provisioning. Default is false.
* `preemptible` - Boolean whether to enable preemptibility. Default is false.
* `auto_restart` - Boolean whether to enable auto_restart. Default is true.
* `on_host_maintenance` - What to do on host maintenance. Default is "MIGRATE".
* `on_host_maintenance` - What to do on host maintenance. Can be set to `MIGRATE` or `TERMINATE` Default is `MIGRATE`.
* `service_accounts` or `scopes` - An array of OAuth2 account scopes for
services that the instance will have access to. Those can be both full API
scopes, just endpoint aliases (the part after `...auth/`), and `gcloud`
Expand Down
22 changes: 12 additions & 10 deletions lib/vagrant-google/action/assign_instance_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def call(env)
zone_config = env[:machine].provider_config.get_zone_config(zone)
instance_name = zone_config.name
instance_group_name = zone_config.instance_group
network = zone_config.network
subnetwork = zone_config.subnetwork

if instance_group_name
group = env[:google_compute].instance_groups.get(instance_group_name,
Expand All @@ -44,25 +46,25 @@ def call(env)
instance_group_config = {
name: instance_group_name,
zone: zone,
description: "Created by Vagrant"
description: "Created by Vagrant",
network: network,
subnetwork: subnetwork,
}
env[:google_compute].instance_groups.create(instance_group_config)
end

# Add the machine to instance group
env[:ui].info(I18n.t("vagrant_google.instance_group_add"))

response = env[:google_compute].instance_groups.add_instance(
group: instance_group_name,
zone: zone,
instance: instance_name
response = env[:google_compute].add_instance_group_instances(
instance_group_name,
zone,
[instance_name]
)
unless response.body["status"] == "DONE"
operation = env[:google_compute].operations.get(
operation.body["name"], zone
)
unless response.status == "DONE"
operation = env[:google_compute].operations.get(response.name, zone)
env[:ui].info(I18n.t("vagrant_google.waiting_for_operation",
name: operation.body["name"]))
name: operation.name))
operation.wait_for { ready? }
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/vagrant-google/action/connect_google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ def call(env)
:google_project => provider_config.google_project_id,
:google_client_email => provider_config.google_client_email
}
if provider_config.google_json_key_location.nil?
fog_config[:google_key_location] = find_key(provider_config.google_key_location, env)
else
fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
end

fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)

@logger.info("Connecting to Google...")
env[:google_compute] = Fog::Compute.new(fog_config)
Expand Down
4 changes: 2 additions & 2 deletions lib/vagrant-google/action/read_ssh_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def read_ssh_info(google, machine)

# Default to use public ip address
ssh_info = {
:host => server.public_ip_address,
:host => server.public_ip_addresses[0],
:port => 22
}

if use_private_ip then
ssh_info = {
:host => server.private_ip_address,
:host => server.private_ip_addresses[0],
:port => 22
}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/vagrant-google/action/read_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def read_state(google, machine)
@logger.info(e.message)
server = nil
end
if server.nil? || [:"shutting-down", :terminated].include?(server.state.to_sym)
if server.nil? || [:"shutting-down", :terminated].include?(server.status.to_sym)
# The machine can't be found
@logger.info("Machine '#{zone}:#{machine.id}' not found or terminated, assuming it got destroyed.")
machine.id = nil
return :not_created
end

# Return the state
return server.state.to_sym
return server.status.to_sym
end
end
end
Expand Down
55 changes: 41 additions & 14 deletions lib/vagrant-google/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
network = zone_config.network
subnetwork = zone_config.subnetwork
metadata = zone_config.metadata
labels = zone_config.labels
tags = zone_config.tags
can_ip_forward = zone_config.can_ip_forward
use_private_ip = zone_config.use_private_ip
Expand All @@ -62,15 +63,12 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
on_host_maintenance = zone_config.on_host_maintenance
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
project_id = zone_config.google_project_id

# Launch!
env[:ui].info(I18n.t("vagrant_google.launching_instance"))
env[:ui].info(" -- Name: #{name}")
env[:ui].info(" -- Project: #{project_id}")
env[:ui].info(" -- Type: #{machine_type}")
env[:ui].info(" -- Disk type: #{disk_type}")
env[:ui].info(" -- Disk size: #{disk_size} GB")
Expand All @@ -82,6 +80,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
env[:ui].info(" -- Network: #{network}") if network
env[:ui].info(" -- Subnetwork: #{subnetwork}") if subnetwork
env[:ui].info(" -- Metadata: '#{metadata}'")
env[:ui].info(" -- Labels: '#{labels}'")
env[:ui].info(" -- Tags: '#{tags}'")
env[:ui].info(" -- IP Forward: #{can_ip_forward}")
env[:ui].info(" -- Use private IP: #{use_private_ip}")
Expand All @@ -91,6 +90,36 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
env[:ui].info(" -- On Maintenance: #{on_host_maintenance}")
env[:ui].info(" -- Autodelete Disk: #{autodelete_disk}")
env[:ui].info(" -- Scopes: #{service_accounts}")

# Munge image configs
image = env[:google_compute].images.get(image).self_link

# 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).self_link
end

# Munge network configs
if network != 'default'
network = "projects/#{project_id}/global/networks/#{network}"
subnetwork = "projects/#{project_id}/regions/#{zone.split('-')[0..1].join('-')}/subnetworks/#{subnetwork}"
else
network = "global/networks/default"
end

if external_ip == false
# No external IP
network_interfaces = [ { :network => network, :subnetwork => subnetwork } ]
else
network_interfaces = [ { :network => network, :subnetwork => subnetwork, :access_configs => [{:name => 'External NAT', :type => 'ONE_TO_ONE_NAT'}]} ]
end

# Munge scheduling configs
scheduling = { :automatic_restart => auto_restart, :on_host_maintenance => on_host_maintenance, :preemptible => preemptible}

# Munge service_accounts / scopes config
service_accounts = [ { :scopes => service_accounts } ]

begin
request_start_time = Time.now.to_i

Expand Down Expand Up @@ -129,22 +158,20 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize

defaults = {
:name => name,
:zone_name => zone,
:zone => zone,
:machine_type => machine_type,
:disk_size => disk_size,
:disk_type => disk_type,
:image => image,
:network => network,
:subnetwork => subnetwork,
:metadata => metadata,
:tags => tags,
:network_interfaces => network_interfaces,
:metadata => { :items => metadata.each.map { |k, v| {:key => k.to_s, :value => v.to_s} } },
:labels => labels,
:tags => { :items => tags },
:can_ip_forward => can_ip_forward,
:use_private_ip => use_private_ip,
:external_ip => external_ip,
:preemptible => preemptible,
:auto_restart => auto_restart,
:on_host_maintenance => on_host_maintenance,
:disks => [disk.get_as_boot_disk(true, autodelete_disk)],
:scheduling => scheduling,
:service_accounts => service_accounts
}
server = env[:google_compute].servers.create(defaults)
Expand Down Expand Up @@ -212,7 +239,7 @@ def terminate(env)
def get_disk_type(env, disk_type, zone)
begin
# TODO(temikus): Outsource parsing logic to fog-google
disk_type = env[:google_compute].get_disk_type(disk_type, zone).body["selfLink"]
disk_type = env[:google_compute].get_disk_type(disk_type, zone).self_link
rescue Fog::Errors::NotFound
raise Errors::DiskTypeError,
:disktype => disk_type
Expand Down
18 changes: 7 additions & 11 deletions lib/vagrant-google/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
# @return [String]
attr_accessor :google_client_email

# The path to the Service Account private key
#
# @return [String]
attr_accessor :google_key_location

# The path to the Service Account json-formatted private key
#
# @return [String]
Expand Down Expand Up @@ -97,6 +92,11 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
# @return [Array]
attr_accessor :tags

# Labels to apply to the instance
#
# @return [Hash<String, String>]
attr_accessor :labels

# whether to enable ip forwarding
#
# @return Boolean
Expand Down Expand Up @@ -157,7 +157,6 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt

def initialize(zone_specific=false)
@google_client_email = UNSET_VALUE
@google_key_location = UNSET_VALUE
@google_json_key_location = UNSET_VALUE
@google_project_id = UNSET_VALUE
@image = UNSET_VALUE
Expand All @@ -172,6 +171,7 @@ def initialize(zone_specific=false)
@network = UNSET_VALUE
@subnetwork = UNSET_VALUE
@tags = []
@labels = {}
@can_ip_forward = UNSET_VALUE
@external_ip = UNSET_VALUE
@use_private_ip = UNSET_VALUE
Expand Down Expand Up @@ -256,7 +256,6 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC
# Try to get access keys from standard Google environment variables; they
# will default to nil if the environment variables are not present.
@google_client_email = ENV['GOOGLE_CLIENT_EMAIL'] if @google_client_email == UNSET_VALUE
@google_key_location = ENV['GOOGLE_KEY_LOCATION'] if @google_key_location == UNSET_VALUE
@google_json_key_location = ENV['GOOGLE_JSON_KEY_LOCATION'] if @google_json_key_location == UNSET_VALUE
@google_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE

Expand Down Expand Up @@ -366,12 +365,9 @@ def validate(machine)
config.google_project_id.nil?
errors << I18n.t("vagrant_google.config.google_client_email_required") if \
config.google_client_email.nil?
errors << I18n.t("vagrant_google.config.google_duplicate_key_location") if \
!config.google_key_location.nil? and !config.google_json_key_location.nil?
errors << I18n.t("vagrant_google.config.google_key_location_required") if \
config.google_key_location.nil? and config.google_json_key_location.nil?
config.google_json_key_location.nil?
errors << I18n.t("vagrant_google.config.private_key_missing") unless \
File.exist?(config.google_key_location.to_s) or \
File.exist?(config.google_json_key_location.to_s)

if config.preemptible
Expand Down
5 changes: 0 additions & 5 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ en:
google_key_location_required: |-
A private key pathname is required via:
"google_json_key_location" (for JSON keys)
or
"google_key_location" (for P12 keys)
google_duplicate_key_location: |-
Both "google_json_key_location" and "google_key_location" are specified.
Config must specify only one key location.
google_project_id_required: |-
A Google Cloud Project ID is required via "google_project_id".
auto_restart_invalid_on_preemptible: |-
Expand Down
4 changes: 2 additions & 2 deletions tasks/acceptance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace :acceptance do
task :check_env do
yellow "NOTE: For acceptance tests to be functional, correct ssh key needs to be added to GCE metadata."

if !ENV["GOOGLE_JSON_KEY_LOCATION"] && !ENV["GOOGLE_KEY_LOCATION"]
abort "Environment variables GOOGLE_JSON_KEY_LOCATION or GOOGLE_KEY_LOCATION are not set. Aborting."
if !ENV["GOOGLE_JSON_KEY_LOCATION"]
abort "Environment variables GOOGLE_JSON_KEY_LOCATION is not set. Aborting."
end

unless ENV["GOOGLE_PROJECT_ID"]
Expand Down
Loading

0 comments on commit a86df95

Please sign in to comment.