Skip to content

Commit

Permalink
refactor: account for the situation where docker-compose doesn't exist
Browse files Browse the repository at this point in the history
For a long time, I've been using docker-compose v1.29 on x86_64 VPSes.

However, after setting up an arm VPS, I had to use the v2 as prior
versions were not supported on arm.

As a result, the script failed because `docker-compose` wasn't found.

This commit addresses this problem.
  • Loading branch information
engineervix committed Oct 31, 2023
1 parent b08c525 commit 0712568
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
14 changes: 13 additions & 1 deletion cron.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 32 additions & 8 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import datetime
import os
import shutil
import subprocess

import tomli
from colorama import Fore, init
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"""
Expand All @@ -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(
Expand All @@ -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"})
Expand Down

0 comments on commit 0712568

Please sign in to comment.