From 36632c4bb736e4ea582e6cd0cf8fd054af7c3db1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 22 Dec 2011 13:00:55 -0800 Subject: [PATCH] Handle the case properly where the VM in `.vagrant` doesn't exist anymore --- lib/vagrant/driver/virtualbox.rb | 14 ++++++++++++++ lib/vagrant/vm.rb | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index b399a1629..c850a80c5 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -5,9 +5,16 @@ module Vagrant module Driver # This class contains the logic to drive VirtualBox. class VirtualBox + # This is raised if the VM is not found when initializing a driver + # with a UUID. + class VMNotFound < StandardError; end + # Include this so we can use `Subprocess` more easily. include Vagrant::Util + # The UUID of the virtual machine we represent + attr_reader :uuid + # The version of virtualbox that is running. attr_reader :version @@ -15,6 +22,13 @@ module Vagrant @logger = Log4r::Logger.new("vagrant::driver::virtualbox") @uuid = uuid + if @uuid + # Verify the VM exists, and if it doesn't, then don't worry + # about it (mark the UUID as nil) + r = raw("showvminfo", @uuid) + raise VMNotFound if r.exit_code != 0 + end + # Read and assign the version of VirtualBox we know which # specific driver to instantiate. begin diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 3a22c6b55..e6ce82d50 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -114,7 +114,14 @@ module Vagrant end def reload! - @driver = Driver::VirtualBox.new(@uuid) + begin + @driver = Driver::VirtualBox.new(@uuid) + rescue Driver::VirtualBox::VMNotFound + # Clear the UUID since this VM doesn't exist. Note that this calls + # back into `reload!` but shouldn't ever result in infinite + # recursion since `@uuid` will be nil. + self.uuid = nil + end end def package(options=nil)