Class: Chef::Knife::TidyServerClean
Instance Method Summary
collapse
Methods included from TidyBase
#completion_message, included, #rest, #server, #tidy
Instance Method Details
#all_orgs ⇒ Object
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 138
def all_orgs
orgs = []
report_files.each do |file|
org = ::File.basename(file).match(/^(.*?)_(cookbook_count|unused_cookbooks|stale_nodes)\.json/).captures[0]
if org
orgs.push(org) unless orgs.include?(org)
end
end
orgs
end
|
#clean_cookbooks(org) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 71
def clean_cookbooks(org)
ui.warn "Cleaning cookbooks is a feature not yet enabled."
return
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
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 99
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
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 88
def delete_cookbook_job(org, cookbook, version)
path = "/organizations/#{org}/cookbooks/#{cookbook}/#{version}"
if config[:dry_run]
printf("DRYRUN: Would have executed `rest.delete(#{path})`\n")
return
end
printf("INFO: Deleting #{path}\n")
rest.delete(path)
rescue Net::HTTPServerException
end
|
#delete_node_job(org, node) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 111
def delete_node_job(org, node)
paths = ["/organizations/#{org}/nodes/#{node}", "/organizations/#{org}/clients/#{node}"]
paths.each do |path|
if config[:dry_run]
printf("DRYRUN: Would have executed `rest.delete(#{path})`\n")
next
else
begin
printf("INFO: Deleting #{path}\n")
rest.delete(path)
rescue Net::HTTPServerException
end
end
end
end
|
#ensure_reports_dir ⇒ Object
127
128
129
130
131
132
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 127
def ensure_reports_dir
unless ::File.directory?(tidy.reports_dir)
ui.error "#{tidy.reports_dir} not found!"
exit 1
end
end
|
#report_files ⇒ Object
134
135
136
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 134
def report_files
Dir[::File.join(tidy.reports_dir, '**.json')]
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
66
67
68
69
|
# File 'lib/chef/knife/tidy_server_clean.rb', line 33
def run
STDOUT.sync = true
ensure_reports_dir
configure_chef
if config[:only_cookbooks] && config[:only_nodes]
ui.error 'Cannot use --only-cookbooks AND --only-nodes'
exit 1
end
deletions = if config[:only_cookbooks]
"cookbooks"
elsif config[:only_nodes]
"nodes (and associated clients and ACLs)"
else
"cookbooks and nodes (and associated clients and ACLs)"
end
orgs = if config[:org_list]
config[:org_list].split(',')
else
all_orgs
end
ui.warn "This operation will affect the following Orgs on #{server.root_url}\n\n#{orgs}\n\n"
ui.confirm("About to delete #{deletions} from the Chef Server identified in the #{tidy.reports_dir} directory! Are you sure you wish to continue") unless config[:unattended]
orgs.each do |org|
clean_cookbooks(org) unless config[:only_nodes]
clean_nodes(org) unless config[:only_cookbooks]
end
completion_message
end
|