From ed76e4b0e35684efca0b809a7b01e6f2f3b8b06b Mon Sep 17 00:00:00 2001 From: Artur Roszczyk Date: Thu, 6 Jun 2013 21:56:01 +0200 Subject: [PATCH 1/2] Allow to define custom IP resolver --- lib/vagrant-hostmanager/config.rb | 12 +++++++++++- lib/vagrant-hostmanager/hosts_file.rb | 20 +++++++++++++------- locales/en.yml | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) 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}'" From 7fd5ceeb053d3bac08bbbe4846023a2f78e0edb7 Mon Sep 17 00:00:00 2001 From: Artur Roszczyk Date: Sat, 8 Jun 2013 14:46:15 +0300 Subject: [PATCH 2/2] Update README.md Add description of custom IP resolver --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 0f11188..e9f1184 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,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.