service-mattermost/entrypoint.sh

100 lines
3.1 KiB
Bash

#!/bin/bash
set -u
set -e
# Function to generate a random salt
generate_salt() {
tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 48 | head -n 1
}
# Update configuration file according to value
config_set() {
local value="$1"
local file="$2"
jq "$value" "$file" > "$file.tmp"
mv "$file.tmp" "$file"
}
# Parse environment variables
chmod +x /parseurl.py
/parseurl.py MM_DB_
eval "$(/parseurl.py MM_DB_)"
# 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}
if [ "${1:0:1}" = '-' ]; then
set -- platform "$@"
fi
if [ "$1" = 'platform' ]; then
# Check CLI args for a -config option
for ARG in "$@";
do
case "$ARG" in
-config=*)
MM_CONFIG=${ARG#*=};;
esac
done
if [ ! -f "$MM_CONFIG" ]
then
# If there is no configuration file, create it with some default values
echo "No configuration file" "$MM_CONFIG"
echo "Creating a new one"
# Copy default configuration file
cp /config.json.save "$MM_CONFIG"
# Substitute some parameters with jq
config_set '.ServiceSettings.ListenAddress = ":80"' "$MM_CONFIG"
config_set '.LogSettings.EnableConsole = false' "$MM_CONFIG"
config_set '.LogSettings.ConsoleLevel = "INFO"' "$MM_CONFIG"
config_set '.FileSettings.Directory = "/mattermost/data/"' "$MM_CONFIG"
config_set '.FileSettings.EnablePublicLink = true' "$MM_CONFIG"
config_set ".FileSettings.PublicLinkSalt = \"$(generate_salt)\"" "$MM_CONFIG"
config_set '.EmailSettings.SendEmailNotifications = false' "$MM_CONFIG"
config_set '.EmailSettings.FeedbackEmail = ""' "$MM_CONFIG"
config_set '.EmailSettings.SMTPServer = ""' "$MM_CONFIG"
config_set '.EmailSettings.SMTPPort = ""' "$MM_CONFIG"
config_set ".EmailSettings.InviteSalt = \"$(generate_salt)\"" "$MM_CONFIG"
config_set ".EmailSettings.PasswordResetSalt = \"$(generate_salt)\"" "$MM_CONFIG"
config_set '.RateLimitSettings.Enable = true' "$MM_CONFIG"
config_set '.SqlSettings.DriverName = "postgres"' "$MM_CONFIG"
config_set ".SqlSettings.AtRestEncryptKey = \"$(generate_salt)\"" "$MM_CONFIG"
else
echo "Using existing config file" "$MM_CONFIG"
fi
# Configure database access
if [ -z "$MM_SQLSETTINGS_DATASOURCE" ]
then
echo -ne "Configure database connection..."
export MM_SQLSETTINGS_DATASOURCE="postgres://$MM_USERNAME:$MM_PASSWORD@$DB_HOST:$DB_PORT_NUMBER/$MM_DBNAME?sslmode=disable&connect_timeout=10"
echo OK
else
echo "Using existing database connection"
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
echo "Starting platform"
fi
exec "$@"