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,20 +1,22 @@
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
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::Provision use VM::Provision
end) 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
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::CleanMachineFolder use VM::CleanMachineFolder
use VM::ClearForwardedPorts use VM::ClearForwardedPorts
@ -28,47 +30,59 @@ module Vagrant
use VM::Customize use VM::Customize
use VM::Modify use VM::Modify
use VM::Boot use VM::Boot
end) 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
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::DiscardState use VM::DiscardState
use VM::Halt use VM::Halt
end) end
end
# suspend - Suspends the VM # suspend - Suspends the VM
register(:suspend, Builder.new do registry.register(:suspend) do
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::Suspend use VM::Suspend
end) end
end
# resume - Resume a VM # resume - Resume a VM
register(:resume, Builder.new do registry.register(:resume) do
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::Resume use VM::Resume
end) end
end
# reload - Halts then restarts the VM # reload - Halts then restarts the VM
register(:reload, Builder.new do registry.register(:reload) do
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use Action[:halt] use Action[:halt]
use Action[:start] use Action[:start]
end) 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
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use VM::CheckBox use VM::CheckBox
use VM::Import use VM::Import
use VM::MatchMACAddress use VM::MatchMACAddress
use VM::CheckGuestAdditions use VM::CheckGuestAdditions
use Action[:start] use Action[:start]
end) 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
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use Action[:halt], :force => true use Action[:halt], :force => true
use VM::ProvisionerCleanup use VM::ProvisionerCleanup
@ -76,10 +90,12 @@ module Vagrant
use VM::Destroy use VM::Destroy
use VM::CleanMachineFolder use VM::CleanMachineFolder
use VM::DestroyUnusedNetworkInterfaces use VM::DestroyUnusedNetworkInterfaces
end) end
end
# package - Export and package the VM # package - Export and package the VM
register(:package, Builder.new do registry.register(:package) do
Builder.new do
use VM::CheckAccessible use VM::CheckAccessible
use Action[:halt] use Action[:halt]
use VM::ClearForwardedPorts use VM::ClearForwardedPorts
@ -88,30 +104,37 @@ module Vagrant
use VM::Export use VM::Export
use VM::PackageVagrantfile use VM::PackageVagrantfile
use VM::Package use VM::Package
end) 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
Builder.new do
use Box::Download use Box::Download
use Box::Unpackage use Box::Unpackage
use Box::Verify use Box::Verify
end) 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
Builder.new do
use Box::Destroy use Box::Destroy
end) end
end
# box_repackage - Repackages a box. # box_repackage - Repackages a box.
register(:box_repackage, Builder.new do registry.register(:box_repackage) do
Builder.new do
use Box::Package use Box::Package
end) 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|