Allow to define custom IP resolver

This commit is contained in:
Artur Roszczyk 2013-06-06 21:56:01 +02:00
parent a078935cb0
commit ed76e4b0e3
3 changed files with 25 additions and 8 deletions

View File

@ -5,6 +5,7 @@ module VagrantPlugins
attr_accessor :ignore_private_ip
attr_accessor :aliases
attr_accessor :include_offline
attr_accessor :ip_resolver
alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
@ -14,6 +15,7 @@ module VagrantPlugins
@ignore_private_ip = UNSET_VALUE
@aliases = Array.new
@include_offline = false
@ip_resolver = nil
end
def finalize!
@ -36,7 +38,7 @@ module VagrantPlugins
end
# check if aliases option is an Array
if !machine.config.hostmanager.aliases.kind_of?(Array) and
if !machine.config.hostmanager.aliases.kind_of?(Array) &&
!machine.config.hostmanager.aliases.kind_of?(String)
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
:config_key => 'hostmanager.aliases',
@ -44,6 +46,14 @@ module VagrantPlugins
})
end
if !machine.config.hostmanager.ip_resolver.nil? &&
!machine.config.hostmanager.ip_resolver.kind_of?(Proc)
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
:config_key => 'hostmanager.ip_resolver',
:is_class => ip_resolver.class.to_s,
})
end
errors.compact!
{ "HostManager configuration" => errors }
end

View File

@ -7,17 +7,23 @@ module VagrantPlugins
def generate(env, provider)
machines = []
# define a lambda for looking up a machine's ip address
get_ip_address = lambda do |machine|
ip = nil
if machine.config.hostmanager.ignore_private_ip != true
machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
next if ip
custom_ip_resolver = machine.config.hostmanager.ip_resolver
if custom_ip_resolver
custom_ip_resolver.call(machine)
else
ip = nil
if machine.config.hostmanager.ignore_private_ip != true
machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
next if ip
end
end
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
end
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
end
# create the temporary hosts file

View File

@ -7,3 +7,4 @@ en:
config:
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"
not_an_array_or_string: "A value for %{config_key} must be an Array or String, not type '%{is_class}'"
not_a_proc: "A value for %{config_key} must be a Proc, not type '%{is_class}'"