Skip to content

Commit

Permalink
Merge pull request capistrano#84 from prevailhs/opt-out-no-matching-s…
Browse files Browse the repository at this point in the history
…ervers

Update Servers#find_servers to not throw exception on unknown roles
  • Loading branch information
leehambley committed Aug 24, 2011
2 parents 94ac218 + 84506c0 commit b5681ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/capistrano/configuration/servers.rb
Expand Up @@ -54,13 +54,14 @@ def find_servers(options={})
filter_server_list(hosts.uniq)
end
else
roles = role_list_from(ENV['ROLES'] || options[:roles] || self.roles.keys)
roles = roles & Array(options[:roles]) if preserve_roles && !options[:roles].nil?
roles = role_list_from(ENV['ROLES'] || options[:roles] || self.roles.keys)
roles = roles & Array(options[:roles]) if preserve_roles && !options[:roles].nil?

only = options[:only] || {}
except = options[:except] || {}

servers = roles.inject([]) { |list, role| list.concat(self.roles[role]) }
# If we don't have a def for a role it means its bogus, skip it so higher level can handle
servers = roles.inject([]) { |list, role| list.concat(self.roles[role] || []) }
servers = servers.select { |server| only.all? { |key,value| server.options[key] == value } }
servers = servers.reject { |server| except.any? { |key,value| server.options[key] == value } }

Expand Down Expand Up @@ -103,7 +104,6 @@ def role_list_from(roles)
roles = build_list(roles)
roles.map do |role|
role = String === role ? role.strip.to_sym : role
raise ArgumentError, "unknown role `#{role}'" unless self.roles.key?(role)
role
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/configuration/servers_test.rb
Expand Up @@ -39,11 +39,11 @@ def test_task_with_single_role_should_apply_only_to_that_role
assert_equal %w(web1 web2).sort, @config.find_servers_for_task(task).map { |s| s.host }.sort
end

def test_task_with_unknown_role_should_raise_exception
# NOTE Rather than throw an error, as it used to, we return an
# empty array so that if a task is okay with a missing role it can continue on
def test_task_with_unknown_role_should_return_empty_array
task = new_task(:testing, @config, :roles => :bogus)
assert_raises(ArgumentError) do
@config.find_servers_for_task(task)
end
assert_equal [], @config.find_servers_for_task(task)
end

def test_task_with_hosts_option_should_apply_only_to_those_hosts
Expand Down

0 comments on commit b5681ad

Please sign in to comment.