From 79692db45dfe2fec6d0fec280bfba2be740d58a5 Mon Sep 17 00:00:00 2001
From: Paul van Tilburg <paul@luon.net>
Date: Sun, 31 May 2020 22:49:07 +0200
Subject: [PATCH] First version of cargo-deb packaging setup

---
 Cargo.toml                    | 22 ++++++++++++++++
 debian/config                 | 23 +++++++++++++++++
 debian/env                    | 48 +++++++++++++++++++++++++++++++++++
 debian/matrix-conduit.service | 21 +++++++++++++++
 debian/postinst               | 26 +++++++++++++++++++
 debian/postrm                 | 22 ++++++++++++++++
 debian/templates              | 14 ++++++++++
 7 files changed, 176 insertions(+)
 create mode 100644 debian/config
 create mode 100644 debian/env
 create mode 100644 debian/matrix-conduit.service
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
 create mode 100644 debian/templates

diff --git a/Cargo.toml b/Cargo.toml
index 8b29be8f..1feb7caa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -72,3 +72,25 @@ required-features = ["conduit_bin"]
 [lib]
 name = "conduit"
 path = "src/lib.rs"
+
+[package.metadata.deb]
+name = "matrix-conduit"
+maintainer = "Paul van Tilburg <paul@luon.net>"
+copyright = "2020, Timo Kösters <timo@koesters.xyz>"
+license-file = ["LICENSE", "3"]
+depends = "$auto, ca-certificates"
+extended-description = """\
+A fast Matrix homeserver that is optimized for smaller, personal servers, \
+instead of a server that has high scalability."""
+section = "net"
+priority = "optional"
+assets = [
+  ["debian/env", "etc/matrix-conduit/env", "644"],
+  ["README.md", "usr/share/doc/matrix-conduit/", "644"],
+  ["target/release/conduit", "usr/sbin/matrix-conduit", "755"],
+]
+conf-files = [
+  "/etc/matrix-conduit/env"
+]
+maintainer-scripts = "debian/"
+systemd-units = { unit-name = "matrix-conduit" }
diff --git a/debian/config b/debian/config
new file mode 100644
index 00000000..a9ad4984
--- /dev/null
+++ b/debian/config
@@ -0,0 +1,23 @@
+#!/bin/sh
+set -e
+
+# Source debconf library.
+. /usr/share/debconf/confmodule
+
+CONDUIT_CONFIG_PATH=/etc/matrix-conduit
+CONDUIT_CONFIG_FILE="$CONDUIT_CONFIG_PATH/env"
+
+# Ask for the Matrix homeserver name and port.
+db_input high matrix-conduit/hostname || true
+db_go
+
+db_input medium matrix-conduit/port || true
+db_go
+
+# Update the values in the config.
+db_get matrix-conduit/hostname
+sed -i -e "s/^ROCKET_SERVER_NAME=.*/ROCKET_SERVER_NAME=\"$RET\"/" $CONDUIT_CONFIG_FILE
+db_get matrix-conduit/port
+sed -i -e "s/^ROCKET_PORT=.*/ROCKET_PORT=\"$RET\"/" $CONDUIT_CONFIG_FILE
+
+exit 0
diff --git a/debian/env b/debian/env
new file mode 100644
index 00000000..3f72c5b2
--- /dev/null
+++ b/debian/env
@@ -0,0 +1,48 @@
+# Conduit homeserver configuration
+#
+# Conduit is an application based on the Rocket web framework.
+# Configuration of Conduit can happen either via a `Rocket.toml` file that
+# is placed in /var/lib/matrix-conduit or via setting the environment
+# variables below.
+
+# The server (host)name of the Matrix homeserver.
+#
+# This is the hostname the homeserver will be reachable at via a client.
+ROCKET_SERVER_NAME="YOURSERVERNAME.HERE"
+
+# The address the Matrix homeserver listens on.
+#
+# By default the server listens on 0.0.0.0. Change this for example to
+# 127.0.0.1 to only listen on the localhost when using a reverse proxy.
+#ROCKET_ADDRESS="0.0.0.0"
+
+# The port of the Matrix homeserver.
+#
+# This port is often accessed by a reverse proxy.
+ROCKET_PORT="14004"
+
+# The maximum size of a Matrix HTTP requests in bytes.
+#
+# This mostly affects the size of files that can be downloaded/uploaded.
+ROCKET_MAX_REQUEST_SIZE=20000000
+
+# Whether user registration is allowed.
+#
+# User registration is allowed by default.
+#ROCKET_REGISTRATION_DISABLED=true
+
+# Whether encryption is enabled.
+#
+# (End-to-end) encryption is enabled by default.
+#ROCKET_ENCRYPTION_DISABLED=true
+
+# Whether federation with other Matrix servers is enabled.
+#
+# Federation is disabled by default; it is still experimental.
+#ROCKET_FEDERATION_ENABLED=true
+
+# The log level of the homeserver.
+#
+# The log level is "critical" by default.
+# Allowed values are: "off", "normal", "debug", "critical"
+#ROCKET_LOG="normal"
diff --git a/debian/matrix-conduit.service b/debian/matrix-conduit.service
new file mode 100644
index 00000000..96c4856a
--- /dev/null
+++ b/debian/matrix-conduit.service
@@ -0,0 +1,21 @@
+[Unit]
+Description=Conduit Matrix homeserver
+After=network.target
+
+[Service]
+User=_matrix-conduit
+Group=_matrix-conduit
+Type=simple
+
+Environment="ROCKET_ENV=production"
+Environment="ROCKET_DATABASE_PATH=/var/lib/matrix-conduit"
+EnvironmentFile=/etc/matrix-conduit/env
+
+ExecStart=/usr/sbin/matrix-conduit
+Restart=on-failure
+RestartSec=10
+StartLimitInterval=1m
+StartLimitBurst=5
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/postinst b/debian/postinst
new file mode 100644
index 00000000..ee684dae
--- /dev/null
+++ b/debian/postinst
@@ -0,0 +1,26 @@
+#!/bin/sh
+set -e
+
+CONDUIT_DATABASE_PATH=/var/lib/matrix-conduit
+
+case "$1" in
+  configure)
+    # Create the `_matrix-conduit` user if it does not exist yet.
+    if ! getent passwd _matrix-conduit > /dev/null ; then
+      echo 'Adding system user for the Conduit Matrix homeserver' 1>&2
+      adduser --system --group --quiet \
+        --home $CONDUIT_DATABASE_PATH \
+        --disabled-login \
+        --force-badname \
+        _matrix-conduit
+    fi
+
+    # Create the database path if it does not exist yet.
+    if [ ! -d "$CONDUIT_DATABASE_PATH" ]; then
+      mkdir -p "$CONDUIT_DATABASE_PATH"
+      chown _matrix-conduit "$CONDUIT_DATABASE_PATH"
+    fi
+    ;;
+esac
+
+#DEBHELPER#
diff --git a/debian/postrm b/debian/postrm
new file mode 100644
index 00000000..04ca3254
--- /dev/null
+++ b/debian/postrm
@@ -0,0 +1,22 @@
+#!/bin/sh
+set -e
+
+CONDUIT_CONFIG_PATH=/etc/matrix-conduit
+CONDUIT_DATABASE_PATH=/var/lib/matrix-conduit
+
+case $1 in
+  purge)
+    # Per https://www.debian.org/doc/debian-policy/ch-files.html#behavior
+    # "configuration files must be preserved when the package is removed, and
+    #  only deleted when the package is purged."
+    if [ -d "$CONDUIT_CONFIG_PATH" ]; then
+      rm -r "$CONDUIT_CONFIG_PATH"
+    fi
+
+    if [ -d "$CONDUIT_DATABASE_PATH" ]; then
+      rm -r "$CONDUIT_DATABASE_PATH"
+    fi
+    ;;
+esac
+
+#DEBHELPER#
diff --git a/debian/templates b/debian/templates
new file mode 100644
index 00000000..66bf55cd
--- /dev/null
+++ b/debian/templates
@@ -0,0 +1,14 @@
+Template: matrix-conduit/hostname
+Type: string
+Default: localhost
+Description: The server (host)name of the Matrix homeserver.
+ This is the hostname the homeserver will be reachable at via a client.
+ .
+ If set to "localhost", you can connect with a client locally and clients
+ from other hosts and also other servers will not be able to reach you!
+
+Template: matrix-conduit/port
+Type: string
+Default: 14004
+Description: The port of the Matrix homeserver
+ This port is often accessed by a reverse proxy.