Skip to content
Snippets Groups Projects
brightcm_account_create.py 1.58 KiB
Newer Older
#!/usr/bin/env python
import sys
import json
from os import popen
from rc_rmq import RCRMQ

task = 'bright_account'

# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})

# Define your callback function
def bright_account_create(ch, method, properties, body):
    # Retrieve message
    msg = json.loads(body)
    logger.info("Received {}".format(msg))
    username = msg['username']
    uid = msg['uid']
    email = msg['email']
    fullname = msg['fullname']
    success = False
    try:
        # Bright command to create user
        cmd = '/cm/local/apps/cmd/bin/cmsh -c '
        cmd += f'"user; add {username}; set userid {uid}; set email {email}; set commonname \\"{fullname}\\"; '
        cmd += 'commit;"'

        if not args.dry_run:
            popen(cmd)
        logger.info(f'Bright command to create user:{cmd}')
        msg['task'] = task
        msg['success'] = True
    except Exception:
        logger.exception("Fatal error:")

    # Acknowledge message
    ch.basic_ack(delivery_tag=method.delivery_tag)

    # send confirm message
    logger.debug('rc_rmq.publish_msg()')
    rc_rmq.publish_msg({
        'routing_key': 'confirm.' + username,
    logger.info('confirmation sent')
logger.info("Start listening to queue: {}".format(task))
rc_rmq.start_consume({
    'queue': task,
    'routing_key': "create.*",
    'cb': bright_account_create
})

rc_rmq.disconnect()