def update_tenant(ctx, name, yamlfile, admin, charset, enable, disable, days_to_close, days_to_orphan, ml_name_format, new_ml_account, welcome_file, readme_file, add_file, remove_file, reopen_file, goodbye_file, report_subject, report_file, orphaned_subject, orphaned_file, closed_subject, closed_file): tenant = db.get_tenant(name) if tenant is None: logging.error("tenant %s not found", name) ctx.exit(1) tenant_config = {} if yamlfile: tenant_config = yaml.load(yamlfile.read()) if len(admin) > 0: tenant_config['admins'] = normalize(admin) if charset is not None: tenant_config['charset'] = charset if enable is True: tenant_config['status'] = const.TENANT_STATUS_ENABLED elif disable is True: tenant_config['status'] = const.TENANT_STATUS_DISABLED if days_to_close is not None: tenant_config['days_to_close'] = days_to_close if days_to_orphan is not None: tenant_config['days_to_orphan'] = days_to_orphan if ml_name_format is not None: tenant_config['ml_name_format'] = ml_name_format if new_ml_account is not None: tenant_config['new_ml_account'] = new_ml_account if welcome_file is not None: tenant_config['welcome_msg'] = welcome_file.read() if readme_file is not None: tenant_config['readme_msg'] = readme_file.read() if add_file is not None: tenant_config['add_msg'] = add_file.read() if remove_file is not None: tenant_config['remove_msg'] = remove_file.read() if goodbye_file is not None: tenant_config['goodbye_msg'] = goodbye_file.read() if reopen_file is not None: tenant_config['reopen_msg'] = reopen_file.read() if report_subject is not None: tenant_config['report_subject'] = report_subject if report_file is not None: tenant_config['report_msg'] = report_file.read() if orphaned_subject is not None: tenant_config['orphaned_subject'] = orphaned_subject if orphaned_file is not None: tenant_config['orphaned_msg'] = orphaned_file.read() if closed_subject is not None: tenant_config['closed_subject'] = closed_subject if closed_file is not None: tenant_config['closed_msg'] = closed_file.read() logging.debug("tenant_name: %s", name) logging.debug("tenant_config: %s", tenant_config) config = ctx.obj['config'] db.init_db(config['db_url'], config['db_name']) db.update_tenant(name, "CLI", **tenant_config)
def delete_tenant(ctx, name): config = ctx.obj['config'] db.init_db(config['db_url'], config['db_name']) tenant = db.get_tenant(name) if tenant is None: logging.error("tenant %s not found", name) ctx.exit(1) db.delete_tenant(name)
def report_status(relay_host=None, relay_port=None, db_url=None, db_name=None, domain=None, debug=False, **kwargs): db.init_db(db_url, db_name) tenants = db.find_tenants({'status': const.TENANT_STATUS_ENABLED}) for tenant in tenants: report_tenant_status( relay_host=relay_host, relay_port=relay_port, db_url=db_url, db_name=db_name, domain=domain, debug=debug, **tenant)
def __init__(self, relay_host=None, relay_port=None, db_url=None, db_name=None, domain=None, debug=False, **kwargs): self.relay_host = relay_host self.relay_port = relay_port self.at_domain = "@" + domain self.debug = debug db.init_db(db_url, db_name) self.tenants = db.find_tenants({'status': const.TENANT_STATUS_ENABLED}) logging.debug("tenants: %s", self.tenants)
def show_tenant(ctx, name): config = ctx.obj['config'] db.init_db(config['db_url'], config['db_name']) tenant = db.get_tenant(name) if tenant is None: logging.error("tenant %s not found", name) ctx.exit(1) for key in ['_id', 'logs']: if key in tenant: del tenant[key] print(yaml.dump(tenant, allow_unicode=True, line_break='|'))
def report_tenant_status(relay_host=None, relay_port=None, db_url=None, db_name=None, report_subject=None, report_msg=None, days_to_close=None, charset='utf8', admins=None, domain=None, debug=False, **kwargs): db.init_db(db_url, db_name) new = db.find_mls({'status': const.STATUS_NEW}, sortkey='updated') new = [convert(_) for _ in new] _open = db.find_mls({'status': const.STATUS_OPEN}, sortkey='updated') _open = [convert(_) for _ in _open] orphaned = db.find_mls({'status': const.STATUS_ORPHANED}, sortkey='updated') orphaned = [convert(_) for _ in orphaned] closed_after = datetime.now() - timedelta(days=days_to_close) closed = db.find_mls({'status': const.STATUS_CLOSED, 'updated': {'$gt': closed_after}}, sortkey='updated', reverse=False) closed = [convert(_) for _ in closed] params = dict(new=new, open=_open, orphaned=orphaned, closed=closed) temp = Environment(newline_sequence='\r\n') content = temp.from_string(report_msg).render(params) # Format a report message _from = ERROR_RETURN + "@" + domain _to = ", ".join(admins) logging.debug("From: %s", _from) logging.debug("To: %s", _to) logging.debug("Subject: %s", report_subject) logging.debug("\n%s", content) message = Message() message['To'] = message['Reply-To'] = _to message['From'] = message['Return-Path'] = _from message['Subject'] = Header(report_subject, charset) message.set_payload(content.encode(charset)) message.set_charset(charset) # Send the report to the relay host relay = smtplib.SMTP(relay_host, relay_port) if debug: relay.set_debuglevel(1) relay.sendmail(_from, admins, message.as_string()) relay.quit() logging.debug("Sent a report mail")
def __init__(self, listen_address=None, listen_port=None, relay_host=None, relay_port=None, db_url=None, db_name=None, domain=None, debug=False, **kwargs): self.relay_host = relay_host self.relay_port = relay_port self.at_domain = "@" + domain self.debug = debug db.init_db(db_url, db_name) return super().__init__((listen_address, listen_port), None)
def setUp(self): self.db_name = "test%04d" % random.randint(0, 1000) db.init_db("mongodb://localhost", self.db_name)
def list_tenant(ctx): config = ctx.obj['config'] db.init_db(config['db_url'], config['db_name']) tenants = db.find_tenants({}) for tenant in tenants: print("%(tenant_name)s: %(status)s %(created)s" % tenant)