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

Merge pull request #105 from diedpigs/fix-linting

Fix linting
parents ba3b3c03 38f1df25
No related branches found
No related tags found
No related merge requests found
#!/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))
......
#!/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"
)
......
......@@ -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))
......
......@@ -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))
......
......@@ -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))
......
......@@ -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)
......
#!/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)
......
#!/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
......
#!/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
......
......@@ -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"
......
#!/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
}
)
......
......@@ -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:
......
#!/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
......
......@@ -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()
......
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