From 3207457a246b304271d3d68fc90af2bd3916ae1c Mon Sep 17 00:00:00 2001
From: Quentin Duchemin <quentinduchemin@tuta.io>
Date: Tue, 28 Jul 2020 15:15:25 +0200
Subject: [PATCH] [GraphBot] Initialize directory with Compose and credentials
 injection

---
 pica-graphbot/Dockerfile                   |  3 +
 pica-graphbot/README.md                    | 31 ++++++++++
 pica-graphbot/config/config.json           | 70 ++++++++++++++++++++++
 pica-graphbot/docker-compose.yml           | 27 +++++++++
 pica-graphbot/inject_env.sh                | 18 ++++++
 pica-graphbot/secrets/sftp.secrets.example |  2 +
 6 files changed, 151 insertions(+)
 create mode 100644 pica-graphbot/Dockerfile
 create mode 100644 pica-graphbot/README.md
 create mode 100644 pica-graphbot/config/config.json
 create mode 100644 pica-graphbot/docker-compose.yml
 create mode 100644 pica-graphbot/inject_env.sh
 create mode 100644 pica-graphbot/secrets/sftp.secrets.example

diff --git a/pica-graphbot/Dockerfile b/pica-graphbot/Dockerfile
new file mode 100644
index 00000000..95936c59
--- /dev/null
+++ b/pica-graphbot/Dockerfile
@@ -0,0 +1,3 @@
+FROM chosto/graphbot:v1.4
+
+CMD [ "/inject_env.sh" ]
diff --git a/pica-graphbot/README.md b/pica-graphbot/README.md
new file mode 100644
index 00000000..4b7fc23e
--- /dev/null
+++ b/pica-graphbot/README.md
@@ -0,0 +1,31 @@
+## GraphBot
+
+### Introduction
+
+Ce dossier contient les fichiers nécessaires pour lancer [Docker Graph Bot](https://gitlab.utc.fr/picasoft/projets/graph-bot) sur l'infrastructure de Picasoft. Voir le projet pour la documentation de référence.
+
+Le Dockerfile se base sur l'image officielle et rajoute un entrypoint permettant d'injecter les identifiants du serveur FTP au moment du lancement du conteneur, à partir de l'environnement. Sans cela, les identifiants seraient dans le fichier de configuration, ce qui empêche de le versionner.
+
+### Configuration
+
+La configuration se fait :
+* Dans le [docker-compose.yml](./docker-compose.yml) pour les variables d'environnement (niveau de log, dossier de configuration...)
+* Dans [config.json](./config/config.json) pour le comportement de DGB
+
+Pour le premier lancement, dans la mesure où DGB interroge plusieurs machines virtuelles, dont certaines à distance, il est nécessaire de créer un dossier `./config/auth` contenant les clés privées et certificats pour communiquer avec les socket Docker distants.
+
+Voir :
+* [La nomenclature des fichiers](https://gitlab.utc.fr/picasoft/projets/graph-bot#hosts)
+* [Le renouvellement des certificats pour se connecter à distance au socket Docker](https://wiki.picasoft.net/doku.php?id=technique:adminsys:docker:docker-socket-certs) si les certificats sont expirés ou qu'ils n'existent pas sur la machine de lancement.
+
+La configuration actuelle prévoit que DGB soit lancé sur la machine `monitoring`.
+
+### Secrets
+
+Au premier lancement, copier `./secrets/sftp.secrets.example` dans `./secrets/sftp.secrets` et remplacer par les valeurs de production.
+
+### Mise à jour
+
+Il suffit de changer le tag dans le [Dockerfile](./Dockerfile), de construire l'image, la pousser sur le registre de Picasoft et mettre à jour le Docker Compose.
+
+Vérifier le cas échéant les changements de configuration dans les [releases](https://gitlab.utc.fr/picasoft/projets/graph-bot/-/releases).
diff --git a/pica-graphbot/config/config.json b/pica-graphbot/config/config.json
new file mode 100644
index 00000000..00f9e697
--- /dev/null
+++ b/pica-graphbot/config/config.json
@@ -0,0 +1,70 @@
+{
+	"organization": "Picasoft",
+	"merge": false,
+	"hosts": [
+		{
+			"name": "pica01",
+			"url": "pica01.picasoft.net",
+			"port": 2376,
+			"exclude": [],
+			"tls_config":
+			{
+				"ca_cert": "auth/pica01/ca.pem",
+				"cert": "auth/pica01/cert.pem",
+				"key": "auth/pica01/key.pem"
+			}
+		},
+		{
+			"name": "pica02",
+			"url": "pica02.picasoft.net",
+			"port": 2376,
+			"exclude": [],
+			"tls_config":
+			{
+				"ca_cert": "auth/pica02/ca.pem",
+				"cert": "auth/pica02/cert.pem",
+				"key": "auth/pica02/key.pem"
+			}
+		},
+		{
+			"name": "pica01-test",
+			"url": "pica01-test.picasoft.net",
+			"port": 2376,
+			"exclude": [],
+			"tls_config":
+			{
+				"ca_cert": "auth/pica01-test/ca.pem",
+				"cert": "auth/pica01-test/cert.pem",
+				"key": "auth/pica01-test/key.pem"
+			}
+		},
+		{
+			"name": "monitoring",
+			"url": "localhost"
+		}
+	],
+  "color_scheme": {
+    "traefik": "#edb591",
+    "port": "#86c49b",
+    "link": "#75e9cd",
+    "image": "#e1efe6",
+    "container": "#ffffff",
+    "network": "#ffffff",
+    "volume": "#819cd9",
+    "bind_mount": "#b19cd9",
+    "host": "#c7ceea",
+    "dark_text": "#32384f",
+    "bright_text": "#ffffff"
+  },
+  "hide": ["binds"],
+	"actions": [
+		{
+			"type": "sftp",
+			"hostname": "uploads.picasoft.net",
+			"port": 2222,
+			"login": "pica",
+			"password": "SFTP_USER",
+			"remote_path": "SFTP_PASSWORD"
+		}
+	]
+}
diff --git a/pica-graphbot/docker-compose.yml b/pica-graphbot/docker-compose.yml
new file mode 100644
index 00000000..10e363b3
--- /dev/null
+++ b/pica-graphbot/docker-compose.yml
@@ -0,0 +1,27 @@
+version: "3.7"
+
+networks:
+  graphbot:
+
+volumes:
+  output:
+    name: graphbot
+
+services:
+  app:
+    image: registry.picasoft.net/graph-bot:v1.4
+    container_name: graph-bot
+    init: true
+    networks:
+      - graphbot
+    volumes:
+      - ./config:/config
+      - /var/run/docker.sock:/var/run/docker.sock
+      - output:/output
+    environment:
+      CONFIG_PATH: '/config'
+      OUTPUT_PATH: '/output'
+      CRON_CONFIG: '0 0 * * *'
+    env_file:
+      - ./secrets/sftp.secrets
+    restart: unless-stopped
diff --git a/pica-graphbot/inject_env.sh b/pica-graphbot/inject_env.sh
new file mode 100644
index 00000000..90baa392
--- /dev/null
+++ b/pica-graphbot/inject_env.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+if [ -z "${SFTP_USER}" ]; then
+  echo "SFTP_USER not set, exiting!"
+  exit 1
+else
+
+if [ -z "${SFTP_PASSWORD}" ]; then
+  echo "SFTP_PASSWORD not set, exiting!"
+  exit 1
+else
+
+echo "Inject SFTP credentials..."
+sed -i "s/SFTP_USER/$SFTP_USER/g" /entrypoint.sh
+sed -i "s/SFTP_PASSWORD/$SFTP_PASSWORD/g" /entrypoint.sh
+
+echo "Launching original entrypoint..."
+/entrypoint.sh
diff --git a/pica-graphbot/secrets/sftp.secrets.example b/pica-graphbot/secrets/sftp.secrets.example
new file mode 100644
index 00000000..6aa58bda
--- /dev/null
+++ b/pica-graphbot/secrets/sftp.secrets.example
@@ -0,0 +1,2 @@
+SFTP_USER=user
+SFTP_PASSWORD=password
-- 
GitLab