From 2e5d89b08a9927bf07e90854cc0f4cc57e0d62e2 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Tue, 16 Mar 2021 22:19:52 +0100
Subject: [PATCH] [Docker] Use `SELECT 1` to wait for database

This removes the need to have host and port as environment
variables (we might get it in a YAML file from a K8s ConfigMap, or
some such). Also, it ensures that the system answering is a database
rather than an IoT Waffeleisen with a bad taste in port selection.
---
 docker-startup.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/docker-startup.sh b/docker-startup.sh
index f0fd18ee6..eddb82ee2 100755
--- a/docker-startup.sh
+++ b/docker-startup.sh
@@ -2,9 +2,6 @@
 
 HTTP_PORT=${HTTP_PORT:8000}
 
-export ALEKSIS_database__host=${ALEKSIS_database__host:-127.0.0.1}
-export ALEKSIS_database__port=${ALEKSIS_database__port:-5432}
-
 if [[ -z $ALEKSIS_secret_key ]]; then
     if [[ ! -e /var/lib/aleksis/secret_key ]]; then
 	touch /var/lib/aleksis/secret_key; chmod 600 /var/lib/aleksis/secret_key
@@ -13,9 +10,12 @@ if [[ -z $ALEKSIS_secret_key ]]; then
     ALEKSIS_secret_key=$(</var/lib/aleksis/secret_key)
 fi
 
-while ! nc -z $ALEKSIS_database__host $ALEKSIS_database__port; do
-    sleep 0.1
+echo -n "Waiting for database."
+while ! aleksis-admin dbshell -- -c "SELECT 1" >/dev/null 2>&1; do
+    sleep 0.5
+    echo -n .
 done
+echo
 
 aleksis-admin migrate
 aleksis-admin createinitialrevisions
-- 
GitLab