Disable/Enable services

This commit is contained in:
jpk 2023-12-03 16:43:30 +01:00
parent b3c4c2f63d
commit 128f9eb138
3 changed files with 86 additions and 9 deletions

View File

@ -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,

View File

@ -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")

View File

@ -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: