mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
Add Actor#current_task and simplify Task#servers
git-svn-id: http://svn.rubyonrails.org/rails/tools/switchtower@3602 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
929a016b4d
commit
0fcc748bdd
3 changed files with 20 additions and 9 deletions
|
@ -1,5 +1,7 @@
|
|||
*0.11.0* *SVN*
|
||||
|
||||
* Add Actor#current_task and simplify Task#servers
|
||||
|
||||
* Add Actor#connect! method for working around lazy connection establishing
|
||||
|
||||
* Make sure IO::TRUNC is specified for Net::SFTP uploads (#3510)
|
||||
|
|
|
@ -68,18 +68,22 @@ module SwitchTower
|
|||
|
||||
# Represents the definition of a single task.
|
||||
class Task #:nodoc:
|
||||
attr_reader :name, :options
|
||||
attr_reader :name, :actor, :options
|
||||
|
||||
def initialize(name, options)
|
||||
@name, @options = name, options
|
||||
def initialize(name, actor, options)
|
||||
@name, @actor, @options = name, actor, options
|
||||
@servers = nil
|
||||
end
|
||||
|
||||
# Returns the list of servers (_not_ connections to servers) that are
|
||||
# the target of this task.
|
||||
def servers(configuration)
|
||||
def servers
|
||||
unless @servers
|
||||
roles = [*(@options[:roles] || configuration.roles.keys)].map { |name| configuration.roles[name] or raise ArgumentError, "task #{self.name.inspect} references non-existant role #{name.inspect}" }.flatten
|
||||
roles = [*(@options[:roles] || actor.configuration.roles.keys)].
|
||||
map { |name|
|
||||
actor.configuration.roles[name] or
|
||||
raise ArgumentError, "task #{self.name.inspect} references non-existant role #{name.inspect}"
|
||||
}.flatten
|
||||
only = @options[:only] || {}
|
||||
|
||||
unless only.empty?
|
||||
|
@ -111,7 +115,7 @@ module SwitchTower
|
|||
# Define a new task for this actor. The block will be invoked when this
|
||||
# task is called.
|
||||
def define_task(name, options={}, &block)
|
||||
@tasks[name] = (options[:task_class] || Task).new(name, options)
|
||||
@tasks[name] = (options[:task_class] || Task).new(name, self, options)
|
||||
define_method(name) do
|
||||
send "before_#{name}" if respond_to? "before_#{name}"
|
||||
logger.debug "executing task #{name}"
|
||||
|
@ -334,6 +338,11 @@ module SwitchTower
|
|||
execute_on_servers(options) { }
|
||||
end
|
||||
|
||||
def current_task
|
||||
return nil if task_call_frames.empty?
|
||||
tasks[task_call_frames.last.name]
|
||||
end
|
||||
|
||||
def metaclass
|
||||
class << self; self; end
|
||||
end
|
||||
|
@ -372,8 +381,8 @@ module SwitchTower
|
|||
end
|
||||
|
||||
def execute_on_servers(options)
|
||||
task = tasks[task_call_frames.last.name]
|
||||
servers = task.servers(configuration)
|
||||
task = current_task
|
||||
servers = task.servers
|
||||
|
||||
if servers.empty?
|
||||
raise "The #{task.name} task is only run for servers matching #{task.options.inspect}, but no servers matched"
|
||||
|
|
|
@ -163,7 +163,7 @@ class ActorTest < Test::Unit::TestCase
|
|||
run "do this"
|
||||
end
|
||||
|
||||
assert_equal %w(01.example.com 02.example.com 03.example.com 04.example.com 05.example.com 06.example.com 07.example.com all.example.com), @actor.tasks[:foo].servers(@actor.configuration).sort
|
||||
assert_equal %w(01.example.com 02.example.com 03.example.com 04.example.com 05.example.com 06.example.com 07.example.com all.example.com), @actor.tasks[:foo].servers.sort
|
||||
end
|
||||
|
||||
def test_run_in_task_without_explicit_roles_selects_all_roles
|
||||
|
|
Loading…
Add table
Reference in a new issue