diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 5244db3..b1c63a9 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -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 diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 505b82e..b8840e4 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -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 diff --git a/locales/en.yml b/locales/en.yml index 758b7d3..3b0a3df 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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}'"