diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 68612ba68..0c0910f81 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -51,5 +51,4 @@ require 'vagrant/command' require 'vagrant/provisioners' require 'vagrant/systems' require 'vagrant/version' -Vagrant::Action.builtin! Vagrant::Plugin.load! diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index ba5d1e419..03258b98b 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -1,117 +1,140 @@ module Vagrant class Action - # Registers the builtin actions. These are locked away in a - # method so that their definition can be deferred until after - # all the necessary Vagrant libraries are loaded. Hopefully - # in the future this will no longer be necessary with autoloading. - def self.builtin! - return + # Registers the builtin actions with a specific registry. + # + # These are the pre-built action sequences that are shipped with + # Vagrant itself. + def self.builtin!(registry) # provision - Provisions a running VM - register(:provision, Builder.new do - use VM::CheckAccessible - use VM::Provision - end) + registry.register(:provision) do + Builder.new do + use VM::CheckAccessible + use VM::Provision + end + end # start - Starts a VM, assuming it already exists on the # environment. - register(:start, Builder.new do - use VM::CheckAccessible - use VM::CleanMachineFolder - use VM::ClearForwardedPorts - use VM::ForwardPorts - use VM::Provision - use VM::NFS - use VM::ClearSharedFolders - use VM::ShareFolders - use VM::HostName - use VM::Network - use VM::Customize - use VM::Modify - use VM::Boot - end) + registry.register(:start) do + Builder.new do + use VM::CheckAccessible + use VM::CleanMachineFolder + use VM::ClearForwardedPorts + use VM::ForwardPorts + use VM::Provision + use VM::NFS + use VM::ClearSharedFolders + use VM::ShareFolders + use VM::HostName + use VM::Network + use VM::Customize + use VM::Modify + use VM::Boot + end + end # halt - Halts the VM, attempting gracefully but then forcing # a restart if fails. - register(:halt, Builder.new do - use VM::CheckAccessible - use VM::DiscardState - use VM::Halt - end) + registry.register(:halt) do + Builder.new do + use VM::CheckAccessible + use VM::DiscardState + use VM::Halt + end + end # suspend - Suspends the VM - register(:suspend, Builder.new do - use VM::CheckAccessible - use VM::Suspend - end) + registry.register(:suspend) do + Builder.new do + use VM::CheckAccessible + use VM::Suspend + end + end # resume - Resume a VM - register(:resume, Builder.new do - use VM::CheckAccessible - use VM::Resume - end) + registry.register(:resume) do + Builder.new do + use VM::CheckAccessible + use VM::Resume + end + end # reload - Halts then restarts the VM - register(:reload, Builder.new do - use VM::CheckAccessible - use Action[:halt] - use Action[:start] - end) + registry.register(:reload) do + Builder.new do + use VM::CheckAccessible + use Action[:halt] + use Action[:start] + end + end # up - Imports, prepares, then starts a fresh VM. - register(:up, Builder.new do - use VM::CheckAccessible - use VM::CheckBox - use VM::Import - use VM::MatchMACAddress - use VM::CheckGuestAdditions - use Action[:start] - end) + registry.register(:up) do + Builder.new do + use VM::CheckAccessible + use VM::CheckBox + use VM::Import + use VM::MatchMACAddress + use VM::CheckGuestAdditions + use Action[:start] + end + end # destroy - Halts, cleans up, and destroys an existing VM - register(:destroy, Builder.new do - use VM::CheckAccessible - use Action[:halt], :force => true - use VM::ProvisionerCleanup - use VM::ClearNFSExports - use VM::Destroy - use VM::CleanMachineFolder - use VM::DestroyUnusedNetworkInterfaces - end) + registry.register(:destroy) do + Builder.new do + use VM::CheckAccessible + use Action[:halt], :force => true + use VM::ProvisionerCleanup + use VM::ClearNFSExports + use VM::Destroy + use VM::CleanMachineFolder + use VM::DestroyUnusedNetworkInterfaces + end + end # package - Export and package the VM - register(:package, Builder.new do - use VM::CheckAccessible - use Action[:halt] - use VM::ClearForwardedPorts - use VM::ClearSharedFolders - use VM::Modify - use VM::Export - use VM::PackageVagrantfile - use VM::Package - end) + registry.register(:package) do + Builder.new do + use VM::CheckAccessible + use Action[:halt] + use VM::ClearForwardedPorts + use VM::ClearSharedFolders + use VM::Modify + use VM::Export + use VM::PackageVagrantfile + use VM::Package + end + end # box_add - Download and add a box. - register(:box_add, Builder.new do - use Box::Download - use Box::Unpackage - use Box::Verify - end) + registry.register(:box_add) do + Builder.new do + use Box::Download + use Box::Unpackage + use Box::Verify + end + end # box_remove - Removes/deletes a box. - register(:box_remove, Builder.new do - use Box::Destroy - end) + registry.register(:box_remove) do + Builder.new do + use Box::Destroy + end + end # box_repackage - Repackages a box. - register(:box_repackage, Builder.new do - use Box::Package - end) + registry.register(:box_repackage) do + Builder.new do + use Box::Package + end + end # Other callbacks. There will be more of these in the future. For # now, these are limited to what are needed internally. - register(:before_action_run, Builder.new do +# registry.register(:before_action_run, Builder.new do # use General::Validate - end) +# end) end end end diff --git a/lib/vagrant/action/environment.rb b/lib/vagrant/action/environment.rb index 08d5d73da..38a0909ff 100644 --- a/lib/vagrant/action/environment.rb +++ b/lib/vagrant/action/environment.rb @@ -1,10 +1,12 @@ +require 'vagrant/util/hash_with_indifferent_access' + module Vagrant class Action # Represents an action environment which is what is passed # to the `call` method of each action. This environment contains # some helper methods for accessing the environment as well # as being a hash, to store any additional options. - class Environment < Hash + class Environment < Util::HashWithIndifferentAccess def initialize @interrupted = false end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index a29000314..ba1265f25 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -216,7 +216,13 @@ module Vagrant # # @return [Action::Registry] def action_registry - @action_registry ||= Action::Registry.new + return @action_registry if defined?(@action_registry) + + # The action registry hasn't been loaded yet, so load it + # and setup the built-in actions with it. + @action_registry = Action::Registry.new + Vagrant::Action.builtin!(@action_registry) + @action_registry end # Loads on initial access and reads data from the global data store. diff --git a/test/unit/vagrant/action/environment_test.rb b/test/unit/vagrant/action/environment_test.rb index c16850d06..4f278d566 100644 --- a/test/unit/vagrant/action/environment_test.rb +++ b/test/unit/vagrant/action/environment_test.rb @@ -9,6 +9,11 @@ describe Vagrant::Action::Environment do instance["foo"].should == "bar" end + it "should be a hash accessible by string or symbol" do + instance["foo"] = "bar" + instance[:foo].should == "bar" + end + it "should keep track of interrupted state" do instance.should_not be_interrupted instance.interrupt! diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 881a08453..c56938a5f 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -44,6 +44,18 @@ describe Vagrant::Environment do collection.directory.should == instance.boxes_path end + it "has an action runner" do + instance.action_runner.should be_kind_of(Vagrant::Action::Runner) + end + + it "has an action registry" do + instance.action_registry.should be_kind_of(Vagrant::Action::Registry) + end + + it "should have the built-in actions in the registry" do + instance.action_registry.get(:provision).should_not be_nil + end + describe "loading configuration" do it "should load global configuration" do environment = isolated_environment do |env|