diff --git a/plugins/providers/virtualbox/provider.rb b/plugins/providers/virtualbox/provider.rb index 9d9d5c9c8..2854a7d9c 100644 --- a/plugins/providers/virtualbox/provider.rb +++ b/plugins/providers/virtualbox/provider.rb @@ -1,11 +1,24 @@ +require "log4r" + module VagrantPlugins module ProviderVirtualBox class Provider < Vagrant.plugin("1", :provider) attr_reader :driver def initialize(machine) + @logger = Log4r::Logger.new("vagrant::provider::virtualbox") @machine = machine - @driver = Driver::Meta.new(@machine.id) + + begin + @logger.debug("Instantiating the driver for machine ID: #{@machine.id.inspect}") + @driver = Driver::Meta.new(@machine.id) + rescue Driver::Meta::VMNotFound + # The virtual machine doesn't exist, so we probably have a stale + # ID. Just clear the id out of the machine and reload it. + @logger.debug("VM not found! Clearing saved machine ID and reloading.") + @machine.id = nil + retry + end end # @see Vagrant::Plugin::V1::Provider#action diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index d32fed62d..20661d55e 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -283,6 +283,17 @@ describe Vagrant::Machine do instance.id = "foo" new_instance.id.should == "foo" end + + it "should delete the ID" do + instance.id = "foo" + + second = new_instance + second.id.should == "foo" + second.id = nil + + third = new_instance + third.id.should be_nil + end end describe "ssh info" do