From ed0fe89dd0900c3198cf68f3f97cc1c283a08f97 Mon Sep 17 00:00:00 2001 From: Lukasz Jastrzebski Date: Sun, 15 Dec 2013 17:11:51 -0800 Subject: [PATCH 1/4] fixing etc hosts handling to avoid duplicates --- lib/vagrant-hostmanager/hosts_file.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index e7f0f80..54863e2 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -60,6 +60,7 @@ module VagrantPlugins entries = [] destroyed_entries = [] ids = [] + hosts_aliases = [] get_machines.each do |name, p| if @provider == p machine = @global_env.machine(name, p) @@ -70,8 +71,9 @@ module VagrantPlugins if id.nil? destroyed_entries << "#{ip}\t#{host} #{aliases}" else - entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" + entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" ids << id unless ids.include?(id) + hosts_aliases << "#{host} #{aliases}" end end end @@ -82,6 +84,7 @@ module VagrantPlugins File.open(file).each_line do |line| # Eliminate lines for machines that have been destroyed next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ } + next if hosts_aliases.any? { |host_aliases| line =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s#{host_aliases}\s# VAGRANT ID:\s.*/} tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ } end From 24dc1c8ed6eb6c6fc45dc7acf5af1a429ea0d162 Mon Sep 17 00:00:00 2001 From: Lukasz Jastrzebski Date: Thu, 19 Dec 2013 14:25:05 -0800 Subject: [PATCH 2/4] fixing incorrect merge --- lib/vagrant-hostmanager/hosts_file.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 54863e2..e7f0f80 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -60,7 +60,6 @@ module VagrantPlugins entries = [] destroyed_entries = [] ids = [] - hosts_aliases = [] get_machines.each do |name, p| if @provider == p machine = @global_env.machine(name, p) @@ -71,9 +70,8 @@ module VagrantPlugins if id.nil? destroyed_entries << "#{ip}\t#{host} #{aliases}" else - entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" + entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" ids << id unless ids.include?(id) - hosts_aliases << "#{host} #{aliases}" end end end @@ -84,7 +82,6 @@ module VagrantPlugins File.open(file).each_line do |line| # Eliminate lines for machines that have been destroyed next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ } - next if hosts_aliases.any? { |host_aliases| line =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s#{host_aliases}\s# VAGRANT ID:\s.*/} tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ } end From 1b63859a576830c7282a711e15108fc2373c79c2 Mon Sep 17 00:00:00 2001 From: Lukasz Jastrzebski Date: Thu, 19 Dec 2013 16:08:31 -0800 Subject: [PATCH 3/4] adding current_machine to resolution --- lib/vagrant-hostmanager/hosts_file.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index e7f0f80..e07f929 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -19,7 +19,7 @@ module VagrantPlugins # download and modify file with Vagrant-managed entries file = @global_env.tmp_path.join("hosts.#{machine.name}") machine.communicate.download(realhostfile, file) - update_file(file) + update_file(machine, file) # upload modified file and remove temporary file machine.communicate.upload(file, '/tmp/hosts') @@ -49,13 +49,13 @@ module VagrantPlugins end FileUtils.cp(hosts_location, file) - update_file(file) + update_file(nil, file) copy_proc.call end private - def update_file(file) + def update_file(current_machine, file) # build array of host file entries from Vagrant configuration entries = [] destroyed_entries = [] @@ -65,7 +65,7 @@ module VagrantPlugins machine = @global_env.machine(name, p) host = machine.config.vm.hostname || name id = machine.id - ip = get_ip_address(machine) + ip = get_ip_address(current_machine, machine) aliases = machine.config.hostmanager.aliases.join(' ').chomp if id.nil? destroyed_entries << "#{ip}\t#{host} #{aliases}" @@ -94,10 +94,10 @@ module VagrantPlugins end end - def get_ip_address(machine) + def get_ip_address(current_machine, machine) custom_ip_resolver = machine.config.hostmanager.ip_resolver if custom_ip_resolver - custom_ip_resolver.call(machine) + custom_ip_resolver.call(current_machine, machine) else ip = nil if machine.config.hostmanager.ignore_private_ip != true From 566d1f2e3b045228bb1c77d02a7495412e575a26 Mon Sep 17 00:00:00 2001 From: Lukasz Jastrzebski Date: Fri, 10 Jan 2014 20:51:50 -0800 Subject: [PATCH 4/4] tweaking couple of code review suggestions --- README.md | 5 +++-- lib/vagrant-hostmanager/hosts_file.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index db196e4..287644d 100644 --- a/README.md +++ b/README.md @@ -81,10 +81,11 @@ 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: +for each machine by yourself, giving You also access to the machine that is +updating /etc/hosts. For example: ```ruby -config.hostmanager.ip_resolver = proc do |vm| +config.hostmanager.ip_resolver = proc do |vm, resolving_vm| if hostname = (vm.ssh_info && vm.ssh_info[:host]) `host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1] end diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index e07f929..94d0933 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -19,7 +19,7 @@ module VagrantPlugins # download and modify file with Vagrant-managed entries file = @global_env.tmp_path.join("hosts.#{machine.name}") machine.communicate.download(realhostfile, file) - update_file(machine, file) + update_file(file, machine) # upload modified file and remove temporary file machine.communicate.upload(file, '/tmp/hosts') @@ -49,13 +49,13 @@ module VagrantPlugins end FileUtils.cp(hosts_location, file) - update_file(nil, file) + update_file(file) copy_proc.call end private - def update_file(current_machine, file) + def update_file(file, resolving_machine=nil) # build array of host file entries from Vagrant configuration entries = [] destroyed_entries = [] @@ -65,7 +65,7 @@ module VagrantPlugins machine = @global_env.machine(name, p) host = machine.config.vm.hostname || name id = machine.id - ip = get_ip_address(current_machine, machine) + ip = get_ip_address(machine, resolving_machine) aliases = machine.config.hostmanager.aliases.join(' ').chomp if id.nil? destroyed_entries << "#{ip}\t#{host} #{aliases}" @@ -94,10 +94,10 @@ module VagrantPlugins end end - def get_ip_address(current_machine, machine) + def get_ip_address(machine, resolving_machine) custom_ip_resolver = machine.config.hostmanager.ip_resolver if custom_ip_resolver - custom_ip_resolver.call(current_machine, machine) + custom_ip_resolver.call(machine, resolving_machine) else ip = nil if machine.config.hostmanager.ignore_private_ip != true