Skip to content

Commit d10cccb

Browse files
authored
DB config, including port, through libpq env vars (#1647)
Use libpq PG* environment variables for DB configuration handling and provide backward compatibility for legacy DB_* environment variables
1 parent a4908f5 commit d10cccb

File tree

6 files changed

+34
-17
lines changed

6 files changed

+34
-17
lines changed

.env.template

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ HTTPS_PORT=443
1515
# SERVICE_NODE_OPTIONS=
1616

1717
# Optional: connect to a custom database server
18-
# DB_HOST=
19-
# DB_USER=
20-
# DB_PASSWORD=
21-
# DB_NAME=
18+
# PGHOST=
19+
# PGUSER=
20+
# PGPASSWORD=
21+
# PGDATABASE=
22+
# Refer to the installation documentation (https://docs.getodk.org/central-install/)
23+
# to learn about advanced database configuration.
2224

2325
# Optional: configure a custom mail server
2426
# EMAIL_FROM=

docker-compose.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ services:
4747
volumes:
4848
- secrets:/etc/secrets
4949
- /data/transfer:/data/transfer
50+
env_file: .env
5051
environment:
5152
- DOMAIN=${DOMAIN}
5253
- SYSADMIN_EMAIL=${SYSADMIN_EMAIL}
5354
- HTTPS_PORT=${HTTPS_PORT:-443}
5455
- NODE_OPTIONS=${SERVICE_NODE_OPTIONS:-}
55-
- DB_HOST=${DB_HOST:-postgres14}
56-
- DB_USER=${DB_USER:-odk}
57-
- DB_PASSWORD=${DB_PASSWORD:-odk}
5856
- DB_POOL_SIZE=${DB_POOL_SIZE:-10}
59-
- DB_NAME=${DB_NAME:-odk}
60-
- DB_SSL=${DB_SSL:-null}
6157
- EMAIL_FROM=${EMAIL_FROM:-no-reply@$DOMAIN}
6258
- EMAIL_HOST=${EMAIL_HOST:-mail}
6359
- EMAIL_PORT=${EMAIL_PORT:-25}
@@ -79,7 +75,7 @@ services:
7975
- S3_SECRET_KEY=${S3_SECRET_KEY:-}
8076
- S3_BUCKET_NAME=${S3_BUCKET_NAME:-}
8177
- SESSION_LIFETIME=${SESSION_LIFETIME:-86400}
82-
command: [ "wait-for-it", "${DB_HOST:-postgres14}:5432", "--", "./start-odk.sh" ]
78+
command: [ "./start-odk.sh" ]
8379
restart: always
8480
logging:
8581
driver: local

files/service/config.json.template

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"default": {
33
"database": {
4-
"host": "${DB_HOST}",
5-
"user": "${DB_USER}",
6-
"password": "${DB_PASSWORD}",
7-
"database": "${DB_NAME}",
8-
"ssl": ${DB_SSL},
4+
"host": "",
5+
"user": "",
6+
"password": "",
7+
"database": "",
98
"maximumPoolSize": ${DB_POOL_SIZE}
109
},
1110
"email": {

files/service/scripts/start-odk.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ SENTRY_TAGS="{ \"version.central\": \"$(cat sentry-versions/central)\", \"versio
1717
# shellcheck disable=SC2090
1818
export SENTRY_TAGS
1919

20+
# handle legacy DB_SSL configuration, as we couldn't conditionally template that in docker-compose.yml
21+
if [[ -v DB_SSL && "${DB_SSL}" = "true" ]]; then
22+
if [[ -v PGSSLMODE && -v PGREQUIRESSL ]]; then
23+
export PGSSLMODE="require"
24+
else
25+
echo "Fatal: legacy 'DB_SSL=true' specified, but PGSSLMODE or PGREQUIRESSL has already been set. To resolve ambiguity, remove 'DB_SSL=true' from your .env file." > /dev/stderr
26+
exit 100
27+
fi
28+
fi
29+
30+
# handle other legacy DB_* configuration, and fill in our defaults if nothing is specified
31+
# When these PG* variables are _defined_ (even if _empty_), they will be left as-is.
32+
[[ -v PGHOST ]] || export PGHOST=${DB_HOST:-postgres14}
33+
[[ -v PGUSER ]] || export PGUSER=${DB_USER:-odk}
34+
[[ -v PGPASSWORD ]] || export PGPASSWORD=${DB_PASSWORD:-odk}
35+
[[ -v PGDATABASE ]] || export PGDATABASE=${DB_NAME:-odk}
36+
[[ -v PGAPPNAME ]] || export PGAPPNAME=odkcentral
37+
38+
echo "waiting for PostgreSQL to become connectable to..."
39+
while ! (psql --no-password --quiet --command "" > /dev/null 2>&1 || (echo "sleeping 1 second waiting for a database connection"; false)); do sleep 1; done
40+
2041
echo "running migrations.."
2142
node ./lib/bin/run-migrations
2243

server

Submodule server updated 64 files

service.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ RUN apt-get update \
4848
&& apt-get install -y --no-install-recommends \
4949
gpg \
5050
cron \
51-
wait-for-it \
5251
procps \
5352
postgresql-client-14 \
5453
netcat-traditional \

0 commit comments

Comments
 (0)