Skip to content

Commit e0aff2f

Browse files
authored
Merge pull request #177 from milest/add-google-provider
Add google provider
2 parents bdfa1c7 + bb4b232 commit e0aff2f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures
137137
* The tag informations be unique for the instance
138138
* Enable Elastic IP for the instance
139139

140+
## Using Google as a provider
141+
142+
If you'd like a Google provider using [vagrant-google](https://github.com/mitchellh/vagrant-google), this plugin will detect the public IP from the name of the instance.
143+
[vagrant-google](https://github.com/mitchellh/vagrant-google) provides a default name, but you can specify your own as follows:
144+
145+
config.vm.provider :google do |google, override|
146+
google.name = "somename"
147+
...
148+
end
149+
150+
* [Google Cloud SDK](https://cloud.google.com/sdk/) is required.
151+
140152
## Installing development version
141153

142154
If you would like to install vagrant-hostsupdater on the development version perform the following:

lib/vagrant-hostsupdater/HostsUpdater.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def getIps
1616

1717
if ip = getAwsPublicIp
1818
ips.push(ip)
19+
elsif ip = getGooglePublicIp
20+
ips.push(ip)
1921
else
2022
@machine.config.vm.networks.each do |network|
2123
key, options = network[0], network[1]
@@ -236,6 +238,19 @@ def getAwsPublicIp
236238
return nil
237239
end
238240
end
241+
242+
def getGooglePublicIp
243+
return nil if ! defined?(VagrantPlugins::Google)
244+
google_conf = @machine.config.vm.get_provider_config(:google)
245+
return nil if ! google_conf.is_a?(VagrantPlugins::Google::Config)
246+
cmd = 'gcloud compute instances list --filter="name=%s" --format="value(networkInterfaces[0].accessConfigs[0].natIP)"'
247+
cmd = sprintf(cmd, google_conf.name)
248+
stdout, stderr, stat = Open3.capture3(cmd)
249+
@ui.error "Failed to execute '#{cmd}' : #{stderr}" if stderr != ''
250+
ip = stdout.strip
251+
return nil if stat.exitstatus != 0 || ip == nil || ip == ''
252+
return ip
253+
end
239254
end
240255
end
241256
end

0 commit comments

Comments
 (0)