Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add docker entrypoint to ensure db migration on startup (#270)

Madison Swain-Bowden7ee62451343e
diff --git a/.github/workflows/push_pull_request_test.yml b/.github/workflows/push_pull_request_test.yml
index f15b0402d..886e6a3db 100644
--- a/.github/workflows/push_pull_request_test.yml
+++ b/.github/workflows/push_pull_request_test.yml
@@ -16,7 +16,4 @@ jobs:
- name: Change permissions for Docker steps
run: chmod -R 777 openverse_catalog/ tests/
- name: Test
- run: |
- just up
- sleep 5 # TODO: Find a more thorough way of addressing this
- just test
+ run: just test
diff --git a/docker-compose.override.yml b/docker-compose.override.yml
index edef313ff..fa4d20782 100644
--- a/docker-compose.override.yml
+++ b/docker-compose.override.yml
@@ -54,6 +54,8 @@ services:
build:
args:
- REQUIREMENTS_FILE=requirements_dev.txt
+ # This command ensures an admin account is created on startup
+ command: init
# Volume override to include *all* of openverse_catalog, not just the DAGs folder
# Note, this overrides the whole block, so existing volumes must be duplicated
volumes:
diff --git a/docker/airflow/Dockerfile b/docker/airflow/Dockerfile
index b6981f6c8..6a434ffa1 100644
--- a/docker/airflow/Dockerfile
+++ b/docker/airflow/Dockerfile
@@ -37,11 +37,11 @@ USER airflow
WORKDIR ${AIRFLOW_HOME}
# Always add the prod req because the dev reqs depend on it for deduplication
-COPY ${REQUIREMENTS_FILE} requirements_prod.txt wait_for_db.py ${AIRFLOW_HOME}/
+COPY ${REQUIREMENTS_FILE} requirements_prod.txt docker/airflow/wait_for_db.py ${AIRFLOW_HOME}/
RUN pip install --user -r ${REQUIREMENTS_FILE} -c ${CONSTRAINTS_FILE}
-CMD python wait_for_db.py && \
- airflow db init && \
- airflow users create -r Admin -u airflow -f Air -l Flow -p airflow --email [email protected] && \
- (airflow scheduler & airflow webserver)
+COPY docker/airflow/entrypoint.sh entrypoint.sh
+
+ENTRYPOINT ["./entrypoint.sh"]
+CMD ["server"]
diff --git a/docker/airflow/entrypoint.sh b/docker/airflow/entrypoint.sh
new file mode 100755
index 000000000..8b29acb9b
--- /dev/null
+++ b/docker/airflow/entrypoint.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+set -e
+
+function help_text() {
+ cat << 'END'
+
+ ,-. ;-. ,--. . . . , ,--. ,-. ,-. ,--.
+ / \ | ) | |\ | | / | | ) ( ` |
+ | | |-' |- | \| | / |- |-< `-. |-
+ \ / | | | | |/ | | \ . ) |
+ `-' ' `--' ' ' ' `--' ' ' `-' `--'
+
+Docker entrypoint script for Openverse Airflow. Unless specified, all commands
+will wait for the database to be ready and will upgrade the Airflow schema.
+
+Usage:
+ help - print this help text and exit
+ init - create an admin user account before starting the scheduler+webserver
+ server - start the scheduler+webserver
+ (anything else) - run the command provided
+END
+}
+
+
+function header() {
+ size=${COLUMNS:-80}
+ # Print centered text between two dividers of length $size
+ printf '#%.0s' $(seq 1 $size) && echo
+ printf "%*s\n" $(( (${#1} + size) / 2)) "$1"
+ printf '#%.0s' $(seq 1 $size) && echo
+}
+
+if [ "$1" == help ] || [ "$1" == --help ]; then help_text && exit 0; fi
+sleep 0.1; # The $COLUMNS variable takes a moment to populate
+
+# Wait for postgres
+header "WAITING FOR POSTGRES"
+python wait_for_db.py
+# Upgrade the database -- command is idempotent.
+header "MIGRATING DATABASE"
+airflow db upgrade
+if [[ "$1" == "init" ]]; then
+ header "CREATING ADMIN USER"
+ airflow users create -r Admin -u airflow -f Air -l Flow -p airflow --email [email protected]
+fi
+
+case "$1" in
+ init|server|"")
+ # Start scheduler and webserver in same container
+ header "STARTING AIRFLOW"
+ airflow scheduler &
+ exec airflow webserver
+ ;;
+ *)
+ # The command is something like bash, not an airflow subcommand. Just run it in the right environment.
+ header "RUNNING \"$*\""
+ exec "$@"
+ ;;
+esac
diff --git a/wait_for_db.py b/docker/airflow/wait_for_db.py
similarity index 100%
rename from wait_for_db.py
rename to docker/airflow/wait_for_db.py
diff --git a/justfile b/justfile
index 051762623..4e7878a4b 100644
--- a/justfile
+++ b/justfile
@@ -53,7 +53,7 @@ lint:
pre-commit run --all-files
# Mount the tests directory and run a particular command
-@_mount-tests command: up
+@_mount-tests command: (up "postgres s3")
# The test directory is mounted into the container only during testing
docker-compose {{ DOCKER_FILES }} run \
-v {{ justfile_directory() }}/tests:/usr/local/airflow/tests/ \