From 7c86d235a9d5a6427c8975979d039a5dfd1b6bcc Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 23 Mar 2019 16:25:54 +0100 Subject: [PATCH] fix: [peertube] peertube would fail if postgres is not ready at start --- peertube/build/Dockerfile | 7 ++++- peertube/build/src/entrypoint.sh | 30 +++++++++++++++++++ .../hooks/postgres_database-relation-joined | 17 +++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100755 peertube/build/src/entrypoint.sh diff --git a/peertube/build/Dockerfile b/peertube/build/Dockerfile index 9fb23d9..736002e 100644 --- a/peertube/build/Dockerfile +++ b/peertube/build/Dockerfile @@ -94,7 +94,12 @@ RUN apk add nodejs-npm ## runtime deps RUN apk add openssl +## to check if postgres is fine BEFORE running the app +RUN apk add postgresql-client + +COPY src/ . + USER peertube WORKDIR /opt/apps/peertube -CMD ["npm", "start"] +CMD ["/entrypoint.sh"] diff --git a/peertube/build/src/entrypoint.sh b/peertube/build/src/entrypoint.sh new file mode 100755 index 0000000..5664b2b --- /dev/null +++ b/peertube/build/src/entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +timeout=120 +start="$(date +%s)" +while true; do + if err=$(echo "SELECT 1;" | psql 2>&1 >/dev/null); then + break + fi + now=$(date +%s) + if ! [ "$err" = "psql: FATAL: the database system is starting up" ]; then + echo "postgres connection error:" \ + "Could not connect to '$PGHOST'. (will retry)" + if [ "$err" ]; then + echo " Error:" >&2 + printf "%s\n" "$err" | sed -r "s/^/ ! /g" >&2 + fi + else + echo "Got 'database system is starting up' error... waiting before launching npm start" >&2 + fi + elapsed=$((now - start)) + if [ "$elapsed" -gt "$timeout" ]; then + echo "postgre connection error:"\ + "Could not connect to db on '$PGHOST'" \ + "container's IP." + exit 1 + fi + sleep 0.5 +done + +exec npm start \ No newline at end of file diff --git a/peertube/hooks/postgres_database-relation-joined b/peertube/hooks/postgres_database-relation-joined index ccf6dda..8bcced2 100755 --- a/peertube/hooks/postgres_database-relation-joined +++ b/peertube/hooks/postgres_database-relation-joined @@ -22,4 +22,21 @@ database: EOF +## XXXvlab: needed only for psql access to check if database is ready +## before launching the app. +uid=$(docker_get_uid "$SERVICE_NAME" peertube) || exit 1 + +chown "$uid" "$SERVICE_CONFIGSTORE/root/.pgpass" || exit 1 + +config-add " +services: + $MASTER_BASE_SERVICE_NAME: + environment: + PGUSER: $USER + PGHOST: $TARGET_SERVICE_NAME + PGDATABASE: $DBNAME + volumes: + - $SERVICE_CONFIGSTORE/root/.pgpass:/var/lib/peertube/.pgpass +" + info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."