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(): projects = kc.get_projects() subject = '[NREC] Your project has expired' logfile = 'logs/expired-notify-{}.log'.format(date.today().isoformat()) mail = utils.get_client(Mail, options, logger) fromaddr = mail.get_config('mail', 'from_addr') if options.template: template = options.template else: template = 'notify/notify_expired_first.txt' for project in projects: project = expired_project(project) if not project or hasattr(project, 'notified'): continue mapping = dict(project=project.name, enddate=project.enddate) body_content = utils.load_template(inputfile=template, mapping=mapping, log=logger) msg = mail.get_mime_text(subject, body_content, fromaddr) mail.send_mail(project.admin, msg, fromaddr) print "mail sendt to {}".format(project.admin) if not options.dry_run: utils.append_to_file(logfile, project.admin) # Add metadata to project for the time of notification kc.update_project(project_id=project.id, notified=str(date.today()))
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(): users = dict() instances = novaclient.get_instances(options.aggregate) # update metadata if not options.dry_run: metadata = {'mail': options.date} novaclient.update_aggregate(options.aggregate, metadata=metadata) # Generate instance list per user for i in instances: email = None user = ksclient.get_by_id('user', i.user_id) if not user: project = ksclient.get_by_id('project', i.tenant_id) if hasattr(project, 'admin'): email = project.admin else: continue if not email: if not 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: mail = Mail(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']) action_date = himutils.get_date(options.date, date.today(), '%Y-%m-%d') mapping = dict(region=ksclient.region.upper(), date=action_date.strftime("%d %B %Y"), region_lower=ksclient.region.lower(), instances=user_instances) body_content = himutils.load_template(inputfile=options.template, 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) msg = MIMEText(body_content, 'plain', 'utf-8') msg['Subject'] = ('[UH-IaaS]: Your legacy instances will be terminated on %s (%s)' % (options.date, ksclient.region)) if not options.dry_run: mail.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 mail_user(email, template, subject): body_content = himutils.load_template(inputfile=template, mapping={'email': email}, log=logger) mail = Mail(options.config, debug=False, log=logger) mail.set_dry_run(options.dry_run) mail.mail_user(body_content, subject, email) mail.close()
def action_notify(): users = dict() instances = novaclient.get_instances(options.aggregate) # update metadata if not options.dry_run: metadata = {'mail': options.date} novaclient.update_aggregate(options.aggregate, metadata=metadata) # Generate instance list per user for i in instances: email = None user = ksclient.get_by_id('user', i.user_id) if not user: project = ksclient.get_by_id('project', i.tenant_id) if hasattr(project, 'admin'): email = project.admin else: continue if not email: if not 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, region_lower=ksclient.region.lower()) 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) mail = Mail(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: mail.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 parse_template(): mapping = {} if options.region: mapping['region'] = options.region.upper() if options.date: mapping['date'] = options.date msg_content = himutils.load_template(inputfile=options.template, mapping=mapping) stripped_msg = msg_content.rstrip('\n') return stripped_msg
def action_expired(): max_days = 90 projects = kc.get_projects(type='demo') subject = '[NREC] Your demo instance is due for deletion' logfile = 'logs/demo-logs/expired_instances/demo-notify-expired-instances-{}.log'.format( date.today().isoformat()) mail = utils.get_client(Mail, options, logger) fromaddr = mail.get_config('mail', 'from_addr') cc = '*****@*****.**' inputday = options.day question = 'Send mail to instances that have been running for {} days?'.format( inputday) if not options.force and not utils.confirm_action(question): return template = options.template if not utils.file_exists(template, logger): utils.sys_error('Could not find template file {}'.format(template)) if not options.template: utils.sys_error( 'Specify a template file. E.g. -t notify/demo-notify-expired-instances.txt' ) if not options.day: utils.sys_error( 'Specify the number of days for running demo instances. E.g. -d 30' ) for region in regions: nc = utils.get_client(Nova, options, logger, region) for project in projects: instances = nc.get_project_instances(project_id=project.id) for instance in instances: created = utils.get_date(instance.created, None, '%Y-%m-%dT%H:%M:%SZ') active_days = (date.today() - created).days kc.debug_log('{} running for {} days'.format( instance.id, active_days)) if (int(active_days) == int(inputday)): mapping = dict(project=project.name, enddate=int((max_days) - int(inputday)), activity=int(active_days), region=region.upper(), instance=instance.name) body_content = utils.load_template(inputfile=template, mapping=mapping, log=logger) msg = mail.get_mime_text(subject, body_content, fromaddr, cc) kc.debug_log( 'Sending mail to {} that has been active for {} days'. format(instance.id, active_days)) mail.send_mail(project.admin, msg, fromaddr) utils.append_to_logfile(logfile, date.today(), region, project.admin, instance.name, active_days) print('Mail sendt to {}'.format(project.admin))
def action_grant(): for user in options.users: if not ksclient.is_valid_user(email=user, domain=options.domain): himutils.sys_error('User %s not found as a valid user.' % user) project = ksclient.get_project_by_name(project_name=options.project) if not project: himutils.sys_error('No project found with name "%s"' % options.project) if hasattr(project, 'type') and (project.type == 'demo' or project.type == 'personal'): himutils.sys_error('Project are %s. User access not allowed!' % project.type) role = ksclient.grant_role(project_name=options.project, email=user) if role: output = role.to_dict() if not isinstance(role, dict) else role output['header'] = "Roles for %s" % options.project printer.output_dict(output) if options.mail: mail = Mail(options.config, debug=options.debug) mail.set_dry_run(options.dry_run) if options.rt is None: himutils.sys_error('--rt parameter is missing.') else: rt_mapping = dict(users='\n'.join(options.users)) rt_subject = 'NREC: Access granted to users in %s' % options.project rt_body_content = himutils.load_template( inputfile=access_granted_msg_file, mapping=rt_mapping) rt_mime = mail.rt_mail(options.rt, rt_subject, rt_body_content) mail.send_mail('*****@*****.**', rt_mime) for user in options.users: mapping = dict(project_name=options.project, admin=project.admin) body_content = himutils.load_template( inputfile=access_granted_user_msg_file, mapping=mapping) msg = MIMEText(body_content, 'plain') msg['subject'] = 'NREC: You have been given access to project %s' % options.project mail.send_mail(user, msg, fromaddr='*****@*****.**')
def action_grant(): for user in options.users: if not ksclient.is_valid_user(email=user, domain=options.domain): himutils.sys_error('User %s not found as a valid user.' % user) project = ksclient.get_project_by_name(project_name=options.project) if not project: himutils.sys_error('No project found with name "%s"' % options.project) if hasattr(project, 'type') and (project.type == 'demo' or project.type == 'personal'): himutils.sys_error('Project are %s. User access not allowed!' % project.type) role = ksclient.grant_role(project_name=options.project, email=user) if role: output = role.to_dict() if not isinstance(role, dict) else role output['header'] = "Roles for %s" % options.project printer.output_dict(output) if options.mail: mail = Mail(options.config, debug=options.debug) mail.set_dry_run(options.dry_run) if options.rt is None: himutils.sys_error('--rt parameter is missing.') else: rt_mapping = dict(users='\n'.join(options.users)) rt_subject = 'UH-IaaS: Access granted to users in %s' % options.project rt_body_content = himutils.load_template(inputfile=access_msg_file, mapping=rt_mapping) rt_mime = mail.rt_mail(options.rt, rt_subject, rt_body_content) mail.send_mail('*****@*****.**', rt_mime) for user in options.users: mapping = dict(project_name=options.project, admin=project.admin) body_content = himutils.load_template(inputfile=access_user_msg_file, mapping=mapping) msg = MIMEText(body_content, 'plain') msg['subject'] = 'UH-IaaS: You have been given access to project %s' % options.project mail.send_mail(user, msg, fromaddr='*****@*****.**')
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_file(): q = 'Send mail template {} to all emails in {}'.format( options.template, options.email_file) if not utils.confirm_action(q): return sent_mail_counter = 0 mail = Mail(options.config, debug=options.debug) mail.set_dry_run(options.dry_run) body_content = utils.load_template(inputfile=options.template, mapping={}, log=logger) emails = utils.load_file(inputfile=options.email_file, log=logger) for email in emails: mail.mail_user(body_content, options.subject, email) sent_mail_counter += 1 mail.close() printer.output_dict({'header': 'Mail counter', 'count': sent_mail_counter})
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_disable(): projects = kc.get_projects() subject = '[NREC] Your project is due for deletion' logfile = 'logs/expired-disabled-{}.log'.format(date.today().isoformat()) if options.template: template = options.template else: template = 'notify/notify_expired_last.txt' mail = utils.get_client(Mail, options, logger) fromaddr = mail.get_config('mail', 'from_addr') for project in projects: project = expired_project(project) if not project: continue # Allow 30 days gracetime before we disable disabled_date = utils.get_date(project.notified, None, '%Y-%m-%d') gracetime = timedelta(30) if date.today() - disabled_date < gracetime: continue # stop instances for region in regions: nc = utils.get_client(Nova, options, logger, region) instances = nc.get_project_instances(project_id=project.id) for i in instances: if i.status == 'ACTIVE': i.stop() mapping = dict(project=project.name, enddate=project.enddate) body_content = utils.load_template(inputfile=template, mapping=mapping, log=logger) msg = mail.get_mime_text(subject, body_content, fromaddr) mail.send_mail(project.admin, msg, fromaddr) print "mail sendt to {}".format(project.admin) if not options.dry_run: utils.append_to_file(logfile, project.admin) # Add metadata to project for the time of project disable kc.update_project(project_id=project.id, enabled=False, disabled=str(date.today()))
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()
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})
def action_terminate(): users = dict() instances = novaclient.get_instances(options.aggregate) snapshot_name = "-legacy_terminate_" + datetime.now().strftime("%Y-%m-%d") # Generate instance list per user count = 0 for i in instances: count += 1 if count > options.limit: break email = None user = ksclient.get_by_id('user', i.user_id) if not user: project = ksclient.get_by_id('project', i.tenant_id) if hasattr(project, 'admin'): email = project.admin else: continue if not email: if not 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] = {'snapshot': i.name + snapshot_name} # Snapshot and terminate instance if not options.dry_run: project = ksclient.get_by_id('project', i.tenant_id) metadata = { 'created_by': 'automated by uh-iaas team', 'owner': project.id, 'visibility': 'shared' } if i.status == 'ACTIVE': i.stop() status = i.status while status != 'SHUTOFF': time.sleep(5) tmp_i = novaclient.get_by_id('server', i.id) status = tmp_i.status image_name = i.name + snapshot_name image_id = i.create_image(image_name=image_name, metadata=metadata) time.sleep(2) image = glclient.get_image_by_id(image_id) ksclient.debug_log('create snapshot %s' % image_name) while image.status != 'active': time.sleep(5) image = glclient.get_image_by_id(image_id) ksclient.debug_log('waiting for snapshot %s to be ready' % image_name) #i.delete() if users: mail = Mail(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 (snapshot: %s)\n" % (server, info['snapshot']) #action_date = himutils.get_date(options.date, date.today(), '%Y-%m-%d') mapping = dict(region=ksclient.region.upper(), #date=action_date.strftime("%d %B %Y"), region_lower=ksclient.region.lower(), instances=user_instances) body_content = himutils.load_template(inputfile=options.template, 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) msg = MIMEText(body_content, 'plain', 'utf-8') msg['Subject'] = ('[UH-IaaS]: Your legacy instances have been terminated (%s)' % (ksclient.region)) if not options.dry_run: mail.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_create(): if not ksclient.is_valid_user( options.admin, options.domain) and options.type == 'personal': himutils.sys_error('not valid user', 1) quota = himutils.load_config('config/quotas/%s.yaml' % options.quota) if options.quota and not quota: himutils.sys_error('Could not find quota in config/quotas/%s.yaml' % options.quota) test = 1 if options.type == 'test' else 0 project_msg = project_msg_file enddate = himutils.get_date(options.enddate, None, '%d.%m.%Y') if options.type == 'hpc': project_msg = project_hpc_msg_file if not enddate: himutils.sys_error('HPC projects must have an enddate', 1) createdate = datetime.today() # Parse the "contact" option, setting to None if not used # Exit with error if contact is not a valid email address contact = None if options.contact is not None: contact = options.contact.lower() if not ksclient._Keystone__validate_email(contact): errmsg = "%s is not a valid email address." % contact himutils.sys_error(errmsg, 1) if not options.force: print 'Project name: %s\nDescription: %s\nAdmin: %s\nContact: %s\nOrganization: %s\nType: %s\nEnd date: %s\nQuota: %s\nRT: %s' \ % (options.project, ksclient.convert_ascii(options.desc), options.admin.lower(), contact, options.org, options.type, str(enddate), options.quota, options.rt) if not himutils.confirm_action( 'Are you sure you want to create this project?'): himutils.sys_error('Aborted', 1) project = ksclient.create_project(project_name=options.project, admin=options.admin.lower(), contact=contact, org=options.org, test=test, type=options.type, description=options.desc, enddate=str(enddate), createdate=createdate.isoformat(), quota=options.quota, rt=options.rt) if not ksclient.is_valid_user(options.admin, options.domain): himutils.sys_error( 'WARNING: "%s" is not a valid user.' % options.admin, 0) if not project: himutils.sys_error('Failed creating %s' % options.project, 1) else: output = Keystone.get_dict(project) output['header'] = "Show information for %s" % options.project printer.output_dict(output) # Do stuff for regions for region in regions: # Get objects novaclient = himutils.get_client(Nova, options, logger, region) cinderclient = himutils.get_client(Cinder, options, logger, region) neutronclient = himutils.get_client(Neutron, options, logger, region) glanceclient = himutils.get_client(Glance, options, logger, region) # Find the project ID project_id = Keystone.get_attr(project, 'id') # Update quotas for Cinder, Nova, Neutron if quota and 'cinder' in quota and project: cinderclient.update_quota(project_id=project_id, updates=quota['cinder']) if quota and 'nova' in quota and project: novaclient.update_quota(project_id=project_id, updates=quota['nova']) if quota and 'neutron' in quota and project: neutronclient.update_quota(project_id=project_id, updates=quota['neutron']) # Grant UiO Managed images if shared UiO project if options.org == 'uio' and options.type not in ['personal', 'demo']: tags = ['uio'] filters = {'status': 'active', 'tag': tags, 'visibility': 'shared'} images = glanceclient.get_images(filters=filters) for image in images: glanceclient.set_image_access(image_id=image.id, project_id=project.id, action='grant') printer.output_msg( 'GRANT access to image {} for project {}'.format( image.name, project.name)) if options.mail: mail = Mail(options.config, debug=options.debug) mail.set_dry_run(options.dry_run) if options.rt is None: himutils.sys_error('--rt parameter is missing.') else: mapping = dict(project_name=options.project, admin=options.admin.lower(), quota=options.quota, end_date=str(enddate)) subject = 'NREC: Project %s has been created' % options.project body_content = himutils.load_template(inputfile=project_msg, mapping=mapping) if not body_content: himutils.sys_error('ERROR! Could not find and parse mail body in \ %s' % options.msg) mime = mail.rt_mail(options.rt, subject, body_content) mail.send_mail('*****@*****.**', mime)
def action_mail(): if options.mail_user: if not ksclient.is_valid_user(email=options.mail_user, domain=options.domain): print "%s is not a valid user. Please check your spelling or case." % options.mail_user sys.exit(1) 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 utils.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 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 = utils.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 = '*****@*****.**' # 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 project.admin == user: admin_counter += 1 else: member_counter += 1 # Construct mail content body_content = utils.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_aggregate(): users = dict() for region in regions: nova = utils.get_client(Nova, options, logger, region) #neutron = utils.get_client(Neutron, options, logger) #network = neutron.list_networks() instances = nova.get_instances(options.aggregate) if not instances: continue for i in instances: email = None project = kc.get_by_id('project', i.tenant_id) if hasattr(project, 'admin'): email = project.admin else: kc.debug_log('could not find admin for project {}'.format( project.name)) continue instance_data = { 'name': i.name, 'region': region, #'status': i.status, #'created': i.created, #'flavor': i.flavor['original_name'], #'ip': next(iter(neutron.get_network_ip(i.addresses, network)), None), 'project': project.name } if email not in users: users[email] = list() users[email].append(instance_data) # Add metadata to aggregate if options.date: meta_msg = options.date + ' (updated {})'.format( utils.get_current_date()) else: meta_msg = 'unknown (updated {})'.format(utils.get_current_date()) metadata = {'maintenance': meta_msg} nova.update_aggregate(options.aggregate, metadata=metadata) mailer = utils.get_client(Mail, options, logger) if '[NREC]' not in options.subject: subject = '[NREC] ' + options.subject else: subject = options.subject sent_mail_counter = 0 message = None fromaddr = options.from_addr for user, instances in users.iteritems(): columns = ['project', 'region'] mapping = dict(region=options.region, date=options.date, instances=utils.get_instance_table(instances, columns), admin=user) body_content = utils.load_template(inputfile=options.template, mapping=mapping, log=logger) message = mailer.get_mime_text(subject, body_content, fromaddr=fromaddr) mailer.send_mail(user, message) sent_mail_counter += 1 if options.dry_run and message: print "\nExample mail sendt from this run:" print "----------------------------------" print message mailer.close() printer.output_dict({'header': 'Mail counter', 'count': sent_mail_counter})
def action_create(): if not ksclient.is_valid_user(options.admin, options.domain) and options.type == 'personal': himutils.sys_error('not valid user', 1) quota = himutils.load_config('config/quotas/%s.yaml' % options.quota) if options.quota and not quota: himutils.sys_error('Could not find quota in config/quotas/%s.yaml' % options.quota) test = 1 if options.type == 'test' else 0 if options.enddate: try: enddate = datetime.strptime(options.enddate, '%d.%m.%Y').date() except ValueError: himutils.sys_error('date format DD.MM.YYYY not valid for %s' % options.enddate, 1) else: enddate = None createdate = datetime.today() if not options.force: print 'Project name: %s\nAdmin: %s\nType: %s\nEnd date: %s\nQuota: %s\nRT: %s' \ % (options.project, options.admin.lower(), options.type, str(enddate), options.quota, options.rt) if not himutils.confirm_action('Are you sure you want to create this project?'): himutils.sys_error('Aborted', 1) project = ksclient.create_project(project_name=options.project, admin=options.admin.lower(), test=test, type=options.type, description=options.desc, enddate=str(enddate), createdate=createdate.isoformat(), quota=options.quota, rt=options.rt) if not ksclient.is_valid_user(options.admin, options.domain): himutils.sys_error('WARNING: "%s" is not a valid user.' % options.admin, 0) if not project: himutils.sys_error('Failed creating %s' % options.project, 1) else: output = Keystone.get_dict(project) output['header'] = "Show information for %s" % options.project printer.output_dict(output) # Quotas for region in regions: novaclient = Nova(options.config, debug=options.debug, log=logger, region=region) cinderclient = Cinder(options.config, debug=options.debug, log=logger, region=region) neutronclient = Neutron(options.config, debug=options.debug, log=logger, region=region) cinderclient.set_dry_run(options.dry_run) novaclient.set_dry_run(options.dry_run) neutronclient.set_dry_run(options.dry_run) project_id = Keystone.get_attr(project, 'id') if quota and 'cinder' in quota and project: cinderclient.update_quota(project_id=project_id, updates=quota['cinder']) if quota and 'nova' in quota and project: novaclient.update_quota(project_id=project_id, updates=quota['nova']) if quota and 'neutron' in quota and project: neutronclient.update_quota(project_id=project_id, updates=quota['neutron']) if options.mail: mail = Mail(options.config, debug=options.debug) mail.set_dry_run(options.dry_run) if options.rt is None: himutils.sys_error('--rt parameter is missing.') else: mapping = dict(project_name=options.project, admin=options.admin.lower(), quota=options.quota, end_date=str(enddate)) subject = 'UH-IaaS: Project %s has been created' % options.project body_content = himutils.load_template(inputfile=project_msg_file, mapping=mapping) if not body_content: himutils.sys_error('ERROR! Could not find and parse mail body in \ %s' % options.msg) mime = mail.rt_mail(options.rt, subject, body_content) mail.send_mail('*****@*****.**', mime)