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
110
111
112
113
114
115
116
|
# File 'lib/chef/knife/audit.rb', line 39
def run
if @name_args.empty?
display_cookbooks = {}
else
display_cookbooks = @name_args
end
self.config = Chef::Config.merge!(config)
env = config[:environment]
num_versions = config[:all_versions] ? "num_versions=all" : "num_versions=1"
if display_cookbooks.empty?
api_endpoint = env ? "/environments/#{env}/cookbooks?#{num_versions}" : "/cookbooks?#{num_versions}"
cookbook_list = rest.get_rest(api_endpoint)
else
cookbook_list = {}
display_cookbooks.each do |cookbook_name|
api_endpoint = env ? "/environments/#{env}/cookbooks/#{cookbook_name}" : "cookbooks/#{cookbook_name}"
begin
cookbook_list.merge!(rest.get_rest(api_endpoint))
rescue
ui.error("Cookbook #{cookbook_name} could not be found on the server!")
exit 1
end
end
end
cookbook_list.each do |name,book|
book["count"] = 0
book["nodes"] = []
end
query = "*:*"
Shef::Extensions.extend_context_object(self)
node_list = nodes.find(query)
node_list.each do |node|
recipes = node.expand!.recipes.to_a
node_cookbook_list = recipes.map{ |x| x.match(/[^\:]+/)[0] }.uniq
node_cookbook_list.each do |cookbook|
if cookbook_list.has_key?(cookbook)
cookbook_list[cookbook]["count"] += 1
cookbook_list[cookbook]["nodes"] << node.name
end
end
end
format_cookbook_audit_list_for_display(cookbook_list).each do |line|
ui.msg(line)
end
end
|