Class: Chef::Knife::TidyServerReport

Inherits:
Chef::Knife show all
Includes:
TidyBase
Defined in:
lib/chef/knife/tidy_server_report.rb

Instance Method Summary collapse

Methods included from TidyBase

#completion_message, included, #rest, #server, #tidy

Instance Method Details

#all_environments(org) ⇒ Object



157
158
159
# File 'lib/chef/knife/tidy_server_report.rb', line 157

def all_environments(org)
  rest.get("/organizations/#{org}/environments").values
end

#all_orgsObject



153
154
155
# File 'lib/chef/knife/tidy_server_report.rb', line 153

def all_orgs
  rest.get('organizations').keys
end

#check_cookbook_list(cb_list, cb, version) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/knife/tidy_server_report.rb', line 176

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/chef/knife/tidy_server_report.rb', line 192

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



133
134
135
136
137
138
139
# File 'lib/chef/knife/tidy_server_report.rb', line 133

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef/knife/tidy_server_report.rb', line 118

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_reportsObject



92
93
94
95
96
97
98
# File 'lib/chef/knife/tidy_server_report.rb', line 92

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



88
89
90
# File 'lib/chef/knife/tidy_server_report.rb', line 88

def ensure_reports_dir!
  Dir.mkdir(tidy.reports_dir) unless Dir.exist?(tidy.reports_dir)
end

#environment_constraints(org) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/chef/knife/tidy_server_report.rb', line 161

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/knife/tidy_server_report.rb', line 102

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

#runObject



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
# 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

  pre_12_3_nodes = []
  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)

    nodes.each do |node|
      chef_version = Chef::VersionString.new(node['chef_packages']['chef']['version'])
      if chef_version < "12.3"
        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

    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, '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"))
    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"))

    if pre_12_3_nodes.length > 0
      ui.warn "#{pre_12_3_nodes.length} nodes have been detected in the organization #{org} running chef-client versions prior to 12.3 - this means that the list of stale cookbooks for these nodes may not have been correctly calculated and your report may not be complete for this organization."
    end
  end

  completion_message
end

#unused_cookbooks(used_list, cb_list) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chef/knife/tidy_server_report.rb', line 141

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