Skip to content

Commit 7003a93

Browse files
committed
Compose updates & fixes
1 parent 3879bf4 commit 7003a93

14 files changed

Lines changed: 187 additions & 81 deletions

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
!/sqlite3
2020
!/requirements
2121
!/bin/entrypoint-colocus.sh
22+
!/bin/entrypoint-migrate-and-load.sh
23+
!envs/env.test
2224

2325
# Ignore unnecessary files inside allowed directories
2426
# This should go after the allowed directories

.github/workflows/ci.yml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,22 @@ jobs:
3030
- name: Run pre-commit
3131
run: uv run pre-commit run --all-files
3232

33-
# With no caching at all the entire ci process takes 4m 30s to complete!
3433
pytest:
3534
runs-on: ubuntu-latest
3635
steps:
3736
- name: Checkout Code Repository
3837
uses: actions/checkout@v4
3938

40-
- name: Install uv
41-
uses: astral-sh/setup-uv@v3
42-
43-
- name: Set up Python
44-
run: uv python install 3.10
45-
46-
- name: Install dependencies
47-
run: uv sync
48-
49-
- name: Run pytest
50-
run: uv run pytest
39+
- name: Run tests
5140
env:
52-
DJANGO_SECRET_KEY: 1234
53-
DJANGO_SETTINGS_MODULE: config.settings.test
41+
COMPOSE_PROJECT_NAME: colocus_tests
42+
run: |
43+
docker compose --env-file envs/env.test -f docker-compose.tests.yml up \
44+
--force-recreate \
45+
--build \
46+
--abort-on-container-exit \
47+
--exit-code-from django
48+
49+
- name: Tear down
50+
if: ${{ always() }}
51+
run: docker compose --env-file envs/env.test -f docker-compose.tests.yml down --volumes --remove-orphans

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,6 @@ database/**
337337

338338
# Private settings
339339
workflow/config/config.yml
340+
341+
# Docker compose
342+
docker-compose.override.yml

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ RUN \
5959
COPY --chown=colocus:colocus . /opt/colocus/
6060

6161
# Run test cases
62-
RUN env DJANGO_SECRET_KEY=1234 DJANGO_SETTINGS_MODULE=config.settings.test \
63-
bash -c '. .venv/bin/activate && pytest'
62+
# RUN env DJANGO_SECRET_KEY=1234 DJANGO_SETTINGS_MODULE=config.settings.test \
63+
# bash -c '. .venv/bin/activate && pytest'
6464

6565
# Frequently changing metadata here to avoid cache misses
6666
ARG BUILD_DATE

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,15 @@ docker compose up -d --build --force-recreate django
4848
docker compose exec db bash -c 'psql -U colocus -c "DROP DATABASE core WITH (FORCE)"'
4949
docker compose exec db bash /docker-entrypoint-initdb.d/init-db.sh
5050
docker compose exec django bash -c 'source .venv/bin/activate && python3 manage.py migrate --database=core'
51-
docker compose exec django bash -c 'source .venv/bin/activate && python3 scripts/load_dataset.py /data'
51+
docker compose exec django bash -c "
52+
source .venv/bin/activate
53+
for subdir in \$(find /data -mindepth 1 -maxdepth 1 -type d); do
54+
subdir=\"\${subdir%/}\"
55+
if [ -d \"\$subdir\" ]; then
56+
uv run python3 scripts/load_dataset.py \"\${subdir}\"
57+
fi
58+
done
59+
"
5260
```
5361

5462
For debugging a new dataset load:

bin/compose-reload-data.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
docker compose exec db bash -c 'psql -U colocus -c "DROP DATABASE core WITH (FORCE)"'
5+
docker compose exec db bash /docker-entrypoint-initdb.d/init-db.sh
6+
docker compose exec django bash -c 'source .venv/bin/activate && python3 manage.py migrate --database=core'
7+
docker compose exec django bash -c "
8+
source .venv/bin/activate
9+
for subdir in \$(find /data -mindepth 1 -maxdepth 1 -type d); do
10+
subdir=\"\${subdir%/}\"
11+
if [ -d \"\$subdir\" ]; then
12+
uv run python3 scripts/load_dataset.py \"\${subdir}\"
13+
fi
14+
done
15+
"

bin/compose-run-pytest.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
# This is to avoid conflict with how containers get named in the other compose stack
5+
export COMPOSE_PROJECT_NAME="colocus_tests"
6+
7+
# Ensure cleanup on exit
8+
trap 'docker compose --env-file envs/env.test -f docker-compose.tests.yml down --volumes --remove-orphans' EXIT
9+
10+
# Run the test suite
11+
docker compose --env-file envs/env.test -f docker-compose.tests.yml up --force-recreate --build --abort-on-container-exit --exit-code-from django
12+
13+
# If you want to debug into the django container:
14+
# docker compose --env-file envs/env.test -f docker-compose.tests.yml run --entrypoint bash --rm django -l

bin/docker-build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
set -euxo pipefail
33

44
COLOCUS_VERSION=`git describe --tags --abbrev=11 | sed 's/^v//' | sed 's/-g/-/'`
5+
COLOCUS_BRANCH=`git describe --all --tags --abbrev=11 | sed 's#heads/##' | sed s'#/#-#' | sed 's/-g/-/'`
56
GIT_SHA=`git rev-parse HEAD`
67
BUILD_DATE=`date -u +'%Y-%m-%dT%H:%M:%SZ'`
78

8-
docker build --pull -t colocus:${COLOCUS_VERSION} \
9+
docker build --pull \
10+
-t colocus:${COLOCUS_VERSION} \
11+
-t colocus:${COLOCUS_BRANCH} \
912
--build-arg MAKEFLAGS="-j 3" \
1013
--build-arg CMAKE_BUILD_PARALLEL_LEVEL=3 \
1114
--build-arg BUILD_DATE=${BUILD_DATE} \

bin/entrypoint-colocus.sh

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,12 @@
11
#!/bin/bash
22
set -e
33

4-
source .venv/bin/activate
5-
6-
# Function to check if migrations are needed
7-
check_migrations_needed() {
8-
python3 manage.py showmigrations --plan | grep -q '\[ \]'
9-
}
10-
11-
# Function to check if core app tables exist
12-
check_core_tables_exist() {
13-
python3 -c "
14-
import os
15-
import django
16-
from django.db import connections
17-
18-
# Check that django settings module is set
19-
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
20-
raise EnvironmentError('DJANGO_SETTINGS_MODULE environment variable is not set; set before running container')
21-
22-
django.setup()
23-
24-
try:
25-
with connections['core'].cursor() as cursor:
26-
cursor.execute(\"SELECT COUNT(*) FROM core_datasubmission\")
27-
count = cursor.fetchone()[0]
28-
exit(0 if count > 0 else 1)
29-
except Exception:
30-
exit(1)
31-
"
32-
}
33-
34-
# Check if core tables exist and migrations are up to date
35-
if check_core_tables_exist && ! check_migrations_needed; then
36-
echo "Database already initialized and up to date"
37-
else
38-
echo "Running database migrations..."
39-
python3 manage.py migrate
40-
python3 manage.py migrate --database=core
41-
42-
# Only load dataset if it's a fresh database
43-
if ! check_core_tables_exist; then
44-
echo "Loading initial dataset..."
45-
python3 scripts/load_dataset.py /data
46-
fi
47-
fi
4+
# First run migrations and load data if needed
5+
/opt/colocus/bin/entrypoint-migrate-and-load.sh
486

497
# Now run uvicorn
508
HOST=${UVICORN_HOST:-127.0.0.1}
519
PORT=${UVICORN_PORT:-8000}
5210
WORKERS=${WEB_CONCURRENCY:-1}
53-
/opt/colocus/.venv/bin/uvicorn config.asgi:application --host $HOST --port $PORT --workers $WORKERS "$@"
11+
12+
uv run uvicorn config.asgi:application --host $HOST --port $PORT --workers $WORKERS "$@"

bin/entrypoint-migrate-and-load.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
set -e
3+
4+
source .venv/bin/activate
5+
6+
# Function to check if migrations are needed
7+
check_migrations_needed() {
8+
uv run python3 manage.py showmigrations --plan | grep -q '\[ \]'
9+
}
10+
11+
# Function to check if core app tables exist
12+
check_core_tables_exist() {
13+
uv run python3 -c "
14+
import os
15+
import django
16+
from django.db import connections
17+
18+
# Check that django settings module is set
19+
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
20+
raise EnvironmentError('DJANGO_SETTINGS_MODULE environment variable is not set; set before running container')
21+
22+
django.setup()
23+
24+
try:
25+
with connections['core'].cursor() as cursor:
26+
cursor.execute(\"SELECT COUNT(*) FROM core_datasubmission\")
27+
count = cursor.fetchone()[0]
28+
exit(0 if count > 0 else 1)
29+
except Exception:
30+
exit(1)
31+
"
32+
}
33+
34+
# Check if core tables exist and migrations are up to date
35+
if check_core_tables_exist && ! check_migrations_needed; then
36+
echo "Database already initialized and up to date"
37+
else
38+
echo "Running database migrations..."
39+
uv run python3 manage.py migrate
40+
uv run python3 manage.py migrate --database=core
41+
42+
# Only load datasets if it's a fresh database
43+
if ! check_core_tables_exist; then
44+
echo "Loading initial dataset(s)..."
45+
for subdir in `find /data -type d -mindepth 1 -maxdepth 1`
46+
do
47+
# Remove the trailing slash
48+
subdir="${subdir%/}"
49+
if [ -d "$subdir" ]; then
50+
# Load dataset
51+
uv run python3 scripts/load_dataset.py "${subdir}"
52+
fi
53+
done
54+
fi
55+
fi

0 commit comments

Comments
 (0)