Allow to define custom IP resolver
This commit is contained in:
parent
a078935cb0
commit
ed76e4b0e3
|
@ -5,6 +5,7 @@ module VagrantPlugins
|
||||||
attr_accessor :ignore_private_ip
|
attr_accessor :ignore_private_ip
|
||||||
attr_accessor :aliases
|
attr_accessor :aliases
|
||||||
attr_accessor :include_offline
|
attr_accessor :include_offline
|
||||||
|
attr_accessor :ip_resolver
|
||||||
|
|
||||||
alias_method :enabled?, :enabled
|
alias_method :enabled?, :enabled
|
||||||
alias_method :include_offline?, :include_offline
|
alias_method :include_offline?, :include_offline
|
||||||
|
@ -14,6 +15,7 @@ module VagrantPlugins
|
||||||
@ignore_private_ip = UNSET_VALUE
|
@ignore_private_ip = UNSET_VALUE
|
||||||
@aliases = Array.new
|
@aliases = Array.new
|
||||||
@include_offline = false
|
@include_offline = false
|
||||||
|
@ip_resolver = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def finalize!
|
def finalize!
|
||||||
|
@ -36,7 +38,7 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
|
|
||||||
# check if aliases option is an Array
|
# 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)
|
!machine.config.hostmanager.aliases.kind_of?(String)
|
||||||
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
|
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
|
||||||
:config_key => 'hostmanager.aliases',
|
:config_key => 'hostmanager.aliases',
|
||||||
|
@ -44,6 +46,14 @@ module VagrantPlugins
|
||||||
})
|
})
|
||||||
end
|
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!
|
errors.compact!
|
||||||
{ "HostManager configuration" => errors }
|
{ "HostManager configuration" => errors }
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,8 +7,13 @@ module VagrantPlugins
|
||||||
def generate(env, provider)
|
def generate(env, provider)
|
||||||
machines = []
|
machines = []
|
||||||
|
|
||||||
|
|
||||||
# define a lambda for looking up a machine's ip address
|
# define a lambda for looking up a machine's ip address
|
||||||
get_ip_address = lambda do |machine|
|
get_ip_address = lambda do |machine|
|
||||||
|
custom_ip_resolver = machine.config.hostmanager.ip_resolver
|
||||||
|
if custom_ip_resolver
|
||||||
|
custom_ip_resolver.call(machine)
|
||||||
|
else
|
||||||
ip = nil
|
ip = nil
|
||||||
if machine.config.hostmanager.ignore_private_ip != true
|
if machine.config.hostmanager.ignore_private_ip != true
|
||||||
machine.config.vm.networks.each do |network|
|
machine.config.vm.networks.each do |network|
|
||||||
|
@ -19,6 +24,7 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# create the temporary hosts file
|
# create the temporary hosts file
|
||||||
path = env.tmp_path.join('hosts')
|
path = env.tmp_path.join('hosts')
|
||||||
|
|
|
@ -7,3 +7,4 @@ en:
|
||||||
config:
|
config:
|
||||||
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"
|
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_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}'"
|
||||||
|
|
Loading…
Reference in New Issue