fix: add support for starting and stopping all Docker services
This commit is contained in:
parent
0ef4c4a779
commit
07dec180c7
@ -44,13 +44,78 @@ def run_docker_compose(args, service_name=None, compose_file=None):
|
|||||||
return result.returncode
|
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):
|
def cmd_start(args):
|
||||||
"""Start a Docker service"""
|
"""Start a Docker 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)
|
return run_docker_compose(["up", "-d"], service_name=args.service)
|
||||||
|
|
||||||
|
|
||||||
def cmd_stop(args):
|
def cmd_stop(args):
|
||||||
"""Stop a Docker service"""
|
"""Stop a Docker 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)
|
return run_docker_compose(["down"], service_name=args.service)
|
||||||
|
|
||||||
|
|
||||||
@ -200,11 +265,19 @@ def main():
|
|||||||
|
|
||||||
# Start command
|
# Start command
|
||||||
start_parser = subparsers.add_parser("start", help="Start a Docker service")
|
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 command
|
||||||
stop_parser = subparsers.add_parser("stop", help="Stop a Docker service")
|
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 command
|
||||||
restart_parser = subparsers.add_parser("restart", help="Restart a Docker service")
|
restart_parser = subparsers.add_parser("restart", help="Restart a Docker service")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user