forked from redhat-consulting/ose-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoo-list
executable file
·396 lines (367 loc) · 11.4 KB
/
oo-list
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env oo-ruby
# Copyright 2012 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# oo-list - Utility to list various OpenShift statistics in a form
# compatible with traditional tools e.g. grep, Excel, etc.
#
# Usage: See usage function below
#
require 'getoptlong'
require 'pp'
$:.unshift('/usr/local/lib')
opts = GetoptLong.new(
["--all", GetoptLong::NO_ARGUMENT],
["--csv", "-C", GetoptLong::NO_ARGUMENT],
["--applications", "-a", GetoptLong::NO_ARGUMENT],
["--cartridges", "-c", GetoptLong::NO_ARGUMENT],
["--headers", "-h", GetoptLong::NO_ARGUMENT],
["--gears", "-g", GetoptLong::NO_ARGUMENT],
["--profiles", "-p", GetoptLong::REQUIRED_ARGUMENT],
["--nodes", "-n", GetoptLong::NO_ARGUMENT],
["--district", "-d", GetoptLong::REQUIRED_ARGUMENT],
["--show", "-s", GetoptLong::REQUIRED_ARGUMENT],
["--statistics", GetoptLong::NO_ARGUMENT],
["--login", "-l", GetoptLong::REQUIRED_ARGUMENT],
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
["--users", "-u", GetoptLong::NO_ARGUMENT],
)
def usage()
abort "Usage:
oo-list [--csv|-C] [--headers|-h] [--output|-o file] --show|-s report \\
[--login|-l login] [--district|-d district] [--profile|-p gear_profile]
Options:
--login - filter by specified login(s)
--district - filter by specified district(s)
--profile - filter by specified gear profile(s)
--statistics - collect and show statistics
--all - include expired/obsolete information
Above options only apply when appropriate.
Reports available - select one:-
applications - list applications
cartridges - list currently used cartridges
domains - list domains
districts - list districts
gears - list gears
nodes - list nodes
tokens - list authorization tokens
users - list users
"
end
args = {}
begin
opts.each{ |k,v| args[k]=v }
rescue GetoptLong::Error => e
usage
end
usage unless args.length > 0
headers = args["--headers"]
sep1=' '
sep2=','
if args["--csv"]
sep1=','
sep2=' '
end
output = $stdout
report = ''
match = 0
reports = [ "applications", "cartridges", "domains", "districts", "gears", "nodes", "tokens", "users" ].sort
reports.each do |r|
if args["--show"]
if args["--show"].downcase == r.slice(0,args["--show"].length)
match += 1 unless report == r
report = r
end
end
if args["--#{r}"]
match += 1 unless report == r
report = r
end
end
usage unless match == 1
if args["--output"] && args["--output"] != '-'
begin
output = File.open(args["--output"], "w")
rescue Exception => e
abort("#{e}")
end
end
require "#{ENV['OPENSHIFT_BROKER_DIR'] || '/var/www/openshift/broker'}/config/environment"
Rails.configuration.analytics[:enabled] = false
Mongoid.raise_not_found_error = false
user = nil
logins = []
user_ids = []
users = {}
if args["--login"]
args["--login"].split(/[ ,]/).each do |l|
login = CloudUser.normalize_login(l)
begin
user = CloudUser.with(consistency: :eventual).find_by(login: login)
logins << user
user_ids << user._id
users[user._id] = user.login
rescue Mongoid::Errors::DocumentNotFound
$stderr.puts "User #{l} not found."
end
end
abort "No users defined" unless logins.count > 0
end
begin
case report
when "applications"
#users = {}
filter = {}
if user_ids.count > 0
filter = { :owner_id.in => user_ids }
else
CloudUser.all.each do |u|
users[u._id] = u.login
end
end
if headers
h = ["login","application","components","created","application_id"]
output.puts h.join(sep1)
end
Application.where( filter ).sort{ |a,b|
a.owner_id == b.owner_id ? a['name'] <=> b['name'] : users[a.owner_id] <=> users[b.owner_id]
}.each do |app|
o = []
o << users[app.owner_id]
o << "#{app.canonical_name}-#{app.domain_namespace}"
o << app.component_instances.map { |ci| ci.cartridge_name }.sort.join(sep2)
o << app.created_at.strftime("%Y-%m-%d %H:%M:%S")
o << app._id
output.puts o.join(sep1)
end
when "districts"
filter = args["--district"] ? { 'name' => args["--district"] } : {}
if headers
h = ["name","gear_profile","gears","available_gears","nodes","district_id"]
output.puts h.join(sep1)
end
districts = District.where( filter ).sort{|a,b| a['name']<=>b['name']}.each do |d|
o = []
o << d.name
o << d.gear_size
o << d.max_capacity - d.available_capacity
o << d.max_capacity
o << d.active_servers_size
o << d.uuid
output.puts o.join(sep1)
end
when "domains"
users = Hash.new
filter = {}
if user_ids.count > 0
filter = { :owner_id.in => user_ids }
else
CloudUser.all.each do |u|
users[u._id] = u.login
end
end
if headers
h = ["domain","allowed_gears","owner","admins","members"]
output.puts h.join(sep1)
end
Domain.where( filter ).sort{|a,b| a['canonical_namespace']<=>b['canonical_namespace']}.each do |d|
u = {}
#pp d
#pp d.members
o = []
o << d.canonical_namespace
o << d.allowed_gear_sizes.join(sep2)
o << users[d.owner_id]
u[users[d.owner_id]] = true
o << d.members.map { |m|
unless u[m.name]
if m.role == :admin
u[m.name] = true
m.name
end
end
}.delete_if {|m| m.nil?}.sort.join(sep2)
o << d.members.map { |m|
unless u[m.name]
u[m.name] = true
m.name
end
}.delete_if {|m| m.nil?}.sort.join(sep2)
output.puts o.join(sep1)
end
when "gears"
servers = Hash.new
filter2 = args["--district"] ? { 'name' => args["--district"] } : {}
District.where( filter2 ).each do |district|
district.servers.each do |server|
server["gear_size"] = district.gear_size
server["district"] = district.name
server["region_name"] = server["region_name"] || "none"
server["zone_name"] = server["zone_name"] || "none"
servers[ server.name ] = server
end
end
users = Hash.new
filter1 = {}
if user_ids.count > 0
filter1 = { :owner_id.in => user_ids }
else
CloudUser.all.each do |u|
users[u._id] = u.login
end
end
if headers
h=["application","login","cartridge","gear_profile","district","node","gear_id"]
output.puts h.join(sep1)
end
Application.where( filter1 ).sort{|a,b| a['canonical_name']+a['domain_namespace']<=>b['canonical_name']+b['domain_namespace']}.each do |app|
carts = {}
app.component_instances.each do |ci|
carts[ci.group_instance_id] = ci.cartridge_name
end
app.group_instances.each do |gi|
gi.gears.each do |g|
next unless servers[g.server_identity]
o = []
o << "#{app.canonical_name}-#{app.domain_namespace}"
o << users[app.owner_id]
o << carts[gi._id]
o << servers[g.server_identity]["gear_size"]
o << servers[g.server_identity]["district"]
o << g.server_identity
o << g._id
output.puts o.join(sep1)
end
end
end
when "nodes"
servers = {}
filter = args["--district"] ? { 'name' => args["--district"] } : {}
District.where( filter ).each do |district|
district.servers.each do |s|
server = {}
server[:gears] = 0
server[:max_gears] = 0
server[:gear_size] = district.gear_size
server[:district] = district.name
server[:region_name] = s["region_name"] || "none"
server[:zone_name] = s["zone_name"] || "none"
if args["--statistics"]
stats = OpenShift::ApplicationContainerProxy.instance(s.name).get_node_details %w[
max_active_gears
gears_started_count
gears_idle_count
gears_stopped_count
gears_deploying_count
gears_unknown_count
gears_total_count
gears_active_count
node_profile
]
server[:gears] = stats[:gears_active_count]
server[:max_gears] = stats[:max_active_gears]
end
server[:active] = s["active"]
servers[ s.name ] = server
end
end
if headers
h =["node","gear_profile","district","status","region","zone"]
if args["--statistics"]
h << "gears"
h << "max_gears"
end
output.puts h.join(sep1)
end
unless servers.empty?
servers.sort.each do |n,v|
o = []
o << n
o << v[:gear_size]
o << v[:district]
o << "#{v[:active] ? '' : "in"}active"
o << v[:region_name]
o << v[:zone_name]
if args["--statistics"]
o << v[:gears].to_s
o << "of" unless headers
o << v[:max_gears].to_s
o << "gears" unless headers
end
output.puts o.join(sep1)
end
end
when "cartridges"
users = Hash.new
carts = Hash.new
CloudUser.all.each do |u|
users[u._id] = u.login
end
Application.all.each do |app|
continue unless users[app.owner_id]
app.component_instances.each do |comp|
carts[comp.cartridge_name] = carts[comp.cartridge_name] ? carts[comp.cartridge_name] + 1 : 1
end
end
if headers
h = ["cartridge","instances"]
output.puts h.join(sep1)
end
carts.sort.each do |name,value|
o = []
o << name
o << value
output.puts o.join(sep1)
end
when "tokens"
if headers
h = ["login","token","expires_at","scope"]
output.puts h.join(sep1)
end
# --all includes expired tokens in list
expiry = args["--all"] ? Time.at(0) : Time.now + 1
filter = filter = user_ids.count > 0 ? { :_id.in => user_ids } : {}
CloudUser.where( filter ).sort{|a,b| a['login']<=>b['login']}.each do |u|
Authorization.where(user_id: u._id).each do |a|
next unless a.expires_at > expiry
o = []
o << u.login
o << a.token
o << a.expires_at.strftime("%Y-%m-%d %H:%M:%S")
o << a.scopes.gsub(/ /,sep2)
output.puts o.join(sep1)
end
end
when "users"
if headers
h = ["login","gear_profiles","applications","gears","max_gears","user_id"]
output.puts h.join(sep1)
end
filter = user_ids.count > 0 ? { :_id.in => user_ids } : {}
CloudUser.where( filter ).sort{|a,b| a['login']<=>b['login']}.each do |u|
o = []
o << u.login
o << u.capabilities['gear_sizes'].join(sep2)
o << Application.where(owner_id: u._id).count.to_s
o << 'apps' unless headers
o << u.consumed_gears.to_s
o << "of" unless headers
o << u.capabilities["max_gears"].to_s
o << u._id
output.puts o.join(sep1)
end
else
usage
end
end