Disable/Enable services
This commit is contained in:
parent
b3c4c2f63d
commit
128f9eb138
48
doxy/cli.py
48
doxy/cli.py
|
@ -6,8 +6,10 @@ from click_aliases import ClickAliasedGroup
|
||||||
|
|
||||||
from doxy import output
|
from doxy import output
|
||||||
from doxy.config import Config
|
from doxy.config import Config
|
||||||
from doxy.services import (docker_compose_command, find_services,
|
from doxy.services import (disable_compose_file, docker_compose_command,
|
||||||
get_compose_file, only_if_service_exists)
|
enable_compose_file, find_disabled_services,
|
||||||
|
find_services, get_compose_file,
|
||||||
|
only_if_service_exists)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
CONFIG = Config()
|
CONFIG = Config()
|
||||||
|
@ -21,6 +23,7 @@ def complete_service_name(ctx, param, incomplete):
|
||||||
return [
|
return [
|
||||||
k
|
k
|
||||||
for k in find_services(Path(CONFIG.root_directory), False)
|
for k in find_services(Path(CONFIG.root_directory), False)
|
||||||
|
+ find_disabled_services(Path(CONFIG.root_directory), False)
|
||||||
if k.startswith(incomplete)
|
if k.startswith(incomplete)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -69,6 +72,9 @@ def change_dir(ctx):
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def list(ctx, sub_services):
|
def list(ctx, sub_services):
|
||||||
output.print_services(ctx, find_services(Path(CONFIG.root_directory), 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")
|
@main.command(help="edit the compose file")
|
||||||
|
@ -81,6 +87,44 @@ def edit(ctx, service):
|
||||||
click.edit(filename=str(compose_file))
|
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(
|
@main.command(
|
||||||
context_settings=dict(
|
context_settings=dict(
|
||||||
ignore_unknown_options=True,
|
ignore_unknown_options=True,
|
||||||
|
|
|
@ -6,9 +6,9 @@ from rich.rule import Rule
|
||||||
from rich.tree import Tree
|
from rich.tree import Tree
|
||||||
|
|
||||||
|
|
||||||
def _print_services_fancy(services: List):
|
def _print_services_fancy(services: List, status: str):
|
||||||
print(Rule("Listing services"))
|
print(Rule("Listing services"))
|
||||||
tree = Tree("[bold]Available Services")
|
tree = Tree(f"[bold]{status.capitalize()} Services")
|
||||||
for service in services:
|
for service in services:
|
||||||
if isinstance(service, tuple):
|
if isinstance(service, tuple):
|
||||||
compose, subservices = service
|
compose, subservices = service
|
||||||
|
@ -20,21 +20,25 @@ def _print_services_fancy(services: List):
|
||||||
print(tree)
|
print(tree)
|
||||||
|
|
||||||
|
|
||||||
def _print_services_simple(services: List[str]):
|
def _print_services_simple(services: List[str], status: str):
|
||||||
for service in services:
|
for service in services:
|
||||||
if isinstance(service, tuple):
|
if isinstance(service, tuple):
|
||||||
compose, subservices = service
|
compose, subservices = service
|
||||||
echo(f"{compose}\t{','.join(subservices)}")
|
echo(f"{compose}\t{','.join(subservices)} ({status})")
|
||||||
else:
|
else:
|
||||||
echo(service)
|
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"]:
|
match ctx.obj["FORMAT"]:
|
||||||
case "fancy":
|
case "fancy":
|
||||||
_print_services_fancy(services)
|
_print_services_fancy(services, status)
|
||||||
case "simple":
|
case "simple":
|
||||||
_print_services_simple(services)
|
_print_services_simple(services, status)
|
||||||
case _:
|
case _:
|
||||||
echo("Unknown format choice")
|
echo("Unknown format choice")
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,37 @@ def find_services(root: Path, sub_services: bool) -> List:
|
||||||
return services
|
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:
|
def get_compose_file(service_path: Path) -> Path:
|
||||||
compose_files = glob.glob("docker-compose.y*ml", root_dir=service_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:
|
try:
|
||||||
return service_path / compose_files[0]
|
return service_path / compose_files[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
|
Loading…
Reference in New Issue