Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (45 sloc) 1.61 KB
# Knife Script to view all unused cookbooks
# Written by Jared Williams <thisis@jared.nyc>
#
#
# If you have any questions or comments, heres the ways to reach me:
# email: thisis@jared.nyc
# slack: https://devopschat.co signing up is free and my username is @jaredwilliams
# github: @jaredmichaelwilliams
# reddit: jaredw
#
# Last tested with chef version:
# ± % knife -v !8317
# Chef: 12.4.1
#
#
# Instructions
# 1. Run `knife exec unusedcookbooks.rb`
# 2. Thats it!
#
# It may be simple but before i Parallelized it and if you had a lot of cookbooks
# and versions of those cokbooks it would take FOR EV ER (http://bit.ly/1FgzEle)
# For example: I ran this on my 12" Retina MacBook with 542 versions of cookbooks
# and it took 8 minutes and 59 seconds.
#
# Now that its parallelized it takes 1 minute and 9 seconds.
#
# First, we get a list of all the cookbooks
require 'parallel'
all_cookbooks = api.get("cookbooks").keys
used_cookbooks = Array.new
# Next, we expand the run list of each node,
# and use the API to determine cookbook
# dependencies.
Parallel.map(nodes.all) do |n|
expanded_run_list = n.expand!('server').recipes
cookbook_hash = api.post("environments/#{n.chef_environment}/cookbook_versions",
{:run_list => expanded_run_list})
used_cookbooks += cookbook_hash.keys
end
# Remove duplicates
used_cookbooks.uniq!
# Diff the two lists
puts "Unused Cookbooks:"
puts all_cookbooks - used_cookbooks
exit 0