Skip to content

Commit a86df95

Browse files
authored
Merge pull request #180 from KohlsTechnology/update-fog-google
Update to use fog-google v1
2 parents c73200b + 72c4579 commit a86df95

13 files changed

+89
-75
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ pkg/*
99
tags
1010
Gemfile.lock
1111
/vagrant-google/*
12+
vendor/*
1213

1314
# Vagrant
1415
.vagrant
1516
/Vagrantfile*
1617

1718
# Intellij projects folder
18-
.idea
19+
.idea

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 2.0.0 (Release Date TBD)
2+
3+
* Update to use fog-google gem v1
4+
* Drop support for configuration option `google_key_location`(GCP P12 key)
5+
* Add new configuration option `labels` for setting [labels](https://cloud.google.com/compute/docs/labeling-resources) on GCE instances
6+
7+
# 1.0.0 (July 2017)
8+
# 0.2.5 (October 2016)
9+
# 0.2.4 (April 2016)
10+
# 0.2.3 (January 2016)
11+
112
# 0.2.2 (October 2015)
213

314
* Cleanup instance and disks on backend failures [p0deje]

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ This provider exposes quite a few provider-specific configuration options:
165165

166166
* `google_client_email` - The Client Email address for your Service Account.
167167
(Can also be configured with `GOOGLE_CLIENT_EMAIL` environment variable.)
168-
* `google_key_location` - The location of the P12 private key file matching your
169-
Service Account.
170-
(Can also be configured with `GOOGLE_KEY_LOCATION` environment variable.)
171168
* `google_json_key_location` - The location of the JSON private key file matching your
172169
Service Account.
173170
(Can also be configured with `GOOGLE_JSON_KEY_LOCATION` environment variable.)
@@ -182,20 +179,22 @@ This provider exposes quite a few provider-specific configuration options:
182179
* `disk_size` - The disk size in GB. The default is 10.
183180
* `disk_name` - The disk name to use. If the disk exists, it will be reused, otherwise created.
184181
* `disk_type` - Whether to use Standard disk or SSD disk. Use either `pd-ssd` or `pd-standard`.
182+
* `autodelete_disk` - Boolean whether to delete the disk when the instance is deleted or not. Default is true.
185183
* `metadata` - Custom key/value pairs of metadata to add to the instance.
186184
* `name` - The name of your instance. The default is "i-yyyymmddhh-randomsd",
187185
e.g. 10/08/2015 13:15:15 is "i-2015081013-15637fda".
188186
* `network` - The name of the network to use for the instance. Default is
189187
"default".
190188
* `subnetwork` - The name of the subnetwork to use for the instance.
191189
* `tags` - An array of tags to apply to this instance.
190+
* `labels` - Custom key/value pairs of labels to add to the instance.
192191
* `zone` - The zone name where the instance will be created.
193192
* `can_ip_forward` - Boolean whether to enable IP Forwarding.
194193
* `external_ip` - The external IP address to use (supports names). Set to `false` to not assign an external address.
195194
* `use_private_ip` - Boolean whether to use private IP for SSH/provisioning. Default is false.
196195
* `preemptible` - Boolean whether to enable preemptibility. Default is false.
197196
* `auto_restart` - Boolean whether to enable auto_restart. Default is true.
198-
* `on_host_maintenance` - What to do on host maintenance. Default is "MIGRATE".
197+
* `on_host_maintenance` - What to do on host maintenance. Can be set to `MIGRATE` or `TERMINATE` Default is `MIGRATE`.
199198
* `service_accounts` or `scopes` - An array of OAuth2 account scopes for
200199
services that the instance will have access to. Those can be both full API
201200
scopes, just endpoint aliases (the part after `...auth/`), and `gcloud`

lib/vagrant-google/action/assign_instance_groups.rb

+12-10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def call(env)
3434
zone_config = env[:machine].provider_config.get_zone_config(zone)
3535
instance_name = zone_config.name
3636
instance_group_name = zone_config.instance_group
37+
network = zone_config.network
38+
subnetwork = zone_config.subnetwork
3739

3840
if instance_group_name
3941
group = env[:google_compute].instance_groups.get(instance_group_name,
@@ -44,25 +46,25 @@ def call(env)
4446
instance_group_config = {
4547
name: instance_group_name,
4648
zone: zone,
47-
description: "Created by Vagrant"
49+
description: "Created by Vagrant",
50+
network: network,
51+
subnetwork: subnetwork,
4852
}
4953
env[:google_compute].instance_groups.create(instance_group_config)
5054
end
5155

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

55-
response = env[:google_compute].instance_groups.add_instance(
56-
group: instance_group_name,
57-
zone: zone,
58-
instance: instance_name
59+
response = env[:google_compute].add_instance_group_instances(
60+
instance_group_name,
61+
zone,
62+
[instance_name]
5963
)
60-
unless response.body["status"] == "DONE"
61-
operation = env[:google_compute].operations.get(
62-
operation.body["name"], zone
63-
)
64+
unless response.status == "DONE"
65+
operation = env[:google_compute].operations.get(response.name, zone)
6466
env[:ui].info(I18n.t("vagrant_google.waiting_for_operation",
65-
name: operation.body["name"]))
67+
name: operation.name))
6668
operation.wait_for { ready? }
6769
end
6870
end

lib/vagrant-google/action/connect_google.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ def call(env)
3535
:google_project => provider_config.google_project_id,
3636
:google_client_email => provider_config.google_client_email
3737
}
38-
if provider_config.google_json_key_location.nil?
39-
fog_config[:google_key_location] = find_key(provider_config.google_key_location, env)
40-
else
41-
fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
42-
end
38+
39+
fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
4340

4441
@logger.info("Connecting to Google...")
4542
env[:google_compute] = Fog::Compute.new(fog_config)

lib/vagrant-google/action/read_ssh_info.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def read_ssh_info(google, machine)
4747

4848
# Default to use public ip address
4949
ssh_info = {
50-
:host => server.public_ip_address,
50+
:host => server.public_ip_addresses[0],
5151
:port => 22
5252
}
5353

5454
if use_private_ip then
5555
ssh_info = {
56-
:host => server.private_ip_address,
56+
:host => server.private_ip_addresses[0],
5757
:port => 22
5858
}
5959
end

lib/vagrant-google/action/read_state.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def read_state(google, machine)
4343
@logger.info(e.message)
4444
server = nil
4545
end
46-
if server.nil? || [:"shutting-down", :terminated].include?(server.state.to_sym)
46+
if server.nil? || [:"shutting-down", :terminated].include?(server.status.to_sym)
4747
# The machine can't be found
4848
@logger.info("Machine '#{zone}:#{machine.id}' not found or terminated, assuming it got destroyed.")
4949
machine.id = nil
5050
return :not_created
5151
end
5252

5353
# Return the state
54-
return server.state.to_sym
54+
return server.status.to_sym
5555
end
5656
end
5757
end

lib/vagrant-google/action/run_instance.rb

+41-14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
5353
network = zone_config.network
5454
subnetwork = zone_config.subnetwork
5555
metadata = zone_config.metadata
56+
labels = zone_config.labels
5657
tags = zone_config.tags
5758
can_ip_forward = zone_config.can_ip_forward
5859
use_private_ip = zone_config.use_private_ip
@@ -62,15 +63,12 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
6263
on_host_maintenance = zone_config.on_host_maintenance
6364
autodelete_disk = zone_config.autodelete_disk
6465
service_accounts = zone_config.service_accounts
65-
66-
# If image_family is set, get the latest image image from the family.
67-
unless image_family.nil?
68-
image = env[:google_compute].images.get_from_family(image_family).name
69-
end
66+
project_id = zone_config.google_project_id
7067

7168
# Launch!
7269
env[:ui].info(I18n.t("vagrant_google.launching_instance"))
7370
env[:ui].info(" -- Name: #{name}")
71+
env[:ui].info(" -- Project: #{project_id}")
7472
env[:ui].info(" -- Type: #{machine_type}")
7573
env[:ui].info(" -- Disk type: #{disk_type}")
7674
env[:ui].info(" -- Disk size: #{disk_size} GB")
@@ -82,6 +80,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
8280
env[:ui].info(" -- Network: #{network}") if network
8381
env[:ui].info(" -- Subnetwork: #{subnetwork}") if subnetwork
8482
env[:ui].info(" -- Metadata: '#{metadata}'")
83+
env[:ui].info(" -- Labels: '#{labels}'")
8584
env[:ui].info(" -- Tags: '#{tags}'")
8685
env[:ui].info(" -- IP Forward: #{can_ip_forward}")
8786
env[:ui].info(" -- Use private IP: #{use_private_ip}")
@@ -91,6 +90,36 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
9190
env[:ui].info(" -- On Maintenance: #{on_host_maintenance}")
9291
env[:ui].info(" -- Autodelete Disk: #{autodelete_disk}")
9392
env[:ui].info(" -- Scopes: #{service_accounts}")
93+
94+
# Munge image configs
95+
image = env[:google_compute].images.get(image).self_link
96+
97+
# If image_family is set, get the latest image image from the family.
98+
unless image_family.nil?
99+
image = env[:google_compute].images.get_from_family(image_family).self_link
100+
end
101+
102+
# Munge network configs
103+
if network != 'default'
104+
network = "projects/#{project_id}/global/networks/#{network}"
105+
subnetwork = "projects/#{project_id}/regions/#{zone.split('-')[0..1].join('-')}/subnetworks/#{subnetwork}"
106+
else
107+
network = "global/networks/default"
108+
end
109+
110+
if external_ip == false
111+
# No external IP
112+
network_interfaces = [ { :network => network, :subnetwork => subnetwork } ]
113+
else
114+
network_interfaces = [ { :network => network, :subnetwork => subnetwork, :access_configs => [{:name => 'External NAT', :type => 'ONE_TO_ONE_NAT'}]} ]
115+
end
116+
117+
# Munge scheduling configs
118+
scheduling = { :automatic_restart => auto_restart, :on_host_maintenance => on_host_maintenance, :preemptible => preemptible}
119+
120+
# Munge service_accounts / scopes config
121+
service_accounts = [ { :scopes => service_accounts } ]
122+
94123
begin
95124
request_start_time = Time.now.to_i
96125

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

130159
defaults = {
131160
:name => name,
132-
:zone_name => zone,
161+
:zone => zone,
133162
:machine_type => machine_type,
134163
:disk_size => disk_size,
135164
:disk_type => disk_type,
136165
:image => image,
137-
:network => network,
138-
:subnetwork => subnetwork,
139-
:metadata => metadata,
140-
:tags => tags,
166+
:network_interfaces => network_interfaces,
167+
:metadata => { :items => metadata.each.map { |k, v| {:key => k.to_s, :value => v.to_s} } },
168+
:labels => labels,
169+
:tags => { :items => tags },
141170
:can_ip_forward => can_ip_forward,
142171
:use_private_ip => use_private_ip,
143172
:external_ip => external_ip,
144-
:preemptible => preemptible,
145-
:auto_restart => auto_restart,
146-
:on_host_maintenance => on_host_maintenance,
147173
:disks => [disk.get_as_boot_disk(true, autodelete_disk)],
174+
:scheduling => scheduling,
148175
:service_accounts => service_accounts
149176
}
150177
server = env[:google_compute].servers.create(defaults)
@@ -212,7 +239,7 @@ def terminate(env)
212239
def get_disk_type(env, disk_type, zone)
213240
begin
214241
# TODO(temikus): Outsource parsing logic to fog-google
215-
disk_type = env[:google_compute].get_disk_type(disk_type, zone).body["selfLink"]
242+
disk_type = env[:google_compute].get_disk_type(disk_type, zone).self_link
216243
rescue Fog::Errors::NotFound
217244
raise Errors::DiskTypeError,
218245
:disktype => disk_type

lib/vagrant-google/config.rb

+7-11
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
2222
# @return [String]
2323
attr_accessor :google_client_email
2424

25-
# The path to the Service Account private key
26-
#
27-
# @return [String]
28-
attr_accessor :google_key_location
29-
3025
# The path to the Service Account json-formatted private key
3126
#
3227
# @return [String]
@@ -97,6 +92,11 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
9792
# @return [Array]
9893
attr_accessor :tags
9994

95+
# Labels to apply to the instance
96+
#
97+
# @return [Hash<String, String>]
98+
attr_accessor :labels
99+
100100
# whether to enable ip forwarding
101101
#
102102
# @return Boolean
@@ -157,7 +157,6 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
157157

158158
def initialize(zone_specific=false)
159159
@google_client_email = UNSET_VALUE
160-
@google_key_location = UNSET_VALUE
161160
@google_json_key_location = UNSET_VALUE
162161
@google_project_id = UNSET_VALUE
163162
@image = UNSET_VALUE
@@ -172,6 +171,7 @@ def initialize(zone_specific=false)
172171
@network = UNSET_VALUE
173172
@subnetwork = UNSET_VALUE
174173
@tags = []
174+
@labels = {}
175175
@can_ip_forward = UNSET_VALUE
176176
@external_ip = UNSET_VALUE
177177
@use_private_ip = UNSET_VALUE
@@ -256,7 +256,6 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC
256256
# Try to get access keys from standard Google environment variables; they
257257
# will default to nil if the environment variables are not present.
258258
@google_client_email = ENV['GOOGLE_CLIENT_EMAIL'] if @google_client_email == UNSET_VALUE
259-
@google_key_location = ENV['GOOGLE_KEY_LOCATION'] if @google_key_location == UNSET_VALUE
260259
@google_json_key_location = ENV['GOOGLE_JSON_KEY_LOCATION'] if @google_json_key_location == UNSET_VALUE
261260
@google_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE
262261

@@ -366,12 +365,9 @@ def validate(machine)
366365
config.google_project_id.nil?
367366
errors << I18n.t("vagrant_google.config.google_client_email_required") if \
368367
config.google_client_email.nil?
369-
errors << I18n.t("vagrant_google.config.google_duplicate_key_location") if \
370-
!config.google_key_location.nil? and !config.google_json_key_location.nil?
371368
errors << I18n.t("vagrant_google.config.google_key_location_required") if \
372-
config.google_key_location.nil? and config.google_json_key_location.nil?
369+
config.google_json_key_location.nil?
373370
errors << I18n.t("vagrant_google.config.private_key_missing") unless \
374-
File.exist?(config.google_key_location.to_s) or \
375371
File.exist?(config.google_json_key_location.to_s)
376372

377373
if config.preemptible

locales/en.yml

-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ en:
6666
google_key_location_required: |-
6767
A private key pathname is required via:
6868
"google_json_key_location" (for JSON keys)
69-
or
70-
"google_key_location" (for P12 keys)
71-
google_duplicate_key_location: |-
72-
Both "google_json_key_location" and "google_key_location" are specified.
73-
Config must specify only one key location.
7469
google_project_id_required: |-
7570
A Google Cloud Project ID is required via "google_project_id".
7671
auto_restart_invalid_on_preemptible: |-

tasks/acceptance.rake

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ namespace :acceptance do
2929
task :check_env do
3030
yellow "NOTE: For acceptance tests to be functional, correct ssh key needs to be added to GCE metadata."
3131

32-
if !ENV["GOOGLE_JSON_KEY_LOCATION"] && !ENV["GOOGLE_KEY_LOCATION"]
33-
abort "Environment variables GOOGLE_JSON_KEY_LOCATION or GOOGLE_KEY_LOCATION are not set. Aborting."
32+
if !ENV["GOOGLE_JSON_KEY_LOCATION"]
33+
abort "Environment variables GOOGLE_JSON_KEY_LOCATION is not set. Aborting."
3434
end
3535

3636
unless ENV["GOOGLE_PROJECT_ID"]

0 commit comments

Comments
 (0)