diff --git a/app/entrypoint.sh b/app/entrypoint.sh index 3b4d805..134772b 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -51,29 +51,32 @@ if [ "$1" = 'mattermost' ]; then jq '.SqlSettings.DriverName = "postgres"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG jq '.SqlSettings.AtRestEncryptKey = "'$(generate_salt)'"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG jq '.PluginSettings.Directory = "/mattermost/plugins/"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG + + # Configure database access + if [[ -z "$MM_SQLSETTINGS_DATASOURCE" ]] + then + echo -ne "Configure database connection..." + # URLEncode the password, allowing for special characters + ENCODED_PASSWORD=$(printf %s $MM_PASSWORD | jq -s -R -r @uri) + export MM_SQLSETTINGS_DATASOURCE="postgres://$MM_USERNAME:$ENCODED_PASSWORD@$DB_HOST:$DB_PORT_NUMBER/$MM_DBNAME?sslmode=disable&connect_timeout=10" + echo OK + else + echo "Using existing database connection" + fi else echo "Using existing config file" $MM_CONFIG fi - # Configure database access - if [ -z "$MM_SQLSETTINGS_DATASOURCE" ] + if [ ! -z "$DB_HOST" ] then - echo -ne "Configure database connection..." - # URLEncode the password, allowing for special characters - ENCODED_PASSWORD=$(printf %s $MM_PASSWORD | jq -s -R -r @uri) - export MM_SQLSETTINGS_DATASOURCE="postgres://$MM_USERNAME:$ENCODED_PASSWORD@$DB_HOST:$DB_PORT_NUMBER/$MM_DBNAME?sslmode=disable&connect_timeout=10" - echo OK - else - echo "Using existing database connection" + # Wait for database to be reachable + echo "Wait until database $DB_HOST:$DB_PORT_NUMBER is ready..." + until nc -z $DB_HOST $DB_PORT_NUMBER + do + sleep 1 + done fi - # Wait for database to be reachable - echo "Wait until database $DB_HOST:$DB_PORT_NUMBER is ready..." - until nc -z $DB_HOST $DB_PORT_NUMBER - do - sleep 1 - done - # Wait another second for the database to be properly started. # Necessary to avoid "panic: Failed to open sql connection pq: the database system is starting up" sleep 1