From 870c302492996f54cd29ca2ba94d0cb6c26fb68a Mon Sep 17 00:00:00 2001
From: Quentin Duchemin <quentinduchemin@tuta.io>
Date: Sat, 21 Dec 2019 20:52:15 +0100
Subject: [PATCH] Try to build Mattermost with CI

---
 .gitlab-ci.yml                                |  1 -
 docker-compose.yml                            | 45 +++++++++++++++++
 pica-mattermost/Dockerfile                    | 49 +++++++++++++++++++
 pica-mattermost/README.md                     | 25 ++++++++++
 pica-mattermost/entrypoint.sh                 | 33 +++++++++++++
 .../secrets/mattermost-db.secrets.example     |  6 +++
 6 files changed, 158 insertions(+), 1 deletion(-)
 create mode 100644 docker-compose.yml
 create mode 100644 pica-mattermost/Dockerfile
 create mode 100644 pica-mattermost/README.md
 create mode 100644 pica-mattermost/entrypoint.sh
 create mode 100644 pica-mattermost/secrets/mattermost-db.secrets.example

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c0dfde81..a537fb8f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -85,7 +85,6 @@ build:
     refs:
       - master
       - dev-ci
-      - dev-sympa
 
 # Run CoreOS' Clair and make the CI failed if a critical vulnerability isn't in the whitelist
 clair:
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 00000000..70c35369
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,45 @@
+version : "2.4"
+  networks:
+    docker_default:
+      external: true
+      name: "docker_default"
+
+volumes:
+  mattermost-config:
+    external: true
+  mattermost-data:
+    external: true
+  mattermost-plugins:
+    external: true
+  mattermost-db:
+    external: true
+
+services:
+  mattermost:
+    image: registry.picasoft.net/pica-mattermost:5.18.0
+    container_name: mattermost-app
+    links:
+      - mattermost-db:mattermost-db
+    volumes:
+      - mattermost-config:/mattermost/config
+      - mattermost-data:/mattermost/data
+      - mattermost-plugins:/mattermost/plugins
+      - /etc/localtime:/etc/localtime:ro
+    environment:
+      - DB_HOST=mattermost-db
+    env_file: ./secrets/mattermost-db.secrets
+    labels:
+      - "traefik.frontend.rule=Host:team.picasoft.net"
+      - "traefik.port=8000"
+      - "traefik.frontend.passHostHeader=true"
+      - "traefik.enable=true"
+    restart: unless-stopped
+
+  mattermost-db:
+    image: postgres:9.4-alpine
+    container_name: mattermost-db
+    volumes:
+      - mattermost-db:/var/lib/postgresql/data
+      - /etc/localtime:/etc/localtime:ro
+    env_file: ./secrets/mattermost-db.secrets
+    restart: unless-stopped
diff --git a/pica-mattermost/Dockerfile b/pica-mattermost/Dockerfile
new file mode 100644
index 00000000..efac727c
--- /dev/null
+++ b/pica-mattermost/Dockerfile
@@ -0,0 +1,49 @@
+FROM alpine:3.10
+
+# Some ENV variables
+ENV PATH="/mattermost/bin:${PATH}"
+
+# Picasoft : change these values. The team version is used by default.
+ENV MM_VERSION=5.18.0
+# Get these ids with the output of `id mattermost` on the VM hosting the app
+ARG PUID=5000
+ARG PGID=5000
+
+# Install some needed packages
+RUN apk add --no-cache \
+	ca-certificates \
+	curl \
+	jq \
+	libc6-compat \
+	libffi-dev \
+  libcap \
+	linux-headers \
+	mailcap \
+	netcat-openbsd \
+	xmlsec-dev \
+	tzdata \
+	&& rm -rf /tmp/*
+
+# Get Mattermost
+RUN mkdir -p /mattermost/data /mattermost/plugins /mattermost/client/plugins
+    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 \
+    && rm -rf /mattermost/config/config.json \
+    && addgroup -g ${PGID} mattermost \
+    && adduser -D -u ${PUID} -G mattermost -h /mattermost -D mattermost \
+    && chown -R mattermost:mattermost /mattermost /config.json.save /mattermost/plugins /mattermost/client/plugins \
+    && setcap cap_net_bind_service=+ep /mattermost/bin/mattermost
+
+USER mattermost
+
+#Healthcheck to make sure container is ready
+HEALTHCHECK CMD curl --fail http://localhost:8000 || exit 1
+
+# Configure entrypoint and command
+COPY entrypoint.sh /
+ENTRYPOINT ["/entrypoint.sh"]
+WORKDIR /mattermost
+CMD ["mattermost"]
+
+# Expose port 8000 of the container
+EXPOSE 8000
diff --git a/pica-mattermost/README.md b/pica-mattermost/README.md
new file mode 100644
index 00000000..2410ce09
--- /dev/null
+++ b/pica-mattermost/README.md
@@ -0,0 +1,25 @@
+## Mattermost
+
+### Introduction
+
+Ce dossier contient une adaptation minimaliste du [Dockerfile officiel](https://github.com/mattermost/mattermost-docker) de Mattermost.
+
+L'idée de garder une copie du Dockerfile sur ce dépôt est motivée par trois choses :
+* Aucune image n'est disponible **officiellement** sur le Docker Hub, même s'il en existe
+* En cas de problèmes de sécurité (CVE), on pourra directement agir dessus
+* On peut changer les arguments du Dockerfile, comme le type d'instance (`team`) et l'UID de l'utilisateur (à retrouver sur le LDAP).
+
+Aussi, on n'utilise pas le système de sauvegarde `WAL-e`, ce qui nous permet d'utiliser une image `postgres` de base plutôt que de rajouter la couche proposée par l'équipe Mattermost.
+
+Enfin, le Docker Compose est adapté à notre configuration.
+
+### Utilisation
+
+Copier `secrets/mattermost-db.secrets.example` dans `secrets/mattermost-db.secrets` et remplacer les valeurs des identifiants.
+Lancer `docker-compose up -d`. On notera l'utilisation de la variable `DB_HOST` dans le fichier Docker Compose, qui ne devrait pas avoir à être modifiée si on ne touche pas le nom du service de base de données.
+
+### Procédure de mise à jour
+
+Il suffit de changer l'argument correspondant à la version dans le `Dockerfile` ainsi que le tag d'image dans le `docker-compose.yml`. Régulièrement, on vérifiera l'upstream pour s'assurer qu'il n'y a pas de changements majeurs, auxquel cas on les intègrera dans le `Dockerfile` local.
+
+Ce n'est pas le plus pratique, mais ni la CI ni Docker ne permet de reprendre un `Dockerfile` distant et d'y intégrer des modifications.
diff --git a/pica-mattermost/entrypoint.sh b/pica-mattermost/entrypoint.sh
new file mode 100644
index 00000000..93f2d5ab
--- /dev/null
+++ b/pica-mattermost/entrypoint.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+# Read environment variables or set default values
+DB_HOST=${DB_HOST:-db}
+DB_PORT_NUMBER=${DB_PORT_NUMBER:-5432}
+MM_DBNAME=${MM_DBNAME:-mattermost}
+MM_CONFIG=${MM_CONFIG:-/mattermost/config/config.json}
+
+if [ "${1:0:1}" = '-' ]; then
+    set -- mattermost "$@"
+fi
+
+if [ "$1" = 'mattermost' ]; then
+  # 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
+
+  echo "Starting mattermost"
+fi
+
+exec "$@"
diff --git a/pica-mattermost/secrets/mattermost-db.secrets.example b/pica-mattermost/secrets/mattermost-db.secrets.example
new file mode 100644
index 00000000..751ae5a1
--- /dev/null
+++ b/pica-mattermost/secrets/mattermost-db.secrets.example
@@ -0,0 +1,6 @@
+MM_USERNAME=mattermost
+MM_PASSWORD=test
+MM_DBNAME=mattermost
+POSTGRES_USER=mattermost
+POSTGRES_PASSWORD=test
+POSTGRES_DB=mattermost
-- 
GitLab