Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor downloading Docker Compose bundle #31

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
109 changes: 95 additions & 14 deletions development.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,111 @@ COMMAND=${1:-}
DOCKER_COMPOSE_CMD="docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.custom.yml"

instruct_and_exit() {
echo "Usage: ${0} <command>"
echo "Usage: ${0} <command> [<argument>]"
echo ""
echo "Available commands:"
echo "start Start the dependencies and the dockerized application"
echo "start:deps Start the dependencies only"
echo "generate:jooq Start the dependencies and generate jOOQ classes"
echo "build:data-inserter Installs required dependencies and builds the timetables data inserter"
echo "stop Stop the dependencies and the dockerized application"
echo ""
echo "start [<commit_ref>] Start the dependencies and the dockerized application. A commit"
echo " reference can be given as an argument to fetch a specific version of"
echo " Docker Compose bundle. Without argument, the latest commit in the main"
echo " branch of the jore4-docker-compose-bundle repository is used."
echo ""
echo "start:deps [<commit_ref>] Start the dependencies only. A commit reference can be given as an"
echo " argument to fetch a specific version of Docker Compose bundle. Without"
echo " argument, the latest commit in the main branch of the"
echo " jore4-docker-compose-bundle repository is used."
echo ""
echo "generate:jooq Generate jOOQ classes"
echo ""
echo "build:data-inserter Installs required dependencies and builds the timetables data inserter"
echo ""
echo "stop Stop the dependencies and the dockerized application"
echo ""
exit 1
}

download_docker_bundle() {
# based on https://github.com/HSLdevcom/jore4-tools#download-docker-bundlesh
# Download Docker Compose bundle from the "jore4-docker-compose-bundle"
# repository. GitHub CLI is required to be installed.
#
# A commit reference can be given as an argument. It can contain, for example,
# only a substring of an actual SHA digest.
download_docker_compose_bundle() {
local commit_ref="${1:-main}"

local repo_name="jore4-docker-compose-bundle"
local repo_owner="HSLdevcom"
local gh_common_path="/repos/${repo_owner}/${repo_name}"

# Check GitHub CLI availability.
if ! command -v gh &> /dev/null; then
echo "Please install the GitHub CLI (gh) on your machine."
exit 1
fi

# Make sure the user is authenticated to GitHub.
gh auth status || gh auth login

echo "Using the commit reference '${commit_ref}' to fetch a Docker Compose bundle..."

# First, try to find a commit on GitHub that matches the given reference.
local commit_sha=$(
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${gh_common_path}/commits/${commit_ref}" \
--jq '.sha'
)

# Then, check if a match wasn't found, meaning the previous GitHub CLI command
# failed.
if [[ $? -ne 0 ]]; then
echo "Error: Querying GitHub API with the commit ref '${commit_ref}' failed." >&2
exit 1
fi

echo "Commit with the following SHA digest was found: ${commit_sha}"

local zip_file="/tmp/${repo_name}.zip"
local unzip_target_dir_prefix="/tmp/${repo_owner}-${repo_name}"

# Remove old temporary directories if any remain.
rm -fr "$unzip_target_dir_prefix"-*

echo "Downloading latest version of E2E docker-compose package..."
curl https://raw.githubusercontent.com/HSLdevcom/jore4-tools/main/docker/download-docker-bundle.sh | bash
echo "Downloading the JORE4 Docker Compose bundle..."

# Download the latest Docker Compose bundle from the
# jore4-docker-compose-bundle repository as a ZIP file and extract its
# contents to a temporary directory.
gh api "${gh_common_path}/zipball/${commit_sha}" > "$zip_file" \
&& unzip -q "$zip_file" -d /tmp

# Clean untracked files from `docker` directory even if they are git-ignored.
git clean -fx ./docker

# Copy files from the `docker-compose` directory of the ZIP file to your
# local `docker` directory.
mv "$unzip_target_dir_prefix"-*/docker-compose/* ./docker

# Remove the temporary files and directories created above.
rm -fr "$zip_file" "$unzip_target_dir_prefix"-*

echo "Generating a release version file for the downloaded bundle..."

# Create a release version file containing the SHA digest of the referenced
# commit.
echo "$commit_sha" > ./docker/RELEASE_VERSION.txt
}

start_all() {
download_docker_bundle
$DOCKER_COMPOSE_CMD up -d jore4-hasura jore4-testdb
$DOCKER_COMPOSE_CMD up --build -d jore4-timetables-api
prepare_timetables_data_inserter
}

start_deps() {
download_docker_bundle
# Runs the following services:
# jore4-hasura - Hasura. We have to start Hasura because it ensures that db migrations are run to the Jore 4 database.
# jore4-testdb - Jore 4 database. This is the database used by the API.
$DOCKER_COMPOSE_CMD -f ./docker/docker-compose.test.yml up --build -d jore4-hasura jore4-testdb jore4-hasura-test jore4-testdb-test
prepare_timetables_data_inserter
}

generate_jooq() {
Expand Down Expand Up @@ -82,13 +155,21 @@ if [[ -z ${COMMAND} ]]; then
instruct_and_exit
fi

# Shift other arguments after the command so that we can refer to them later
# with "$@".
shift

if [[ ${COMMAND} == "start" ]]; then
download_docker_compose_bundle "$@"
start_all
prepare_timetables_data_inserter
exit 0
fi

if [[ ${COMMAND} == "start:deps" ]]; then
download_docker_compose_bundle "$@"
start_deps
prepare_timetables_data_inserter
exit 0
fi

Expand Down
1 change: 0 additions & 1 deletion docker/docker-compose.custom.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
services:
# build and run the local image instead
# note: it's currently expose on port 3009
Expand Down
Loading