Class: Chef::Knife::TidyServerReport
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::TidyServerReport
- Includes:
- TidyBase
- Defined in:
- lib/chef/knife/tidy_server_report.rb
Instance Method Summary collapse
- #all_environments(org) ⇒ Object
- #all_orgs ⇒ Object
- #check_cookbook_list(cb_list, cb, version) ⇒ Object
- #check_environment_pins(used_cookbooks, pins, cb_list) ⇒ Object
- #cookbook_count(cb_list) ⇒ Object
- #cookbook_list(org) ⇒ Object
- #delete_existing_reports ⇒ Object
- #ensure_reports_dir! ⇒ Object
- #environment_constraints(org) ⇒ Object
- #nodes_list(org) ⇒ Object
- #run ⇒ Object
- #unused_cookbooks(used_list, cb_list) ⇒ Object
Methods included from TidyBase
#completion_message, included, #rest, #server, #tidy
Instance Method Details
#all_environments(org) ⇒ Object
139 140 141 |
# File 'lib/chef/knife/tidy_server_report.rb', line 139 def all_environments(org) rest.get("/organizations/#{org}/environments").values end |
#all_orgs ⇒ Object
135 136 137 |
# File 'lib/chef/knife/tidy_server_report.rb', line 135 def all_orgs rest.get('organizations').keys end |
#check_cookbook_list(cb_list, cb, version) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/chef/knife/tidy_server_report.rb', line 158 def check_cookbook_list(cb_list, cb, version) if cb_list[cb] cb_list[cb].each do |v| if Gem::Dependency.new('', version).match?('', v) Chef::Log.debug("Pin of #{cb} can be satisfied by #{v}, adding to used list") return [v] else Chef::Log.debug("Pin of #{cb} version #{version} not satisfied by #{v}") end end else Chef::Log.info("Cookbook #{cb} version #{version} is pinned in an environment, but does not exist on the server in this org.") end return nil end |
#check_environment_pins(used_cookbooks, pins, cb_list) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/chef/knife/tidy_server_report.rb', line 174 def check_environment_pins(used_cookbooks, pins, cb_list) pins.each do |cb, versions| versions.each do |version| if used_cookbooks[cb] # This pinned cookbook is in the used list, now check for a matching version. used_cookbooks[cb].each do |v| if Gem::Dependency.new('', version).match?('', v) # This version in used_cookbooks satisfies the pin Chef::Log.debug("Pin of #{cb}: #{version} is satisfied by #{v}") break end end result = check_cookbook_list(cb_list, cb, version) used_cookbooks[cb].push(result[0]) if result else # No cookbook version for that pin, look through the full cookbook list for a match Chef::Log.debug("No used cookbook #{cb}, checking the full cookbook list") result = check_cookbook_list(cb_list, cb, version) used_cookbooks[cb] = result if result end end end used_cookbooks end |
#cookbook_count(cb_list) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/chef/knife/tidy_server_report.rb', line 115 def cookbook_count(cb_list) cb_count_list = {} cb_list.each do |name, versions| cb_count_list[name] = versions.count end cb_count_list end |
#cookbook_list(org) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/chef/knife/tidy_server_report.rb', line 100 def cookbook_list(org) cb_list = {} rest.get("/organizations/#{org}/cookbooks?num_versions=all").each do |name, data| data['versions'].each do |version_hash| version = Gem::Version.new(version_hash['version']).to_s if cb_list[name] && !cb_list[name].include?(version) cb_list[name].push(version) else cb_list[name] = [version] end end end cb_list end |
#delete_existing_reports ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/chef/knife/tidy_server_report.rb', line 81 def delete_existing_reports files = Dir[::File.join(tidy.reports_dir, '*.json')] unless files.empty? ui.confirm("You have existing reports in #{tidy.reports_dir}. Remove") FileUtils.rm(files, :force => true) end end |
#ensure_reports_dir! ⇒ Object
77 78 79 |
# File 'lib/chef/knife/tidy_server_report.rb', line 77 def ensure_reports_dir! Dir.mkdir(tidy.reports_dir) unless Dir.exist?(tidy.reports_dir) end |
#environment_constraints(org) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/chef/knife/tidy_server_report.rb', line 143 def environment_constraints(org) constraints = {} all_environments(org).each do |env| e = rest.get(env) e['cookbook_versions'].each do |cb, version| if constraints[cb] constraints[cb].push(version) unless constraints[cb].include?(version) else constraints[cb] = [version] end end end constraints end |
#nodes_list(org) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/chef/knife/tidy_server_report.rb', line 89 def nodes_list(org) Chef::Search::Query.new("#{server.root_url}/organizations/#{org}").search( :node, '*:*', :filter_result => { 'name' => ['name'], 'cookbooks' => ['cookbooks'], 'ohai_time' => ['ohai_time'] } ) end |
#run ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/chef/knife/tidy_server_report.rb', line 20 def run ensure_reports_dir! ui.warn "Writing to #{tidy.reports_dir} directory" delete_existing_reports orgs = if config[:org_list] config[:org_list].split(',') else all_orgs end stale_orgs = [] node_threshold = config[:node_threshold].to_i orgs.each do |org| ui.info " Organization: #{org}" cb_list = cookbook_list(org) version_count = cookbook_count(cb_list).sort_by(&:last).reverse.to_h used_cookbooks = {} nodes = nodes_list(org)[0] nodes.select{|node| !node['cookbooks'].nil?}.each do |node| node['cookbooks'].each do |name, version_hash| version = Gem::Version.new(version_hash['version']).to_s if used_cookbooks[name] && !used_cookbooks[name].include?(version) used_cookbooks[name].push(version) else used_cookbooks[name] = [version] end end end Chef::Log.debug("Used cookbook list before checking environments: #{used_cookbooks}") pins = environment_constraints(org) used_cookbooks = check_environment_pins(used_cookbooks, pins, cb_list) Chef::Log.debug("Used cookbook list after checking environments: #{used_cookbooks}") stale_nodes = [] nodes.each do |n| if (Time.now.to_i - n['ohai_time'].to_i) >= node_threshold * 86400 stale_nodes.push(n['name']) end end stale_nodes_hash = {'threshold_days': node_threshold, 'count': stale_nodes.count, 'list': stale_nodes} stale_orgs.push(org) if stale_nodes.count == nodes.count tidy.write_new_file(unused_cookbooks(used_cookbooks, cb_list), ::File.join(tidy.reports_dir, "#{org}_unused_cookbooks.json")) tidy.write_new_file(unused_cookbooks(used_cookbooks, cb_list), ::File.join(tidy.reports_dir, "#{org}_unused_cookbooks.json")) tidy.write_new_file(version_count, ::File.join(tidy.reports_dir, "#{org}_cookbook_count.json")) tidy.write_new_file(stale_nodes_hash, ::File.join(tidy.reports_dir, "#{org}_stale_nodes.json")) end end |
#unused_cookbooks(used_list, cb_list) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/chef/knife/tidy_server_report.rb', line 123 def unused_cookbooks(used_list, cb_list) unused_list = {} cb_list.each do |name, versions| if used_list[name].nil? # Not in the used list at all (Remove all versions) unused_list[name] = versions elsif used_list[name].sort != versions # Is in the used cookbook list, but version arrays do not match (Find unused versions) unused_list[name] = versions - used_list[name] end end unused_list end |