diff --git a/README.md b/README.md index ef7b583..57f4e8d 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,25 @@ Use: config.vm.provision :hostmanager ``` +Custom IP resolver +------------------ + +You can customize way, how host manager resolves IP address +for each machine. This might be handy in case of aws provider, +where host name is stored in ssh_info hash of each machine. +This causes generation of invalid /etc/hosts file. + +Custom IP resolver gives you oportunity to calculate IP address +for each machine by yourself. For example: + +```ruby +config.hostmanager.ip_resolver = proc do |vm| + if hostname = (vm.ssh_info && vm.ssh_info[:host]) + `host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1] + end +end +``` + Contribute ---------- Contributions are welcome. diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index a553d6c..31f293b 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -6,6 +6,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 @@ -17,6 +18,9 @@ module VagrantPlugins @ignore_private_ip = UNSET_VALUE @include_offline = UNSET_VALUE @aliases = [] + @aliases = Array.new + @include_offline = false + @ip_resolver = nil end def finalize! @@ -35,7 +39,8 @@ module VagrantPlugins errors << validate_bool('hostmanager.include_offline', @include_offline) errors.compact! - if !machine.config.hostmanager.aliases.kind_of?(Array) and + # check if aliases option is an Array + 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', @@ -43,7 +48,16 @@ module VagrantPlugins }) end - { 'HostManager configuration' => errors } + 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 private diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index ef644cd..a76d871 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -91,12 +91,17 @@ module VagrantPlugins end def get_ip_address(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 end ip || (machine.ssh_info ? machine.ssh_info[:host] : nil) diff --git a/locales/en.yml b/locales/en.yml index e70efc9..0070d70 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}'"