Files
boltage/docker/peo-entrypoint.sh
T

65 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2026 Bart Broere
# SPDX-License-Identifier: GPL-3.0-or-later
# Runs PostgreSQL (through the stock postgres entrypoint), the peo bridge and
# pgAdmin side by side. The container stops when PostgreSQL or the bridge exits;
# pgAdmin is optional and its failure does not take the others down.
set -euo pipefail
if [ "${1:-}" != "postgres" ]; then
exec docker-entrypoint.sh "$@"
fi
export PEO_PG_DATABASE="${PEO_PG_DATABASE:-${POSTGRES_DB:-${POSTGRES_USER:-postgres}}}"
start_pgadmin() {
local app=/opt/pgadmin/app
local email="${PGADMIN_DEFAULT_EMAIL:-admin@example.com}"
if [ ! -f /var/lib/pgadmin/pgadmin4.db ]; then
# First start: create the login user and register the bundled PostgreSQL.
cat > /var/lib/pgadmin/servers.json <<EOF
{"Servers": {"1": {"Name": "peo", "Group": "Servers", "Host": "127.0.0.1", "Port": 5432,
"MaintenanceDB": "${PEO_PG_DATABASE}", "Username": "${POSTGRES_USER:-postgres}", "SSLMode": "prefer"}}}
EOF
chown postgres:postgres /var/lib/pgadmin/servers.json
PGADMIN_SETUP_EMAIL="$email" \
PGADMIN_SETUP_PASSWORD="${PGADMIN_DEFAULT_PASSWORD:-${POSTGRES_PASSWORD:-}}" \
gosu postgres /opt/pgadmin/bin/python "$app/setup.py" setup-db \
&& gosu postgres /opt/pgadmin/bin/python "$app/setup.py" load-servers \
/var/lib/pgadmin/servers.json --user "$email" \
|| { rm -f /var/lib/pgadmin/pgadmin4.db; return 1; }
fi
gosu postgres /opt/pgadmin/bin/gunicorn --bind "0.0.0.0:${PEO_PGADMIN_PORT}" \
--workers 1 --threads 25 --timeout 0 --chdir "$app" --log-level warning pgAdmin4:app &
pgadmin_pid=$!
echo "peo: pgAdmin on http://localhost:${PEO_PGADMIN_PORT}/ (login: $email)"
}
docker-entrypoint.sh "$@" &
pg_pid=$!
gosu postgres /opt/peo/venv/bin/peo &
bridge_pid=$!
pgadmin_pid=
if [ "${PEO_PGADMIN}" = "true" ]; then
start_pgadmin || echo "peo: WARNING: pgAdmin setup failed, continuing without it" >&2
fi
shutdown() {
for pid in "$bridge_pid" $pgadmin_pid "$pg_pid"; do
kill -TERM "$pid" 2>/dev/null || true
done
}
trap shutdown TERM INT
# Wait until PostgreSQL or the bridge exits, then stop everything.
set +e
wait -n "$pg_pid" "$bridge_pid"
status=$?
shutdown
wait 2>/dev/null
exit "$status"