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
Need the block here to get the search method to invoke multiple searches and aggregate results for result sets over 1k.
- #run ⇒ Object
- #unused_cookbooks(used_list, cb_list) ⇒ Object
Methods included from TidyBase
#action_needed, #action_needed_file_path, #completion_message, included, #rest, #server, #server_warnings_file_path, #tidy
Instance Method Details
#all_environments(org) ⇒ Object
181 182 183 |
# File 'lib/chef/knife/tidy_server_report.rb', line 181 def all_environments(org) rest.get("/organizations/#{org}/environments").values end |
#all_orgs ⇒ Object
177 178 179 |
# File 'lib/chef/knife/tidy_server_report.rb', line 177 def all_orgs rest.get('organizations').keys end |
#check_cookbook_list(cb_list, cb, version) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/chef/knife/tidy_server_report.rb', line 200 def check_cookbook_list(cb_list, cb, version) if cb_list[cb] cb_list[cb].each do |v| versions_not_satisfied = [] if Gem::Dependency.new('', version).match?('', v) return [v] else versions_not_satisfied.push(v) end if v == cb_list[cb].last ui.warn("Pin of #{cb} #{version} not satisfied by current versions of cookbook: [#{versions_not_satisfied.join(', ')}]") end end else ui.warn("Cookbook #{cb} #{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
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/chef/knife/tidy_server_report.rb', line 219 def check_environment_pins(used_cookbooks, pins, cb_list) pins.each do |cb, versions| versions.each do |version| next if version == "<= 0.0.0" 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) break end end result = check_cookbook_list(cb_list, cb, version) used_cookbooks[cb].push(result[0]) if result && !used_cookbooks[cb].include?(result[0]) else # No cookbook version for that pin, look through the full cookbook list for a match 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
156 157 158 159 160 161 162 |
# File 'lib/chef/knife/tidy_server_report.rb', line 156 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chef/knife/tidy_server_report.rb', line 141 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
115 116 117 118 119 120 121 |
# File 'lib/chef/knife/tidy_server_report.rb', line 115 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
111 112 113 |
# File 'lib/chef/knife/tidy_server_report.rb', line 111 def ensure_reports_dir! Dir.mkdir(tidy.reports_dir) unless Dir.exist?(tidy.reports_dir) end |
#environment_constraints(org) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/chef/knife/tidy_server_report.rb', line 185 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
Need the block here to get the search method to invoke multiple searches and aggregate results for result sets over 1k.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/chef/knife/tidy_server_report.rb', line 125 def nodes_list(org) node_results = [] Chef::Search::Query.new("#{server.root_url}/organizations/#{org}").search( :node, '*:*', :filter_result => { 'name' => ['name'], 'cookbooks' => ['cookbooks'], 'ohai_time' => ['ohai_time'], 'chef_packages' => ['chef_packages'] } ) do |node| node_results << node end node_results 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/chef/knife/tidy_server_report.rb', line 20 def run ensure_reports_dir! FileUtils.rm_f(server_warnings_file_path) ui.stdout.puts(ui.color("Writing to #{tidy.reports_dir} directory", :magenta)) 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| pre_12_3_nodes = [] unconverged_recent_nodes = [] 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) db_nodes = rest.get("/organizations/#{org}/nodes") unless nodes.length == db_nodes.length = "Search index is out of date! No cleanup action will be taken for #{org}." ui.error() action_needed(, server_warnings_file_path) next end nodes.each do |node| # If the node hasn't checked in. if !node['chef_packages'] # If the node is under an hour old. if (Time.now.to_i - node['ohai_time'].to_i) < 3600 unconverged_recent_nodes << node['name'] end next end chef_version = Gem::Version.new(node['chef_packages']['chef']['version']) # If the node has checked in within the node_threshold with a client older than 12.3 if chef_version < Gem::Version.new("12.3") && (Time.now.to_i - node['ohai_time'].to_i) <= node_threshold * 86400 pre_12_3_nodes << node['name'] end end 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 pins = environment_constraints(org) used_cookbooks = check_environment_pins(used_cookbooks, pins, cb_list) 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, 'org_total_node_count': nodes.count, '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"), backup=false) tidy.write_new_file(version_count, ::File.join(tidy.reports_dir, "#{org}_cookbook_count.json"), backup=false) tidy.write_new_file(stale_nodes_hash, ::File.join(tidy.reports_dir, "#{org}_stale_nodes.json"), backup=false) if pre_12_3_nodes.length > 0 = "#{pre_12_3_nodes.length} nodes in organization #{org} have converged in the last #{node_threshold} days with a chef-client < 12.3. These nodes' cookbook versions WILL NOT be factored in the stale cookbooks versions report. Continuing with the server cleanup will delete cookbooks in-use by these nodes." ui.warn() action_needed(, server_warnings_file_path) end if unconverged_recent_nodes.length > 0 "#{unconverged_recent_nodes.length} nodes have been created in the last hour that have yet to converge in organization #{org}. These nodes WILL NOT be factored in the stale cookbook verisons report. Continuing with the server cleanup will delete cookbooks in-use by these nodes." ui.warn() action_needed(, server_warnings_file_path) end end end |
#unused_cookbooks(used_list, cb_list) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/chef/knife/tidy_server_report.rb', line 164 def unused_cookbooks(used_list, cb_list) unused_list = {} cb_list.each do |name, versions| versions.sort! {| a, b | Gem::Version.new(a) <=> Gem::Version.new(b) } 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] - [versions.last] # Don't delete the most recent version as it might not be in a run_list yet. end end unused_list end |