From 37762dd1cacec8652b142b614d76518bf647a9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 17 Feb 2021 10:52:29 +0100 Subject: [PATCH] [__matrix_synapse_worker] initial implementation --- .../files/matrix-synapse-worker@.service | 25 ++++++ .../files/worker.yaml.sh | 25 ++++++ type/__matrix_synapse_worker/man.rst | 77 +++++++++++++++++++ type/__matrix_synapse_worker/manifest | 67 ++++++++++++++++ .../parameter/default/replication-host | 1 + .../parameter/default/replication-port | 1 + .../parameter/optional | 3 + .../parameter/optional_multiple | 1 + .../parameter/required | 3 + 9 files changed, 203 insertions(+) create mode 100644 type/__matrix_synapse_worker/files/matrix-synapse-worker@.service create mode 100755 type/__matrix_synapse_worker/files/worker.yaml.sh create mode 100644 type/__matrix_synapse_worker/man.rst create mode 100755 type/__matrix_synapse_worker/manifest create mode 100644 type/__matrix_synapse_worker/parameter/default/replication-host create mode 100644 type/__matrix_synapse_worker/parameter/default/replication-port create mode 100644 type/__matrix_synapse_worker/parameter/optional create mode 100644 type/__matrix_synapse_worker/parameter/optional_multiple create mode 100644 type/__matrix_synapse_worker/parameter/required diff --git a/type/__matrix_synapse_worker/files/matrix-synapse-worker@.service b/type/__matrix_synapse_worker/files/matrix-synapse-worker@.service new file mode 100644 index 0000000..cb5ac0a --- /dev/null +++ b/type/__matrix_synapse_worker/files/matrix-synapse-worker@.service @@ -0,0 +1,25 @@ +[Unit] +Description=Synapse %i +AssertPathExists=/etc/matrix-synapse/workers/%i.yaml + +# This service should be restarted when the synapse target is restarted. +PartOf=matrix-synapse.target + +# if this is started at the same time as the main, let the main process start +# first, to initialise the database schema. +After=matrix-synapse.service + +[Service] +Type=notify +NotifyAccess=main +User=matrix-synapse +WorkingDirectory=/var/lib/matrix-synapse +EnvironmentFile=/etc/default/matrix-synapse +ExecStart=/opt/venvs/matrix-synapse/bin/python -m synapse.app.generic_worker --config-path=/etc/matrix-synapse/homeserver.yaml --config-path=/etc/matrix-synapse/conf.d/ --config-path=/etc/matrix-synapse/workers/%i.yaml +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +RestartSec=3 +SyslogIdentifier=matrix-synapse-%i + +[Install] +WantedBy=matrix-synapse.target diff --git a/type/__matrix_synapse_worker/files/worker.yaml.sh b/type/__matrix_synapse_worker/files/worker.yaml.sh new file mode 100755 index 0000000..0d24636 --- /dev/null +++ b/type/__matrix_synapse_worker/files/worker.yaml.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +cat << EOF +worker_app: "${WORKER_APP:?}" +worker_name: "${WORKER_NAME:?}" + +# The replication listener on the main synapse process. +worker_replication_host: "${WORKER_REPLICATION_HOST:?}" +worker_replication_http_port: "${WORKER_REPLICATION_PORT:?}" + +worker_listeners: + - type: http + port: "${WORKER_PORT:?}" + resources: + - names: +EOF + +for resource in ${WORKER_RESOURCES:?}; do +echo " - \"$resource\"" +done + +cat << EOF + +worker_log_config: "${WORKER_LOG_CONFIG:?}" +EOF diff --git a/type/__matrix_synapse_worker/man.rst b/type/__matrix_synapse_worker/man.rst new file mode 100644 index 0000000..c8150f6 --- /dev/null +++ b/type/__matrix_synapse_worker/man.rst @@ -0,0 +1,77 @@ +cdist-type__matrix_synapse(7) +====================== + +NAME +---- +cdist-type__matrix_synapse_worker - Configure a synapse worker + + +DESCRIPTION +----------- +This type configures and start a matrix worker. This type does not install +synapse: `cdist-type__matrix_synapse(7) `_ +type must be run first. + +It is also recommended to take a look at: + + - `upstream's high-level overview on workers (matrix.org blog post) `_ + - `upstream's documentation on workers `_ + +REQUIRED PARAMETERS +------------------- +app + Worker application to be used. A detailed list is available on `upstream's + documentation + `_. + +port + Port on which this worker will listen. + +resource + Resources to be served by this worker. Can be specified multiple times. + +OPTIONAL PARAMETERS +------------------- +replication-host + Replication endpoint host of your main synapse process. Defaults to + localhost. + +replication-port + Replication endpoint port of your main synapse process. Defaults to 9093. + +log-config + Path to log configuration. Defaults to synapse's main process log + configuration. + +EXAMPLES +-------- + +.. code-block:: sh + + __matrix_synapse --server-name ungleich.ch \ + --base-url https://matrix.ungleich.ch \ + --database-engine sqlite3 \ + --database-name /var/lib/matrix-syanpse/homeserver.db \ + --worker-mode + require="__matrix_synapse" __matrix_synapse_worker generic \ + --app 'synapse.app.generic_worker' \ + --port 8083 \ + --resource 'federation' \ + --resource 'client' + +SEE ALSO +-------- +- `cdist-type__matrix_synapse(7) `_ + + +AUTHORS +------- +Timothée Floure + + +COPYING +------- +Copyright \(C) 2019-2021 Timothée Floure. You can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. diff --git a/type/__matrix_synapse_worker/manifest b/type/__matrix_synapse_worker/manifest new file mode 100755 index 0000000..c1867ed --- /dev/null +++ b/type/__matrix_synapse_worker/manifest @@ -0,0 +1,67 @@ +#!/bin/sh + +# TODO: check if matrix-synapse package is installed (fail if not - it's not +# this type's job to install it). + +name=$__object_id +os=$(cat "$__global/explorer/os") + +case "$os" in + debian) + synapse_conf_dir='/etc/synapse' + synapse_workers_conf_dir="$synapse_conf_dir/workers" + + # Synapse log configuration on debian - default value of config-log + # parameter. + WORKER_LOG_CONFIG="$synapse_conf_dir/log.yaml" + + # As of writing, debian's matrix-synapse package does not install the + # matrix-synapse-worker@.service systemd unit. + systemd_worker_service_override=present + systemd_worker_service="matrix-synapse-worker@$name" + ;; + *) + printf "Your operating system (%s) is currently not supported by this type (%s)\n" "$os" "${__type##*/}" >&2 + printf "Please contribute an implementation for it if you can.\n" >&2 + exit 1 + ;; +esac + +# Type parameters. +WORKER_NAME=$__object_id +WORKER_APP=$(cat "$__object/parameter/app") +WORKER_PORT=$(cat "$__object/parameter/port") +WORKER_RESOURCES=$(cat "$__object/parameter/resource") + +export WORKER_APP WORKER_NAME WORKER_PORT + +if [ -f "$__object/parameter/log-config" ]; then + WORKER_LOG_CONFIG=$(cat "$__object/parameter/log-config") +fi +export WORKER_LOG_CONFIG + +WORKER_REPLICATION_HOST=$(cat "$__object/parameter/replication-host") +WORKER_REPLICATION_PORT=$(cat "$__object/parameter/replication-port") +export WORKER_REPLICATION_HOST WORKER_REPLICATION_PORT + +# Generate and deploy configuration files. +mkdir -p "$__object/files" +"$__type/files/worker.yaml.sh" > "$__object/files/worker.yaml" + +__directory "$synapse_workers_conf_dir" --parents +require="__directory/$synapse_workers_conf_dir" \ + __file "$synapse_workers_conf_dir/$name.yaml" \ + --source "$__object/files/worker.yaml" \ + --mode 0644 + +__file "/etc/systemd/systemd/matrix-synapse-worker@.service" \ + --source "$__type/files/matrix-synapse-worker@.service" \ + --mode 0644 \ + --state $systemd_worker_service_override + +# Start service, enable at boot. +require="__file/$synapse_workers_conf_dir/$name.yaml \ + __file/etc/systemd/systemd/matrix-synapse-worker@.service" \ + __service "$systemd_worker_service" --action start +require="__service/$systemd_worker_service" \ + __start_on_boot "$systemd_worker_service" diff --git a/type/__matrix_synapse_worker/parameter/default/replication-host b/type/__matrix_synapse_worker/parameter/default/replication-host new file mode 100644 index 0000000..2fbb50c --- /dev/null +++ b/type/__matrix_synapse_worker/parameter/default/replication-host @@ -0,0 +1 @@ +localhost diff --git a/type/__matrix_synapse_worker/parameter/default/replication-port b/type/__matrix_synapse_worker/parameter/default/replication-port new file mode 100644 index 0000000..ed91c92 --- /dev/null +++ b/type/__matrix_synapse_worker/parameter/default/replication-port @@ -0,0 +1 @@ +9093 diff --git a/type/__matrix_synapse_worker/parameter/optional b/type/__matrix_synapse_worker/parameter/optional new file mode 100644 index 0000000..2f61487 --- /dev/null +++ b/type/__matrix_synapse_worker/parameter/optional @@ -0,0 +1,3 @@ +replication-host +replication-port +log-config diff --git a/type/__matrix_synapse_worker/parameter/optional_multiple b/type/__matrix_synapse_worker/parameter/optional_multiple new file mode 100644 index 0000000..91e75c6 --- /dev/null +++ b/type/__matrix_synapse_worker/parameter/optional_multiple @@ -0,0 +1 @@ +resource diff --git a/type/__matrix_synapse_worker/parameter/required b/type/__matrix_synapse_worker/parameter/required new file mode 100644 index 0000000..62d5bd6 --- /dev/null +++ b/type/__matrix_synapse_worker/parameter/required @@ -0,0 +1,3 @@ +app +name +port