lib/capitate/cap_ext/variables.rb in capitate-0.2.6 vs lib/capitate/cap_ext/variables.rb in capitate-0.2.7

- old
+ new

@@ -10,12 +10,10 @@ # Fetch (alias method chained) variable. # Displays usage message from recipe docs if variable not found. # # See capistrano fetch for usage info. # - # Recipe docs are in lib/doc/the_namespace.yml - # def fetch_with_capitate(variable, *args) begin fetch_without_capitate(variable, *args) rescue IndexError message = capitate.usage(variable) @@ -48,9 +46,61 @@ return fetch(variable, *args) rescue IndexError set variable, default end default + end + + # Fetch roles with name and options + # + # ==== Options + # +name+:: Role name to look for + # +options+:: Options to match on, e.g. :primary => true + # + # ==== Examples + # fetch_roles(:db) => [ "10.0.6.71", "10.0.6.72" ] + # fetch_roles(:db, :primary => true) => [ "10.0.6.71" ] + # + def fetch_roles(name, options = {}) + matched_roles = Set.new + + roles.each do |role_info| + role_name = role_info.first + + next unless role_name == name + + role = role_info.last + role.each do |server| + + abort = false + options.each do |k, v| + unless server.options[k] == v + abort = true + break + end + end + next if abort + + matched_roles << server + end + end + matched_roles.to_a + end + + # Fetch first role with name and options + # + # ==== Options + # +name+:: Role name to look for + # +options+:: Options to match on, e.g. :primary => true + # + # ==== Examples + # fetch_roles(:db) => [ "10.0.6.71", "10.0.6.72" ] + # fetch_roles(:db, :primary => true) => [ "10.0.6.71" ] + # + def fetch_role(name, options = {}) + matched = fetch_roles(name, options) + return matched.first if matched + nil end end end \ No newline at end of file