Skip to content

Commit

Permalink
Merge pull request #45 from bdcribbs/fix/44
Browse files Browse the repository at this point in the history
Handle destroyed machines in update_file.  Fixes #44
  • Loading branch information
bdcribbs committed Nov 16, 2013
2 parents 5e785f8 + c53452b commit 6c5f3ed
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def update_host
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
Expand All @@ -62,15 +63,21 @@ def update_file(file)
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

tmp_file = Tempfile.open('hostmanager', @global_env.tmp_path, 'a')
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

Expand Down

0 comments on commit 6c5f3ed

Please sign in to comment.