Skip to content

Commit 9bc81fc

Browse files
author
Bryan Cribbs
committed
Merge branch 'sevos-enable_using_private_ips_from_aws'
2 parents cac3589 + 3abecb0 commit 9bc81fc

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ Use:
7272
config.vm.provision :hostmanager
7373
```
7474

75+
Custom IP resolver
76+
------------------
77+
78+
You can customize way, how host manager resolves IP address
79+
for each machine. This might be handy in case of aws provider,
80+
where host name is stored in ssh_info hash of each machine.
81+
This causes generation of invalid /etc/hosts file.
82+
83+
Custom IP resolver gives you oportunity to calculate IP address
84+
for each machine by yourself. For example:
85+
86+
```ruby
87+
config.hostmanager.ip_resolver = proc do |vm|
88+
if hostname = (vm.ssh_info && vm.ssh_info[:host])
89+
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
90+
end
91+
end
92+
```
93+
7594
Contribute
7695
----------
7796
Contributions are welcome.

lib/vagrant-hostmanager/config.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Config < Vagrant.plugin('2', :config)
66
attr_accessor :ignore_private_ip
77
attr_accessor :aliases
88
attr_accessor :include_offline
9+
attr_accessor :ip_resolver
910

1011
alias_method :enabled?, :enabled
1112
alias_method :include_offline?, :include_offline
@@ -17,6 +18,9 @@ def initialize
1718
@ignore_private_ip = UNSET_VALUE
1819
@include_offline = UNSET_VALUE
1920
@aliases = []
21+
@aliases = Array.new
22+
@include_offline = false
23+
@ip_resolver = nil
2024
end
2125

2226
def finalize!
@@ -35,15 +39,25 @@ def validate(machine)
3539
errors << validate_bool('hostmanager.include_offline', @include_offline)
3640
errors.compact!
3741

38-
if !machine.config.hostmanager.aliases.kind_of?(Array) and
42+
# check if aliases option is an Array
43+
if !machine.config.hostmanager.aliases.kind_of?(Array) &&
3944
!machine.config.hostmanager.aliases.kind_of?(String)
4045
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
4146
:config_key => 'hostmanager.aliases',
4247
:is_class => aliases.class.to_s,
4348
})
4449
end
4550

46-
{ 'HostManager configuration' => errors }
51+
if !machine.config.hostmanager.ip_resolver.nil? &&
52+
!machine.config.hostmanager.ip_resolver.kind_of?(Proc)
53+
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
54+
:config_key => 'hostmanager.ip_resolver',
55+
:is_class => ip_resolver.class.to_s,
56+
})
57+
end
58+
59+
errors.compact!
60+
{ "HostManager configuration" => errors }
4761
end
4862

4963
private

lib/vagrant-hostmanager/hosts_file.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,17 @@ def update_file(file)
9191
end
9292

9393
def get_ip_address(machine)
94-
ip = nil
95-
if machine.config.hostmanager.ignore_private_ip != true
96-
machine.config.vm.networks.each do |network|
97-
key, options = network[0], network[1]
98-
ip = options[:ip] if key == :private_network
99-
next if ip
94+
custom_ip_resolver = machine.config.hostmanager.ip_resolver
95+
if custom_ip_resolver
96+
custom_ip_resolver.call(machine)
97+
else
98+
ip = nil
99+
if machine.config.hostmanager.ignore_private_ip != true
100+
machine.config.vm.networks.each do |network|
101+
key, options = network[0], network[1]
102+
ip = options[:ip] if key == :private_network
103+
next if ip
104+
end
100105
end
101106
end
102107
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)

locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ en:
77
config:
88
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"
99
not_an_array_or_string: "A value for %{config_key} must be an Array or String, not type '%{is_class}'"
10+
not_a_proc: "A value for %{config_key} must be a Proc, not type '%{is_class}'"

0 commit comments

Comments
 (0)