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
|
# File 'lib/chef/knife/cleanup_versions.rb', line 47
def cookbooks
ui.msg "Searching for unused cookboks versions..."
all_cookbooks = rest.get_rest("/cookbooks?num_versions=all")
latest_cookbooks = rest.get_rest("/cookbooks?latest")
cbv = all_cookbooks.inject({}) do |collected, ( cookbook, versions )|
collected[cookbook] = versions["versions"].map {|v| v['version']}
collected
end
latest = latest_cookbooks.inject({}) do |collected, ( cookbook, versions )|
collected[cookbook] = versions["versions"].map {|v| v['version']}
collected
end
latest.each_key do |cb|
cbv[cb].delete(latest[cb][0])
end
Chef::Environment.list.each_key do |env_list|
env = Chef::Environment.load(env_list)
next unless !env.cookbook_versions.empty?
env.cookbook_versions.each_key do |cb|
cb_ver = env.cookbook_versions[cb].split(" ").last
begin
cbv[cb].delete(cb_ver)
rescue
"Skipping..."
end
end
end
confirm("Do you really want to delete unused cookbook versions from the server") if config[:delete]
ui.msg "Cookbook Versions:"
key_length = cbv.empty? ? 0 : cbv.keys.map {|name| name.size }.max + 2
cbv.each_key do |cb|
print " #{cb.ljust(key_length)}"
cbv[cb].each do |cb_ver|
print "#{cb_ver} "
if config[:delete]
dir = ".cleanup/#{cb}/"
backup_cookbook(cb,cb_ver,dir) if config[:backup]
delete_cookbook(cb, cb_ver)
end
end
print "\n"
end
if !config[:delete]
ui.msg "Not deleting unused cookbook versions; use --delete if you want to remove them"
end
end
|