diff --git a/agent_template.py b/agent_template.py index 3e09cfd651d7339c028d0ff6939048d249972a17..7858bdbfc144b7d2e858fb8fb525e5b4609659bd 100644 --- a/agent_template.py +++ b/agent_template.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -import sys import json from rc_rmq import RCRMQ @@ -8,14 +7,17 @@ task = "task_name" # Instantiate rabbitmq object rc_rmq = RCRMQ({"exchange": "RegUsr", "exchange_type": "topic"}) + # Define your callback function def on_message(ch, method, properties, body): # Retrieve routing key routing_key = method.routing_key + print(routing_key) # Retrieve message msg = json.loads(body) + print(msg) # Do Something print("[{}]: Callback called.".format(task)) diff --git a/create_account.py b/create_account.py index 6869f98918653e01d15f587d89d0cc31024e639f..d81478079249ced461f2f53876b20456b8613130 100755 --- a/create_account.py +++ b/create_account.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 import json -import sys import rc_util import argparse import signal @@ -14,9 +13,7 @@ parser.add_argument( parser.add_argument( "reason", nargs="?", default="", help="Reason of requesting" ) -parser.add_argument( - "--domain", default="localhost", help="domain of email" -) +parser.add_argument("--domain", default="localhost", help="domain of email") parser.add_argument( "-v", "--verbose", action="store_true", help="verbose output" ) diff --git a/dev_rmq_agents/ohpc_account_create.py b/dev_rmq_agents/ohpc_account_create.py index 206c4045094814751adc18876bf51d797911fd53..5cdabd5e6f0d0905a42abf85f1f92d10835cd145 100644 --- a/dev_rmq_agents/ohpc_account_create.py +++ b/dev_rmq_agents/ohpc_account_create.py @@ -20,7 +20,7 @@ def ohpc_account_create(ch, method, properties, body): subprocess.call(["sudo", "useradd", username]) print("[{}]: User {} has been added".format(task, username)) success = True - except: + except Exception: e = sys.exc_info()[0] print("[{}]: Error: {}".format(task, e)) @@ -38,9 +38,7 @@ def ohpc_account_create(ch, method, properties, body): if success: # send create message to other agent - rc_rmq.publish_msg( - {"routing_key": "create." + username, "msg": msg} - ) + rc_rmq.publish_msg({"routing_key": "create." + username, "msg": msg}) print("Start Listening to queue: {}".format(task)) diff --git a/dev_rmq_agents/ood_account_create.py b/dev_rmq_agents/ood_account_create.py index 339819ec86044ae180dfe1ecb40345ee6f3d5904..de4ed8a8d73d2af39e9fb86c29a0fb17bf83b67f 100644 --- a/dev_rmq_agents/ood_account_create.py +++ b/dev_rmq_agents/ood_account_create.py @@ -18,15 +18,13 @@ def ood_account_create(ch, method, properties, body): user_gid = str(msg["gid"]) success = False try: - subprocess.call( - ["sudo", "groupadd", "-r", "-g", user_gid, username] - ) + subprocess.call(["sudo", "groupadd", "-r", "-g", user_gid, username]) subprocess.call( ["sudo", "useradd", "-u", user_uid, "-g", user_gid, username] ) print("[{}]: User {} has been added".format(task, username)) success = True - except: + except Exception: e = sys.exc_info()[0] print("[{}]: Error: {}".format(task, e)) diff --git a/dev_rmq_agents/slurm_agent.py b/dev_rmq_agents/slurm_agent.py index eacc44c92ee61de611c6576cc0642adf6ed7992a..f6c4350c6cad79e010a6a7c576260db201a19bd3 100755 --- a/dev_rmq_agents/slurm_agent.py +++ b/dev_rmq_agents/slurm_agent.py @@ -40,7 +40,7 @@ def slurm_account_create(ch, method, properties, body): ) print("SLURM account for user {} has been added".format(username)) success = True - except: + except Exception: e = sys.exc_info()[0] print("[{}]: Error: {}".format(task, e)) diff --git a/flask_producer.py b/flask_producer.py index 1ef993ad630fb5cc1a0ba25a72b69c8a9e619471..908b57f9d9e5c6f752421dc76dc2fd472a4e2259 100755 --- a/flask_producer.py +++ b/flask_producer.py @@ -38,9 +38,7 @@ channel.basic_publish( print(" [x] Sent {}: {}".format(node, json.dumps(message))) # creates a named queue -result = channel.queue_declare( - queue=user_name, exclusive=False, durable=True -) +result = channel.queue_declare(queue=user_name, exclusive=False, durable=True) # bind the queue with exchange channel.queue_bind( @@ -50,10 +48,7 @@ channel.queue_bind( def work(ch, method, properties, body): msg = json.loads(body) - print( - "Received message from {}: \n\t{}".format(method.routing_key, msg) - ) - # queue_unbind(queue, exchange=None, routing_key=None, arguments=None, callback=None) + print("Received message from {}: \n\t{}".format(method.routing_key, msg)) channel.queue_delete(method.routing_key) diff --git a/prod_rmq_agents/dir_verify.py b/prod_rmq_agents/dir_verify.py index fd2fa8842e891e80674c75e1d6f6a96440c0b1ef..4b768c9428a4f1438eeb2b81d3f84d6d9c5c1ebc 100644 --- a/prod_rmq_agents/dir_verify.py +++ b/prod_rmq_agents/dir_verify.py @@ -1,8 +1,6 @@ #!/usr/bin/env python import os -import sys import json -import shutil import rc_util from pathlib import Path from rc_rmq import RCRMQ @@ -46,17 +44,14 @@ def dir_verify(ch, method, properties, body): mask = oct(status.st_mode)[-3:] uid = str(status.st_uid) gid = str(status.st_gid) - if ( - mask != "700" - or uid != msg["uid"] - or gid != msg["gid"] - ): + if mask != "700" or uid != msg["uid"] or gid != msg["gid"]: msg["success"] = False - msg[ - "errmsg" - ] = f"Error: dir {path} permissions or ownership are wrong" + msg["errmsg"] = ( + f"Error: dir {path} permissions or ownership are" + " wrong" + ) - except Exception as exception: + except Exception: msg["success"] = False msg["errmsg"] = "Exception raised, check the logs for stack trace" logger.error("", exc_info=True) diff --git a/prod_rmq_agents/get-next-uid-gid.py b/prod_rmq_agents/get-next-uid-gid.py index 5fbd7e4de50c9edbb4889b654ac4360e06dd9765..42d6cc892b138253c9e3aad2af2f0512357dce16 100644 --- a/prod_rmq_agents/get-next-uid-gid.py +++ b/prod_rmq_agents/get-next-uid-gid.py @@ -1,10 +1,6 @@ #!/usr/bin/env python -import sys import json -import ldap import time -import logging -import argparse import rc_util from os import popen from rc_rmq import RCRMQ @@ -22,9 +18,8 @@ args = rc_util.get_args() # Logger logger = rc_util.get_logger() -# Account creation - +# Account creation def create_account(msg): logger.info(f"Account creation request received: {msg}") @@ -36,7 +31,8 @@ def create_account(msg): # Bright command to create user cmd = "/cm/local/apps/cmd/bin/cmsh -c " - cmd += f'"user; add {username}; set id {uid}; set email {email}; set commonname \\"{fullname}\\"; ' + cmd += f'"user; add {username}; set id {uid}; set email {email};' + cmd += f'set commonname \\"{fullname}\\"; ' cmd += 'commit;"' if not args.dry_run: @@ -65,24 +61,31 @@ def resolve_uid_gid(ch, method, properties, body): msg["gid"] = user_exists.split(":")[3] else: - cmd_uid = "/usr/bin/getent passwd | \ - awk -F: 'BEGIN { maxuid=10000 } ($3>10000) && ($3<20000) && ($3>maxuid) { maxuid=$3; } END { print maxuid+1; }'" + cmd_uid = ( + "/usr/bin/getent passwd | awk -F: 'BEGIN { maxuid=10000 }" + " ($3>10000) && ($3<20000) && ($3>maxuid) { maxuid=$3; } END {" + " print maxuid+1; }'" + ) msg["uid"] = popen(cmd_uid).read().rstrip() logger.info(f"UID query: {cmd_uid}") - cmd_gid = "/usr/bin/getent group | \ - awk -F: 'BEGIN { maxgid=10000 } ($3>10000) && ($3<20000) && ($3>maxgid) { maxgid=$3; } END { print maxgid+1; }'" + cmd_gid = ( + "/usr/bin/getent group | awk -F: 'BEGIN { maxgid=10000 }" + " ($3>10000) && ($3<20000) && ($3>maxgid) { maxgid=$3; } END {" + " print maxgid+1; }'" + ) msg["gid"] = popen(cmd_gid).read().rstrip() logger.info(f"GID query: {cmd_gid}") create_account(msg) msg["task"] = task msg["success"] = True - except Exception as exception: + except Exception: msg["success"] = False - msg[ - "errmsg" - ] = f"Exception raised during account creation, check logs for stack trace" + msg["errmsg"] = ( + "Exception raised during account creation, check logs for stack" + " trace" + ) logger.error("", exc_info=True) # Acknowledge message diff --git a/prod_rmq_agents/git_commit.py b/prod_rmq_agents/git_commit.py index 68513a8ae7161981dcd69fc8a38a3d3ba6528bdc..c8665c17d3a596fa80923cf64b3b333e490a8034 100644 --- a/prod_rmq_agents/git_commit.py +++ b/prod_rmq_agents/git_commit.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import os import sh -import sys import json import rc_util from rc_rmq import RCRMQ @@ -38,8 +37,12 @@ def git_commit(ch, method, properties, body): username = msg["username"] msg["task"] = task msg["success"] = False - branch_name = "issue-add-users-" + \ - username.lower() + "-" + time.strftime("%Y%m%d_%H%M%S") + branch_name = ( + "issue-add-users-" + + username.lower() + + "-" + + time.strftime("%Y%m%d_%H%M%S") + ) user_ldif = users_dir + f"/{username}.ldif" group_ldif = groups_dir + f"/{username}.ldif" @@ -56,14 +59,13 @@ def git_commit(ch, method, properties, body): if not branch_exists: 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 - ) + 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 ldaps://ldapserver -b 'dc=cm,dc=cluster' uid={username} > {user_ldif}" + "ldapsearch -LLL -x -H ldaps://ldapserver -b 'dc=cm,dc=clu" + f"ster' uid={username} > {user_ldif}" ) ldapsearch( "-LLL", @@ -76,7 +78,8 @@ def git_commit(ch, method, properties, body): _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=cl" + f"uster' cn={username} > {group_ldif}" ) ldapsearch( "-LLL", @@ -94,9 +97,7 @@ def git_commit(ch, method, properties, body): 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 - ) + 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") @@ -110,7 +111,7 @@ def git_commit(ch, method, properties, body): logger.info("Added ldif files and committed to git repo") msg["success"] = True - except Exception as exception: + except Exception: logger.error("", exc_info=True) # Send confirm message diff --git a/prod_rmq_agents/notify_user.py b/prod_rmq_agents/notify_user.py index 4f1dd3e47db9c07703eb1261d4ce464e35c5b1d8..d204bad3d1e0e2b73862f98683672a0aef41f6a3 100644 --- a/prod_rmq_agents/notify_user.py +++ b/prod_rmq_agents/notify_user.py @@ -63,7 +63,8 @@ def notify_user(ch, method, properties, body): f"smtp.sendmail({rcfg.Sender}, {receivers}, message)" ) logger.info( - f"table.update({{'username': {username}, 'count': 1, 'sent_at': datetime.now()}}, ['username'])" + f"table.update({{'username': {username}, 'count': 1," + " 'sent_at': datetime.now()}}, ['username'])" ) else: @@ -86,7 +87,7 @@ def notify_user(ch, method, properties, body): logger.debug(f"User {username} inserted into database") msg["success"] = True - except Exception as exception: + except Exception: logger.error("", exc_info=True) msg["errmsg"] = errmsg if errmsg else "Unexpected error" diff --git a/prod_rmq_agents/subscribe_mail_lists.py b/prod_rmq_agents/subscribe_mail_lists.py index fcbc8c8e9c93c41ff66e569a83a8b475d72a534f..a70d007e54721979bd82ef03b4216710a9c74303 100644 --- a/prod_rmq_agents/subscribe_mail_lists.py +++ b/prod_rmq_agents/subscribe_mail_lists.py @@ -1,9 +1,6 @@ #!/usr/bin/env python -import sys import json import smtplib -import logging -import argparse import rc_util from email.message import EmailMessage from rc_rmq import RCRMQ @@ -35,8 +32,10 @@ def mail_list_subscription(ch, method, properties, body): mail_list_bcc = rcfg.Mail_list_bcc server = rcfg.Mail_server - listserv_cmd = f"QUIET ADD hpc-announce {email} {fullname} \ - \nQUIET ADD hpc-users {email} {fullname}" + listserv_cmd = ( + f"QUIET ADD hpc-announce {email} {fullname}\n" + f"QUIET ADD hpc-users {email} {fullname}" + ) logger.info("Adding user{} to mail list".format(username)) msg["success"] = False @@ -55,14 +54,14 @@ def mail_list_subscription(ch, method, properties, body): email_msg.set_content(listserv_cmd) if not args.dry_run: s.send_message(email_msg) - logging.info( + logger.info( f"This email will add user {username} to listserv \n{email_msg}" ) s.quit() msg["task"] = task msg["success"] = True - except Exception as exception: + except Exception: logger.error("", exc_info=True) # Acknowledge message @@ -81,7 +80,7 @@ rc_rmq.start_consume( { "queue": task, # Define your Queue name "routing_key": "verify.*", # Define your routing key - "cb": mail_list_subscription, # Pass in callback function you just define + "cb": mail_list_subscription, # Pass callback function you just define } ) diff --git a/prod_rmq_agents/task_manager.py b/prod_rmq_agents/task_manager.py index 9ad0b78bcdccfc679790fc983f67d65cb517a851..e2e3edc5a746e13c319df8a7f40e4a9bc539a930 100644 --- a/prod_rmq_agents/task_manager.py +++ b/prod_rmq_agents/task_manager.py @@ -259,7 +259,7 @@ def task_manager(ch, method, properties, body): logger.debug(f"Notify level {task_name}? {success}") - except Exception as exception: + except Exception: logger.error("", exc_info=True) if send: diff --git a/prod_rmq_agents/user_reg_event_logger.py b/prod_rmq_agents/user_reg_event_logger.py index 63b6f5bfb0e557a3030631c70d221b2d45aeef9e..177b2c97ff543e0f05936991a1512fa6a975cfdf 100644 --- a/prod_rmq_agents/user_reg_event_logger.py +++ b/prod_rmq_agents/user_reg_event_logger.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -import sys import json from rc_rmq import RCRMQ @@ -8,6 +7,7 @@ task = "user_reg_event_log" # Instantiate rabbitmq object rc_rmq = RCRMQ({"exchange": "RegUsr", "exchange_type": "topic"}) + # Define your callback function def log_user_reg_events(ch, method, properties, body): @@ -18,9 +18,7 @@ def log_user_reg_events(ch, method, properties, body): routing_key = method.routing_key action = routing_key.split(".")[0] user = routing_key.split(".")[1] - print( - f"Got a {action} message for {user} with routing key: {routing_key}" - ) + print(f"Got a {action} message for {user} with routing key: {routing_key}") print(msg) # Acknowledge message diff --git a/rc_rmq.py b/rc_rmq.py index daa61c237e77662b86238b29c18e2afe5ba6c001..c6fdf70e5679d4d27f89d9261cbf6da7cd70d8a8 100644 --- a/rc_rmq.py +++ b/rc_rmq.py @@ -123,10 +123,7 @@ class RCRMQ(object): if self.DEBUG: print( - "Queue: " - + self.QUEUE - + "\nRouting_key: " - + self.ROUTING_KEY + "Queue: " + self.QUEUE + "\nRouting_key: " + self.ROUTING_KEY ) if self._connection is None: @@ -134,9 +131,7 @@ class RCRMQ(object): self.bind_queue() - self._consumer_tag = self._channel.basic_consume( - self.QUEUE, obj["cb"] - ) + self._consumer_tag = self._channel.basic_consume(self.QUEUE, obj["cb"]) self._consuming = True try: self._channel.start_consuming()