Handle database configuration

This commit is contained in:
Glenn Y. Rolland 2022-08-18 10:12:34 +02:00
parent 69d0981ff8
commit adc1dd1c74
3 changed files with 173 additions and 5 deletions

View file

@ -1,5 +1,6 @@
FROM ubuntu:16.04
MAINTAINER Glenn Y. Rolland <glenux@glenux.net>
FROM debian:bullseye
LABEL org.opencontainers.image.authors="Glenn Y. Rolland <glenux@glenux.net>"
LABEL net.glenux.image.version="4.4.1"
# Some ENV variables
ENV PATH="/mattermost/bin:${PATH}"
@ -8,17 +9,21 @@ ENV MM_VERSION=4.4.1
# Install some needed packages
RUN apt-get update \
&& apt-get -y --no-install-recommends install \
curl jq netcat ca-certificates \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin
curl jq netcat ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
/var/cache/apt/archives/*.deb \
/var/cache/apt/archives/partial/*.deb \
/var/cache/apt/*.bin
# Get Mattermost
RUN mkdir -p /mattermost/data \
&& curl https://releases.mattermost.com/$MM_VERSION/mattermost-team-$MM_VERSION-linux-amd64.tar.gz | tar -xvz \
; cp /mattermost/config/config.json /config.json.save \
&& cp /mattermost/config/config.json /config.json.save \
&& rm -rf /mattermost/config/config.json
# Configure entrypoint and command
COPY entrypoint.sh /
COPY parseurl.py /
ENTRYPOINT ["/entrypoint.sh"]
WORKDIR /mattermost/bin
CMD ["platform"]

100
entrypoint.sh Normal file
View file

@ -0,0 +1,100 @@
#!/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 "$@"

63
parseurl.py Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
import sys
import os
from urlparse import urlparse
prefix = sys.argv[1]
# print os.environ.keys
uri_str = os.environ['DATABASE_URL']
uri = urlparse(uri_str)
db_netloc = uri.netloc
db_credential = None
db_username = None
db_password = None
db_host = None
db_port = None
if db_netloc.find("@") >= 0 :
tmp_arr1 = db_netloc.split('@')
db_credential = tmp_arr1[0]
db_host = tmp_arr1[1]
if db_credential and db_credential.find(':') >= 0 :
tmp_arr2 = db_credential.split(':')
db_username = tmp_arr2[0]
db_password = tmp_arr2[1]
if db_host and db_host.find(':') >= 0 :
tmp_arr3 = db_host.split(':')
db_host = tmp_arr3[0]
db_port = tmp_arr3[1]
db_path = uri.path
db_scheme = uri.scheme
db_query = uri.query
db_fragment = uri.fragment
if db_scheme :
print(prefix + 'SCHEME=' + db_scheme)
if db_username :
print(prefix + 'USERNAME=' + db_username)
if db_password :
print(prefix + 'PASSWORD=' + db_password)
if db_host :
print(prefix + 'HOST=' + db_host)
if db_port :
print(prefix + 'PORT=' + db_port)
if db_path :
print(prefix + 'NAME=' + db_path[1:])
if db_query :
print(prefix + 'QUERY=' + db_query)
if db_fragment :
print(prefix + 'FRAGMENT=' + db_fragment)