fix: add support for starting and stopping all Docker services
Some checks failed
Ansible Lint Check / check-ansible (push) Failing after 16s
Nix Format Check / check-format (push) Successful in 53s
Python Lint Check / check-python (push) Failing after 11s

This commit is contained in:
Menno van Leeuwen 2025-03-12 21:19:23 +01:00
parent 0ef4c4a779
commit 07dec180c7
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -44,14 +44,79 @@ def run_docker_compose(args, service_name=None, compose_file=None):
return result.returncode
def get_all_services():
"""Return a list of all available services"""
if not os.path.exists(SERVICES_DIR):
return []
services = [
d
for d in os.listdir(SERVICES_DIR)
if os.path.isdir(os.path.join(SERVICES_DIR, d))
and os.path.exists(os.path.join(SERVICES_DIR, d, "docker-compose.yml"))
]
return sorted(services)
def cmd_start(args):
"""Start a Docker service"""
return run_docker_compose(["up", "-d"], service_name=args.service)
if args.all:
services = get_all_services()
if not services:
printfe("yellow", "No services found to start")
return 0
printfe("blue", f"Starting all services: {', '.join(services)}")
failed_services = []
for service in services:
printfe("blue", f"\n=== Starting {service} ===")
result = run_docker_compose(["up", "-d"], service_name=service)
if result != 0:
failed_services.append(service)
if failed_services:
printfe(
"red",
f"\nFailed to start the following services: {', '.join(failed_services)}",
)
return 1
else:
printfe("green", "\nAll services started successfully")
return 0
else:
return run_docker_compose(["up", "-d"], service_name=args.service)
def cmd_stop(args):
"""Stop a Docker service"""
return run_docker_compose(["down"], service_name=args.service)
if args.all:
running_services = get_all_running_services()
if not running_services:
printfe("yellow", "No running services found to stop")
return 0
printfe("blue", f"Stopping all running services: {', '.join(running_services)}")
failed_services = []
for service in running_services:
printfe("blue", f"\n=== Stopping {service} ===")
result = run_docker_compose(["down"], service_name=service)
if result != 0:
failed_services.append(service)
if failed_services:
printfe(
"red",
f"\nFailed to stop the following services: {', '.join(failed_services)}",
)
return 1
else:
printfe("green", "\nAll running services stopped successfully")
return 0
else:
return run_docker_compose(["down"], service_name=args.service)
def cmd_restart(args):
@ -200,11 +265,19 @@ def main():
# Start command
start_parser = subparsers.add_parser("start", help="Start a Docker service")
start_parser.add_argument("service", help="Service to start")
start_parser_group = start_parser.add_mutually_exclusive_group(required=True)
start_parser_group.add_argument(
"--all", action="store_true", help="Start all services"
)
start_parser_group.add_argument("service", nargs="?", help="Service to start")
# Stop command
stop_parser = subparsers.add_parser("stop", help="Stop a Docker service")
stop_parser.add_argument("service", help="Service to stop")
stop_parser_group = stop_parser.add_mutually_exclusive_group(required=True)
stop_parser_group.add_argument(
"--all", action="store_true", help="Stop all running services"
)
stop_parser_group.add_argument("service", nargs="?", help="Service to stop")
# Restart command
restart_parser = subparsers.add_parser("restart", help="Restart a Docker service")