Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method gets the instance public IP when provider is AWS #74

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ At the moment, the only things you need, are the hostname and a :private_network

This ip and the hostname will be used for the entry in the /etc/hosts file.

## For AWS

If you'd like AWS as a provider using [vagrant-aws](https://github.com/mitchellh/vagrant-aws) or other plugin,
this plugin will detect the instance public IP by the tag infomations.
For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures a tag infomations like the following.

config.vm.provider :aws do |aws, override|
aws.tags = {
"Name" => "vagrant",
...
}
aws.elastic_ip = true
...
end

* [AWS CLI](https://aws.amazon.com/cli/) is required
* Make the tag infomations be unique for the instance
* Enable Elastic IP for the instance

## Versions

### 0.0.11
Expand Down
34 changes: 30 additions & 4 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require 'open3'

module VagrantPlugins
module HostsUpdater
module HostsUpdater
@@hosts_path = Vagrant::Util::Platform.windows? ? File.expand_path('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'

def getIps
ips = []
@machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
ips.push(ip) if ip
if ip = getAwsPublicIp
ips.push(ip)
else
@machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
ips.push(ip) if ip
end
end
return ips
end
Expand Down Expand Up @@ -116,6 +122,26 @@ def sudo(command)
`sudo #{command}`
end
end


private

def getAwsPublicIp
aws_conf = @machine.config.vm.get_provider_config(:aws)
return nil if ! aws_conf.is_a?(VagrantPlugins::AWS::Config)
filters = ( aws_conf.tags || [] ).map {|k,v| sprintf('"Name=tag:%s,Values=%s"', k, v) }.join(' ')
return nil if filters == ''
cmd = 'aws ec2 describe-instances --filter '+filters
stdout, stderr, stat = Open3.capture3(cmd)
@ui.error sprintf("Failed to execute '%s' : %s", cmd, stderr) if stderr != ''
return nil if stat.exitstatus != 0
begin
return JSON.parse(stdout)["Reservations"].first()["Instances"].first()["PublicIpAddress"]
rescue => e
@ui.error sprintf("Failed to get IP from the result of '%s' : %s", cmd, e.message)
return nil
end
end
end
end
end