From 128f9eb138600061d6206615f6547fce2503a08f Mon Sep 17 00:00:00 2001 From: jpk Date: Sun, 3 Dec 2023 16:43:30 +0100 Subject: [PATCH] Disable/Enable services --- doxy/cli.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- doxy/output.py | 18 +++++++++++------- doxy/services.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/doxy/cli.py b/doxy/cli.py index 8c8ce4f..be4483c 100644 --- a/doxy/cli.py +++ b/doxy/cli.py @@ -6,8 +6,10 @@ from click_aliases import ClickAliasedGroup from doxy import output from doxy.config import Config -from doxy.services import (docker_compose_command, find_services, - get_compose_file, only_if_service_exists) +from doxy.services import (disable_compose_file, docker_compose_command, + enable_compose_file, find_disabled_services, + find_services, get_compose_file, + only_if_service_exists) try: CONFIG = Config() @@ -21,6 +23,7 @@ def complete_service_name(ctx, param, incomplete): return [ k for k in find_services(Path(CONFIG.root_directory), False) + + find_disabled_services(Path(CONFIG.root_directory), False) if k.startswith(incomplete) ] @@ -69,6 +72,9 @@ def change_dir(ctx): @click.pass_context def list(ctx, sub_services): output.print_services(ctx, find_services(Path(CONFIG.root_directory), sub_services)) + output.print_services( + ctx, find_disabled_services(Path(CONFIG.root_directory), sub_services), True + ) @main.command(help="edit the compose file") @@ -81,6 +87,44 @@ def edit(ctx, service): click.edit(filename=str(compose_file)) +@main.command(help="disable service") +@click.argument("service", nargs=1, shell_complete=complete_service_name) +@click.option( + "--now", + "-n", + is_flag=True, + default=False, + help="shutdown service prior being disabled", +) +@click.pass_context +@only_if_service_exists +def disable(ctx, service, now): + output.print_header(ctx, f"Disable {service}") + compose_file = get_compose_file(Path(CONFIG.root_directory) / service) + if now: + docker_compose_command(["down", "--remove-orphans"], compose_file) + disable_compose_file(Path(CONFIG.root_directory) / service) + + +@main.command(help="enable service") +@click.argument("service", nargs=1, shell_complete=complete_service_name) +@click.option( + "--now", + "-n", + is_flag=True, + default=False, + help="shutdown service prior being disabled", +) +@click.pass_context +@only_if_service_exists +def enable(ctx, service, now): + output.print_header(ctx, f"Disable {service}") + compose_file = get_compose_file(Path(CONFIG.root_directory) / service) + enable_compose_file(Path(CONFIG.root_directory) / service) + if now: + docker_compose_command(["up", "-d"], compose_file) + + @main.command( context_settings=dict( ignore_unknown_options=True, diff --git a/doxy/output.py b/doxy/output.py index 23d8b84..1b74117 100644 --- a/doxy/output.py +++ b/doxy/output.py @@ -6,9 +6,9 @@ from rich.rule import Rule from rich.tree import Tree -def _print_services_fancy(services: List): +def _print_services_fancy(services: List, status: str): print(Rule("Listing services")) - tree = Tree("[bold]Available Services") + tree = Tree(f"[bold]{status.capitalize()} Services") for service in services: if isinstance(service, tuple): compose, subservices = service @@ -20,21 +20,25 @@ def _print_services_fancy(services: List): print(tree) -def _print_services_simple(services: List[str]): +def _print_services_simple(services: List[str], status: str): for service in services: if isinstance(service, tuple): compose, subservices = service - echo(f"{compose}\t{','.join(subservices)}") + echo(f"{compose}\t{','.join(subservices)} ({status})") else: echo(service) -def print_services(ctx: Context, services: List[str]): +def print_services(ctx: Context, services: List[str], disabled_services=False): + if not disabled_services: + status = "enabled" + else: + status = "disabled" match ctx.obj["FORMAT"]: case "fancy": - _print_services_fancy(services) + _print_services_fancy(services, status) case "simple": - _print_services_simple(services) + _print_services_simple(services, status) case _: echo("Unknown format choice") diff --git a/doxy/services.py b/doxy/services.py index 5d045a8..093df02 100644 --- a/doxy/services.py +++ b/doxy/services.py @@ -47,8 +47,37 @@ def find_services(root: Path, sub_services: bool) -> List: return services +def find_disabled_services(root: Path, sub_services: bool) -> List: + if not sub_services: + services = [ + _.split("/")[0] + for _ in glob.glob("*/docker-compose.y*ml.disabled", root_dir=root) + ] + else: + services = [] + for compose_file in glob.glob("*/docker-compose.y*ml.disabled", root_dir=root): + services.append( + (compose_file.split("/")[0], get_subservices(root / compose_file)) + ) + return services + + +def disable_compose_file(service_path: Path): + target_path = Path(service_path / "docker-compose.yml.disabled") + compose_file = get_compose_file(service_path) + compose_file.rename(target_path) + + +def enable_compose_file(service_path: Path): + target_path = Path(service_path / "docker-compose.yml") + compose_file = get_compose_file(service_path) + compose_file.rename(target_path) + + def get_compose_file(service_path: Path) -> Path: compose_files = glob.glob("docker-compose.y*ml", root_dir=service_path) + if len(compose_files) == 0: + compose_files = glob.glob("docker-compose.y*ml.disabled", root_dir=service_path) try: return service_path / compose_files[0] except IndexError: