Skip to content

Instantly share code, notes, and snippets.

@mikehale
Forked from Farzy/cookbook.rb
Created July 6, 2010 23:03
Show Gist options
  • Save mikehale/466061 to your computer and use it in GitHub Desktop.
Save mikehale/466061 to your computer and use it in GitHub Desktop.
class Chef
module Mixin
module Language
# esearch(:node, 'role\[admin\]')
# recursively search for this role/recipe in all roles
def esearch(context, search)
if context == :role
ret = []
# First solve all roles:
roles = search(:role, search)
if roles
roles.each do |role|
ret << esearch(:role, "run_list:role\\[#{role.name}\\]")
end
end
ret << roles
ret.flatten
elsif context == :node
# Find all roles including this recipe
nodes = []
roles = esearch(:role, "run_list:"+search)
roles.each do |r|
nodes << search(:node, "run_list:role\\[#{r.name}\\]")
end
nodes.flatten!
nodes.sort!
# Unify by to_s value
nodes = nodes.inject({}) do |hash,item|
hash[item.to_s]||=item
hash
end.values
nodes
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment