Integrate new action runner/registry into Vagrant::Environment

This commit is contained in:
Mitchell Hashimoto 2011-12-09 14:39:39 -08:00
parent daf711fd80
commit bbb8f0ac2c
6 changed files with 133 additions and 86 deletions

View File

@ -51,5 +51,4 @@ require 'vagrant/command'
require 'vagrant/provisioners' require 'vagrant/provisioners'
require 'vagrant/systems' require 'vagrant/systems'
require 'vagrant/version' require 'vagrant/version'
Vagrant::Action.builtin!
Vagrant::Plugin.load! Vagrant::Plugin.load!

View File

@ -1,117 +1,140 @@
module Vagrant module Vagrant
class Action class Action
# Registers the builtin actions. These are locked away in a # Registers the builtin actions with a specific registry.
# method so that their definition can be deferred until after #
# all the necessary Vagrant libraries are loaded. Hopefully # These are the pre-built action sequences that are shipped with
# in the future this will no longer be necessary with autoloading. # Vagrant itself.
def self.builtin! def self.builtin!(registry)
return
# provision - Provisions a running VM # provision - Provisions a running VM
register(:provision, Builder.new do registry.register(:provision) do
use VM::CheckAccessible Builder.new do
use VM::Provision use VM::CheckAccessible
end) use VM::Provision
end
end
# start - Starts a VM, assuming it already exists on the # start - Starts a VM, assuming it already exists on the
# environment. # environment.
register(:start, Builder.new do registry.register(:start) do
use VM::CheckAccessible Builder.new do
use VM::CleanMachineFolder use VM::CheckAccessible
use VM::ClearForwardedPorts use VM::CleanMachineFolder
use VM::ForwardPorts use VM::ClearForwardedPorts
use VM::Provision use VM::ForwardPorts
use VM::NFS use VM::Provision
use VM::ClearSharedFolders use VM::NFS
use VM::ShareFolders use VM::ClearSharedFolders
use VM::HostName use VM::ShareFolders
use VM::Network use VM::HostName
use VM::Customize use VM::Network
use VM::Modify use VM::Customize
use VM::Boot use VM::Modify
end) use VM::Boot
end
end
# halt - Halts the VM, attempting gracefully but then forcing # halt - Halts the VM, attempting gracefully but then forcing
# a restart if fails. # a restart if fails.
register(:halt, Builder.new do registry.register(:halt) do
use VM::CheckAccessible Builder.new do
use VM::DiscardState use VM::CheckAccessible
use VM::Halt use VM::DiscardState
end) use VM::Halt
end
end
# suspend - Suspends the VM # suspend - Suspends the VM
register(:suspend, Builder.new do registry.register(:suspend) do
use VM::CheckAccessible Builder.new do
use VM::Suspend use VM::CheckAccessible
end) use VM::Suspend
end
end
# resume - Resume a VM # resume - Resume a VM
register(:resume, Builder.new do registry.register(:resume) do
use VM::CheckAccessible Builder.new do
use VM::Resume use VM::CheckAccessible
end) use VM::Resume
end
end
# reload - Halts then restarts the VM # reload - Halts then restarts the VM
register(:reload, Builder.new do registry.register(:reload) do
use VM::CheckAccessible Builder.new do
use Action[:halt] use VM::CheckAccessible
use Action[:start] use Action[:halt]
end) use Action[:start]
end
end
# up - Imports, prepares, then starts a fresh VM. # up - Imports, prepares, then starts a fresh VM.
register(:up, Builder.new do registry.register(:up) do
use VM::CheckAccessible Builder.new do
use VM::CheckBox use VM::CheckAccessible
use VM::Import use VM::CheckBox
use VM::MatchMACAddress use VM::Import
use VM::CheckGuestAdditions use VM::MatchMACAddress
use Action[:start] use VM::CheckGuestAdditions
end) use Action[:start]
end
end
# destroy - Halts, cleans up, and destroys an existing VM # destroy - Halts, cleans up, and destroys an existing VM
register(:destroy, Builder.new do registry.register(:destroy) do
use VM::CheckAccessible Builder.new do
use Action[:halt], :force => true use VM::CheckAccessible
use VM::ProvisionerCleanup use Action[:halt], :force => true
use VM::ClearNFSExports use VM::ProvisionerCleanup
use VM::Destroy use VM::ClearNFSExports
use VM::CleanMachineFolder use VM::Destroy
use VM::DestroyUnusedNetworkInterfaces use VM::CleanMachineFolder
end) use VM::DestroyUnusedNetworkInterfaces
end
end
# package - Export and package the VM # package - Export and package the VM
register(:package, Builder.new do registry.register(:package) do
use VM::CheckAccessible Builder.new do
use Action[:halt] use VM::CheckAccessible
use VM::ClearForwardedPorts use Action[:halt]
use VM::ClearSharedFolders use VM::ClearForwardedPorts
use VM::Modify use VM::ClearSharedFolders
use VM::Export use VM::Modify
use VM::PackageVagrantfile use VM::Export
use VM::Package use VM::PackageVagrantfile
end) use VM::Package
end
end
# box_add - Download and add a box. # box_add - Download and add a box.
register(:box_add, Builder.new do registry.register(:box_add) do
use Box::Download Builder.new do
use Box::Unpackage use Box::Download
use Box::Verify use Box::Unpackage
end) use Box::Verify
end
end
# box_remove - Removes/deletes a box. # box_remove - Removes/deletes a box.
register(:box_remove, Builder.new do registry.register(:box_remove) do
use Box::Destroy Builder.new do
end) use Box::Destroy
end
end
# box_repackage - Repackages a box. # box_repackage - Repackages a box.
register(:box_repackage, Builder.new do registry.register(:box_repackage) do
use Box::Package Builder.new do
end) use Box::Package
end
end
# Other callbacks. There will be more of these in the future. For # Other callbacks. There will be more of these in the future. For
# now, these are limited to what are needed internally. # 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 # use General::Validate
end) # end)
end end
end end
end end

View File

@ -1,10 +1,12 @@
require 'vagrant/util/hash_with_indifferent_access'
module Vagrant module Vagrant
class Action class Action
# Represents an action environment which is what is passed # Represents an action environment which is what is passed
# to the `call` method of each action. This environment contains # to the `call` method of each action. This environment contains
# some helper methods for accessing the environment as well # some helper methods for accessing the environment as well
# as being a hash, to store any additional options. # as being a hash, to store any additional options.
class Environment < Hash class Environment < Util::HashWithIndifferentAccess
def initialize def initialize
@interrupted = false @interrupted = false
end end

View File

@ -216,7 +216,13 @@ module Vagrant
# #
# @return [Action::Registry] # @return [Action::Registry]
def 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 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.

View File

@ -9,6 +9,11 @@ describe Vagrant::Action::Environment do
instance["foo"].should == "bar" instance["foo"].should == "bar"
end 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 it "should keep track of interrupted state" do
instance.should_not be_interrupted instance.should_not be_interrupted
instance.interrupt! instance.interrupt!

View File

@ -44,6 +44,18 @@ describe Vagrant::Environment do
collection.directory.should == instance.boxes_path collection.directory.should == instance.boxes_path
end 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 describe "loading configuration" do
it "should load global configuration" do it "should load global configuration" do
environment = isolated_environment do |env| environment = isolated_environment do |env|