Actions::Base developer documentation

This commit is contained in:
Mitchell Hashimoto 2010-03-04 00:02:59 -08:00
parent 816a1734e4
commit 94280943f4
1 changed files with 49 additions and 30 deletions

View File

@ -1,22 +1,37 @@
module Vagrant module Vagrant
module Actions module Actions
# Base class for any command actions. A command action handles # Base class for any command actions.
# executing a step or steps on a given Vagrant::VM object. The #
# action should define any callbacks that it will call, or # Actions are the smallest unit of functionality found within
# attach itself to some callbacks on the VM object. # Vagrant. Vagrant composes many actions together to execute
# its complex tasks while keeping the individual pieces of a
# task as discrete reusable actions. Actions are ran exclusively
# by an {Runner action runner} which is simply a subclass of {Runner}.
#
# Actions work by implementing any or all of the following methods
# which a {Runner} executes:
#
# * `prepare` - Called once for each action before any action has `execute!`
# called. This is meant for basic setup.
# * `execute!` - This is where the meat of the action typically goes;
# the main code which executes the action.
# * `cleanup` - This is called exactly once for each action after every
# other action is completed. It is meant for cleaning up any resources.
# * `rescue` - This is called if an exception occurs in _any action_. This
# gives every other action a chance to clean itself up.
#
# For details of each step of an action, read the specific function call
# documentation below.
class Base class Base
# The {Runner runner} which is executing the action
attr_reader :runner attr_reader :runner
# Included so subclasses don't need to include it themselves. # Included so subclasses don't need to include it themselves.
include Vagrant::Util include Vagrant::Util
# Initialization of the actions are done all at once. The guarantee # Initialization of the action, passing any arguments which may have
# is that when an action is initialized, no other action has had # been given to the {Runner runner}. This method can be used by subclasses
# its `prepare` or `execute!` method called yet, so an action can # to save any of the configuration options which are passed in.
# setup anything it needs to with this safety. An example of this
# would be instance_evaling the vm instance to include a module so
# additionally functionality could be defined on the vm which other
# action `prepare` methods may rely on.
def initialize(runner, *args) def initialize(runner, *args)
@runner = runner @runner = runner
end end
@ -24,28 +39,32 @@ module Vagrant
# This method is called once per action, allowing the action # This method is called once per action, allowing the action
# to setup any callbacks, add more events, etc. Prepare is # to setup any callbacks, add more events, etc. Prepare is
# called in the order the actions are defined, and the action # called in the order the actions are defined, and the action
# itself has no control over this, so no race conditions between # itself has no control over this.
# action setups should be done here.
def prepare
# Examples:
# #
# Perhaps we need an additional action to go, specifically # Examples of its usage:
# maybe only if a configuration is set
# #
# Perhaps we need an additional action only if a configuration is set:
#
# def prepare
# @vm.actions << FooAction if Vagrant.config[:foo] == :bar # @vm.actions << FooAction if Vagrant.config[:foo] == :bar
end # end
#
def prepare; end
# This method is called once, after preparing, to execute the # This method is called once, after preparing, to execute the
# actual task. This method is responsible for calling any # actual task. This method is responsible for calling any
# callbacks. Adding new actions here will have NO EFFECT, and # callbacks. Adding new actions here will have unpredictable
# adding callbacks has unpredictable effects. # effects and should never be done.
def execute!
# Example code:
# #
# Examples of its usage:
#
# def execute!
# @vm.invoke_callback(:before_oven, "cookies") # @vm.invoke_callback(:before_oven, "cookies")
# Do lots of stuff here # # Do lots of stuff here
# @vm.invoke_callback(:after_oven, "more", "than", "one", "option") # @vm.invoke_callback(:after_oven, "more", "than", "one", "option")
end # end
#
def execute!; end
# This method is called after all actions have finished executing. # This method is called after all actions have finished executing.
# It is meant as a place where final cleanup code can be done, knowing # It is meant as a place where final cleanup code can be done, knowing