feat: add update command to manage Docker services and support bulk updates
Some checks failed
Nix Format Check / check-format (push) Failing after 37s

This commit is contained in:
Menno van Leeuwen 2025-03-12 12:39:15 +01:00
parent 69126bc510
commit 7e4bc76015
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -53,6 +53,61 @@ def cmd_restart(args):
"""Restart a Docker service"""
return run_docker_compose(["restart"], service_name=args.service)
def get_all_running_services():
"""Return a list of all running services"""
if not os.path.exists(SERVICES_DIR):
return []
running_services = []
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"))]
for service in services:
if check_service_running(service) > 0:
running_services.append(service)
return running_services
def cmd_update(args):
"""Update a Docker service by pulling new images and recreating containers if needed"""
if args.all:
running_services = get_all_running_services()
if not running_services:
printfe("yellow", "No running services found to update")
return 0
printfe("blue", f"Updating all running services: {', '.join(running_services)}")
failed_services = []
for service in running_services:
printfe("blue", f"\n=== Updating {service} ===")
# Pull the latest images
pull_result = run_docker_compose(["pull"], service_name=service)
# Bring the service up with the latest images
up_result = run_docker_compose(["up", "-d"], service_name=service)
if pull_result != 0 or up_result != 0:
failed_services.append(service)
if failed_services:
printfe("red", f"\nFailed to update the following services: {', '.join(failed_services)}")
return 1
else:
printfe("green", "\nAll running services updated successfully")
return 0
else:
# The original single-service update logic
# First pull the latest images
pull_result = run_docker_compose(["pull"], service_name=args.service)
if pull_result != 0:
return pull_result
# Then bring the service up with the latest images
return run_docker_compose(["up", "-d"], service_name=args.service)
def cmd_ps(args):
"""Show Docker service status"""
if args.service:
@ -134,6 +189,12 @@ def main():
restart_parser = subparsers.add_parser("restart", help="Restart a Docker service")
restart_parser.add_argument("service", help="Service to restart")
# Update command
update_parser = subparsers.add_parser("update", help="Update a Docker service (pull new images and recreate if needed)")
update_parser_group = update_parser.add_mutually_exclusive_group(required=True)
update_parser_group.add_argument("--all", action="store_true", help="Update all running services")
update_parser_group.add_argument("service", nargs="?", help="Service to update")
# PS command
ps_parser = subparsers.add_parser("ps", help="Show Docker service status")
ps_parser.add_argument("service", nargs="?", help="Service to check")
@ -144,8 +205,9 @@ def main():
logs_parser.add_argument("-f", "--follow", action="store_true", help="Follow log output")
logs_parser.add_argument("--tail", help="Number of lines to show from the end of logs")
# List command
# List command and its alias
subparsers.add_parser("list", help="List available Docker services")
subparsers.add_parser("ls", help="List available Docker services (alias for list)")
# Parse arguments
args = parser.parse_args()
@ -159,9 +221,11 @@ def main():
"start": cmd_start,
"stop": cmd_stop,
"restart": cmd_restart,
"update": cmd_update,
"ps": cmd_ps,
"logs": cmd_logs,
"list": cmd_list
"list": cmd_list,
"ls": cmd_list # Alias 'ls' to the same function as 'list'
}
return commands[args.command](args)