Skip to content

Instantly share code, notes, and snippets.

@octplane
Created June 18, 2010 16:07
Show Gist options
  • Save octplane/443833 to your computer and use it in GitHub Desktop.
Save octplane/443833 to your computer and use it in GitHub Desktop.
Two Chef additional instructions (0.8 version)
class Chef
class Node
# Recursively descend the Node run list and look for the "recipe"
# node.erun_list('recipe[accountr]')
#
def erun_list?(search, run_list=self.run_list )
Chef::Log.debug("erun_list? for #{search} in #{run_list}.")
in_recipe = run_list.run_list.find { |r| r==search}
if in_recipe != nil
Chef::Log.debug("Found #{search} in base run_list (#{run_list.run_list})...")
end
return true if in_recipe != nil
run_list.roles.each do |role_name|
role = Chef::Role.load(role_name)
if erun_list?(search, role.run_list)
Chef::Log.debug("Found #{search} in role #{role_name}.")
return true
else
Chef::Log.debug("#{search} not found in role #{role_name}.")
end
end
return false
end
end
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 not roles.nil?
roles.each do |role|
ret << esearch(:role, "run_list:role\\[#{role.name}\\]")
end
end
ret << roles
return ret.flatten
elsif context == :node
# Find all roles including this recipe
nodes = []
nodes << search(:node, "run_list:"+search)
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
return nodes
end
end
end
end
end
@octplane
Copy link
Author

Modified to handle Nodes unification correctly.

@jellybob
Copy link

Do you mind submitting this as a patch to Chef itself? This is a feature that would be welcomed by a lot of people I think.

@octplane
Copy link
Author

Unfortunately, I haven't had the opportunity to test this code against version 0.9. However, I've filled a bug at Chef to ask Chef's developer to include this feature in Chef's core features ( http://tickets.opscode.com/browse/CHEF-1355 )...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment