Skip to content

Commit

Permalink
Add a config option for the postgres database port
Browse files Browse the repository at this point in the history
Signed-off-by: David Mehren <git@herrmehren.de>
  • Loading branch information
davidmehren committed Mar 26, 2024
1 parent c9ec673 commit cf0ff4d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Containers/nextcloud/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,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
Expand Down
10 changes: 9 additions & 1 deletion Containers/nextcloud/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 9 additions & 2 deletions Containers/nextcloud/start.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Containers/notify-push/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Containers/postgresql/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion Containers/postgresql/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions php/containers.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@
"image": "nextcloud/aio-postgresql",
"init": true,
"expose": [
"5432"
"%POSTGRES_PORT%"
],
"internal_port": "5432",
"internal_port": "%POSTGRES_PORT%",
"secrets": [
"DATABASE_PASSWORD"
],
Expand All @@ -99,6 +99,7 @@
"POSTGRES_PASSWORD=%DATABASE_PASSWORD%",
"POSTGRES_DB=nextcloud_database",
"POSTGRES_USER=nextcloud",
"POSTGRES_PORT=5432",
"TZ=%TIMEZONE%",
"PGTZ=%TIMEZONE%"
],
Expand Down Expand Up @@ -173,6 +174,7 @@
],
"environment": [
"POSTGRES_HOST=nextcloud-aio-database",
"POSTGRES_PORT=5432",
"POSTGRES_PASSWORD=%DATABASE_PASSWORD%",
"POSTGRES_DB=nextcloud_database",
"POSTGRES_USER=nextcloud",
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit cf0ff4d

Please sign in to comment.