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

Merge branch 'fix-account-cli' into 'main'

Fix account cli

See merge request rc/rabbitmq_agents!152
parents 7be16cf2 97b993d6
No related branches found
Tags v1.3.0
1 merge request!152Fix account cli
Pipeline #11105 passed with stage
in 1 minute and 19 seconds
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import rc_util
import argparse import argparse
import signal import signal
import uuid
import pika
import rc_util import rc_util
from rc_rmq import RCRMQ from rc_rmq import RCRMQ
import rabbit_config as rcfg import rabbit_config as rcfg
import time
parser = argparse.ArgumentParser(description = "Account management driver script") parser = argparse.ArgumentParser(
parser.add_argument( description="Account management driver script"
"username", help="Username that should be locked/unlocked") )
parser.add_argument("username", help="Username that should be locked/unlocked")
parser.add_argument( parser.add_argument(
"state", choices=['ok', 'hold', 'certification', 'pre_certification'], help="Choose from states (ok,hold,certification,pre_certification)") "state",
choices=["ok", "hold", "certification", "pre_certification"],
help="Choose from states (ok,hold,certification,pre_certification)",
)
parser.add_argument( parser.add_argument(
"-s", "--service", nargs='+', default='all', choices=['ssh', 'newjobs', 'expiration', 'all'], help="List one or more services to be blocked (default: %(default)s)") "-s",
"--service",
nargs="+",
default="all",
choices=["ssh", "newjobs", "expiration", "all"],
help="List one or more services to be blocked (default: %(default)s)",
)
parser.add_argument( parser.add_argument(
"-v", "--verbose", action="store_true", help="verbose output") "-v", "--verbose", action="store_true", help="verbose output"
)
parser.add_argument( parser.add_argument(
"-n", "--dry-run", action="store_true", help="enable dry run mode" "-n", "--dry-run", action="store_true", help="enable dry run mode"
) )
...@@ -41,11 +48,12 @@ msg["state"] = state ...@@ -41,11 +48,12 @@ msg["state"] = state
msg["service"] = service msg["service"] = service
msg["queuename"] = queuename msg["queuename"] = queuename
msg["updated_by"], msg["host"] = rc_util.get_caller_info() msg["updated_by"], msg["host"] = rc_util.get_caller_info()
msg["interface"] = "CLI"
# publish msg with acctmgr.{uname} routing key. # publish msg with acctmgr.{uname} routing key.
rc_rmq.publish_msg( rc_rmq.publish_msg(
{ {
"routing_key": f'acctmgr.request.{queuename}', "routing_key": f"acctmgr.request.{queuename}",
"msg": msg, "msg": msg,
} }
) )
...@@ -61,18 +69,23 @@ def callback(ch, method, properties, body): ...@@ -61,18 +69,23 @@ def callback(ch, method, properties, body):
username = msg["username"] username = msg["username"]
if msg["success"]: if msg["success"]:
print(f"Account for {username} has been {msg['action']}ed.\n Updating the user state in DB") print(
f"Account for {username} has been {msg['action']}ed.\n "
"Updating the user state in DB"
)
else: else:
print(f"There's some issue in account management agents for {username}") print(
f"There's some issue in account management agents for {username}"
)
errmsg = msg.get("errmsg", []) errmsg = msg.get("errmsg", [])
for err in errmsg: for err in errmsg:
print(err) print(err)
ch.basic_ack(delivery_tag=method.delivery_tag) ch.basic_ack(delivery_tag=method.delivery_tag)
rc_rmq.stop_consume() rc_rmq.stop_consume()
rc_rmq.delete_queue(queuename) rc_rmq.delete_queue(queuename)
print(f"Request {username} account state set to {state}.") print(f"Request {username} account state set to {state}.")
# Set initial timeout timer # Set initial timeout timer
...@@ -83,7 +96,7 @@ print("Waiting for completion...") ...@@ -83,7 +96,7 @@ print("Waiting for completion...")
rc_rmq.start_consume( rc_rmq.start_consume(
{ {
"queue": queuename, "queue": queuename,
"routing_key": f'certified.{queuename}', "routing_key": f"certified.{queuename}",
"cb": callback, "cb": callback,
} }
) )
...@@ -40,7 +40,7 @@ def manage_group(op, usernames, groupname, debug=False): ...@@ -40,7 +40,7 @@ def manage_group(op, usernames, groupname, debug=False):
response = 0 response = 0
interface = "CLI" interface = "CLI"
executed_by, host = get_caller_info() updated_by, host = get_caller_info()
def handler(ch, method, properties, body): def handler(ch, method, properties, body):
if debug: if debug:
...@@ -78,7 +78,7 @@ def manage_group(op, usernames, groupname, debug=False): ...@@ -78,7 +78,7 @@ def manage_group(op, usernames, groupname, debug=False):
"groups": {f"{op}": [f"{groupname}"]}, "groups": {f"{op}": [f"{groupname}"]},
"username": user, "username": user,
"host": host, "host": host,
"executed_by": executed_by, "updated_by": updated_by,
"interface": interface, "interface": interface,
}, },
} }
......
BEGIN TRANSACTION;
-- Create new table with updated_by column
CREATE TABLE groups_temp (
id INTEGER PRIMARY KEY,
user TEXT,
"group" TEXT,
operation INTEGER,
date DATETIME,
host TEXT,
updated_by TEXT,
interface TEXT
);
-- Copy all entries from old table
INSERT INTO groups_temp(user,"group",operation,date,host,updated_by,interface)
SELECT user,"group",operation,date,host,executed_by,interface
FROM groups;
-- Drop old table
DROP TABLE groups;
-- Rename new table
ALTER TABLE groups_temp
RENAME TO groups;
COMMIT;
...@@ -36,8 +36,8 @@ def insert_db(operation, groupname, msg): ...@@ -36,8 +36,8 @@ def insert_db(operation, groupname, msg):
"operation": op, "operation": op,
"date": datetime.now(), "date": datetime.now(),
"host": msg["host"], "host": msg["host"],
"executed_by": msg["executed_by"], "updated_by": msg["updated_by"],
"interface": msg["interface"], "interface": msg.get("interface", ""),
} }
) )
...@@ -53,7 +53,7 @@ def group_member(ch, method, properties, body): ...@@ -53,7 +53,7 @@ def group_member(ch, method, properties, body):
groups (dict): A dictionary with `add` or `remove` key. groups (dict): A dictionary with `add` or `remove` key.
add (list): A list of groups to be added for the user. add (list): A list of groups to be added for the user.
remove (list): A list of groups to be removed for the user. remove (list): A list of groups to be removed for the user.
executed_by (str): The user who request the change. updated_by (str): The user who request the change.
host (str): Hostname where the request comes from. host (str): Hostname where the request comes from.
interface (str): whether it's from CLI or WebUI. interface (str): whether it's from CLI or WebUI.
......
[tool.black] [tool.black]
line-length=79 line-length = 79
target-version=['py36'] target-version = ['py36']
preview = true
[tool.pylint.main]
disable = ["invalid-name", "import-error", "unused-argument", "broad-except"]
ignore = ["config.py", "tests.py"]
[tool.pylint.format]
max-line-length = 79
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