From 68db80f75909520166a3144e93078934837aceff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ky=C3=A2ne=20Pichou?= Date: Mon, 29 Oct 2018 13:57:59 +0100 Subject: [PATCH] Improve app to db connection setup (#332) --- .travis.yml | 2 +- app/entrypoint.sh | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 245d56e..d18cb4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ install: env: - BUILD="mkdir -p ./volumes/app/mattermost/{data,logs,config,plugins} && docker-compose up -d" - - BUILD="docker run -d --name db -e POSTGRES_USER=mmuser -e POSTGRES_PASSWORD=mmuser_password -e POSTGRES_DB=mattermost mattermost-prod-db && sleep 5 && docker run -d --link db -p 80:8000 --name app mattermost-prod-app" + - BUILD="docker run -d --name db -e POSTGRES_USER=mmuser -e POSTGRES_PASSWORD=mmuser_password -e POSTGRES_DB=mattermost mattermost-prod-db && sleep 5 && docker run -d --link db -p 80:8000 --name app -e MM_USERNAME=mmuser -e MM_PASSWORD=mmuser_password mattermost-prod-app" script: - curl -sSf http://localhost > /dev/null diff --git a/app/entrypoint.sh b/app/entrypoint.sh index ce2138b..b6ee3cd 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -8,8 +8,6 @@ generate_salt() { # Read environment variables or set default values DB_HOST=${DB_HOST:-db} DB_PORT_NUMBER=${DB_PORT_NUMBER:-5432} -MM_USERNAME=${MM_USERNAME:-mmuser} -MM_PASSWORD=${MM_PASSWORD:-mmuser_password} MM_DBNAME=${MM_DBNAME:-mattermost} MM_CONFIG=${MM_CONFIG:-/mattermost/config/config.json} @@ -51,22 +49,22 @@ 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" && ! -z "$MM_USERNAME" && ! -z "$MM_PASSWORD" ]] + 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 + # 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