forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.py
executable file
·634 lines (538 loc) · 26.3 KB
/
report.py
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env python
from himlarcli.keystone import Keystone
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli.mail import Mail
from himlarcli.nova import Nova
from himlarcli.cinder import Cinder
from himlarcli import utils as himutils
from prettytable import PrettyTable
from datetime import date
from datetime import datetime
from datetime import timedelta
import re
import sys
import json
import os
import csv
himutils.is_virtual_env()
parser = Parser()
parser.set_autocomplete(True)
options = parser.parse_args()
printer = Printer(options.format)
ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_dry_run(options.dry_run)
ksclient.set_domain(options.domain)
logger = ksclient.get_logger()
if hasattr(options, 'region'):
regions = ksclient.find_regions(region_name=options.region)
else:
regions = ksclient.find_regions()
if not regions:
himutils.fatal('no regions found with this name!')
#---------------------------------------------------------------------
# Main functions
#---------------------------------------------------------------------
def action_show():
project = ksclient.get_project_by_name(project_name=options.project)
if not project:
himutils.fatal(f"No project found with name {options.project}")
sys.stdout.write(Printer.prettyprint_project_metadata(project, options, logger, regions))
if options.detail:
sys.stdout.write(Printer.prettyprint_project_zones(project, options, logger))
sys.stdout.write(Printer.prettyprint_project_volumes(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_images(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_instances(project, options, logger, regions))
def action_list():
search_filter = dict()
if options.filter and options.filter != 'all':
search_filter['type'] = options.filter
projects = ksclient.get_projects(**search_filter)
# Project counter
count = 0
# Loop through projects
for project in projects:
sys.stdout.write(Printer.prettyprint_project_metadata(project, options, logger, regions))
if options.detail:
sys.stdout.write(Printer.prettyprint_project_zones(project, options, logger))
sys.stdout.write(Printer.prettyprint_project_volumes(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_images(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_instances(project, options, logger, regions))
# Print some vertical space and increase project counter
print("\n\n")
count += 1
# Finally print out number of projects
printer.output_dict({'header': 'Project list count', 'count': count})
def action_resources():
search_filter = dict()
if options.filter and options.filter != 'all':
search_filter['type'] = options.filter
projects = ksclient.get_projects(**search_filter)
# Project counter
count = 0
# Loop through projects
csv_output = []
for project in projects:
kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
kc.set_domain(options.domain)
project_type = project.type if hasattr(project, 'type') else '(unknown)'
project_admin = project.admin if hasattr(project, 'admin') else '(unknown)'
project_created = project.createdate if hasattr(project, 'createdate') else '(unknown)'
project_enddate = project.enddate if hasattr(project, 'enddate') else 'None'
project_contact = project.contact if hasattr(project, 'contact') else 'None'
project_roles = kc.list_roles(project_name=project.name)
project_org = project.org if hasattr(project, 'org') else 'None'
# Make project create date readable
project_created = re.sub(r'T\d\d:\d\d:\d\d.\d\d\d\d\d\d', '', project_created)
# Count users
users = dict()
users['user'] = 0
users['object'] = 0
users['superuser'] = 0
if len(project_roles) > 0:
for role in project_roles:
users[role['role']] += 1
# Count resources
zones = Printer._count_project_zones(project, options, logger)
volumes = Printer._count_project_volumes(project, options, logger, regions)
images = Printer._count_project_images(project, options, logger, regions)
instances = Printer._count_project_instances(project, options, logger, regions)
# Get resources used by instances and volumes
ram = dict()
disk = dict()
vcpus = dict()
hdd = dict()
ssd = dict()
for region in regions:
# Initiate Nova object
nc = himutils.get_client(Nova, options, logger, region)
# Initiate Cinder object
cc = himutils.get_client(Cinder, options, logger, region)
ram[region] = 0
disk[region] = 0
vcpus[region] = 0
hdd[region] = 0
ssd[region] = 0
for instance in nc.get_project_instances(project_id=project.id):
ram[region] += instance.flavor['ram']
disk[region] += instance.flavor['disk']
vcpus[region] += instance.flavor['vcpus']
for volume in cc.get_volumes(detailed=True, search_opts={'project_id': project.id}):
if volume.volume_type == 'mass-storage-ssd':
ssd[region] += volume.size
else:
hdd[region] += volume.size
# Output array
output_project = {
#'header': 'Project Resources',
'project_name': project.name,
'project_id': project.id,
'project_type': project_type,
'project_org': project_org,
'project_admin': project_admin,
'project_contact': project_contact,
'project_enddate': project_enddate,
'project_description': project.description,
'num_users': users['user'],
'num_object_users': users['object'],
'num_zones': zones,
'bgo_num_instances': instances['bgo'],
'osl_num_instances': instances['osl'],
'bgo_num_volumes': volumes['bgo'],
'osl_num_volumes': volumes['osl'],
'bgo_num_images': images['bgo'],
'osl_num_images': images['osl'],
'bgo_instance_ram_mb': ram['bgo'],
'osl_instance_ram_mb': ram['osl'],
'bgo_instance_disk_gb': disk['bgo'],
'osl_instance_disk_gb': disk['osl'],
'bgo_instance_vcpus': vcpus['bgo'],
'osl_instance_vcpus': vcpus['osl'],
'bgo_volume_hdd_gb': hdd['bgo'],
'osl_volume_hdd_gb': hdd['osl'],
'bgo_volume_ssd_gb': ssd['bgo'],
'osl_volume_ssd_gb': ssd['osl'],
}
# Store results (CSV) or print (TEXT / JSON)
if options.format == 'csv':
csv_output.append(output_project)
else:
printer.output_dict(output_project, sort=True, one_line=True)
# Increase project counter
count += 1
# Print results (CSV) or number of projects (TEXT / JSON)
if options.format == 'csv':
writer = csv.DictWriter(sys.stdout, fieldnames=csv_output[0].keys(), dialect='unix')
writer.writeheader()
for row in csv_output:
writer.writerow(row)
else:
printer.output_dict({'header': 'Project list count', 'count': count})
def action_user():
if not ksclient.is_valid_user(email=options.user, domain=options.domain):
print("%s is not a valid user. Please check your spelling or case." % options.user)
sys.exit(1)
user = ksclient.get_user_objects(email=options.user, domain=options.domain)
# Project counter
count = 0
for project in user['projects']:
if options.admin and project.admin != options.user:
continue
sys.stdout.write(Printer.prettyprint_project_metadata(project, options, logger, regions, options.user))
if options.detail:
sys.stdout.write(Printer.prettyprint_project_zones(project, options, logger))
sys.stdout.write(Printer.prettyprint_project_volumes(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_images(project, options, logger, regions))
sys.stdout.write(Printer.prettyprint_project_instances(project, options, logger, regions))
# Print some vertical space and increase project counter
print("\n\n")
count += 1
# Finally print out number of projects
printer.output_dict({'header': 'Project list count', 'count': count})
def action_vendorapi():
data = vendorapi_list()
data_project = data[0]
data_instance = data[1]
if options.outdir:
file_projects = os.path.join(options.outdir, 'project.json')
file_instances = os.path.join(options.outdir, 'instances.json')
with open(file_projects, "w") as outfile:
json.dump(data_project, outfile)
with open(file_instances, "w") as outfile:
json.dump(data_instance, outfile)
else:
projects_object = json.dumps(data_project, indent = 4)
instances_object = json.dumps(data_instance, indent = 4)
print('PROJECTS')
print('-----------------------------------------------------------------------------')
print(projects_object)
print()
print('INSTANCES')
print('-----------------------------------------------------------------------------')
print(instances_object)
def action_mail():
if not options.template:
himutils.fatal("Option '--template' is required when sending mail")
if options.mail_user:
if not ksclient.is_valid_user(email=options.mail_user, domain=options.domain):
himutils.fatal(f"{options.mail_user} is not a valid user. Please check your spelling or case.")
users = [options.mail_user]
else:
users = ksclient.list_users(domain=options.domain)
# We want details
options.detail = True
# Attachment dict
attachment = dict()
# Admin/member dict
admin = dict()
member = dict()
# Project counter
project_counter = 0
# Ask for confirmation
if not options.force and not options.dry_run:
if not himutils.confirm_action('Send mail to (potentially) %d users?' % len(users)):
return
# Loop through projects
for user in users:
# Ignore system users
if not '@' in user:
continue
# Get user object
this_user = ksclient.get_user_objects(email=user, domain=options.domain)
if not this_user:
continue
# Ignore disabled users
if ksclient.is_disabled_user(user):
continue
# Ignore users who only have a DEMO project, i.e. number of
# projects is equal or less than 1
if len(this_user['projects']) <= 1:
continue
# Set common mail parameters
mail = himutils.get_client(Mail, options, logger)
mail = Mail(options.config, debug=options.debug)
mail.set_dry_run(options.dry_run)
if options.fromaddr:
fromaddr = options.fromaddr
else:
fromaddr = '[email protected]'
# Loop through projects collecting info
attachment_payload = ''
admin_counter = 0
member_counter = 0
for project in this_user['projects']:
attachment_payload += Printer.prettyprint_project_metadata(project, options, logger, regions, user)
attachment_payload += Printer.prettyprint_project_zones(project, options, logger)
attachment_payload += Printer.prettyprint_project_volumes(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_images(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_instances(project, options, logger, regions)
# Add some vertical space
attachment_payload += "\n\n"
# Increase counters
if hasattr(project, 'admin') and project.admin == user:
admin_counter += 1
else:
member_counter += 1
# Construct mail content
body_content = himutils.load_template(inputfile=options.template,
mapping={'admin_count': admin_counter,
'member_count': member_counter},
log=logger)
msg = mail.create_mail_with_txt_attachment(options.subject,
body_content,
attachment_payload,
'resources.txt',
fromaddr)
# Send mail to user
mail.send_mail(user, msg, fromaddr)
if options.dry_run:
print("Did NOT send spam to %s" % user)
print(" --> admin for %d projects, member of %d projects" % (admin_counter, member_counter))
else:
print("Spam sent to %s" % user)
def action_enddate():
if not options.list and not options.template:
himutils.fatal("Option '--template' is required when sending mail")
if not options.list and not options.days:
himutils.fatal("Option '--days' is required")
search_filter = dict()
projects = ksclient.get_projects(**search_filter)
# Create datetime object for today at midnight
dt = date.today()
today = datetime.combine(dt, datetime.min.time())
options.detail = True # we want details
if options.list and not options.days:
project_list = dict()
for project in projects:
project_enddate = project.enddate if hasattr(project, 'enddate') else 'None'
project_type = project.type if hasattr(project, 'type') else 'None'
# Ignore DEMO projects
if project_type == 'demo':
continue
# Ignore already quarantined projects
if ksclient.check_project_tag(project.id, 'quarantine_active'):
continue
# Ignore projects without an enddate
if project_enddate == 'None':
continue
# Get current enddate
enddate = datetime.strptime(project.enddate, '%Y-%m-%d')
# store data
project_list[project.name] = (enddate - today).days
for project in dict(sorted(project_list.items(), key=lambda item: item[1])):
print("%-4s %s" % (project_list[project], project))
return
for project in projects:
project_type = project.type if hasattr(project, 'type') else 'None'
project_admin = project.admin if hasattr(project, 'admin') else 'None'
project_enddate = project.enddate if hasattr(project, 'enddate') else 'None'
project_org = project.org if hasattr(project, 'org') else 'None'
project_contact = project.contact if hasattr(project, 'contact') else 'None'
# Ignore DEMO projects
if project_type == 'demo':
continue
# Ignore already quarantined projects
if ksclient.check_project_tag(project.id, 'quarantine_active'):
continue
# Ignore projects without an enddate
if project_enddate == 'None':
continue
# Get current enddate
enddate = datetime.strptime(project.enddate, '%Y-%m-%d')
# Set common mail parameters
mail = himutils.get_client(Mail, options, logger)
mail = Mail(options.config, debug=options.debug)
mail.set_dry_run(options.dry_run)
bccaddr = '[email protected]'
if options.fromaddr:
fromaddr = options.fromaddr
else:
fromaddr = '[email protected]'
if project_contact != 'None':
ccaddr = project_contact
else:
ccaddr = None
for days in options.days:
days = int(days)
if (enddate - today).days == days:
if options.list:
print("%-4s %s" % (days, project.name))
else:
options.admin = project_admin # for prettyprint_project_metadata()
attachment_payload = ''
attachment_payload += Printer.prettyprint_project_metadata(project, options, logger, regions, project_admin)
attachment_payload += Printer.prettyprint_project_zones(project, options, logger)
attachment_payload += Printer.prettyprint_project_volumes(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_images(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_instances(project, options, logger, regions)
# Construct mail content
subject = '[NREC] Project "%s" expires in %d days' % (project.name, days)
body_content = himutils.load_template(inputfile=options.template,
mapping={'project': project.name,
'enddate': project_enddate,
'days': days},
log=logger)
msg = mail.create_mail_with_txt_attachment(subject,
body_content,
attachment_payload,
'resources.txt',
fromaddr,
ccaddr,
bccaddr)
# Send mail to user
mail.send_mail(project_admin, msg, fromaddr, ccaddr, bccaddr)
if options.dry_run:
print("Did NOT send spam to %s;" % project_admin)
print("Subject: %s" % subject)
print("To: %s" % project_admin)
if ccaddr:
print("Cc: %s" % ccaddr)
if bccaddr:
print("Bcc: %s" % bccaddr)
print("From: %s" % fromaddr)
print('---')
print(body_content)
else:
print("Spam sent to %s" % project_admin)
def action_quarantine():
if not options.list and not options.template:
himutils.fatal("Option '--template' is required when sending mail")
if not options.list and not options.days:
himutils.fatal("Option '--days' is required when sending mail")
# Create datetime object for today at midnight
dt = date.today()
today = datetime.combine(dt, datetime.min.time())
# Search for projects
search_filter = dict()
search_filter['tags'] = ['quarantine_active', 'quarantine type: enddate']
if options.days:
search_filter['tags_any'] = list()
for days in options.days:
thisdate = (today - timedelta(days=int(days))).strftime('%Y-%m-%d')
search_filter['tags_any'].append("quarantine date: %s" % thisdate)
projects = ksclient.get_projects(**search_filter)
# we want details
options.detail = True
# Set common mail parameters
mail = himutils.get_client(Mail, options, logger)
mail = Mail(options.config, debug=options.debug)
mail.set_dry_run(options.dry_run)
if options.fromaddr:
fromaddr = options.fromaddr
else:
fromaddr = '[email protected]'
for project in projects:
project_enddate = project.enddate if hasattr(project, 'enddate') else 'None'
project_type = project.type if hasattr(project, 'type') else 'None'
# Ignore DEMO projects
if project_type == 'demo':
continue
# Output error if project is enabled (shouldn't happen)
if project.enabled:
himutils.warning(f"Project {project.name} has quarantine tags but is enabled")
continue
# Get quarantine info
tags = ksclient.list_project_tags(project.id)
r_date = re.compile('^quarantine date: .+$')
date_tags = list(filter(r_date.match, tags))
if len(date_tags) > 1:
himutils.error(f"Too many quarantine dates for project {project.name}")
continue
elif len(date_tags) < 1:
himutils.error(f"No quarantine date for project {project.name}")
continue
m_date = re.match(r'^quarantine date: (\d\d\d\d-\d\d-\d\d)$', date_tags[0])
quarantine_date_iso = m_date.group(1)
quarantine_date = datetime.strptime(quarantine_date_iso, "%Y-%m-%d")
if options.list and not options.days:
print("%-4s %s" % ((today - quarantine_date).days, project.name))
else:
for days in options.days:
days = int(days)
if (today - quarantine_date).days == days:
if options.list:
print("%-4s %s" % (days, project.name))
else:
project_contact = project.contact if hasattr(project, 'contact') else 'None'
bccaddr = '[email protected]'
if project_contact != 'None':
ccaddr = project_contact
else:
ccaddr = None
project_admin = project.admin if hasattr(project, 'admin') else 'None'
options.admin = project_admin # for prettyprint_project_metadata()
attachment_payload = ''
attachment_payload += Printer.prettyprint_project_metadata(project, options, logger, regions, project_admin)
attachment_payload += Printer.prettyprint_project_zones(project, options, logger)
attachment_payload += Printer.prettyprint_project_volumes(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_images(project, options, logger, regions)
attachment_payload += Printer.prettyprint_project_instances(project, options, logger, regions)
# Construct mail content
subject = '[NREC] Project "%s" will be deleted in %d days' % (project.name, 90 - days)
body_content = himutils.load_template(inputfile=options.template,
mapping={'project': project.name,
'enddate': project_enddate,
'ago': days,
'days': 90 - days},
log=logger)
msg = mail.create_mail_with_txt_attachment(subject,
body_content,
attachment_payload,
'resources.txt',
fromaddr,
ccaddr,
bccaddr)
# Send mail to user
mail.send_mail(project_admin, msg, fromaddr, ccaddr, bccaddr)
if options.dry_run:
print("Did NOT send spam to %s;" % project_admin)
print("Subject: %s" % subject)
print("To: %s" % project_admin)
if ccaddr:
print("Cc: %s" % ccaddr)
if bccaddr:
print("Bcc: %s" % bccaddr)
print("From: %s" % fromaddr)
print('---')
print(body_content)
else:
print("Spam sent to %s" % project_admin)
#---------------------------------------------------------------------
# Helper functions
#---------------------------------------------------------------------
def vendorapi_list():
from himlarcli.nova import Nova
data_instance = dict()
data_project = dict()
projects = ksclient.get_projects()
# Loop through projects
for project in projects:
contact = project.contact if hasattr(project, 'contact') else None
admin = project.admin if hasattr(project, 'admin') else None
data_project[project.id] = {
"project_name": project.name,
"project_admin": admin,
"project_contact": contact
}
# Get Instances
for region in regions:
instances = dict()
# Initiate Nova object
nc = himutils.get_client(Nova, options, logger, region)
# Get a list of instances in project
instances[region] = nc.get_project_instances(project_id=project.id)
for instance in instances[region]:
contact = project.contact if hasattr(project, 'contact') else None
data_instance[instance.id] = {
"region": region,
}
return data_project, data_instance
#=====================================================================
# Main program
#=====================================================================
# Run local function with the same name as the action (Note: - => _)
action = locals().get('action_' + options.action.replace('-', '_'))
if not action:
himutils.fatal("Function action_%s() not implemented" % options.action)
action()