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) ## 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. - Match VM names that have parens, brackets, etc.
- Detect when the VirtualBox kernel module is not loaded and error. [GH-677] - 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 - 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 :Util, 'vagrant/util'
autoload :VM, 'vagrant/vm' 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 source root is the path to the root directory of
# the Vagrant gem. # the Vagrant gem.
def self.source_root def self.source_root

View File

@ -1,8 +1,8 @@
require 'vagrant/action/builder' require 'vagrant/action/builder'
require 'vagrant/action/builtin'
module Vagrant module Vagrant
module Action module Action
autoload :Builtin, 'vagrant/action/builtin'
autoload :Environment, 'vagrant/action/environment' autoload :Environment, 'vagrant/action/environment'
autoload :Runner, 'vagrant/action/runner' autoload :Runner, 'vagrant/action/runner'
autoload :Warden, 'vagrant/action/warden' autoload :Warden, 'vagrant/action/warden'

View File

@ -1,145 +1,158 @@
module Vagrant module Vagrant
module Action module Action
# Registers the builtin actions with a specific registry. # A registry object containing the built-in middleware stacks.
# class Builtin < Registry
# These are the pre-built action sequences that are shipped with def initialize
# Vagrant itself. # Properly initialize the registry object
def self.builtin!(registry) super
# provision - Provisions a running VM
registry.register(:provision) do # Register all the built-in stacks
Builder.new do register_builtin!
use General::Validate
use VM::CheckAccessible
use VM::Provision
end
end end
# start - Starts a VM, assuming it already exists on the protected
# 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
# halt - Halts the VM, attempting gracefully but then forcing def register_builtin!
# a restart if fails. # We do this so that the blocks below have a variable to access the
registry.register(:halt) do # outer registry.
Builder.new do registry = self
use General::Validate
use VM::CheckAccessible
use VM::DiscardState
use VM::Halt
end
end
# suspend - Suspends the VM # provision - Provisions a running VM
registry.register(:suspend) do register(:provision) do
Builder.new do Builder.new do
use General::Validate use General::Validate
use VM::CheckAccessible use VM::CheckAccessible
use VM::Suspend use VM::Provision
end
end end
end
# resume - Resume a VM # start - Starts a VM, assuming it already exists on the
registry.register(:resume) do # environment.
Builder.new do register(:start) do
use General::Validate Builder.new do
use VM::CheckAccessible use General::Validate
use VM::CheckPortCollisions use VM::CheckAccessible
use VM::Resume 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
end
# reload - Halts then restarts the VM # halt - Halts the VM, attempting gracefully but then forcing
registry.register(:reload) do # a restart if fails.
Builder.new do register(:halt) do
use General::Validate Builder.new do
use VM::CheckAccessible use General::Validate
use registry.get(:halt) use VM::CheckAccessible
use registry.get(:start) use VM::DiscardState
use VM::Halt
end
end end
end
# up - Imports, prepares, then starts a fresh VM. # suspend - Suspends the VM
registry.register(:up) do register(:suspend) do
Builder.new do Builder.new do
use General::Validate use General::Validate
use VM::CheckAccessible use VM::CheckAccessible
use VM::CheckBox use VM::Suspend
use VM::Import end
use VM::CheckGuestAdditions
use VM::DefaultName
use VM::MatchMACAddress
use registry.get(:start)
end end
end
# destroy - Halts, cleans up, and destroys an existing VM # resume - Resume a VM
registry.register(:destroy) do register(:resume) do
Builder.new do Builder.new do
use General::Validate use General::Validate
use VM::CheckAccessible use VM::CheckAccessible
use registry.get(:halt), :force => true use VM::CheckPortCollisions
use VM::ProvisionerCleanup use VM::Resume
use VM::PruneNFSExports end
use VM::Destroy
use VM::CleanMachineFolder
use VM::DestroyUnusedNetworkInterfaces
end end
end
# package - Export and package the VM # reload - Halts then restarts the VM
registry.register(:package) do register(:reload) do
Builder.new do Builder.new do
use General::Validate use General::Validate
use VM::SetupPackageFiles use VM::CheckAccessible
use VM::CheckAccessible use registry.get(:halt)
use registry.get(:halt) use registry.get(:start)
use VM::ClearForwardedPorts end
use VM::ClearSharedFolders
use VM::Export
use VM::PackageVagrantfile
use VM::Package
end end
end
# box_add - Download and add a box. # up - Imports, prepares, then starts a fresh VM.
registry.register(:box_add) do register(:up) do
Builder.new do Builder.new do
use Box::Download use General::Validate
use Box::Unpackage use VM::CheckAccessible
use Box::Verify use VM::CheckBox
use VM::Import
use VM::CheckGuestAdditions
use VM::DefaultName
use VM::MatchMACAddress
use registry.get(:start)
end
end end
end
# box_remove - Removes/deletes a box. # destroy - Halts, cleans up, and destroys an existing VM
registry.register(:box_remove) do register(:destroy) do
Builder.new do Builder.new do
use Box::Destroy 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
end
# box_repackage - Repackages a box. # package - Export and package the VM
registry.register(:box_repackage) do register(:package) do
Builder.new do Builder.new do
use Box::Package 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 end
end end

View File

@ -197,13 +197,10 @@ module Vagrant
# #
# @return [Registry] # @return [Registry]
def action_registry def action_registry
return @action_registry if defined?(@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
# The action registry hasn't been loaded yet, so load it # global one, but for now there isn't a use case that calls for it.
# and setup the built-in actions with it. Vagrant.actions
@action_registry = Registry.new
Vagrant::Action.builtin!(@action_registry)
@action_registry
end end
# Loads on initial access and reads data from the global data store. # Loads on initial access and reads data from the global data store.