Skip to content

Commit

Permalink
[AP-1696] Added list_schedule_ids and delete_schedule commands (#145)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
amofakhar authored Apr 9, 2024
1 parent ce2c5b6 commit 6382e1a
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/tw-rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runChecks: true
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.7.0
-----
- Add delete_schedule command
- Add list_schedule_ids command


0.5.1
-----
- Bug fix setup
Expand Down
30 changes: 30 additions & 0 deletions cicada/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import sys
import inspect
from pkg_resources import get_distribution

from cicada.lib import utils

Expand All @@ -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")
Expand All @@ -32,6 +35,9 @@ def __init__(self):
"spread_schedules",
"archive_schedule_log",
"ping_slack",
"list_schedule_ids",
"delete_schedule",
"version"
]

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -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."""
Expand Down
22 changes: 22 additions & 0 deletions cicada/commands/delete_schedule.py
Original file line number Diff line number Diff line change
@@ -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!")
19 changes: 19 additions & 0 deletions cicada/commands/list_schedules.py
Original file line number Diff line number Diff line change
@@ -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']))
15 changes: 14 additions & 1 deletion cicada/lib/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_functional_cli_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6382e1a

Please sign in to comment.