Class: Chef::Knife::TidyServerClean
Instance Method Summary
collapse
Methods included from TidyBase
#completion_message, included, #rest, #server, #tidy
Instance Method Details
#all_orgs ⇒ Object
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 141
def all_orgs
orgs = []
report_files.each do |file|
org = ::File.basename(file).match(/^(.*?)_/).captures[0]
if org
orgs.push(org) unless orgs.include?(org)
end
end
orgs
end
|
#clean_cookbooks(org) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 67
def clean_cookbooks(org)
queue = Chef::Util::ThreadedJobQueue.new
unused_cookbooks_file = ::File.join(tidy.reports_dir, "#{org}_unused_cookbooks.json")
return unless ::File.exist?(unused_cookbooks_file)
puts "INFO: Cleaning cookbooks for Org: #{org}, using #{unused_cookbooks_file}"
unused_cookbooks = FFI_Yajl::Parser.parse(::File.read(unused_cookbooks_file), symbolize_names: true)
unused_cookbooks.keys.each do |cookbook|
versions = unused_cookbooks[cookbook]
versions.each do |version|
queue << lambda { delete_cookbook_job(org, cookbook, version) }
end
end
queue.process(config[:concurrency].to_i)
end
|
#clean_nodes(org) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 100
def clean_nodes(org)
queue = Chef::Util::ThreadedJobQueue.new
stale_nodes_file = ::File.join(tidy.reports_dir, "#{org}_stale_nodes.json")
return unless ::File.exist?(stale_nodes_file)
puts "INFO: Cleaning stale nodes for Org: #{org}, using #{stale_nodes_file}"
stale_nodes = FFI_Yajl::Parser.parse(::File.read(stale_nodes_file), symbolize_names: true)
stale_nodes[:list].each do |node|
queue << lambda { delete_node_job(org, node) }
end
queue.process(config[:concurrency].to_i)
end
|
#delete_cookbook_job(org, cookbook, version) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 82
def delete_cookbook_job(org, cookbook, version)
path = "/organizations/#{org}/cookbooks/#{cookbook}/#{version}"
if config[:dry_run]
printf("INFO: Would have executed `rest.delete(#{path})`\n")
return
end
rest.delete(path)
response = '200'
rescue Net::HTTPServerException
response = $!.response.code
ensure
return if config[:dry_run]
formatted = response == '200' ?
ui.color(' Deleting %-20s %-10s %10s', :green) :
ui.color(' Deleting %-20s %-10s %10s', :red)
printf("#{formatted}\n", cookbook, version, response)
end
|
#delete_node_job(org, node) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 112
def delete_node_job(org, node)
path = "/organizations/#{org}/nodes/#{node}"
if config[:dry_run]
printf("INFO: Would have executed `rest.delete(#{path})`\n")
return
end
rest.delete(path)
response = '200'
rescue Net::HTTPServerException
response = $!.response.code
ensure
return if config[:dry_run]
formatted = response == '200' ?
ui.color(' Deleting %-20s %10s', :green) :
ui.color(' Deleting %-20s %10s', :red)
printf("#{formatted}\n", node, response)
end
|
#ensure_reports_dir ⇒ Object
130
131
132
133
134
135
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 130
def ensure_reports_dir
unless ::File.directory?(tidy.reports_dir)
ui.error "#{tidy.reports_dir} not found!"
exit 1
end
end
|
#report_files ⇒ Object
137
138
139
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 137
def report_files
Dir[::File.join(tidy.reports_dir, '**')]
end
|
#run ⇒ Object
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
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 33
def run
ui.warn "This feature is not enabled"
exit
STDOUT.sync = true
ensure_reports_dir
puts "INFO: Reading from #{tidy.reports_dir} directory"
puts "INFO: Using thread concurrency #{config[:concurrency]}"
configure_chef
if config[:only_cookbooks] && config[:only_nodes]
ui.error 'Cannot use --only-cookbooks AND --only-nodes'
exit 1
end
ui.confirm('This operation will delete items on Chef Server, continue') unless config[:unattended]
orgs = if config[:org_list]
config[:org_list].split(',')
else
all_orgs
end
orgs.each do |org|
clean_cookbooks(org) unless config[:only_nodes]
clean_nodes(org) unless config[:only_cookbooks]
end
completion_message
end
|