Skip to content
Snippets Groups Projects
rc_util.py 1.93 KiB
Newer Older
import logging
import argparse
Bo-Chun Chen's avatar
Bo-Chun Chen committed
from rc_rmq import RCRMQ
import json

rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
tasks = {'create_account': None, 'git_commit': None, 'dir_verify': None, 'subscribe_mail_list': None, 'notify_user': None}
logger_fmt = '%(asctime)s [%(module)s] - %(message)s'
Bo-Chun Chen's avatar
Bo-Chun Chen committed

def add_account(username, email, full='', reason=''):
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  rc_rmq.publish_msg({
    'routing_key': 'request.' + username,
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    'msg': {
      "username": username,
      "email": email,
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      "fullname": full,
      "reason": reason
    }
  })
  rc_rmq.disconnect()
Bo-Chun Chen's avatar
Bo-Chun Chen committed

def worker(ch, method, properties, body):
    msg = json.loads(body)
    username = msg['username']
Bo-Chun Chen's avatar
Bo-Chun Chen committed

    if msg['success']:
        print(f'Account for {username} has been created.')
    else:
        print(f"There's some issue while creating account for {username}")
        errmsg = msg.get('errmsg', [])
        for err in errmsg:
            print(err)

    rc_rmq.stop_consume()
    rc_rmq.delete_queue()
Bo-Chun Chen's avatar
Bo-Chun Chen committed
def consume(username, routing_key='', callback=worker, debug=False):
    if routing_key == '':
        routing_key = 'complete.' + username
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    if debug:
        sleep(5)
    else:
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        rc_rmq.start_consume({
Bo-Chun Chen's avatar
Bo-Chun Chen committed
            'queue': username,
Bo-Chun Chen's avatar
Bo-Chun Chen committed
            'routing_key': routing_key,
            'cb': callback
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        })
        rc_rmq.disconnect()
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    return { 'success' : True }

def get_args():
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', action='store_true', help='verbose output')
    parser.add_argument('-n', '--dry-run', action='store_true', help='enable dry run mode')
    return parser.parse_args()

def get_logger(args=None):
    if args is None:
        args = get_args()

    logger_lvl = logging.WARNING

    if args.verbose:
        logger_lvl = logging.DEBUG

    if args.dry_run:
        logger_lvl = logging.INFO

    logging.basicConfig(format=logger_fmt, level=logger_lvl)
    return logging.getLogger(__name__)