diff --git a/lib/vagrant/action/general/check_virtualbox.rb b/lib/vagrant/action/general/check_virtualbox.rb deleted file mode 100644 index cbaecc8ec..000000000 --- a/lib/vagrant/action/general/check_virtualbox.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Vagrant - module Action - module General - # Checks that virtualbox is installed and ready to be used. - class CheckVirtualbox - def initialize(app, env) - @app = app - end - - def call(env) - # Certain actions may not actually have a VM, and thus no - # driver, so we have to be clever about obtaining an instance - # of the driver. - driver = nil - driver = env[:vm].driver if env[:vm] - driver = Driver::VirtualBox.new(nil) if !driver - - # Verify that it is ready to go! This will raise an exception - # if anything goes wrong. - driver.verify! - - # Carry on. - @app.call(env) - end - end - end - end -end diff --git a/lib/vagrant/action/general/validate.rb b/lib/vagrant/action/general/validate.rb index 5045918dd..f566b793f 100644 --- a/lib/vagrant/action/general/validate.rb +++ b/lib/vagrant/action/general/validate.rb @@ -9,7 +9,10 @@ module Vagrant end def call(env) - env[:vm].config.validate!(env[:vm].env) if !env.has_key?("validate") || env["validate"] + if !env.has_key?(:validate) || env[:validate] + env[:machine].config.validate!(env[:machine].env) + end + @app.call(env) end end diff --git a/plugins/commands/destroy/command.rb b/plugins/commands/destroy/command.rb index 9c18dafae..1fcd14f98 100644 --- a/plugins/commands/destroy/command.rb +++ b/plugins/commands/destroy/command.rb @@ -19,6 +19,9 @@ module VagrantPlugins @logger.debug("'Destroy' each target VM...") with_target_vms(argv, :reverse => true) do |vm| + vm.action(:destroy) + next + if vm.created? # Boolean whether we should actually go through with the destroy # or not. This is true only if the "--force" flag is set or if the diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb new file mode 100644 index 000000000..29b5e7a54 --- /dev/null +++ b/plugins/providers/virtualbox/action.rb @@ -0,0 +1,20 @@ +require "vagrant/action/builder" + +module VagrantPlugins + module ProviderVirtualBox + module Action + autoload :CheckAccessible, File.expand_path("../action/check_accessible", __FILE__) + autoload :CheckVirtualbox, File.expand_path("../action/check_virtualbox", __FILE__) + + # This is the action that is primarily responsible for completely + # freeing the resources of the underlying virtual machine. + def self.action_destroy + Vagrant::Action::Builder.new.tap do |b| + b.use CheckVirtualbox + b.use Vagrant::Action::General::Validate + b.use CheckAccessible + end + end + end + end +end diff --git a/lib/vagrant/action/vm/check_accessible.rb b/plugins/providers/virtualbox/action/check_accessible.rb similarity index 73% rename from lib/vagrant/action/vm/check_accessible.rb rename to plugins/providers/virtualbox/action/check_accessible.rb index 69df47194..f000825b1 100644 --- a/lib/vagrant/action/vm/check_accessible.rb +++ b/plugins/providers/virtualbox/action/check_accessible.rb @@ -1,18 +1,18 @@ -module Vagrant - module Action - module VM +module VagrantPlugins + module ProviderVirtualBox + module Action class CheckAccessible def initialize(app, env) @app = app end def call(env) - if env[:vm].state == :inaccessible + if env[:machine].state == :inaccessible # The VM we are attempting to manipulate is inaccessible. This # is a very bad situation and can only be fixed by the user. It # also prohibits us from actually doing anything with the virtual # machine, so we raise an error. - raise Errors::VMInaccessible + raise Vagrant::Errors::VMInaccessible end @app.call(env) diff --git a/plugins/providers/virtualbox/action/check_virtualbox.rb b/plugins/providers/virtualbox/action/check_virtualbox.rb new file mode 100644 index 000000000..1d590fe4f --- /dev/null +++ b/plugins/providers/virtualbox/action/check_virtualbox.rb @@ -0,0 +1,22 @@ +module VagrantPlugins + module ProviderVirtualBox + module Action + # Checks that VirtualBox is installed and ready to be used. + class CheckVirtualbox + def initialize(app, env) + @app = app + end + + def call(env) + # This verifies that VirtualBox is installed and the driver is + # ready to function. If not, then an exception will be raised + # which will break us out of execution of the middleware sequence. + Driver::Meta.new.verify! + + # Carry on. + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index d5c3c130a..d136209c0 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -15,6 +15,8 @@ module VagrantPlugins end end + autoload :Action, File.expand_path("../action", __FILE__) + # Drop some autoloads in here to optimize the performance of loading # our drivers only when they are needed. module Driver diff --git a/plugins/providers/virtualbox/provider.rb b/plugins/providers/virtualbox/provider.rb index eda1de5bb..9bbe2a090 100644 --- a/plugins/providers/virtualbox/provider.rb +++ b/plugins/providers/virtualbox/provider.rb @@ -6,8 +6,30 @@ module VagrantPlugins @driver = Driver::Meta.new(@machine.id) end + # @see Vagrant::Plugin::V1::Provider#action + def action(name) + # Attempt to get the action method from the Action class if it + # exists, otherwise return nil to show that we don't support the + # given action. + action_method = "action_#{name}" + return Action.send(action_method) if Action.respond_to?(action_method) + nil + end + + # Returns a human-friendly string version of this provider which + # includes the machine's ID that this provider represents, if it + # has one. + # + # @return [String] + def to_s + id = @machine.id ? @machine.id : "new VM" + "VirtualBox (#{id})" + end + # Return the state of VirtualBox virtual machine by actually # querying VBoxManage. + # + # @return [Symbol] def state return :not_created if !@driver.uuid state = @driver.read_state diff --git a/test/unit/vagrant/action/runner_test.rb b/test/unit/vagrant/action/runner_test.rb index fedd89fc4..3d4a6af13 100644 --- a/test/unit/vagrant/action/runner_test.rb +++ b/test/unit/vagrant/action/runner_test.rb @@ -1,13 +1,7 @@ require File.expand_path("../../../base", __FILE__) describe Vagrant::Action::Runner do - let(:registry) do - d = double("registry") - d.stub(:get) - d - end - - let(:instance) { described_class.new(registry) } + let(:instance) { described_class.new } it "should raise an error if an invalid callable is given" do expect { instance.run(7) }.to raise_error(ArgumentError, /must be a callable/) @@ -47,7 +41,7 @@ describe Vagrant::Action::Runner do result = env["data"] end - instance = described_class.new(registry, "data" => "bar") + instance = described_class.new("data" => "bar") instance.run(callable) result.should == "bar" end @@ -58,7 +52,7 @@ describe Vagrant::Action::Runner do result = env["data"] end - instance = described_class.new(registry) { { "data" => "bar" } } + instance = described_class.new { { "data" => "bar" } } instance.run(callable) result.should == "bar" end diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index bba1fa57b..4a79ced92 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -93,16 +93,6 @@ describe Vagrant::Environment do end end - describe "action registry" do - it "has an action registry" do - instance.action_registry.should be_kind_of(Vagrant::Registry) - end - - it "should have the built-in actions in the registry" do - instance.action_registry.get(:provision).should_not be_nil - end - end - describe "primary VM" do it "should be the only VM if not a multi-VM environment" do instance.primary_vm.should == instance.vms.values.first