Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DB_HOST format support fix 147 #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ linux/s390x
* `DB_NAME`: Database name (default `nextcloud`)
* `DB_USER`: Username for database (default `nextcloud`)
* `DB_PASSWORD`: Password for database user
* `DB_HOST`: Database host (default `db`)
* `DB_HOST`: Database host, can be `domain/ip` or `domain/ip:port` (default `db`)
* `DB_TIMEOUT`: Time in seconds after which we stop trying to reach the database server. Only used for `mysql` and `pgsql` db type (default `60`)

> [!NOTE]
Expand Down
96 changes: 54 additions & 42 deletions rootfs/etc/cont-init.d/03-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ ln -sf /data/userapps /var/www/userapps &>/dev/null
file_env 'DB_USER'
file_env 'DB_PASSWORD'

if [ "$DB_TYPE" = "mysql" ]; then
echo "Checking mysql database connection..."
if [ "$DB_TYPE" == "pgsql" ] || [ "$DB_TYPE" == "mysql" ]; then
echo "Checking $DB_TYPE database connection..."
if [ -z "$DB_HOST" ]; then
echo >&2 "ERROR: DB_HOST must be defined"
exit 1
Expand All @@ -130,49 +130,61 @@ if [ "$DB_TYPE" = "mysql" ]; then
echo >&2 "ERROR: Either DB_PASSWORD or DB_PASSWORD_FILE must be defined"
exit 1
fi

dbcmd="mysql -h ${DB_HOST} -u "${DB_USER}" "-p${DB_PASSWORD}""

echo "Waiting ${DB_TIMEOUT}s for database to be ready..."
counter=1
while ! ${dbcmd} -e "show databases;" >/dev/null 2>&1; do
sleep 1
counter=$((counter + 1))
if [ ${counter} -gt ${DB_TIMEOUT} ]; then
echo >&2 "ERROR: Failed to connect to database on $DB_HOST"
exit 1
fi
done
echo "Database ready!"
unset dbcmd
fi

if [ "$DB_TYPE" = "pgsql" ]; then
echo "Checking pgsql database connection..."
if [ -z "$DB_HOST" ]; then
echo >&2 "ERROR: DB_HOST must be defined"
exit 1
IFS=":"
read -ra parts <<< "$DB_HOST"
num_parts="${#parts[@]}"
dbhost=$DB_HOST
if [ "$DB_TYPE" = "mysql" ]; then
dbport=3306
elif [ "$DB_TYPE" = "pgsql" ]; then
dbport=5432
fi
if [ -z "$DB_PASSWORD" ]; then
echo >&2 "ERROR: Either DB_PASSWORD or DB_PASSWORD_FILE must be defined"
exit 1
if [ "$num_parts" -eq 2 ]; then
dbhost="${parts[0]}"
dbport="${parts[1]}"
fi

export PGPASSWORD=${DB_PASSWORD}
dbcmd="psql --host=${DB_HOST} --username=${DB_USER} -lqt"

echo "Waiting ${DB_TIMEOUT}s for database to be ready..."
counter=1
while ${dbcmd} | cut -d \| -f 1 | grep -qw "${DB_NAME}" > /dev/null 2>&1; [ $? -ne 0 ]; do
sleep 1
counter=$((counter + 1))
if [ ${counter} -gt ${DB_TIMEOUT} ]; then
echo >&2 "ERROR: Failed to connect to database on $DB_HOST"
exit 1
echo "Waiting ${DB_TIMEOUT}s for database $dbhost:$dbport to be ready..."

if [ "$DB_TYPE" = "mysql" ]; then
dbcmd="mysql -h ${dbhost} -P ${dbport} -u '${DB_USER}' '-p${DB_PASSWORD}'"

counter=1
while ! bash -c ${dbcmd} -e "show databases;" >/dev/null 2>&1; do
sleep 1
counter=$((counter + 1))
if [ ${counter} -gt ${DB_TIMEOUT} ]; then
echo >&2 "ERROR: Failed to connect to database on $DB_HOST"
exit 1
fi
done
echo "Database ready!"
unset dbcmd
elif [ "$DB_TYPE" = "pgsql" ]; then

export PGPASSWORD=${DB_PASSWORD}
dbcmd="pg_isready --host=${dbhost} -p ${dbport} --username=${DB_USER}"
dbexist_cmd="psql --host=${dbhost} -p ${dbport} --username=${DB_USER} -lqt | cut -d \| -f 1 | grep -qw \"${DB_NAME}\""

counter=1
while ! bash -c ${dbcmd} ; do
sleep 1
counter=$((counter + 1))
if [ ${counter} -gt ${DB_TIMEOUT} ]; then
echo >&2 "ERROR: Failed to connect to database on $DB_HOST"
exit 1
fi
done
if ! bash -c ${dbexist_cmd}; then
createdb -h ${dbhost} -p ${dbport} -U ${DB_USER} -E UTF8 ${DB_NAME} # nextcloud bug under 28, https://github.com/nextcloud/server/pull/38750
if ! bash -c ${dbexist_cmd}; then
echo >&2 "ERROR: Failed to create database ${DB_NAME} on $DB_HOST"
exit 1
fi
Comment on lines +177 to +182
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With nextcloud/server#38750 merged, don't think we need this workaround anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just one of the problems.
This container still don't support the DB_HOST set to domain/ip:port.
I didn't find the way to modify the port of the database being connected to.

fi
done
echo "Database ready!"
unset dbcmd PGPASSWORD
echo "Database ready!"
unset dbcmd PGPASSWORD
fi

fi

# Install Nextcloud if config not found
Expand Down