diff --git a/cron.sh b/cron.sh index 42b01ec7..4606670e 100755 --- a/cron.sh +++ b/cron.sh @@ -44,7 +44,19 @@ git pull || { echo "Failed to pull changes from Git."; send_healthcheck_failure; # 4. Run script inside docker container inv up --build || { echo "Failed to build Docker container."; send_healthcheck_failure; exit 1; } -docker-compose run --rm app invoke toolchain || { echo "Failed to run script inside Docker container."; send_healthcheck_failure; exit 1; } +if command -v docker-compose &>/dev/null; then + docker-compose run --rm app invoke toolchain || { + echo "Failed to run script inside Docker container." + send_healthcheck_failure + exit 1 + } +elif command -v docker &>/dev/null; then + docker compose run --rm app invoke toolchain || { + echo "Failed to run script inside Docker container." + send_healthcheck_failure + exit 1 + } +fi inv down || { echo "Failed to stop Docker container."; send_healthcheck_failure; exit 1; } # 5. commit changes diff --git a/tasks.py b/tasks.py index 4df90e2f..f0623ea6 100644 --- a/tasks.py +++ b/tasks.py @@ -1,5 +1,6 @@ import datetime import os +import shutil import subprocess import tomli @@ -7,6 +8,24 @@ from invoke import task +def get_docker_compose_command(): + """ + `docker-compose` is the preferred command, + but if it's not available, we fall back to `docker compose` + """ + if shutil.which("docker-compose"): + return "docker-compose" + elif shutil.which("docker"): + return "docker compose" + else: + raise subprocess.CalledProcessError( + returncode=1, + cmd=None, + output=b"", + stderr=b"Neither 'docker-compose' nor 'docker' executable found in the system path", + ) + + @task def db_snapshot(c, filename_prefix): """Create a Database snapshot using DSLR""" @@ -23,34 +42,38 @@ def db_snapshot(c, filename_prefix): @task(help={"build": "Build images before starting containers."}) def up(c, build=False): """docker-compose up -d""" + docker_compose = get_docker_compose_command() if build: c.run( - "docker-compose -f docker-compose.yml up -d --build 2>&1 | tee build.log", + f"{docker_compose} -f docker-compose.yml up -d --build 2>&1 | tee build.log", pty=True, ) else: - c.run("docker-compose -f docker-compose.yml up -d", pty=True) + c.run(f"{docker_compose} -f docker-compose.yml up -d", pty=True) @task def exec(c, container, command): """docker-compose exec [container] [command(s)]""" - c.run(f"docker-compose exec {container} {command}", pty=True) + docker_compose = get_docker_compose_command() + c.run(f"{docker_compose} exec {container} {command}", pty=True) @task(help={"follow": "Follow log output"}) def logs(c, container, follow=False): """docker-compose logs [container] [-f]""" + docker_compose = get_docker_compose_command() if follow: - c.run(f"docker-compose logs {container} -f", pty=True) + c.run(f"{docker_compose} logs {container} -f", pty=True) else: - c.run(f"docker-compose logs {container}", pty=True) + c.run(f"{docker_compose} logs {container}", pty=True) @task def stop(c): """docker-compose stop""" - c.run("docker-compose stop", pty=True) + docker_compose = get_docker_compose_command() + c.run(f"{docker_compose} stop", pty=True) @task( @@ -60,10 +83,11 @@ def stop(c): ) def down(c, volumes=False): """docker-compose down""" + docker_compose = get_docker_compose_command() if volumes: - c.run("docker-compose down -v", pty=True) + c.run(f"{docker_compose} down -v", pty=True) else: - c.run("docker-compose down", pty=True) + c.run(f"{docker_compose} down", pty=True) @task(help={"dump_file": "The name of the dump file to import"})