1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Merge pull request #32 from johnf/master

Add HOSTROLEFILTER environment variable, allows commands `$ ROLEFILTER=web cap <stage> deploy`
This commit is contained in:
Lee Hambley 2011-05-19 11:52:17 -07:00
commit 3dfe5fafe0
3 changed files with 29 additions and 3 deletions

View file

@ -74,5 +74,8 @@ The following options are understood:
<%= color 'HOSTFILTER', :bold %> <%= color 'HOSTFILTER', :bold %>
Execute tasks against this comma-separated list of host, but only if the host has the proper role for the task. Execute tasks against this comma-separated list of host, but only if the host has the proper role for the task.
<%= color 'HOSTROLEFILTER', :bold %>
Execute tasks against the hosts in this comma-separated list of roles, but only if the host has the proper role for the task.
<%= color 'ROLES', :bold %> <%= color 'ROLES', :bold %>
Execute tasks against this comma-separated list of roles. Hosts which do not have the right roles will be skipped. Execute tasks against this comma-separated list of roles. Hosts which do not have the right roles will be skipped.

View file

@ -26,6 +26,9 @@ module Capistrano
# Yet additionally, if the HOSTFILTER environment variable is set, it # Yet additionally, if the HOSTFILTER environment variable is set, it
# will limit the result to hosts found in that (comma-separated) list. # will limit the result to hosts found in that (comma-separated) list.
# #
# If the HOSTROLEFILTER environment variable is set, it will limit the
# result to hosts found in that (comma-separated) list of roles
#
# Usage: # Usage:
# #
# # return all known servers # # return all known servers
@ -72,9 +75,21 @@ module Capistrano
protected protected
def filter_server_list(servers) def filter_server_list(servers)
return servers unless ENV['HOSTFILTER'] return servers unless ENV['HOSTFILTER'] or ENV['HOSTROLEFILTER']
filters = ENV['HOSTFILTER'].split(/,/) if ENV['HOSTFILTER']
servers.select { |server| filters.include?(server.host) } filters = ENV['HOSTFILTER'].split(/,/)
servers.select { |server| filters.include?(server.host) }
elsif ENV['HOSTROLEFILTER']
filters = ENV['HOSTROLEFILTER'].split(/,/).map do |role|
local_roles = roles[role.to_sym]
if local_roles.is_a? Array
roles[role.to_sym]
else
roles[role.to_sym].servers
end
end.flatten
servers.select { |server| filters.include?(server) }
end
end end
def server_list_from(hosts) def server_list_from(hosts)

View file

@ -131,6 +131,14 @@ class ConfigurationServersTest < Test::Unit::TestCase
ENV.delete('HOSTFILTER') ENV.delete('HOSTFILTER')
end end
def test_task_with_hostrolefilter_environment_variable_should_apply_only_to_those_hosts
ENV['HOSTROLEFILTER'] = "web"
task = new_task(:testing)
assert_equal %w(web1 web2).sort, @config.find_servers_for_task(task).map { |s| s.host }.sort
ensure
ENV.delete('HOSTROLEFILTER')
end
def test_task_with_only_should_apply_only_to_matching_tasks def test_task_with_only_should_apply_only_to_matching_tasks
task = new_task(:testing, @config, :roles => :app, :only => { :primary => true }) task = new_task(:testing, @config, :roles => :app, :only => { :primary => true })
assert_equal %w(app1), @config.find_servers_for_task(task).map { |s| s.host } assert_equal %w(app1), @config.find_servers_for_task(task).map { |s| s.host }