From 54329958a9cc355966753e72ca4ae2e3ee14fd14 Mon Sep 17 00:00:00 2001 From: Mitul Date: Mon, 12 Jul 2021 09:50:56 +0000 Subject: [PATCH 1/2] Added show, enable, disable support. --- xdaemon/cli/cron.py | 28 +++++++++++++ xdaemon/cli/lookup.py | 30 ++++++++++++++ xdaemon/cli/main.py | 53 ++++++++++++++++++++++-- xdaemon/cli/showbiz.py | 91 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 xdaemon/cli/showbiz.py diff --git a/xdaemon/cli/cron.py b/xdaemon/cli/cron.py index 87da5a4..fcb19e1 100644 --- a/xdaemon/cli/cron.py +++ b/xdaemon/cli/cron.py @@ -33,6 +33,34 @@ def setup(cls, job_id, schedule, user=None): logger.info("Setup Successfully") + @classmethod + def enable(cls, job_id, user=None): + + user = user or cls.get_current_user() + + logger.info("Enabling the Job.") + logger.debug(f'job-details: {job_id}, {user}') + + with CronTab(user) as tab: + for job in tab.find_command(cls.get_command(job_id)): + job.enable() + + logger.info("Enabled successfully") + + @classmethod + def disable(cls, job_id, user=None): + + user = user or cls.get_current_user() + + logger.info("Disabling the Job.") + logger.debug(f'job-details: {job_id}, {user}') + + with CronTab(user) as tab: + for job in tab.find_command(cls.get_command(job_id)): + job.enable(False) + + logger.info("Disabled successfully") + @classmethod def remove(cls, job_id, user=None): diff --git a/xdaemon/cli/lookup.py b/xdaemon/cli/lookup.py index 5e5107c..a53f2d2 100644 --- a/xdaemon/cli/lookup.py +++ b/xdaemon/cli/lookup.py @@ -131,6 +131,36 @@ def search_job_by_name(cls, job_name): else: return None + @classmethod + def enable_job_by_id(cls, job_id): + + job_id = int(job_id) + + logger.info("Setting the job enabled..") + logger.debug(f"id: {job_id}") + + job_data = cls.read_job_data() + + if job_id not in job_data[Keys.enabled]: + job_data[Keys.enabled].append(job_id) + + cls.write_job_data(job_data) + + @classmethod + def disable_job_by_id(cls, job_id): + + job_id = int(job_id) + + logger.info("Setting the job disabled..") + logger.debug(f"id: {job_id}") + + job_data = cls.read_job_data() + + if job_id in job_data[Keys.enabled]: + job_data[Keys.enabled].remove(job_id) + + cls.write_job_data(job_data) + @classmethod def remove_job_by_id(cls, job_id): diff --git a/xdaemon/cli/main.py b/xdaemon/cli/main.py index 23f999a..d64ca99 100644 --- a/xdaemon/cli/main.py +++ b/xdaemon/cli/main.py @@ -9,6 +9,7 @@ from .lookup import JSONDataStore as DataStore from .local_logging import setup_logging from .utils import prettify +from .showbiz import ShowBiz, Keys as ShowBizKeys logger = logging.getLogger(__name__) @@ -84,12 +85,14 @@ class Command: Used in conjunction with logging enabled. Commands: - show Show the jobs. + show [--active | --inactive] Show the jobs. setup [-f ] Setup a Job. [Default: ./job.yaml] test [-f ] Test a Job File by executing it. [Default: ./job.yaml] remove (--name | --id ) Remove a job by name or id. + enable (--name | --id ) Enable a job by name or id. + disable (--name | --id ) Disable a job by name or id. execute (--name | --id ) Execute a job by name or id. """ # NOQA: E501 @@ -99,10 +102,18 @@ def show(opts): Show already setup jobs. Usage: - show + show [--active | --inactive] + + Options: + --active Show only active jobs. + --inactive Show only inactive jobs. """ - DataStore.show_jobs() + jobs_type = opts["--active"] and ShowBizKeys.active + jobs_type = jobs_type or (opts["--inactive"] and ShowBizKeys.inactive) + jobs_type = jobs_type or ShowBizKeys.all + + ShowBiz.show_jobs(jobs_type=jobs_type) @staticmethod def setup(opts): @@ -139,6 +150,42 @@ def test(opts): executor = JobExecutor(job) executor.execute() + @staticmethod + def enable(opts): + """ + Enable a disabled job. + + Usage: + enable (--name | --id ) + + Options: + --name Name of the Job. + --id ID of the Job. + """ + + job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name']) + + DataStore.enable_job_by_id(job_id) + Cron.enable(job_id) + + @staticmethod + def disable(opts): + """ + Disable an enabled job. + + Usage: + disable (--name | --id ) + + Options: + --name Name of the Job. + --id ID of the Job. + """ + + job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name']) + + DataStore.disable_job_by_id(job_id) + Cron.disable(job_id) + @staticmethod def remove(opts): """ diff --git a/xdaemon/cli/showbiz.py b/xdaemon/cli/showbiz.py new file mode 100644 index 0000000..d63d1df --- /dev/null +++ b/xdaemon/cli/showbiz.py @@ -0,0 +1,91 @@ +from enum import Enum +from tabulate import tabulate + +from .lookup import JSONDataStore, Keys as DataStoreKeys + + +class Keys(str, Enum): + all = "all" + active = "active" + inactive = "inactive" + + +class ShowBiz: + + jobs_headers = [ + "ID", + "FILE", + "NAME", + "USER", + "STATUS" + ] + + jobs_headers_filtered = [ + "ID", + "FILE", + "NAME", + "USER" + ] + + @staticmethod + def _get_structured_jobs(jobs_type=Keys.all): + jobs_data = JSONDataStore.read_job_data() + + jobs = [] + enabled_jobs = jobs_data[DataStoreKeys.enabled] + + if jobs_type == Keys.all: + for job_id, job in jobs_data[DataStoreKeys.jobs].items(): + jobs.append([ + job_id, + job[DataStoreKeys.file], + job[DataStoreKeys.name], + job[DataStoreKeys.user], + "ENABLED" if int(job_id) in enabled_jobs else "DISABLED" + ]) + else: + + jobs_to_show = [] + + all_jobs = map( + lambda x: int(x), + jobs_data[DataStoreKeys.jobs].keys() + ) + + if jobs_type == Keys.active: + jobs_to_show = enabled_jobs + else: + jobs_to_show = set(all_jobs).difference(set(enabled_jobs)) + + for job_id in jobs_to_show: + job = jobs_data[DataStoreKeys.jobs][str(job_id)] + jobs.append([ + job_id, + job[DataStoreKeys.file], + job[DataStoreKeys.name], + job[DataStoreKeys.user], + ]) + + return jobs + + @classmethod + def show_jobs(cls, jobs_type=Keys.all): + + jobs = cls._get_structured_jobs(jobs_type=jobs_type) + + if jobs_type == Keys.all: + print( + tabulate( + jobs, + headers=cls.jobs_headers, + tablefmt="fancy_grid" + ) + ) + else: + print( + tabulate( + jobs, + headers=cls.jobs_headers_filtered, + tablefmt="fancy_grid" + ) + ) From 5e59b019e1513b0cc2beeedb7c4a4b78779e85b1 Mon Sep 17 00:00:00 2001 From: Mitul Date: Mon, 19 Jul 2021 15:47:31 +0530 Subject: [PATCH 2/2] Added notion page link. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index acce63e..fbffa3b 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,5 @@ XDaemon is an automation tool. ## README -Coming Soon.. +- Coming Soon.. +- [Project Notion Page](https://cooperative-string-818.notion.site/XDaemon-532da9f667294484810bbc55667dc0a4)