From c53452b927faa568e553c7378b502b2ed3f74be8 Mon Sep 17 00:00:00 2001 From: Bryan Cribbs Date: Sat, 2 Nov 2013 15:54:07 -0500 Subject: [PATCH] #44 Handle destroyed machines in update_file Keep track of machines that have been destroyed (they have a nil id). Do not include them when updating the hosts file. Since it is impossible to match on id, this is accomplished by matching the exact ip / hostname / alias. If any of these have been altered since the entry was created the entry will not be removed from the hosts file. Potentially, this could be relaxed to only match on ip / hostname pairs. --- lib/vagrant-hostmanager/hosts_file.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 5735ded..ef644cd 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -54,6 +54,7 @@ module VagrantPlugins def update_file(file) # build array of host file entries from Vagrant configuration entries = [] + destroyed_entries = [] ids = [] get_machines.each do |name, p| if @provider == p @@ -62,8 +63,12 @@ module VagrantPlugins id = machine.id ip = get_ip_address(machine) aliases = machine.config.hostmanager.aliases.join(' ').chomp - entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" - ids << id unless ids.include?(id) + if id.nil? + destroyed_entries << "#{ip}\t#{host} #{aliases}" + else + entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n" + ids << id unless ids.include?(id) + end end end @@ -71,6 +76,8 @@ module VagrantPlugins begin # copy each line not managed by Vagrant 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: .*/ } tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ } end