Skip to content
Snippets Groups Projects
Commit 059ee91c authored by Bo-Chun Chen's avatar Bo-Chun Chen
Browse files

Fix E501 line length 79

parent d59399e0
No related branches found
No related tags found
No related merge requests found
...@@ -13,9 +13,7 @@ parser.add_argument( ...@@ -13,9 +13,7 @@ parser.add_argument(
parser.add_argument( parser.add_argument(
"reason", nargs="?", default="", help="Reason of requesting" "reason", nargs="?", default="", help="Reason of requesting"
) )
parser.add_argument( parser.add_argument("--domain", default="localhost", help="domain of email")
"--domain", default="localhost", help="domain of email"
)
parser.add_argument( parser.add_argument(
"-v", "--verbose", action="store_true", help="verbose output" "-v", "--verbose", action="store_true", help="verbose output"
) )
......
...@@ -38,9 +38,7 @@ def ohpc_account_create(ch, method, properties, body): ...@@ -38,9 +38,7 @@ def ohpc_account_create(ch, method, properties, body):
if success: if success:
# send create message to other agent # send create message to other agent
rc_rmq.publish_msg( rc_rmq.publish_msg({"routing_key": "create." + username, "msg": msg})
{"routing_key": "create." + username, "msg": msg}
)
print("Start Listening to queue: {}".format(task)) print("Start Listening to queue: {}".format(task))
......
...@@ -18,9 +18,7 @@ def ood_account_create(ch, method, properties, body): ...@@ -18,9 +18,7 @@ def ood_account_create(ch, method, properties, body):
user_gid = str(msg["gid"]) user_gid = str(msg["gid"])
success = False success = False
try: try:
subprocess.call( subprocess.call(["sudo", "groupadd", "-r", "-g", user_gid, username])
["sudo", "groupadd", "-r", "-g", user_gid, username]
)
subprocess.call( subprocess.call(
["sudo", "useradd", "-u", user_uid, "-g", user_gid, username] ["sudo", "useradd", "-u", user_uid, "-g", user_gid, username]
) )
......
...@@ -38,9 +38,7 @@ channel.basic_publish( ...@@ -38,9 +38,7 @@ channel.basic_publish(
print(" [x] Sent {}: {}".format(node, json.dumps(message))) print(" [x] Sent {}: {}".format(node, json.dumps(message)))
# creates a named queue # creates a named queue
result = channel.queue_declare( result = channel.queue_declare(queue=user_name, exclusive=False, durable=True)
queue=user_name, exclusive=False, durable=True
)
# bind the queue with exchange # bind the queue with exchange
channel.queue_bind( channel.queue_bind(
...@@ -50,10 +48,7 @@ channel.queue_bind( ...@@ -50,10 +48,7 @@ channel.queue_bind(
def work(ch, method, properties, body): def work(ch, method, properties, body):
msg = json.loads(body) msg = json.loads(body)
print( print("Received message from {}: \n\t{}".format(method.routing_key, msg))
"Received message from {}: \n\t{}".format(method.routing_key, msg)
)
# queue_unbind(queue, exchange=None, routing_key=None, arguments=None, callback=None)
channel.queue_delete(method.routing_key) channel.queue_delete(method.routing_key)
......
...@@ -44,15 +44,12 @@ def dir_verify(ch, method, properties, body): ...@@ -44,15 +44,12 @@ def dir_verify(ch, method, properties, body):
mask = oct(status.st_mode)[-3:] mask = oct(status.st_mode)[-3:]
uid = str(status.st_uid) uid = str(status.st_uid)
gid = str(status.st_gid) gid = str(status.st_gid)
if ( if mask != "700" or uid != msg["uid"] or gid != msg["gid"]:
mask != "700"
or uid != msg["uid"]
or gid != msg["gid"]
):
msg["success"] = False msg["success"] = False
msg[ msg["errmsg"] = (
"errmsg" f"Error: dir {path} permissions or ownership are"
] = f"Error: dir {path} permissions or ownership are wrong" " wrong"
)
except Exception as exception: except Exception as exception:
msg["success"] = False msg["success"] = False
......
...@@ -32,7 +32,8 @@ def create_account(msg): ...@@ -32,7 +32,8 @@ def create_account(msg):
# Bright command to create user # Bright command to create user
cmd = "/cm/local/apps/cmd/bin/cmsh -c " 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;"' cmd += 'commit;"'
if not args.dry_run: if not args.dry_run:
...@@ -61,13 +62,19 @@ def resolve_uid_gid(ch, method, properties, body): ...@@ -61,13 +62,19 @@ def resolve_uid_gid(ch, method, properties, body):
msg["gid"] = user_exists.split(":")[3] msg["gid"] = user_exists.split(":")[3]
else: else:
cmd_uid = "/usr/bin/getent passwd | \ cmd_uid = (
awk -F: 'BEGIN { maxuid=10000 } ($3>10000) && ($3<20000) && ($3>maxuid) { maxuid=$3; } END { print maxuid+1; }'" "/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() msg["uid"] = popen(cmd_uid).read().rstrip()
logger.info(f"UID query: {cmd_uid}") logger.info(f"UID query: {cmd_uid}")
cmd_gid = "/usr/bin/getent group | \ cmd_gid = (
awk -F: 'BEGIN { maxgid=10000 } ($3>10000) && ($3<20000) && ($3>maxgid) { maxgid=$3; } END { print maxgid+1; }'" "/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() msg["gid"] = popen(cmd_gid).read().rstrip()
logger.info(f"GID query: {cmd_gid}") logger.info(f"GID query: {cmd_gid}")
...@@ -76,9 +83,10 @@ def resolve_uid_gid(ch, method, properties, body): ...@@ -76,9 +83,10 @@ def resolve_uid_gid(ch, method, properties, body):
msg["success"] = True msg["success"] = True
except Exception as exception: except Exception as exception:
msg["success"] = False msg["success"] = False
msg[ msg["errmsg"] = (
"errmsg" "Exception raised during account creation, check logs for stack"
] = f"Exception raised during account creation, check logs for stack trace" " trace"
)
logger.error("", exc_info=True) logger.error("", exc_info=True)
# Acknowledge message # Acknowledge message
......
...@@ -37,8 +37,12 @@ def git_commit(ch, method, properties, body): ...@@ -37,8 +37,12 @@ def git_commit(ch, method, properties, body):
username = msg["username"] username = msg["username"]
msg["task"] = task msg["task"] = task
msg["success"] = False msg["success"] = False
branch_name = "issue-add-users-" + \ branch_name = (
username.lower() + "-" + time.strftime("%Y%m%d_%H%M%S") "issue-add-users-"
+ username.lower()
+ "-"
+ time.strftime("%Y%m%d_%H%M%S")
)
user_ldif = users_dir + f"/{username}.ldif" user_ldif = users_dir + f"/{username}.ldif"
group_ldif = groups_dir + f"/{username}.ldif" group_ldif = groups_dir + f"/{username}.ldif"
...@@ -55,14 +59,13 @@ def git_commit(ch, method, properties, body): ...@@ -55,14 +59,13 @@ def git_commit(ch, method, properties, body):
if not branch_exists: if not branch_exists:
logger.debug("git checkout -b %s", branch_name) logger.debug("git checkout -b %s", branch_name)
git.checkout("-b", branch_name) git.checkout("-b", branch_name)
logger.debug( logger.debug("open(%s, 'w'), open(%s, 'w')", user_ldif, group_ldif)
"open(%s, 'w'), open(%s, 'w')", user_ldif, group_ldif
)
with open(user_ldif, "w") as ldif_u, open( with open(user_ldif, "w") as ldif_u, open(
group_ldif, "w" group_ldif, "w"
) as ldif_g: ) as ldif_g:
logger.debug( 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( ldapsearch(
"-LLL", "-LLL",
...@@ -75,7 +78,8 @@ def git_commit(ch, method, properties, body): ...@@ -75,7 +78,8 @@ def git_commit(ch, method, properties, body):
_out=ldif_u, _out=ldif_u,
) )
logger.debug( 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( ldapsearch(
"-LLL", "-LLL",
...@@ -93,9 +97,7 @@ def git_commit(ch, method, properties, body): ...@@ -93,9 +97,7 @@ def git_commit(ch, method, properties, body):
git.add(user_ldif) git.add(user_ldif)
logger.debug("git add %s", group_ldif) logger.debug("git add %s", group_ldif)
git.add(group_ldif) git.add(group_ldif)
logger.debug( logger.debug("git commit -m 'Added new cheaha user: %s'", username)
"git commit -m 'Added new cheaha user: %s'", username
)
git.commit(m="Added new cheaha user: " + username) git.commit(m="Added new cheaha user: " + username)
logger.debug("git checkout master") logger.debug("git checkout master")
git.checkout("master") git.checkout("master")
......
...@@ -63,7 +63,8 @@ def notify_user(ch, method, properties, body): ...@@ -63,7 +63,8 @@ def notify_user(ch, method, properties, body):
f"smtp.sendmail({rcfg.Sender}, {receivers}, message)" f"smtp.sendmail({rcfg.Sender}, {receivers}, message)"
) )
logger.info( 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: else:
......
...@@ -32,8 +32,10 @@ def mail_list_subscription(ch, method, properties, body): ...@@ -32,8 +32,10 @@ def mail_list_subscription(ch, method, properties, body):
mail_list_bcc = rcfg.Mail_list_bcc mail_list_bcc = rcfg.Mail_list_bcc
server = rcfg.Mail_server server = rcfg.Mail_server
listserv_cmd = f"QUIET ADD hpc-announce {email} {fullname} \ listserv_cmd = (
\nQUIET ADD hpc-users {email} {fullname}" f"QUIET ADD hpc-announce {email} {fullname}\n"
f"QUIET ADD hpc-users {email} {fullname}"
)
logger.info("Adding user{} to mail list".format(username)) logger.info("Adding user{} to mail list".format(username))
msg["success"] = False msg["success"] = False
...@@ -78,7 +80,7 @@ rc_rmq.start_consume( ...@@ -78,7 +80,7 @@ rc_rmq.start_consume(
{ {
"queue": task, # Define your Queue name "queue": task, # Define your Queue name
"routing_key": "verify.*", # Define your routing key "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
} }
) )
......
...@@ -17,9 +17,7 @@ def log_user_reg_events(ch, method, properties, body): ...@@ -17,9 +17,7 @@ def log_user_reg_events(ch, method, properties, body):
routing_key = method.routing_key routing_key = method.routing_key
action = routing_key.split(".")[0] action = routing_key.split(".")[0]
user = routing_key.split(".")[1] user = routing_key.split(".")[1]
print( print(f"Got a {action} message for {user} with routing key: {routing_key}")
f"Got a {action} message for {user} with routing key: {routing_key}"
)
print(msg) print(msg)
# Acknowledge message # Acknowledge message
......
...@@ -123,10 +123,7 @@ class RCRMQ(object): ...@@ -123,10 +123,7 @@ class RCRMQ(object):
if self.DEBUG: if self.DEBUG:
print( print(
"Queue: " "Queue: " + self.QUEUE + "\nRouting_key: " + self.ROUTING_KEY
+ self.QUEUE
+ "\nRouting_key: "
+ self.ROUTING_KEY
) )
if self._connection is None: if self._connection is None:
...@@ -134,9 +131,7 @@ class RCRMQ(object): ...@@ -134,9 +131,7 @@ class RCRMQ(object):
self.bind_queue() self.bind_queue()
self._consumer_tag = self._channel.basic_consume( self._consumer_tag = self._channel.basic_consume(self.QUEUE, obj["cb"])
self.QUEUE, obj["cb"]
)
self._consuming = True self._consuming = True
try: try:
self._channel.start_consuming() 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