From 54ba2a33860ac4fa0274cf3a173112cbd7179556 Mon Sep 17 00:00:00 2001 From: atlurie <atlurie@uab.edu> Date: Fri, 20 Mar 2020 20:50:01 +0000 Subject: [PATCH] RabbitMQ agent to create a user in BrightCM. --- brightcm_account_create.py | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 brightcm_account_create.py diff --git a/brightcm_account_create.py b/brightcm_account_create.py new file mode 100644 index 0000000..3d96949 --- /dev/null +++ b/brightcm_account_create.py @@ -0,0 +1,61 @@ +#!/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) + print("Received msg{}".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;"' + + #popen(cmd) + print(cmd) + success = true + except: + e = sys.exc_info()[0] + print("[{}]: Error: {}".format(task, e)) + + # Acknowledge message + ch.basic_ack(delivery_tag=method.delivery_tag) + + # send confirm message + rc_rmq.publish_msg({ + 'routing_key': 'confirm.' + username, + 'msg': { + 'task': task, + 'success': success + } + }) + + if success: + # send create message to verify dir permissions agent + rc_rmq.publish_msg({ + 'routing_key': 'verify.' + username, + 'msg': msg + }) + +print("Start listening to queue: {}".format(task)) +rc_rmq.start_consume({ + 'queue': task, + 'routing_key': "create.*", + 'cb': bright_account_create +}) + +rc_rmq.disconnect() -- GitLab