doxy/doxy/cli.py

205 lines
5.9 KiB
Python
Raw Normal View History

import sys
2023-01-10 08:57:57 +01:00
from pathlib import Path
2023-01-10 15:45:35 +01:00
import click
2023-01-18 11:30:35 +01:00
from click_aliases import ClickAliasedGroup
2023-01-10 10:14:38 +01:00
2023-01-18 11:14:29 +01:00
from doxy import output
2023-01-10 15:45:35 +01:00
from doxy.config import Config
2023-12-03 17:04:28 +01:00
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,
)
2023-01-10 08:57:57 +01:00
try:
CONFIG = Config()
CONFIG.load()
except FileNotFoundError as exc:
click.echo(exc, sys.stderr)
sys.exit(1)
2023-01-10 08:57:57 +01:00
2023-01-16 14:42:00 +01:00
def complete_service_name(ctx, param, incomplete):
return [
k
for k in find_services(Path(CONFIG.root_directory), False)
2023-12-03 16:43:30 +01:00
+ find_disabled_services(Path(CONFIG.root_directory), False)
2023-01-16 14:42:00 +01:00
if k.startswith(incomplete)
]
2023-01-18 11:30:35 +01:00
@click.group(cls=ClickAliasedGroup)
2023-01-16 14:42:00 +01:00
@click.option(
"--format",
"-f",
type=click.Choice(["fancy", "simple"], case_sensitive=False),
default="fancy",
show_default=True,
help="output formatting",
)
@click.option(
"--service-root",
"-r",
type=click.Path(exists=True, file_okay=False, dir_okay=True),
default=Path(CONFIG.root_directory),
help="Service root directory",
show_default=True,
)
2023-01-10 08:57:57 +01:00
@click.pass_context
def main(ctx, format, service_root):
2023-01-10 08:57:57 +01:00
ctx.ensure_object(dict)
CONFIG.root_directory = service_root
ctx.obj["CONFIG"] = CONFIG
2023-01-16 14:42:00 +01:00
ctx.obj["FORMAT"] = format.lower()
2023-01-10 08:57:57 +01:00
2023-06-23 08:25:38 +02:00
@main.command(
name="get-service-directory", help="get the service root directory", aliases=["dir"]
)
@click.pass_context
def change_dir(ctx):
click.echo(ctx.obj["CONFIG"].root_directory)
2023-01-18 11:30:35 +01:00
@main.command(help="list available services", aliases=["l", "ls"])
@click.option(
"--sub-services",
"-s",
is_flag=True,
default=False,
help="list sub services from compose file",
)
2023-01-16 14:42:00 +01:00
@click.pass_context
def list(ctx, sub_services):
output.print_services(ctx, find_services(Path(CONFIG.root_directory), sub_services))
2023-12-03 16:43:30 +01:00
output.print_services(
ctx, find_disabled_services(Path(CONFIG.root_directory), sub_services), True
)
2023-01-10 10:24:38 +01:00
2023-01-18 11:30:35 +01:00
@main.command(help="edit the compose file")
2023-01-10 15:19:16 +01:00
@click.argument("service", nargs=1, shell_complete=complete_service_name)
@click.pass_context
2023-01-18 11:14:29 +01:00
@only_if_service_exists
2023-01-10 15:19:16 +01:00
def edit(ctx, service):
2023-01-16 16:33:11 +01:00
output.print_header(ctx, f"Editing {service}")
2023-01-18 11:14:29 +01:00
compose_file = get_compose_file(Path(CONFIG.root_directory) / service)
2023-12-03 16:08:34 +01:00
click.edit(filename=str(compose_file))
2023-01-10 15:19:16 +01:00
2023-12-03 16:43:30 +01:00
@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)
2023-01-18 11:30:35 +01:00
@main.command(
context_settings=dict(
ignore_unknown_options=True,
2023-01-13 15:03:30 +01:00
),
help="run docker-compose commands",
2023-01-18 11:30:35 +01:00
aliases=["c", "ctrl"],
)
2023-01-10 10:24:38 +01:00
@click.argument("service", nargs=1, shell_complete=complete_service_name)
2023-01-10 08:57:57 +01:00
@click.argument("command", nargs=-1)
@click.pass_context
2023-01-18 11:14:29 +01:00
@only_if_service_exists
2023-01-10 08:57:57 +01:00
def control(ctx, service, command):
2023-01-16 16:33:11 +01:00
output.print_header(ctx, f"Controlling {service}")
2023-01-18 11:14:29 +01:00
compose_file = get_compose_file(Path(CONFIG.root_directory) / service)
docker_compose_command(command, compose_file)
2023-06-23 08:25:38 +02:00
@main.command("start-all", help="start all services")
@click.pass_context
def start_all(ctx):
for service in find_services(Path(CONFIG.root_directory), False):
output.print_header(ctx, f"Starting {service}")
compose_file = get_compose_file(Path(CONFIG.root_directory) / service)
docker_compose_command(["up", "-d"], compose_file)
2023-01-18 11:30:35 +01:00
@main.command(
help="pull the latest service images and restart", aliases=["upd", "sync"]
)
@click.argument("service", nargs=1, shell_complete=complete_service_name)
@click.option(
"--remove", "-r", is_flag=True, default=False, help="remove unused volumes"
)
@click.pass_context
2023-01-18 11:14:29 +01:00
@only_if_service_exists
def update(ctx, service, remove):
2023-01-18 11:14:29 +01:00
compose_file = get_compose_file(Path(CONFIG.root_directory) / service)
2023-02-15 16:30:55 +01:00
command_chain = [
(f"Pulling {service} images", ["pull"]),
(f"Starting {service}", ["up", "-d"]),
]
if remove:
2023-02-15 16:32:40 +01:00
command_chain.insert(0, (f"Stopping {service}", ["down", "--remove-orphans"]))
2023-02-15 16:30:55 +01:00
for title, command in command_chain:
2023-01-16 16:30:17 +01:00
output.print_header(ctx, title)
2023-01-18 11:14:29 +01:00
docker_compose_command(command, compose_file)
2023-01-10 08:57:57 +01:00
@main.command(
help="show service status (images, ps, top, logs)", aliases=["stat", "info"]
)
2023-01-16 14:45:59 +01:00
@click.argument("service", nargs=1, shell_complete=complete_service_name)
@click.option(
"--tail",
type=str,
default=10,
show_default=True,
help="Number of lines to show from the end of the logs",
)
2023-01-16 14:45:59 +01:00
@click.pass_context
2023-01-18 11:14:29 +01:00
@only_if_service_exists
def status(ctx, service, tail):
2023-01-18 11:14:29 +01:00
compose_file = get_compose_file(Path(CONFIG.root_directory) / service)
command_chain = [
("Images", ["images"]),
("Containers", ["ps"]),
("Running processes", ["top"]),
("Log Messages", ["logs", "--tail", tail]),
]
for title, command in command_chain:
2023-01-16 16:30:17 +01:00
output.print_header(ctx, title)
2023-01-18 11:14:29 +01:00
docker_compose_command(command, compose_file)