From 6382e1a5a5ecb53d9ac2f0c78cab07de532cb66c Mon Sep 17 00:00:00 2001 From: Amir Mofakhar Date: Tue, 9 Apr 2024 11:14:28 +0100 Subject: [PATCH] [AP-1696] Added list_schedule_ids and delete_schedule commands (#145) * Added list_schedule_ids and delete_schedule commands * Added list_schedule_ids and delete_schedule commands * added option for getting version * added requirements * add workflow --- .github/tw-rules.yaml | 1 + CHANGELOG.md | 6 +++++ cicada/cli.py | 30 +++++++++++++++++++++++++ cicada/commands/delete_schedule.py | 22 ++++++++++++++++++ cicada/commands/list_schedules.py | 19 ++++++++++++++++ cicada/lib/scheduler.py | 15 ++++++++++++- setup.py | 2 +- tests/test_functional_cli_entrypoint.py | 28 +++++++++++++++++++++++ 8 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 .github/tw-rules.yaml create mode 100644 cicada/commands/delete_schedule.py create mode 100644 cicada/commands/list_schedules.py diff --git a/.github/tw-rules.yaml b/.github/tw-rules.yaml new file mode 100644 index 0000000..295545d --- /dev/null +++ b/.github/tw-rules.yaml @@ -0,0 +1 @@ +runChecks: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 69615be..00befc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +0.7.0 +----- +- Add delete_schedule command +- Add list_schedule_ids command + + 0.5.1 ----- - Bug fix setup diff --git a/cicada/cli.py b/cicada/cli.py index 029b297..f80ffa5 100644 --- a/cicada/cli.py +++ b/cicada/cli.py @@ -3,6 +3,7 @@ import argparse import sys import inspect +from pkg_resources import get_distribution from cicada.lib import utils @@ -15,6 +16,8 @@ from cicada.commands import spread_schedules from cicada.commands import archive_schedule_log from cicada.commands import ping_slack +from cicada.commands import list_schedules +from cicada.commands import delete_schedule @utils.named_exception_handler("Cicada") @@ -32,6 +35,9 @@ def __init__(self): "spread_schedules", "archive_schedule_log", "ping_slack", + "list_schedule_ids", + "delete_schedule", + "version" ] parser = argparse.ArgumentParser( @@ -239,6 +245,30 @@ def ping_slack(): args = parser.parse_args(sys.argv[2:]) ping_slack.main(args.text) + @staticmethod + def list_schedule_ids(): + """List schedule id of all schedules""" + list_schedules.main() + + @staticmethod + def delete_schedule(): + """Delete a schedule using schedule_id""" + parser = argparse.ArgumentParser( + allow_abbrev=False, + add_help=True, + prog=inspect.stack()[0][3], + description="Delete a schedule using schedule_id", + ) + parser.add_argument("--schedule_id", type=str, required=True, help="Id of the schedule") + # now that we're inside a subcommand, ignore the first TWO args + args = parser.parse_args(sys.argv[2:]) + delete_schedule.main(args.schedule_id) + + @staticmethod + def version(): + """Return version of cicada package""" + print(get_distribution("cicada").version) + def main(): """Cicada agent CLI.""" diff --git a/cicada/commands/delete_schedule.py b/cicada/commands/delete_schedule.py new file mode 100644 index 0000000..734b11a --- /dev/null +++ b/cicada/commands/delete_schedule.py @@ -0,0 +1,22 @@ +"""Delete a schedule using schedule_id.""" + +import sys + +from tabulate import tabulate + +from cicada.lib import postgres +from cicada.lib import scheduler +from cicada.lib import utils + + +@utils.named_exception_handler("delete_schedule") +def main(schedule_id, dbname=None): + """Delete a schedule using schedule_id.""" + + db_conn = postgres.db_cicada(dbname) + db_cur = db_conn.cursor() + scheduler.delete_schedule(db_cur, str(schedule_id)) + db_cur.close() + db_conn.close() + + print("schedule_id '" + str(schedule_id) + "' is deleted!") diff --git a/cicada/commands/list_schedules.py b/cicada/commands/list_schedules.py new file mode 100644 index 0000000..db891cc --- /dev/null +++ b/cicada/commands/list_schedules.py @@ -0,0 +1,19 @@ +"""List all schedule ID's.""" + +from tabulate import tabulate + +from cicada.lib import postgres +from cicada.lib import scheduler +from cicada.lib import utils + + +@utils.named_exception_handler("list_schedule_ids") +def main(dbname=None): + """Show all Cicada schedule ID's.""" + db_conn = postgres.db_cicada(dbname) + db_cur = db_conn.cursor() + obj_schedules = scheduler.get_all_schedule_ids(db_cur) + db_cur.close() + db_conn.close() + print("") + print(tabulate(obj_schedules, headers=['Server ID', 'Schedule ID', 'Description'])) diff --git a/cicada/lib/scheduler.py b/cicada/lib/scheduler.py index 47c7e55..8d9755c 100644 --- a/cicada/lib/scheduler.py +++ b/cicada/lib/scheduler.py @@ -25,7 +25,6 @@ def get_host_details(): hostname = hostname[: hostname.find(".")] fqdn = socket.getfqdn() - ip4_address = socket.gethostbyname(fqdn) host_details = {"hostname": hostname, "fqdn": fqdn, "ip4_address": ip4_address} @@ -348,3 +347,17 @@ def get_all_schedules(db_cur, server_id, is_async): obj_schedules.append(schedule_id) return obj_schedules + + +def get_all_schedule_ids(db_cur): + sqlquery = "SELECT server_id, schedule_id, schedule_description from schedules" + db_cur.execute(sqlquery) + cur_schedules = db_cur + + schedule_ids = cur_schedules.fetchall() + return schedule_ids + + +def delete_schedule(db_cur, schedule_id): + sqlquery = f"DELETE from schedules WHERE schedule_id = '{schedule_id}'" + db_cur.execute(sqlquery) diff --git a/setup.py b/setup.py index 8f6c72c..c2d7456 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name="cicada", - version="0.6.0", + version="0.7.0", description="Lightweight, agent-based, distributed scheduler", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_functional_cli_entrypoint.py b/tests/test_functional_cli_entrypoint.py index 25abb76..3b53dcd 100644 --- a/tests/test_functional_cli_entrypoint.py +++ b/tests/test_functional_cli_entrypoint.py @@ -285,3 +285,31 @@ def test_ping_slack_help(): --text TEXT Text to send to Slack """ assert actual == expected + + +def test_delete_schedule(): + """test_delete_schedule""" + actual = subprocess.run(["cicada", "delete_schedule"], check=False, stderr=subprocess.PIPE).stderr.decode("utf-8") + + expected = """usage: delete_schedule [-h] --schedule_id SCHEDULE_ID +delete_schedule: error: the following arguments are required: --schedule_id +""" + assert actual == expected + + +def test_delete_schedule_help(): + """test_delete_schedule_help""" + actual = subprocess.run(["cicada", "delete_schedule", "-h"], check=True, stdout=subprocess.PIPE).stdout.decode( + "utf-8" + ) + + expected = """usage: delete_schedule [-h] --schedule_id SCHEDULE_ID + +Delete a schedule using schedule_id + +optional arguments: + -h, --help show this help message and exit + --schedule_id SCHEDULE_ID + Id of the schedule +""" + assert actual == expected