revised approach

This commit is contained in:
rskntroot
2025-06-14 09:50:15 +00:00
parent 12941cd2c5
commit cc32c6010e
27 changed files with 1092 additions and 133 deletions

88
mattermost/.env Normal file
View File

@@ -0,0 +1,88 @@
# Domain of service
DOMAIN=chat.rskio.com
# Container settings
## Timezone inside the containers. The value needs to be in the form 'Europe/Berlin'.
## A list of these tz database names can be looked up at Wikipedia
## https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TZ=US/Mountain
RESTART_POLICY=unless-stopped
# Postgres settings
## Documentation for this image and available settings can be found on hub.docker.com
## https://hub.docker.com/_/postgres
## Please keep in mind this will create a superuser and it's recommended to use a less privileged
## user to connect to the database.
## A guide on how to change the database user to a nonsuperuser can be found in docs/creation-of-nonsuperuser.md
POSTGRES_IMAGE_TAG=13-alpine
POSTGRES_DATA_PATH=./volumes/db/var/lib/postgresql/data
POSTGRES_USER=mattermost
POSTGRES_PASSWORD=kixvep-sasWaq-gocwy3
POSTGRES_DB=mattermost
# Nginx
## The nginx container will use a configuration found at the NGINX_MATTERMOST_CONFIG. The config aims
## to be secure and uses a catch-all server vhost which will work out-of-the-box. For additional settings
## or changes ones can edit it or provide another config. Important note: inside the container, nginx sources
## every config file inside */etc/nginx/conf.d* ending with a *.conf* file extension.
## Inside the container the uid and gid is 101. The folder owner can be set with
## `sudo chown -R 101:101 ./nginx` if needed.
## Note that this repository requires nginx version 1.25.1 or later
NGINX_IMAGE_TAG=alpine
## The folder containing server blocks and any additional config to nginx.conf
#NGINX_CONFIG_PATH=./nginx/conf.d
#NGINX_DHPARAMS_FILE=./nginx/dhparams4096.pem
#CERT_PATH=./volumes/web/cert/cert.pem
#KEY_PATH=./volumes/web/cert/key-no-password.pem
#GITLAB_PKI_CHAIN_PATH=<path_to_your_gitlab_pki>/pki_chain.pem
#CERT_PATH=./certs/etc/letsencrypt/live/${DOMAIN}/fullchain.pem
#KEY_PATH=./certs/etc/letsencrypt/live/${DOMAIN}/privkey.pem
## Exposed ports to the host. Inside the container 80, 443 and 8443 will be used
#HTTPS_PORT=443
#HTTP_PORT=80
#CALLS_PORT=8443
# Mattermost settings
## Inside the container the uid and gid is 2000. The folder owner can be set with
## `sudo chown -R 2000:2000 ./volumes/app/mattermost`.
MATTERMOST_CONFIG_PATH=./volumes/app/mattermost/config
MATTERMOST_DATA_PATH=./volumes/app/mattermost/data
MATTERMOST_LOGS_PATH=./volumes/app/mattermost/logs
MATTERMOST_PLUGINS_PATH=./volumes/app/mattermost/plugins
MATTERMOST_CLIENT_PLUGINS_PATH=./volumes/app/mattermost/client/plugins
MATTERMOST_BLEVE_INDEXES_PATH=./volumes/app/mattermost/bleve-indexes
## Bleve index (inside the container)
MM_BLEVESETTINGS_INDEXDIR=/mattermost/bleve-indexes
## This will be 'mattermost-enterprise-edition' or 'mattermost-team-edition' based on the version of Mattermost you're installing.
MATTERMOST_IMAGE=mattermost-enterprise-edition
## Update the image tag if you want to upgrade your Mattermost version. You may also upgrade to the latest one. The example is based on the latest Mattermost ESR version.
MATTERMOST_IMAGE_TAG=9.11.6
## Make Mattermost container readonly. This interferes with the regeneration of root.html inside the container. Only use
## it if you know what you're doing.
## See https://github.com/mattermost/docker/issues/18
MATTERMOST_CONTAINER_READONLY=false
## The app port is only relevant for using Mattermost without the nginx container as reverse proxy. This is not meant
## to be used with the internal HTTP server exposed but rather in case one wants to host several services on one host
## or for using it behind another existing reverse proxy.
APP_PORT=8065
## Configuration settings for Mattermost. Documentation on the variables and the settings itself can be found at
## https://docs.mattermost.com/administration/config-settings.html
## Keep in mind that variables set here will take precedence over the same setting in config.json. This includes
## the system console as well and settings set with env variables will be greyed out.
## Below one can find necessary settings to spin up the Mattermost container
MM_SQLSETTINGS_DRIVERNAME=postgres
MM_SQLSETTINGS_DATASOURCE=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable&connect_timeout=10
## Example settings (any additional setting added here also needs to be introduced in the docker-compose.yml)
MM_SERVICESETTINGS_SITEURL=https://${DOMAIN}

62
mattermost/compose.yml Normal file
View File

@@ -0,0 +1,62 @@
services:
postgres:
image: postgres:${POSTGRES_IMAGE_TAG}
restart: ${RESTART_POLICY}
#security_opt:
# - no-new-privileges:true
#pids_limit: 100
#read_only: true
tmpfs:
- /tmp
- /var/run/postgresql
volumes:
- ${POSTGRES_DATA_PATH}:/var/lib/postgresql/data
environment:
- TZ
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
networks:
- default
service:
depends_on:
- postgres
image: mattermost/${MATTERMOST_IMAGE}:${MATTERMOST_IMAGE_TAG}
labels:
- traefik.enable=true
- traefik.http.routers.mattermost.entrypoints=websecure
- traefik.http.routers.mattermost.rule=Host(`chat.rskio.com`)
- traefik.http.routers.mattermost.tls=true
- traefik.http.routers.mattermost.tls.options=external@file
- traefik.http.routers.mattermost.tls.certresolver=rskio_certresolver
- traefik.http.routers.mattermost.service=mattermost@docker
- traefik.http.services.mattermost.loadbalancer.server.port=8065
- traefik.http.routers.mattermost.middlewares=secureHeaders@file
restart: ${RESTART_POLICY}
#security_opt:
# - no-new-privileges:true
#pids_limit: 200
#read_only: ${MATTERMOST_CONTAINER_READONLY}
tmpfs:
- /tmp
volumes:
- ${MATTERMOST_CONFIG_PATH}:/mattermost/config:rw
- ${MATTERMOST_DATA_PATH}:/mattermost/data:rw
- ${MATTERMOST_LOGS_PATH}:/mattermost/logs:rw
- ${MATTERMOST_PLUGINS_PATH}:/mattermost/plugins:rw
- ${MATTERMOST_CLIENT_PLUGINS_PATH}:/mattermost/client/plugins:rw
- ${MATTERMOST_BLEVE_INDEXES_PATH}:/mattermost/bleve-indexes:rw
environment:
- TZ
- MM_SQLSETTINGS_DRIVERNAME
- MM_SQLSETTINGS_DATASOURCE
- MM_BLEVESETTINGS_INDEXDIR
- MM_SERVICESETTINGS_SITEURL
networks:
- default
- traefik
networks:
traefik:
external: true

View File

@@ -0,0 +1,35 @@
# IMPORTANT: Please make sure you have enough disk space available for the backups!
Because it is more complicated to check the available disk space for various disk formatting options provided by different linux distributions, the script does currently not check for if there is enough disk space.
Please check manually before executing this script!
## Upgrading Postgres
```
$ export PATH_TO_MATTERMOST_DOCKER=path/to/mattermost-docker
$ ./scripts/upgrade-postgres.sh
```
Environment variables for upgrading:
`ttf` means, the script 'tries to find' the environment variables.
| Name | Description | Type | Default | Required |
|------|-------------|------|:---------:|:--------:|
| PATH_TO_MATTERMOST_DOCKER | absolute path to your mattermost-docker folder | `string` | n/a | yes |
| POSTGRES_USER | postgres user to connect to the mattermost database | `string` | ttf | yes |
| POSTGRES_PASSWORD | postgres password for the POSTGRES_USER to connect to the mattermost database | `string` | ttf | yes |
| POSTGRES_DB | postgres database name for the mattermost database | `string` | ttf | yes |
| POSTGRES_OLD_VERSION | postgres database old version which should be upgraded from | `semver` | ttf | yes |
| POSTGRES_NEW_VERSION | postgres database new version which should be upgraded to | `semver` | 13 | yes |
| POSTGRES_DOCKER_TAG | postgres docker tag found [here](https://hub.docker.com/_/postgres) including python3-dev | `string` | 13.2-alpine | yes |
| POSTGRES_OLD_DOCKER_FROM | FROM declaration in the postgres Dockerfile to be replaced | `string` | ttf | yes |
| POSTGRES_NEW_DOCKER_FROM | FROM declaration in the postgres Dockerfile replacing POSTGRES_OLD_DOCKER_FROM | `string` | ttf | yes |
| POSTGRES_UPGRADE_LINE | folder name required to upgrade postgres (Needs to match a folder [here](https://github.com/tianon/docker-postgres-upgrade)) | `string` | ttf | yes |
| MM_OLD_VERSION | mattermost old version which should be upgraded from | `semver` | ttf | yes |
| MM_NEW_VERSION | mattermost new version which should be upgraded to | `semver` | 5.32.1 | yes |
You can overwrite any of these variables before running this script with:
```
$ export VAR_NAME_FROM_ABOVE=yourValue
$ export PATH_TO_MATTERMOST_DOCKER=path/to/mattermost-docker
$ ./scripts/upgrade-postgres.sh
```

View File

@@ -0,0 +1,76 @@
#!/bin/bash
usage() {
cat <<EOF
Usage: $0 [-h] <-d DOMAIN> <-o PATH>
Options
-h Print this help
-o Output path (e.g. \${PWD}/certs)
-d Domain certificate is issued for (e.g. mm.example.com)
EOF
}
issue_cert_standalone() {
docker run -it --rm --name certbot -p 80:80 \
-v "${1}/etc/letsencrypt:/etc/letsencrypt" \
-v "${1}/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certonly --standalone -d "${2}"
}
authenticator_to_webroot() {
sed -i 's/standalone/webroot/' "${1}"/etc/letsencrypt/renewal/"${2}".conf
tee -a "${1}"/etc/letsencrypt/renewal/"${2}".conf >/dev/null <<EOF
webroot_path = /usr/share/nginx/html,
[[webroot_map]]
EOF
}
# become root (keeping environment) and make script executable
if [ $EUID != 0 ]; then
chmod +x "$0"
sudo -E ./"$0" "$@"
exit $?
fi
while getopts d:o:h opt; do
case "$opt" in
d)
domain=$OPTARG
;;
o)
output=$OPTARG
;;
h)
usage
exit 0
;;
\?)
usage >&2
exit 64
;;
esac
done
shift $((OPTIND - 1))
if [ -z "$domain" ]; then
echo "-d is required" >&2
usage >&2
exit 64
fi
if [ -z "$output" ]; then
echo "-o is required" >&2
usage >&2
exit 64
fi
if ! which docker 1>/dev/null; then
echo "Can't find Docker command" >&2
exit 64
fi
issue_cert_standalone "${output}" "${domain}"
authenticator_to_webroot "${output}" "${domain}"

View File

@@ -0,0 +1,196 @@
#!/usr/bin/env bash
set -o errexit
##
## Instructions
##
# Dockerfile stolen from contributions in this issue: https://github.com/mattermost/mattermost-docker/issues/489#issuecomment-790277661
# 1. Edit the variables below to match your environment. This uses default variables and assumes you're on 5.31.0.
# If you're wanting to use another version of Postgres/Mattermost , update the variables as desired.
# 2. run 'sudo bash upgrade-postgres.sh' replace upgrade.sh with what you've named the file.
# This may take some time to complete as it's migrating the database to Postgres 13.6 from 9.4
if [[ $PATH_TO_MATTERMOST_DOCKER == "" ]]; then
# shellcheck disable=SC2016
echo 'Please export environment variable PATH_TO_MATTERMOST_DOCKER with "$ export PATH_TO_MATTERMOST_DOCKER=/path/to/mattermost-docker", i.e. $PWD before running this script. '
exit 1
fi
##
## Environment Variables
##
# Below are default values in the mattermost-docker container.
# The script is trying to fetch those variables first. Should fetching fail, please export the variables before running the script.
if [[ $POSTGRES_USER == "" ]]; then
echo "trying to fetch POSTGRES_USER from $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
POSTGRES_USER=$(grep "^.*-.*POSTGRES_USER=.*$" "$PATH_TO_MATTERMOST_DOCKER"/docker-compose.yml | sed s~^.*-.*POSTGRES_USER=~~g)
if [[ $POSTGRES_USER == "" ]]; then
echo "could not find POSTGRES_USER set in $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
echo "please run 'export POSTGRES_USER=yourPostgresUser' before running this script"
exit 1
fi
echo "found POSTGRES_USER=redacted"
fi
if [[ $POSTGRES_PASSWORD == "" ]]; then
echo "trying to fetch POSTGRES_PASSWORD from $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
POSTGRES_PASSWORD=$(grep "^.*-.*POSTGRES_PASSWORD=.*$" "$PATH_TO_MATTERMOST_DOCKER"/docker-compose.yml | sed s~^.*-.*POSTGRES_PASSWORD=~~g)
if [[ $POSTGRES_PASSWORD == "" ]]; then
echo "could not find POSTGRES_PASSWORD set in $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
echo "please run 'export POSTGRES_PASSWORD=yourPostgresPassword' before running this script"
exit 1
fi
echo "found POSTGRES_PASSWORD=redacted"
fi
if [[ $POSTGRES_DB == "" ]]; then
echo "trying to fetch POSTGRES_DB from $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
POSTGRES_DB=$(grep "^.*-.*POSTGRES_DB=.*$" "$PATH_TO_MATTERMOST_DOCKER"/docker-compose.yml | sed s~^.*-.*POSTGRES_DB=~~g)
if [[ $POSTGRES_DB == "" ]]; then
echo "could not find POSTGRES_DB set in $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
echo "please run 'export POSTGRES_DB=yourPostgresDatabase' before running this script"
exit 1
fi
echo "found POSTGRES_DB=$POSTGRES_DB"
fi
printf "\n"
if [[ $POSTGRES_OLD_VERSION == "" ]]; then
echo "trying to fetch POSTGRES_OLD_VERSION by connecting to database container and echoing the environment variable PG_VERSION"
POSTGRES_OLD_VERSION=$(docker exec mattermost-docker_db_1 bash -c 'echo $PG_VERSION') # i.e. 9.4
if [[ $POSTGRES_OLD_VERSION == "" ]]; then
echo "could not connect to database container to get PG_VERSION"
echo "please run 'export POSTGRES_OLD_VERSION=i.e. 9.4' before running this script"
echo "check by i.e. running 'sudo cat $PATH_TO_MATTERMOST_DOCKER/volumes/db/var/lib/postgresql/data/PG_VERSION'"
exit 1
fi
echo "found POSTGRES_OLD_VERSION=$POSTGRES_OLD_VERSION"
fi
if [[ $POSTGRES_NEW_VERSION == "" ]]; then
echo "no exported POSTGRES_NEW_VERSION environment variable found"
echo "setting POSTGRES_NEW_VERSION environment variable to default 13"
POSTGRES_NEW_VERSION=13 # i.e. 13
echo "set POSTGRES_NEW_VERSION=$POSTGRES_NEW_VERSION"
fi
if [[ $POSTGRES_DOCKER_TAG == "" ]]; then
echo "no exported POSTGRES_DOCKER_TAG environment variable found"
echo "setting POSTGRES_DOCKER_TAG environment variable to default 13.2-alpine"
echo "tag needs to be an alpine release to include python3-dev found here - https://hub.docker.com/_/postgres"
POSTGRES_DOCKER_TAG=13.2-alpine # i.e. '13.2-alpine'
echo "set POSTGRES_DOCKER_TAG=$POSTGRES_DOCKER_TAG"
fi
if [[ $POSTGRES_OLD_DOCKER_FROM == "" ]]; then
echo "no exported POSTGRES_OLD_DOCKER_FROM environment variable found"
echo "setting POSTGRES_OLD_DOCKER_FROM to default '$(grep 'FROM postgres' "$PATH_TO_MATTERMOST_DOCKER"/db/Dockerfile)'"
POSTGRES_OLD_DOCKER_FROM=$(grep 'FROM postgres' "$PATH_TO_MATTERMOST_DOCKER/db/Dockerfile")
echo "set POSTGRES_OLD_DOCKER_FROM=$POSTGRES_OLD_DOCKER_FROM"
fi
if [[ $POSTGRES_NEW_DOCKER_FROM == "" ]]; then
echo "no exported POSTGRES_NEW_DOCKER_FROM environment variable found"
echo "setting POSTGRES_NEW_DOCKER_FROM to default 'FROM postgres:$POSTGRES_DOCKER_TAG'"
POSTGRES_NEW_DOCKER_FROM="FROM postgres:$POSTGRES_DOCKER_TAG"
echo "set POSTGRES_NEW_DOCKER_FROM=$POSTGRES_NEW_DOCKER_FROM"
fi
if [[ $POSTGRES_UPGRADE_LINE == "" ]]; then
echo "no exported POSTGRES_UPGRADE_LINE environment variable found"
echo "setting POSTGRES_UPGRADE_LINE to default $POSTGRES_OLD_VERSION-to-$POSTGRES_POSTGRES_NEW_VERSION"
echo "the POSTGRES_UPGRADE_LINE needs to match a folder found here - https://github.com/tianon/docker-postgres-upgrade"
echo "it should read 'old-to-new'"
POSTGRES_UPGRADE_LINE=$POSTGRES_OLD_VERSION-to-$POSTGRES_NEW_VERSION # i.e. '9.4-to-13'
echo "set POSTGRES_UPGRADE_LINE=$POSTGRES_UPGRADE_LINE"
fi
printf "\n"
if [[ $MM_OLD_VERSION == "" ]]; then
echo "trying to fetch MM_OLD_VERSION from $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
MM_OLD_VERSION=$(grep ".*-.*MM_VERSION=.*" "$PATH_TO_MATTERMOST_DOCKER"/docker-compose.yml | sed s~.*-.*MM_VERSION=~~g)
if [[ $MM_OLD_VERSION == "" ]]; then
echo "could not find MM_OLD_VERSION set in $PATH_TO_MATTERMOST_DOCKER/docker-compose.yml"
echo "please run 'export MM_OLD_VERSION=yourMMVersion' before running this script"
exit 1
fi
echo "found MM_OLD_VERSION=$MM_OLD_VERSION"
fi
if [[ $MM_NEW_VERSION == "" ]]; then
echo "no exported MM_NEW_VERSION environment variable found"
echo "setting MM_NEW_VERSION to default 5.32.1"
MM_NEW_VERSION=5.32.1
echo "found MM_NEW_VERSION=$MM_NEW_VERSION"
fi
printf "\n"
echo "Path to mattermost-docker: $PATH_TO_MATTERMOST_DOCKER"
echo "Postgres user: redacted"
echo "Postgres password: redacted"
echo "Postgres database name: $POSTGRES_DB"
echo "Postgres old version: $POSTGRES_OLD_VERSION"
echo "Postgres new version: $POSTGRES_NEW_VERSION"
echo "Postgres alpine docker tag including python3-dev: $POSTGRES_DOCKER_TAG"
echo "Postgres old Dockerfile: $POSTGRES_OLD_DOCKER_FROM"
echo "Postgres new Dockerfile: $POSTGRES_NEW_DOCKER_FROM"
echo "Postgres upgrade-line matches a folder here - https://github.com/tianon/docker-postgres-upgrade: $POSTGRES_UPGRADE_LINE"
echo "Mattermost old version: $MM_OLD_VERSION"
echo "Mattermost new version: $MM_NEW_VERSION"
printf "\n"
df -h
read -rp "Please make sure you have enough disk space left on your devices. Try to backup and upgrade now? (y/n)" choice
if [[ "$choice" != "y" && "$choice" != "Y" && "$choice" != "yes" ]]; then
exit 0;
fi
##
## Script Start
##
cd "$PATH_TO_MATTERMOST_DOCKER"
docker-compose stop
# Creating a backup folder and backing up the mattermost / database.
mkdir "$PATH_TO_MATTERMOST_DOCKER"/backups
DATE=$(date +'%F-%H-%M')
cp -ra "$PATH_TO_MATTERMOST_DOCKER"/volumes/app/mattermost/ "$PATH_TO_MATTERMOST_DOCKER"/backups/mattermost-backup-"$DATE"/
cp -ra "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/ "$PATH_TO_MATTERMOST_DOCKER"/backups/database-backup-"$DATE"/
mkdir "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/"$POSTGRES_OLD_VERSION"
mv "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/var/lib/postgresql/data/ "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/"$POSTGRES_OLD_VERSION"
rm -rf "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/var
mkdir -p "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/$POSTGRES_NEW_VERSION/data
sed -i "s/$POSTGRES_OLD_DOCKER_FROM/$POSTGRES_NEW_DOCKER_FROM/" "$PATH_TO_MATTERMOST_DOCKER"/db/Dockerfile
sed -i "s/python-dev/python3-dev/" "$PATH_TO_MATTERMOST_DOCKER"/db/Dockerfile
sed -i "s/$MM_OLD_VERSION/$MM_NEW_VERSION/" "$PATH_TO_MATTERMOST_DOCKER"/app/Dockerfile
# replacing the old postgres path with a new path
sed -i "s#./volumes/db/var/lib/postgresql/data:/var/lib/postgresql/data#./volumes/db/$POSTGRES_NEW_VERSION/data:/var/lib/postgresql/data#" "$PATH_TO_MATTERMOST_DOCKER"/docker-compose.yml
# migrate the database to the new postgres version
docker run --rm \
-e PGUSER="$POSTGRES_USER" \
-e POSTGRES_INITDB_ARGS=" -U $POSTGRES_USER" \
-e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
-e POSTGRES_DB="$POSTGRES_DB" \
-v "$PATH_TO_MATTERMOST_DOCKER"/volumes/db:/var/lib/postgresql \
tianon/postgres-upgrade:"$POSTGRES_UPGRADE_LINE" \
--link
cp -p "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/"$POSTGRES_OLD_VERSION"/data/pg_hba.conf "$PATH_TO_MATTERMOST_DOCKER"/volumes/db/$POSTGRES_NEW_VERSION/data/
# rebuild the containers
docker-compose build
docker-compose up -d
# reindex the database
echo "REINDEX SCHEMA CONCURRENTLY public;" | docker exec mattermost-docker_db_1 psql -U "$POSTGRES_USER" "$POSTGRES_DB"
cd -