Permalink
Browse files

Merge pull request #51 from cgriego/opt-out-no-matching-servers

It should be possible to continue executing task chain on NoMatchingServersError
  • Loading branch information...
2 parents cdc36ec + 216f3b9 commit edf709256d243749e0da8e0e684634ad911ffbf1 @leehambley leehambley committed Aug 2, 2011
@@ -111,6 +111,9 @@ def invoke_command(cmd, options={}, &block)
# * :except - specifies a condition limiting which hosts will be selected to
# run the command. This is the inverse of :only (hosts that do _not_ match
# the condition will be selected).
+ # * :on_no_matching_servers - if :continue, will continue to execute tasks if
+ # no matching servers are found for the host criteria. The default is to raise
+ # a NoMatchingServersError exception.
# * :once - if true, only the first matching server will be selected. The default
# is false (all matching servers will be selected).
# * :max_hosts - specifies the maximum number of hosts that should be selected
@@ -156,7 +156,7 @@ def execute_on_servers(options={})
servers = find_servers_for_task(task, options)
if servers.empty?
- if ENV['HOSTFILTER']
+ if ENV['HOSTFILTER'] || task.options.merge(options)[:on_no_matching_servers] == :continue
logger.info "skipping `#{task.fully_qualified_name}' because no servers matched"
return
else
@@ -170,7 +170,10 @@ def execute_on_servers(options={})
end
else
servers = find_servers(options)
- raise Capistrano::NoMatchingServersError, "no servers found to match #{options.inspect}" if servers.empty?
+ if servers.empty?
+ raise Capistrano::NoMatchingServersError, "no servers found to match #{options.inspect}" if options[:on_no_matching_servers] != :continue
+ return
+ end
end
servers = [servers.first] if options[:once]
@@ -189,6 +189,11 @@ def test_execute_on_servers_without_current_task_should_raise_error_if_no_matchi
assert_raises(Capistrano::NoMatchingServersError) { @config.execute_on_servers(:a => :b, :c => :d) { |list| } }
end
+ def test_execute_on_servers_without_current_task_should_not_raise_error_if_no_matching_servers_and_continue_on_no_matching_servers
+ @config.expects(:find_servers).with(:a => :b, :c => :d, :on_no_matching_servers => :continue).returns([])
+ assert_nothing_raised { @config.execute_on_servers(:a => :b, :c => :d, :on_no_matching_servers => :continue) { |list| } }
+ end
+
def test_execute_on_servers_should_raise_an_error_if_the_current_task_has_no_matching_servers_by_default
@config.current_task = mock_task
@config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([])
@@ -198,7 +203,27 @@ def test_execute_on_servers_should_raise_an_error_if_the_current_task_has_no_mat
end
end
end
-
+
+ def test_execute_on_servers_should_not_raise_an_error_if_the_current_task_has_no_matching_servers_by_default_and_continue_on_no_matching_servers
+ @config.current_task = mock_task(:on_no_matching_servers => :continue)
+ @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([])
+ assert_nothing_raised do
+ @config.execute_on_servers do
+ flunk "should not get here"
+ end
+ end
+ end
+
+ def test_execute_on_servers_should_not_raise_an_error_if_the_current_task_has_no_matching_servers_by_default_and_command_continues_on_no_matching_servers
+ @config.current_task = mock_task
+ @config.expects(:find_servers_for_task).with(@config.current_task, :on_no_matching_servers => :continue).returns([])
+ assert_nothing_raised do
+ @config.execute_on_servers(:on_no_matching_servers => :continue) do
+ flunk "should not get here"
+ end
+ end
+ end
+
def test_execute_on_servers_should_determine_server_list_from_active_task
assert @config.sessions.empty?
@config.current_task = mock_task

0 comments on commit edf7092

Please sign in to comment.