def action_notify(): q = "Send mail to all users of these instances about termination? (yes|no) " answer = raw_input(q) if answer.lower() != 'yes': print "Abort sending mail!" return search_filter = dict() if options.type: search_filter['type'] = options.type projects = ksclient.get_projects(domain=options.domain, **search_filter) for region in regions: novaclient = Nova(options.config, debug=options.debug, log=logger, region=region) for project in projects: if not options.filter or options.filter in project.name: instances = novaclient.get_project_instances(project.id) mapping = dict(region=region.upper(), project=project.name) body_content = himutils.load_template( inputfile=options.template, mapping=mapping, log=logger) subject = ('UH-IaaS: Your instances will be terminated (%s)' % (region)) notify = Notify(options.config, debug=False, log=logger) notify.set_keystone_client(ksclient) notify.set_dry_run(options.dry_run) users = notify.mail_instance_owner(instances, body_content, subject) notify.close() print users
def action_notify(): q = "Send mail to all users of these instances about termination? (yes|no) " answer = raw_input(q) if answer.lower() != 'yes': print "Abort sending mail!" return search_filter = dict() if options.type: search_filter['type'] = options.type projects = ksclient.get_projects(domain=options.domain, **search_filter) for region in regions: novaclient = Nova(options.config, debug=options.debug, log=logger, region=region) for project in projects: if not options.filter or options.filter in project.name: instances = novaclient.get_project_instances(project.id) verified_instances = list() for i in instances: network = i.addresses.keys()[0] if len(i.addresses.keys()) > 0 else 'unknown' if options.network and options.network != network: continue verified_instances.append(i) mapping = dict(region=region.upper(), project=project.name) body_content = himutils.load_template(inputfile=options.template, mapping=mapping, log=logger) subject = ('UH-IaaS: Your instances will be terminated (%s)' % (region)) notify = Notify(options.config, debug=False, log=logger) notify.set_keystone_client(ksclient) notify.set_dry_run(options.dry_run) users = notify.mail_instance_owner(verified_instances, body_content, subject) notify.close() if users: print users
def action_notify(): users = dict() instances = novaclient.get_instances(options.aggregate) # update metadata if not options.dry_run: metadata = {'notify': options.date} novaclient.update_aggregate(options.aggregate, metadata=metadata) # Generate instance list per user for i in instances: user = ksclient.get_by_id('user', i.user_id) if not hasattr(user, 'name'): continue if "@" not in user.name: continue email = user.name.lower() if email not in users: users[email] = dict() users[email][i.name] = {'status': i.status} if users: mapping = dict(region=ksclient.region.upper(), date=options.date) body_content = himutils.load_template(inputfile=msg_file, mapping=mapping, log=ksclient.get_logger()) if not body_content: print 'ERROR! Could not find and parse mail body in \ %s' % options.msg sys.exit(1) notify = Notify(options.config, debug=options.debug) # Email each users for user, instances in users.iteritems(): user_instances = "" for server, info in instances.iteritems(): user_instances += "%s (current status %s)\n" % (server, info['status']) msg = MIMEText(user_instances + body_content, 'plain', 'utf-8') msg['Subject'] = ( 'UH-IaaS: Your instances will be rebooted on %s (%s)' % (options.date, ksclient.region)) if not options.dry_run: notify.send_mail(user, msg) print "Sending email to user %s" % user else: print "Dry-run: Mail would be sendt to user %s" % user pp = pprint.PrettyPrinter(indent=1) print "\nComplete list of users and instances:" print "=====================================" pp.pprint(users)
def action_instance(): for region in regions: novaclient = Nova(options.config, debug=options.debug, log=logger, region=region) instances = novaclient.get_instances() mapping = dict(region=region.upper()) body_content = himutils.load_template(inputfile=options.template, mapping=mapping, log=logger) subject = options.subject notify = Notify(options.config, debug=False, log=logger) notify.set_keystone_client(ksclient) notify.set_dry_run(options.dry_run) users = notify.mail_instance_owner(instances=instances, body=body_content, subject=subject, admin=True, options=['project', 'az']) notify.close() printer.output_dict(users)
def action_users(): users = ksclient.get_users(domain=options.domain) body_content = himutils.load_template(inputfile=options.template, mapping={}, log=logger) subject = options.subject notify = Notify(options.config, debug=False, log=logger) notify.set_dry_run(options.dry_run) #sent_email = himutils.load_file('temp_email2.txt', log=ksclient.get_logger()) for user in users: # if user.email in sent_email: # logger.debug('=> %s email sent, dropping' % user.email) # continue if hasattr(user, 'email'): if options.dry_run: logger.debug('=> DRY-RUN: sending mail to %s' % user.email) notify.mail_user(body_content, subject, user.email) time.sleep(2) notify.close()
#!/usr/bin/python import sys import utils from himlarcli.nova import Nova from himlarcli.keystone import Keystone from himlarcli.notify import Notify from email.mime.text import MIMEText options = utils.get_options('Notify all users', dry_run=True, hosts=False) keystone = Keystone(options.config, debug=options.debug) notify = Notify(options.config, debug=options.debug) region = keystone.region print "Remove these lines if you want to run this and send mail to all!" sys.exit(0) # Edit this to send new email to all users subject = 'UH-IaaS: Purge of all data (%s)' % region body_file = 'misc/notify_reinstall.txt' with open(body_file, 'r') as body_txt: body_content = body_txt.read() projects = keystone.list_projects('Dataporten') for project in projects: msg = MIMEText(body_content) msg['Subject'] = subject if not options.dry_run: notify.send_mail(project, msg) print '\nProject: %s' % project
#!/usr/bin/python import sys import utils from himlarcli.nova import Nova from himlarcli.keystone import Keystone from himlarcli.notify import Notify from email.mime.text import MIMEText print "Depricated! Use aggregate.py" sys.exit(1) options = utils.get_options('Notify users of rebuild host', dry_run=True, hosts=1) notify = Notify(options.config, debug=options.debug) with open('misc/notify_email.txt', 'r') as body_txt: body_content = body_txt.read() # Find users novaclient = Nova(options.config, options.host[0], debug=options.debug) region = novaclient.get_config('openstack', 'region') if not novaclient.valid_host(): print "ERROR: could not find host %s" % options.host[0] sys.exit(1) toaddr = novaclient.list_instances() for user, instance in toaddr.iteritems(): user_instances = "" for server, info in instance.iteritems(): user_instances += "%s (%s)\n" % (server, info['ip']) msg = MIMEText(user_instances + body_content) msg['Subject'] = 'UH-IaaS: Terminating instance (%s)' % region
def action_notify(): question = 'Send mail to all users about demo projects' if options.dry_run: question = 'DRY-RUN: %s' % question if not himutils.confirm_action(question): return projects = ksclient.get_projects(domain=options.domain) count = 0 for project in projects: if hasattr(project, 'notify') and project.notify == 'converted': himutils.sys_error( 'personal project %s converted' % (project.name), 0) continue found = False if hasattr(project, 'type') and project.type == 'personal': print "%s (new personal project)" % project.name count += 1 found = True elif '@' in project.name and not hasattr(project, 'type'): print "%s (old personal project)" % project.name count += 1 found = True if found: if '@' not in project.name: himutils.sys_error( 'unable to find email for project %s' % project.name, 0) continue user = ksclient.get_user_by_email(project.name, 'api', options.domain) if not user: himutils.sys_error( 'unable to find user for project %s' % project.name, 0) continue search_filter = dict({'type': 'demo'}) demo_project = ksclient.get_user_projects(email=user.email.lower(), domain=options.domain, **search_filter) demo_project = demo_project[0] if demo_project else None if not demo_project: himutils.sys_error( 'unable to find demo project for %s' % user.name, 0) continue delete_date = '2017-10-31' sent_email = himutils.load_file('temp_email.txt', log=ksclient.get_logger()) if user.email in sent_email: himutils.sys_error('%s email sent, dropping' % user.email, 0) continue #ksclient.update_project(project_id=project.id, notify=delete_date) #mapping = dict(region=region.upper(), project=project.name) mapping = { 'personal': project.name, 'date': delete_date, 'demo': demo_project.name } body_content = himutils.load_template( inputfile='misc/notify_demo2.txt', mapping=mapping, log=logger) subject = ('[UH-IaaS] Your personal project will be deleted') notify = Notify(options.config, debug=False, log=logger) notify.set_dry_run(options.dry_run) notify.mail_user(body_content, subject, user.email) notify.close() time.sleep(3) printer.output_dict({'Personal projects': count})