From 047c3555ba3d42b021ecf2262ba62ae328d1ad1d Mon Sep 17 00:00:00 2001 From: David Mehren Date: Mon, 2 Oct 2023 20:27:47 +0200 Subject: [PATCH 1/2] Add a config option for the postgres database port Signed-off-by: David Mehren --- Containers/nextcloud/entrypoint.sh | 6 +++++- Containers/nextcloud/healthcheck.sh | 10 +++++++++- Containers/nextcloud/start.sh | 11 +++++++++-- Containers/notify-push/start.sh | 6 +++++- Containers/postgresql/healthcheck.sh | 7 ++++++- Containers/postgresql/start.sh | 10 +++++++++- php/containers.json | 3 +++ 7 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Containers/nextcloud/entrypoint.sh b/Containers/nextcloud/entrypoint.sh index 37d24eb8..4cc709cb 100644 --- a/Containers/nextcloud/entrypoint.sh +++ b/Containers/nextcloud/entrypoint.sh @@ -212,7 +212,11 @@ if ! [ -f "$NEXTCLOUD_DATA_DIR/skip.update" ]; then DATADIR_PERMISSION_CONF echo "Installing with PostgreSQL database" - INSTALL_OPTIONS+=(--database pgsql --database-name "$POSTGRES_DB" --database-user "$POSTGRES_USER" --database-pass "$POSTGRES_PASSWORD" --database-host "$POSTGRES_HOST") + # Set a default value for POSTGRES_PORT + if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 + fi + INSTALL_OPTIONS+=(--database pgsql --database-name "$POSTGRES_DB" --database-user "$POSTGRES_USER" --database-pass "$POSTGRES_PASSWORD" --database-host "$POSTGRES_HOST" --database-port "$POSTGRES_PORT") echo "Starting Nextcloud installation..." if ! php /var/www/html/occ maintenance:install "${INSTALL_OPTIONS[@]}"; then diff --git a/Containers/nextcloud/healthcheck.sh b/Containers/nextcloud/healthcheck.sh index 97d6a689..054b1da4 100644 --- a/Containers/nextcloud/healthcheck.sh +++ b/Containers/nextcloud/healthcheck.sh @@ -1,6 +1,14 @@ #!/bin/bash -nc -z "$POSTGRES_HOST" 5432 || exit 0 +# Set a default value for POSTGRES_PORT +if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 +fi + + +# POSTGRES_HOST must be set in the containers env vars and POSTGRES_PORT has a default above +# shellcheck disable=SC2153 +nc -z "$POSTGRES_HOST" "$POSTGRES_PORT" || exit 0 if ! nc -z localhost 9000; then exit 1 diff --git a/Containers/nextcloud/start.sh b/Containers/nextcloud/start.sh index 108dbdcf..a3f19f9d 100644 --- a/Containers/nextcloud/start.sh +++ b/Containers/nextcloud/start.sh @@ -1,7 +1,14 @@ #!/bin/bash +# Set a default value for POSTGRES_PORT +if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 +fi + # Only start container if database is accessible -while ! sudo -u www-data nc -z "$POSTGRES_HOST" 5432; do +# POSTGRES_HOST must be set in the containers env vars and POSTGRES_PORT has a default above +# shellcheck disable=SC2153 +while ! sudo -u www-data nc -z "$POSTGRES_HOST" "$POSTGRES_PORT"; do echo "Waiting for database to start..." sleep 5 done @@ -13,7 +20,7 @@ export POSTGRES_USER # Fix false database connection on old instances if [ -f "/var/www/html/config/config.php" ]; then sleep 2 - while ! sudo -u www-data psql -d "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB" -c "select now()"; do + while ! sudo -u www-data psql -d "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB" -c "select now()"; do echo "Waiting for the database to start..." sleep 5 done diff --git a/Containers/notify-push/start.sh b/Containers/notify-push/start.sh index e524e20d..6180162f 100644 --- a/Containers/notify-push/start.sh +++ b/Containers/notify-push/start.sh @@ -44,8 +44,12 @@ fi echo "notify-push was started" +# Set a default value for POSTGRES_PORT +if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 +fi # Set sensitive values as env -export DATABASE_URL="postgres://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST/$POSTGRES_DB" +export DATABASE_URL="postgres://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB" export REDIS_URL="redis://:$REDIS_HOST_PASSWORD@$REDIS_HOST" # Run it diff --git a/Containers/postgresql/healthcheck.sh b/Containers/postgresql/healthcheck.sh index c2ee4ec7..2b29a955 100644 --- a/Containers/postgresql/healthcheck.sh +++ b/Containers/postgresql/healthcheck.sh @@ -2,4 +2,9 @@ test -f "/mnt/data/backup-is-running" && exit 0 -psql -d "postgresql://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "select now()" || exit 1 +# Set a default value for POSTGRES_PORT +if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 +fi + +psql -d "postgresql://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/$POSTGRES_DB" -c "select now()" || exit 1 diff --git a/Containers/postgresql/start.sh b/Containers/postgresql/start.sh index 4ae7369f..0e7a35aa 100644 --- a/Containers/postgresql/start.sh +++ b/Containers/postgresql/start.sh @@ -6,6 +6,14 @@ export DUMP_DIR="/mnt/data" DUMP_FILE="$DUMP_DIR/database-dump.sql" export PGPASSWORD="$POSTGRES_PASSWORD" +# Set a default value for POSTGRES_PORT +if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT=5432 +fi + +# Set PGPORT (the variable used by the postgres entrypoint) to the configured value +export PGPORT=$POSTGRES_PORT + # Don't start database as long as backup is running while [ -f "$DUMP_DIR/backup-is-running" ]; do echo "Waiting for backup container to finish..." @@ -131,7 +139,7 @@ EOSQL pg_ctl stop -m fast # Change database port back to default - export PGPORT=5432 + export PGPORT=$POSTGRES_PORT # Don't exit if command fails anymore set +ex diff --git a/php/containers.json b/php/containers.json index 0c780618..f34bbd04 100644 --- a/php/containers.json +++ b/php/containers.json @@ -99,6 +99,7 @@ "POSTGRES_PASSWORD=%DATABASE_PASSWORD%", "POSTGRES_DB=nextcloud_database", "POSTGRES_USER=nextcloud", + "POSTGRES_PORT=5432", "TZ=%TIMEZONE%", "PGTZ=%TIMEZONE%" ], @@ -173,6 +174,7 @@ ], "environment": [ "POSTGRES_HOST=nextcloud-aio-database", + "POSTGRES_PORT=5432", "POSTGRES_PASSWORD=%DATABASE_PASSWORD%", "POSTGRES_DB=nextcloud_database", "POSTGRES_USER=nextcloud", @@ -266,6 +268,7 @@ "REDIS_HOST=nextcloud-aio-redis", "REDIS_HOST_PASSWORD=%REDIS_PASSWORD%", "POSTGRES_HOST=nextcloud-aio-database", + "POSTGRES_PORT=5432", "POSTGRES_PASSWORD=%DATABASE_PASSWORD%", "POSTGRES_DB=nextcloud_database", "POSTGRES_USER=nextcloud" From b1ad854de6de5282be68578fbc4c254f113070c4 Mon Sep 17 00:00:00 2001 From: "Simon L." Date: Fri, 17 May 2024 14:03:59 +0200 Subject: [PATCH 2/2] Revert changes to database container Signed-off-by: Simon L --- Containers/postgresql/healthcheck.sh | 7 +------ Containers/postgresql/start.sh | 10 +--------- php/containers.json | 1 - 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/Containers/postgresql/healthcheck.sh b/Containers/postgresql/healthcheck.sh index 2b29a955..c2ee4ec7 100644 --- a/Containers/postgresql/healthcheck.sh +++ b/Containers/postgresql/healthcheck.sh @@ -2,9 +2,4 @@ test -f "/mnt/data/backup-is-running" && exit 0 -# Set a default value for POSTGRES_PORT -if [ -z "$POSTGRES_PORT" ]; then - POSTGRES_PORT=5432 -fi - -psql -d "postgresql://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/$POSTGRES_DB" -c "select now()" || exit 1 +psql -d "postgresql://oc_$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "select now()" || exit 1 diff --git a/Containers/postgresql/start.sh b/Containers/postgresql/start.sh index 0e7a35aa..4ae7369f 100644 --- a/Containers/postgresql/start.sh +++ b/Containers/postgresql/start.sh @@ -6,14 +6,6 @@ export DUMP_DIR="/mnt/data" DUMP_FILE="$DUMP_DIR/database-dump.sql" export PGPASSWORD="$POSTGRES_PASSWORD" -# Set a default value for POSTGRES_PORT -if [ -z "$POSTGRES_PORT" ]; then - POSTGRES_PORT=5432 -fi - -# Set PGPORT (the variable used by the postgres entrypoint) to the configured value -export PGPORT=$POSTGRES_PORT - # Don't start database as long as backup is running while [ -f "$DUMP_DIR/backup-is-running" ]; do echo "Waiting for backup container to finish..." @@ -139,7 +131,7 @@ EOSQL pg_ctl stop -m fast # Change database port back to default - export PGPORT=$POSTGRES_PORT + export PGPORT=5432 # Don't exit if command fails anymore set +ex diff --git a/php/containers.json b/php/containers.json index f34bbd04..fd1e137e 100644 --- a/php/containers.json +++ b/php/containers.json @@ -99,7 +99,6 @@ "POSTGRES_PASSWORD=%DATABASE_PASSWORD%", "POSTGRES_DB=nextcloud_database", "POSTGRES_USER=nextcloud", - "POSTGRES_PORT=5432", "TZ=%TIMEZONE%", "PGTZ=%TIMEZONE%" ],