2018-02-09 00:55:45 +01:00
|
|
|
#!/bin/sh
|
2017-02-23 06:37:43 +01:00
|
|
|
|
2017-10-14 16:47:44 +02:00
|
|
|
# Function to generate a random salt
|
2017-06-10 23:01:53 +02:00
|
|
|
generate_salt() {
|
2018-10-27 17:13:32 +02:00
|
|
|
tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 48 | head -n 1
|
2017-06-10 23:01:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 16:47:44 +02:00
|
|
|
# Read environment variables or set default values
|
2016-04-16 06:04:33 +02:00
|
|
|
DB_HOST=${DB_HOST:-db}
|
2017-06-07 22:48:41 +02:00
|
|
|
DB_PORT_NUMBER=${DB_PORT_NUMBER:-5432}
|
2016-04-16 06:04:33 +02:00
|
|
|
MM_DBNAME=${MM_DBNAME:-mattermost}
|
2017-06-29 14:11:45 +02:00
|
|
|
MM_CONFIG=${MM_CONFIG:-/mattermost/config/config.json}
|
2017-02-23 06:37:43 +01:00
|
|
|
|
|
|
|
if [ "${1:0:1}" = '-' ]; then
|
2018-06-16 15:42:52 +02:00
|
|
|
set -- mattermost "$@"
|
2015-12-04 09:58:43 +01:00
|
|
|
fi
|
2015-12-09 09:17:25 +01:00
|
|
|
|
2018-06-16 15:42:52 +02:00
|
|
|
if [ "$1" = 'mattermost' ]; then
|
2017-10-14 16:47:44 +02:00
|
|
|
# Check CLI args for a -config option
|
|
|
|
for ARG in $@;
|
|
|
|
do
|
|
|
|
case "$ARG" in
|
|
|
|
-config=*)
|
|
|
|
MM_CONFIG=${ARG#*=};;
|
|
|
|
esac
|
|
|
|
done
|
2017-02-23 06:37:43 +01:00
|
|
|
|
2017-10-14 16:47:44 +02:00
|
|
|
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
|
|
|
|
# Substitue some parameters with jq
|
2018-04-15 19:33:49 +02:00
|
|
|
jq '.ServiceSettings.ListenAddress = ":8000"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
2017-10-14 16:47:44 +02:00
|
|
|
jq '.LogSettings.EnableConsole = false' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.LogSettings.ConsoleLevel = "INFO"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.FileSettings.Directory = "/mattermost/data/"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.FileSettings.EnablePublicLink = true' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.FileSettings.PublicLinkSalt = "'$(generate_salt)'"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.SendEmailNotifications = false' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.FeedbackEmail = ""' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.SMTPServer = ""' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.SMTPPort = ""' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.InviteSalt = "'$(generate_salt)'"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.EmailSettings.PasswordResetSalt = "'$(generate_salt)'"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
jq '.RateLimitSettings.Enable = true' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
|
|
|
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
|
2018-08-22 14:47:58 +02:00
|
|
|
jq '.PluginSettings.Directory = "/mattermost/plugins/"' $MM_CONFIG > $MM_CONFIG.tmp && mv $MM_CONFIG.tmp $MM_CONFIG
|
2017-10-14 16:47:44 +02:00
|
|
|
else
|
|
|
|
echo "Using existing config file" $MM_CONFIG
|
|
|
|
fi
|
2017-06-08 22:02:53 +02:00
|
|
|
|
2018-10-29 13:57:59 +01:00
|
|
|
# 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
|
|
|
|
|
2017-10-14 16:47:44 +02:00
|
|
|
# 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
|
2015-12-09 09:17:25 +01:00
|
|
|
|
2018-06-16 15:42:52 +02:00
|
|
|
echo "Starting mattermost"
|
2017-02-23 06:37:43 +01:00
|
|
|
fi
|
2016-01-29 07:36:19 +01:00
|
|
|
|
2017-02-23 06:37:43 +01:00
|
|
|
exec "$@"
|