[chef] Re: Re: Re: Re: Listing all cookbooks used in an environment


Chronological Thread 
  • From: DV < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Listing all cookbooks used in an environment
  • Date: Fri, 10 May 2013 14:44:46 -0700

Thanks a lot, this works. I've added some formatting and gotten rid of "ERROR: ArgumentError: Attribute recipes is not defined!" error by adding an extra check. The error showed up because we have some node objects that don't have a runlist.

require 'chef'
Chef::Config.from_file('knife.rb')
envs = Chef::Environment.list.keys
q = Chef::Search::Query.new
envs.sort.each do |e|
  if e.include? "sandbox"
    next
  end
  puts "Environment: #{e}\n"
  constrained_cookbooks = Chef::Environment.load(e).cookbook_versions.keys
  all_cookbooks =[]
  nodes = q.search(:node, "chef_environment:#{e}").first.reject(&:nil?)
  nodes.each do |n|
    if !n or !n.has_key? 'recipes'
      next
    end
    n.recipes.map{|r| r.split('::')[0]}.each do |cookbook|
      all_cookbooks << cookbook unless all_cookbooks.include?(cookbook)
    end
  end
  puts "Unconstrained cookbooks: #{(all_cookbooks - constrained_cookbooks).join ' '}\n\n"
end



On Wed, May 8, 2013 at 11:59 AM, Ranjib Dey < " target="_blank"> > wrote:
i think the bug was in the  search call, which returns an array with 3 element, first being the nodes, second is starting index and third with total nodes,


On Wed, May 8, 2013 at 11:58 AM, Ranjib Dey < " target="_blank"> > wrote:
just updated the gist,
can you try agains?


On Wed, May 8, 2013 at 11:35 AM, DV < " target="_blank"> > wrote:
Thanks :) I gave it a try and so far after correcting syntax errors it's saying that there's no recipes attribute for the array (n, I guess), so I'll have to look into that.


On Mon, May 6, 2013 at 4:01 PM, Ranjib Dey < " target="_blank"> > wrote:
try something like this,
check for syntax . i havent run this :-)


On Mon, May 6, 2013 at 3:43 PM, DV < " target="_blank"> > wrote:
Hey all,

So I've been trying to write a script which will show me which cookbooks are used in an environment that are not version-locked.

I thought I have it all figured out, with the following command fetching a list of cookbooks used in an environment. It collects "recipes" attribute of all nodes in the environment and extracts cookbook names:

knife search node "chef_environment:${CHEF_ENVIRONMENT}" -a recipes| grep -v -e ^id -e ^recipes -e found$ -e ^$ | awk -F':' '{print $1}' | sed 's/^ *//; s/; */;/g' |sort|uniq

The command does its job, but it has a major downside: it doesn't list cookbooks that are not explicitly listed in the "recipes" attribute. So, for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat" cookbook is assigned to some node in the environment, the command above will list "tomcat" but not "java".

Is there a better way to fetch a list of cookbooks? Or should I add logic to resolve cookbook dependencies? Thanks.

--
Best regards, Dmitriy V.




--
Best regards, Dmitriy V.





--
Best regards, Dmitriy V.



Archive powered by MHonArc 2.6.16.

§