From 906ad5a09936bd36af867cdf52b640f39344470f Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Wed, 20 Oct 2021 00:43:46 +0530 Subject: [PATCH] feat: bench drop {bench_path} * Drops bench if no sites' folders found * Tries to drop crontab entry made for backups --- bench/app.py | 20 ++++++++++++++++++++ bench/cli.py | 5 ++++- bench/commands/__init__.py | 3 ++- bench/commands/make.py | 7 +++++++ bench/utils.py | 15 +++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/bench/app.py b/bench/app.py index 24c8c81e..c2a13514 100755 --- a/bench/app.py +++ b/bench/app.py @@ -188,6 +188,26 @@ def remove_from_excluded_apps_txt(app, bench_path='.'): apps.remove(app) return write_excluded_apps_txt(apps, bench_path=bench_path) +def drop_bench(bench_path): + if not os.path.exists(bench_path): + print(f"Bench {bench_path} does not exist") + return + + import shutil + from bench.utils import remove_backups_crontab + + sites_exist = [ + x for x in os.listdir(os.path.join(bench_path, 'sites')) if x not in ('assets', 'apps.txt', 'common_site_config.json') + ] + if sites_exist: + raise Exception("Cannot remove non-empty bench directory") + remove_backups_crontab(bench_path) + shutil.rmtree(bench_path) + print('Bench dropped') + +def get_bench_name(git_url, bench_path): + return "temp-bench" + def get_app(git_url, branch=None, bench_path='.', skip_assets=False, verbose=False, restart_bench=True, overwrite=False): import requests import shutil diff --git a/bench/cli.py b/bench/cli.py index f194349c..01fe5eb7 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -65,7 +65,7 @@ def cli(): not is_bench_directory() and not cmd_requires_root() and len(sys.argv) > 1 - and sys.argv[1] not in ("init", "find", "src") + and sys.argv[1] not in ("init", "find", "src", "drop", "get", "get-app") ): log("Command not being executed in bench directory", level=3) @@ -100,6 +100,7 @@ def cli(): if return_code: logger.warning(f"{command} executed with exit code {return_code}") if isinstance(e, Exception): + click.secho(f"ERROR: {e}", fg="red") raise e finally: try: @@ -209,6 +210,8 @@ def change_working_directory(): """Allows bench commands to be run from anywhere inside a bench directory""" cur_dir = os.path.abspath(".") bench_path = find_parent_bench(cur_dir) + bench.current_path = os.getcwd() + bench.updated_path = bench_path if bench_path: os.chdir(bench_path) diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index b5ba5a4f..51ec037e 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -17,8 +17,9 @@ def bench_command(bench_path='.'): bench.set_frappe_version(bench_path=bench_path) -from bench.commands.make import init, get_app, new_app, remove_app, exclude_app_for_update, include_app_for_update, pip +from bench.commands.make import init, drop, get_app, new_app, remove_app, exclude_app_for_update, include_app_for_update, pip bench_command.add_command(init) +bench_command.add_command(drop) bench_command.add_command(get_app) bench_command.add_command(new_app) bench_command.add_command(remove_app) diff --git a/bench/commands/make.py b/bench/commands/make.py index 20c93659..a87ee48a 100755 --- a/bench/commands/make.py +++ b/bench/commands/make.py @@ -50,6 +50,13 @@ def init(path, apps_path, frappe_path, frappe_branch, no_procfile, no_backups, c shutil.rmtree(path) +@click.command('drop') +@click.argument('path') +def drop(path): + from bench.app import drop_bench + drop_bench(path) + + @click.command('get-app', help='Clone an app from the internet or filesystem and set it up in your bench') @click.argument('name', nargs=-1) # Dummy argument for backward compatibility @click.argument('git-url') diff --git a/bench/utils.py b/bench/utils.py index 99f7870b..4bdba379 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -415,6 +415,21 @@ def setup_backups(bench_path='.'): system_crontab.write() +def remove_backups_crontab(bench_path='.'): + from crontab import CronTab, CronItem + from bench.config.common_site_config import get_config + logger.log('removing backup cronjob') + + bench_dir = os.path.abspath(bench_path) + user = get_config(bench_path=bench_dir).get('frappe_user') + logfile = os.path.join(bench_dir, 'logs', 'backup.log') + system_crontab = CronTab(user=user) + backup_command = f"cd {bench_dir} && {sys.argv[0]} --verbose --site all backup" + job_command = f"{backup_command} >> {logfile} 2>&1" + + system_crontab.remove_all(command=job_command) + + def setup_sudoers(user): if not os.path.exists('/etc/sudoers.d'): os.makedirs('/etc/sudoers.d')