-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkatello-cleanup
executable file
·165 lines (145 loc) · 5.27 KB
/
katello-cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env ruby
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
require 'optparse'
require 'yaml'
require 'apipie-bindings'
require 'time'
@defaults = {
:noop => false,
:uri => 'https://localhost/',
:user => 'admin',
:pass => 'changeme',
:verify_ssl => true,
:timeout => 300,
:org => 1,
:ignore => [],
:age => 90*24*60*60,
}
@options = {
:yamlfile => 'katello-cleanup.yaml',
}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [options]"
opts.version = "0.1"
opts.on("-U", "--uri=URI", "URI to the Satellite") do |u|
@options[:uri] = u
end
opts.on("-t", "--timeout=TIMEOUT", OptionParser::DecimalInteger, "Timeout value in seconds for any API calls. -1 means never timeout") do |t|
@options[:timeout] = t
end
opts.on("-u", "--user=USER", "User to log in to Satellite") do |u|
@options[:user] = u
end
opts.on("-p", "--pass=PASS", "Password to log in to Satellite") do |p|
@options[:pass] = p
end
opts.on("-o", "--organization-id=ID", "ID of the Organization") do |o|
@options[:org] = o
end
opts.on("-c", "--config=FILE", "configuration in YAML format") do |c|
@options[:yamlfile] = c
end
opts.on("-n", "--noop", "do not actually execute anything") do
@options[:noop] = true
end
opts.on("--no-verify-ssl", "don't verify SSL certs") do
@options[:verify_ssl] = false
end
end
optparse.parse!
@yaml = YAML.load_file(@options[:yamlfile])
if @yaml.has_key?(:settings) and @yaml[:settings].is_a?(Hash)
@yaml[:settings].each do |key,val|
if not @options.has_key?(key)
@options[key] = val
end
end
end
@defaults.each do |key,val|
if not @options.has_key?(key)
@options[key] = val
end
end
@ignoreregex = []
@options[:ignore].each do |i|
@ignoreregex << Regexp.new(i)
end
def cleanup()
api = ApipieBindings::API.new({:uri => @options[:uri], :username => @options[:user], :password => @options[:pass], :api_version => '2', :timeout => @options[:timeout]}, {:verify_ssl => @options[:verify_ssl]})
systems = []
unique_systems = {}
dropped_systems = []
page = 0
req = nil
now = Time.now()
# we could make this faster if we add ":search => 'lastCheckin:<=$(date -I -d '90 days ago')'" here
# but then we would miss the most duplicates
while (page == 0 or req['results'].length == req['per_page'].to_i)
page += 1
req = api.resource(:hosts).call(:index, {:organization_id => @options[:org], :page => page, :per_page => 100})
systems.concat(req['results'])
end
systems.each do |system|
puts "Checking #{system['name']}"
skip_system = false
@ignoreregex.each do |i|
if i.match(system['name'])
skip_system = true
puts " System #{system['name']} matches #{i}, skipping"
end
end
if not skip_system and system['build']
skip_system = true
puts " System #{system['name']} is in build mode, skipping"
end
next if skip_system
if system['checkin_time']
t = Time.xmlschema(system['checkin_time']) rescue Time.parse(system['checkin_time'])
elsif system.key?('subscription_facet_attributes') and system['subscription_facet_attributes']['last_checkin']
t = Time.xmlschema(system['subscription_facet_attributes']['last_checkin']) rescue Time.parse(system['subscription_facet_attributes']['last_checkin'])
else
t = Time.new(0)
end
system['checkin_time_time'] = t
if now - system['checkin_time_time'] > @options[:age]
reason = "deleting #{system['name']}: #{system['id']} did not check in since #{system['checkin_time_time']}"
system['reason'] = reason
dropped_systems << system
elsif not unique_systems.has_key?(system['name'])
unique_systems[system['name']] = system
else
if system['checkin_time_time'] > unique_systems[system['name']]['checkin_time_time']
reason = "replacing #{system['name']}: #{unique_systems[system['name']]['id']} is older than #{system['id']}"
unique_systems[system['name']]['reason'] = reason
dropped_systems << unique_systems[system['name']]
unique_systems[system['name']] = system
else
reason = "replacing #{system['name']}: #{system['id']} is older than #{unique_systems[system['name']]['id']}"
system['reason'] = reason
dropped_systems << system
end
end
end
dropped_systems.each do |system|
puts "#{system['name']} (#{system['id']} #{system['checkin_time']} #{system['reason']})"
if not @options[:noop]
api.resource(:hosts).call(:destroy, {:id => system['id']})
else
puts " [noop] destroy system id #{system['id']}"
end
end
end
cleanup