From 88ba3a3619dd1b0068a69f12684c12f4e84a5d60 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 28 Jan 2012 17:12:30 -0800 Subject: [PATCH] Change middleware internals to make plugin lives easier [GH-684] --- CHANGELOG.md | 2 + lib/vagrant.rb | 6 + lib/vagrant/action.rb | 2 +- lib/vagrant/action/builtin.rb | 253 ++++++++++++++++++---------------- lib/vagrant/environment.rb | 11 +- 5 files changed, 146 insertions(+), 128 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df7df0db0..1d771ee99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.9.4 (unreleased) + - Important internal changes to middlewares that make plugin developer's + lives much easier. [GH-684] - Match VM names that have parens, brackets, etc. - Detect when the VirtualBox kernel module is not loaded and error. [GH-677] - Set `:auto_config` to false on any networking option to not automatically diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 84902c6e7..2008d8785 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -74,6 +74,12 @@ module Vagrant autoload :Util, 'vagrant/util' autoload :VM, 'vagrant/vm' + # Returns a `Vagrant::Registry` object that contains all the built-in + # middleware stacks. + def self.actions + @actions ||= Vagrant::Action::Builtin.new + end + # The source root is the path to the root directory of # the Vagrant gem. def self.source_root diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 8d5821ffe..2d920fc9a 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -1,8 +1,8 @@ require 'vagrant/action/builder' -require 'vagrant/action/builtin' module Vagrant module Action + autoload :Builtin, 'vagrant/action/builtin' autoload :Environment, 'vagrant/action/environment' autoload :Runner, 'vagrant/action/runner' autoload :Warden, 'vagrant/action/warden' diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 14b31d0dc..eb10ab42e 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -1,145 +1,158 @@ module Vagrant module Action - # 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 - registry.register(:provision) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::Provision - end + # A registry object containing the built-in middleware stacks. + class Builtin < Registry + def initialize + # Properly initialize the registry object + super + + # Register all the built-in stacks + register_builtin! end - # start - Starts a VM, assuming it already exists on the - # environment. - registry.register(:start) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::CleanMachineFolder - use VM::ClearForwardedPorts - use VM::CheckPortCollisions, :port_collision_handler => :correct - use VM::ForwardPorts - use VM::Provision - use VM::PruneNFSExports - use VM::NFS - use VM::ClearSharedFolders - use VM::ShareFolders - use VM::HostName - use VM::ClearNetworkInterfaces - use VM::Network - use VM::Customize - use VM::Boot - end - end + protected - # halt - Halts the VM, attempting gracefully but then forcing - # a restart if fails. - registry.register(:halt) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::DiscardState - use VM::Halt - end - end + def register_builtin! + # We do this so that the blocks below have a variable to access the + # outer registry. + registry = self - # suspend - Suspends the VM - registry.register(:suspend) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::Suspend + # provision - Provisions a running VM + register(:provision) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::Provision + end end - end - # resume - Resume a VM - registry.register(:resume) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::CheckPortCollisions - use VM::Resume + # start - Starts a VM, assuming it already exists on the + # environment. + register(:start) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::CleanMachineFolder + use VM::ClearForwardedPorts + use VM::CheckPortCollisions, :port_collision_handler => :correct + use VM::ForwardPorts + use VM::Provision + use VM::PruneNFSExports + use VM::NFS + use VM::ClearSharedFolders + use VM::ShareFolders + use VM::HostName + use VM::ClearNetworkInterfaces + use VM::Network + use VM::Customize + use VM::Boot + end end - end - # reload - Halts then restarts the VM - registry.register(:reload) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use registry.get(:halt) - use registry.get(:start) + # halt - Halts the VM, attempting gracefully but then forcing + # a restart if fails. + register(:halt) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::DiscardState + use VM::Halt + end end - end - # up - Imports, prepares, then starts a fresh VM. - registry.register(:up) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use VM::CheckBox - use VM::Import - use VM::CheckGuestAdditions - use VM::DefaultName - use VM::MatchMACAddress - use registry.get(:start) + # suspend - Suspends the VM + register(:suspend) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::Suspend + end end - end - # destroy - Halts, cleans up, and destroys an existing VM - registry.register(:destroy) do - Builder.new do - use General::Validate - use VM::CheckAccessible - use registry.get(:halt), :force => true - use VM::ProvisionerCleanup - use VM::PruneNFSExports - use VM::Destroy - use VM::CleanMachineFolder - use VM::DestroyUnusedNetworkInterfaces + # resume - Resume a VM + register(:resume) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::CheckPortCollisions + use VM::Resume + end end - end - # package - Export and package the VM - registry.register(:package) do - Builder.new do - use General::Validate - use VM::SetupPackageFiles - use VM::CheckAccessible - use registry.get(:halt) - use VM::ClearForwardedPorts - use VM::ClearSharedFolders - use VM::Export - use VM::PackageVagrantfile - use VM::Package + # reload - Halts then restarts the VM + register(:reload) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use registry.get(:halt) + use registry.get(:start) + end end - end - # box_add - Download and add a box. - registry.register(:box_add) do - Builder.new do - use Box::Download - use Box::Unpackage - use Box::Verify + # up - Imports, prepares, then starts a fresh VM. + register(:up) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use VM::CheckBox + use VM::Import + use VM::CheckGuestAdditions + use VM::DefaultName + use VM::MatchMACAddress + use registry.get(:start) + end end - end - # box_remove - Removes/deletes a box. - registry.register(:box_remove) do - Builder.new do - use Box::Destroy + # destroy - Halts, cleans up, and destroys an existing VM + register(:destroy) do + Builder.new do + use General::Validate + use VM::CheckAccessible + use registry.get(:halt), :force => true + use VM::ProvisionerCleanup + use VM::PruneNFSExports + use VM::Destroy + use VM::CleanMachineFolder + use VM::DestroyUnusedNetworkInterfaces + end end - end - # box_repackage - Repackages a box. - registry.register(:box_repackage) do - Builder.new do - use Box::Package + # package - Export and package the VM + register(:package) do + Builder.new do + use General::Validate + use VM::SetupPackageFiles + use VM::CheckAccessible + use registry.get(:halt) + use VM::ClearForwardedPorts + use VM::ClearSharedFolders + use VM::Export + use VM::PackageVagrantfile + use VM::Package + end + end + + # box_add - Download and add a box. + 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) do + Builder.new do + use Box::Destroy + end + end + + # box_repackage - Repackages a box. + register(:box_repackage) do + Builder.new do + use Box::Package + end end end end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 7bb072bdd..b92c1af5d 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -197,13 +197,10 @@ module Vagrant # # @return [Registry] def action_registry - 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 = Registry.new - Vagrant::Action.builtin!(@action_registry) - @action_registry + # For now we return the global built-in actions registry. In the future + # we may want to create an isolated registry that inherits from this + # global one, but for now there isn't a use case that calls for it. + Vagrant.actions end # Loads on initial access and reads data from the global data store.