Change middleware internals to make plugin lives easier [GH-684]

This commit is contained in:
Mitchell Hashimoto 2012-01-28 17:12:30 -08:00
parent fb89a6c0a5
commit 88ba3a3619
5 changed files with 146 additions and 128 deletions

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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.