Skip to content
Snippets Groups Projects
Unverified Commit 8298392c authored by Ravi Tripathi's avatar Ravi Tripathi Committed by GitHub
Browse files

Merge branch 'feat-cod-rmq' into feat-resolve-uid-gid

parents cdf49d24 137f4d23
No related branches found
No related tags found
6 merge requests!147Merge previous default branch feat-cod-rmq into main,!85kill nginx process running under user from login node,!51Fix acct create wait,!39WIP:Feat cod rmq,!38WIP: Feat cod rmq,!28Feat resolve uid gid
#!/usr/bin/env python #!/usr/bin/env python3
import sys import sys
import rc_util import rc_util
......
#!/usr/bin/env python
import sys
import json
import shutil
import rc_util
from pathlib import Path
from rc_rmq import RCRMQ
task = 'dir_verify'
dirs = ['/home', '/data/user', '/data/scratch']
args = rc_util.get_args()
logger = rc_util.get_logger(args)
# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
def dir_verify(ch, method, properties, body):
msg = json.loads(body)
username = msg['username']
msg['task'] = task
msg['success'] = False
try:
for d in dirs:
path = Path(d) / msg['username']
if args.dry_run:
logger.info(f'Checking dirs: {path}')
else:
if not path.exists():
# Make sure folder exists and with right permission
path.mkdir(mode=0o700)
# Make sure ownership is correct
shutil.chown(path, int(msg['uid']), int(msg['gid']))
logger.debug(f'{path} created')
msg['success'] = True
except Exception as exception:
logger.error('', exc_info=True)
# send confirm message
rc_rmq.publish_msg({
'routing_key': 'confirm.' + username,
'msg': msg
})
logger.debug(f'User {username} confirmation sent')
ch.basic_ack(delivery_tag=method.delivery_tag)
logger.info(f'Start listening to queue: {task}')
rc_rmq.start_consume({
'queue': task,
'routing_key': "verify.*",
'cb': dir_verify
})
logger.info('Disconnected')
rc_rmq.disconnect()
#!/usr/bin/env python
import os
import sh
import sys
import json
import rc_util
from rc_rmq import RCRMQ
task = 'git_commit'
# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
# Define some location
repo_location = os.path.expanduser('~/git/rc-users')
users_dir = repo_location + '/users'
groups_dir = repo_location + '/groups'
args = rc_util.get_args()
logger = rc_util.get_logger(args)
if not args.dry_run:
git = sh.git.bake('-C', repo_location)
ldapsearch = sh.Command('ldapsearch')
else:
git = sh.echo.bake('git', '-C', repo_location)
ldapsearch = sh.echo.bake('ldapsearch')
def git_commit(ch, method, properties, body):
msg = json.loads(body)
username = msg['username']
ticketnum = msg.get('ticketnum', 'add-users-' + username.lower())
msg['task'] = task
msg['success'] = False
branch_name = 'issue-' + ticketnum
user_ldif = users_dir + f'/{username}.ldif'
group_ldif = groups_dir + f'/{username}.ldif'
logger.info("Received: %s", msg)
logger.debug("ticketnum: %s", ticketnum)
logger.debug("branch_name: %s", branch_name)
try:
logger.debug('git checkout master')
git.checkout('master')
logger.debug('git pull')
git.pull()
logger.debug('git checkout -b %s', branch_name)
git.checkout('-b', branch_name)
logger.debug("open(%s, 'w'), open(%s, 'w')", user_ldif, group_ldif)
with open(user_ldif, 'w') as ldif_u,\
open(group_ldif, 'w') as ldif_g:
logger.debug(f"ldapsearch -LLL -x -h ldapserver -b 'dc=cm,dc=cluster' uid={username} > {user_ldif}")
ldapsearch('-LLL', '-x', '-h', 'ldapserver', '-b', "dc=cm,dc=cluster", f"uid={username}", _out=ldif_u)
logger.debug(f"ldapsearch -LLL -x -h ldapserver -b 'ou=Group,dc=cm,dc=cluster' cn={username} > {group_ldif}")
ldapsearch('-LLL', '-x', '-h', 'ldapserver', '-b', "ou=Group,dc=cm,dc=cluster", f"cn={username}", _out=ldif_g)
logger.info('user ldif files generated.')
logger.debug('git add %s', user_ldif)
git.add(user_ldif)
logger.debug('git add %s', group_ldif)
git.add(group_ldif)
logger.debug("git commit -m 'Added new cheaha user: %s'", username)
git.commit(m="Added new cheaha user: " + username)
logger.debug('git checkout master')
git.checkout('master')
logger.debug('git merge %s --no-ff --no-edit', branch_name)
git.merge(branch_name, '--no-ff', '--no-edit')
logger.debug('git push origin master')
git.push('origin', 'master')
# merge with gitlab api
logger.info('Added ldif files and committed to git repo')
msg['success'] = True
except Exception as exception:
logger.error('', exc_info=True)
# Send confirm message
logger.debug('rc_rmq.publish_msge()')
rc_rmq.publish_msg({
'routing_key': 'confirm.' + username,
'msg': msg
})
logger.info('confirmation sent')
# Acknowledge message
logger.debug('ch.basic_ack()')
ch.basic_ack(delivery_tag=method.delivery_tag)
logger.info("Start listening to queue: %s", task)
rc_rmq.start_consume({
'queue': task,
'routing_key': "verify.*",
'cb': git_commit
})
logger.info("Disconnected")
rc_rmq.disconnect()
# Some variable for email
Server = 'localhost'
My_email = 'root@localhost'
Sender = 'ROOT@LOCALHOST'
Sender_alias = 'Services'
Subject = 'New User Account'
Info_url = 'https://www.google.com'
Head = f"""From: {Sender_alias} <{Sender}>
To: <{{{{ to }}}}>
Subject: {Subject}
"""
Body = f"""
Hi {{{{ username }}}}
Your account has been set up with:
============================
User ID: {{{{ username }}}}
============================
If you have any questions, please visit:
{Info_url}
or email at {My_email}
Cheers,
"""
Whole_mail = Head + Body
#!/usr/bin/env python
import sys
import json
import rc_util
import smtplib
import dataset
from rc_rmq import RCRMQ
from jinja2 import Template
from datetime import datetime
import mail_config as mail_cfg
task = 'notify_user'
args = rc_util.get_args()
logger = rc_util.get_logger(args)
db = dataset.connect(f'sqlite:///.agent_db/{task}.db')
table = db['notified_user']
# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
# Email instruction to user
def notify_user(ch, method, properties, body):
msg = json.loads(body)
username = msg['username']
user_email = msg['email']
msg['task'] = task
msg['success'] = False
try:
# Search username in database
record = table.find_one(username=username)
if record:
# Update counter
count = record['count']
table.update({'username': username, 'count': count + 1}, ['username'])
logger.debug(f'User {username} counter updated to {count + 1}')
else:
# Send email to user
receivers = [user_email, mail_cfg.My_email]
message = Template(mail_cfg.Whole_mail).render(username=username, to=user_email)
if args.dry_run:
logger.info(f'smtp = smtplib.SMTP({mail_cfg.Server})')
logger.info(f'smtp.sendmail({mail_cfg.Sender}, {receivers}, message)')
logger.info(f"table.insert({{'username': {username}, 'count': 1, 'sent_at': datetime.now()}})")
else:
smtp = smtplib.SMTP(mail_cfg.Server)
smtp.sendmail(mail_cfg.Sender, receivers, message)
logger.debug(f'Email sent to: {user_email}')
table.insert({
'username': username,
'count': 1,
'sent_at': datetime.now()
})
logger.debug(f'User {username} inserted into database')
msg['success'] = True
except Exception as exception:
logger.error('', exc_info=True)
# Send confirm message
rc_rmq.publish_msg({
'routing_key': 'confirm.' + username,
'msg': msg
})
logger.debug(f'User {username} confirmation sent')
# Acknowledge the message
ch.basic_ack(delivery_tag=method.delivery_tag)
if __name__ == "__main__":
logger.info(f'Start listening to queue: {task}')
rc_rmq.start_consume({
'queue': task,
'routing_key': "notify.*",
'cb': notify_user
})
logger.info('Disconnected')
rc_rmq.disconnect()
pika==1.1.0 pika==1.1.0
pyldap==3.0.0.post1 pyldap==3.0.0.post1
dataset==1.3.1
Jinja2==2.11.2
sh==1.12.14
\ No newline at end of file
#!/usr/bin/env python
import sys
import json
import rc_util
from rc_rmq import RCRMQ
from datetime import datetime
task = 'task_manager'
args = rc_util.get_args()
logger = rc_util.get_logger(args)
record = {
'uid': -1,
'gid': -1,
'email': '',
'fullname': '',
'last_update': datetime.now(),
'request': {
'get_next_uid_gid': None
},
'create': {
'subscribe_mail_list': None,
'bright_account': None
},
'verify': {
'git_commit': None,
'dir_verify': None
},
'notify': {
'notify_user': None
},
'delivery_tags': None
}
# Currently tracking users
tracking = {}
# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
def task_manager(ch, method, properties, body):
msg = json.loads(body)
username = method.routing_key.split('.')[1]
task_name = msg['task']
done = success = msg['success']
routing_key = ""
if username not in tracking:
current = tracking[username] = record.copy()
current['delivery_tags'] = []
current['uid'] = msg.get('uid', -1)
current['gid'] = msg.get('gid', -1)
current['email'] = msg.get('email', '')
current['fullname'] = msg.get('fullname', '')
logger.debug(f'Tracking user {username}')
else:
current = tracking[username]
# Save the delivery tags for future use
current['delivery_tags'].append(method.delivery_tag)
try:
if task_name in current['request']:
current['request'][task_name] = success
routing_key = 'create.' + username
done = success
logger.debug(f'Request level task(s) done?{done}')
elif task_name in current['create']:
current['create'][task_name] = success
routing_key = 'verify.' + username
done = True
for status in current['create'].values():
if status is not True:
done = False
logger.debug(f'Create level task(s) done?{done}')
elif task_name in current['verify']:
current['verify'][task_name] = success
routing_key = 'notify.' + username
done = True
for status in current['verify'].values():
if status is not True:
done = False
logger.debug(f'Verify level task(s) done?{done}')
elif task_name in current['notify']:
current['verify'][task_name] = success
routing_key = 'complete.' + username
done = success
logger.debug(f'Notify level task(s) done?{done}')
except Exception as exception:
logger.error('', exc_info=True)
if done:
# Send trigger message
rc_rmq.publish_msg({
'routing_key': routing_key,
'msg': {
'username': username,
'fullname': current['fullname'],
'email': current['email'],
'uid': current['uid'],
'gid': current['gid']
}
})
logger.debug(f"Trigger message '{routing_key}' sent")
# Acknowledge all message from last level
for tag in current['delivery_tags']:
ch.basic_ack(tag)
current['delivery_tags'] = []
logger.debug('Previous level messages acknowledged')
logger.info(f'Start listening to queue: {task}')
rc_rmq.start_consume({
'queue': task,
'routing_key': "confirm.*",
'cb': task_manager
})
logger.info('Disconnected')
rc_rmq.disconnect()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment