From 782168492cf5727cf02fe27debb4fecafa0ab41c Mon Sep 17 00:00:00 2001 From: Brian Johnson Date: Mon, 17 Jun 2013 12:59:07 -0700 Subject: [PATCH] - Ensure that empty regex isn't processed for deletes. - Set the hook for deletion to run prior to machine deletion so that the id is retrieveable. --- .../action/delete_guests.rb | 5 ++-- lib/vagrant-hostmanager/hosts_file.rb | 30 +++++++++++-------- lib/vagrant-hostmanager/plugin.rb | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/vagrant-hostmanager/action/delete_guests.rb b/lib/vagrant-hostmanager/action/delete_guests.rb index 505350d..244b052 100644 --- a/lib/vagrant-hostmanager/action/delete_guests.rb +++ b/lib/vagrant-hostmanager/action/delete_guests.rb @@ -20,11 +20,12 @@ module VagrantPlugins return @app.call(env) unless @machine.config.hostmanager.enabled? @logger.info 'Updating /etc/hosts file automatically' + # update /etc/hosts file on each active machine + delete_guests(@machine,@machine.provider_name) + # continue the action stack so the machine will be created @app.call(env) - # update /etc/hosts file on each active machine - delete_guests(@machine,@machine.provider_name) end end end diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 41e2c4d..4f81fed 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -100,21 +100,25 @@ module VagrantPlugins def delete_entry(machine,file_name,sudo=false) host = machine.config.vm.hostname || name temp_file_name = Dir::Tmpname.make_tmpname(File.join(machine.env.tmp_path,'hostmanager'), nil) - tempfile = File.open(temp_file_name,'w') do |f| - File.open(file_name,'r').each_line do |line| - if line.match(/#{machine.id}$/).nil? - f << line - else - @logger.info "Matched #{machine.id}" + if not machine.id.nil? + tempfile = File.open(temp_file_name,'w') do |f| + File.open(file_name,'r').each_line do |line| + if line.match(/#{machine.id}$/).nil? + f << line + else + @logger.info "Matched #{machine.id}" + end + end + end + if sudo == false + @logger.info "copy #{temp_file_name} #{file_name}" + FileUtils.cp(temp_file_name,file_name) + else + machine.env.ui.info I18n.t('vagrant_hostmanager.action.run_sudo') + @logger.info `sudo cp -v #{temp_file_name} #{file_name}` end - end - end - if sudo == false - @logger.info "copy #{temp_file_name} #{file_name}" - FileUtils.cp(temp_file_name,file_name) else - machine.env.ui.info I18n.t('vagrant_hostmanager.action.run_sudo') - @logger.info `sudo cp -v #{temp_file_name} #{file_name}` + @logger.warn "Machine id to delete was empty, skipping..." end end diff --git a/lib/vagrant-hostmanager/plugin.rb b/lib/vagrant-hostmanager/plugin.rb index 7323544..d77a737 100644 --- a/lib/vagrant-hostmanager/plugin.rb +++ b/lib/vagrant-hostmanager/plugin.rb @@ -30,7 +30,7 @@ module VagrantPlugins action_hook(:hostmanager, :machine_action_destroy) do |hook| hook.prepend(Action::DeleteLocalEntry) - hook.append(Action::DeleteGuests) + hook.prepend(Action::DeleteGuests) end provisioner(:hostmanager) do