#!/bin/bash # One-time install of the mautrix-discord bridge. # Run on the Proxmox node that hosts LXC 109 (PostgreSQL) and LXC 151 (Synapse): # pct exec 151 -- cat /opt/matrix-config/discord-bridge/install.sh | bash # Needs this repo's main branch already pulled to /opt/matrix-config on LXC 151 # (the auto-deploy does that). Restarts Synapse once (~30-60s of downtime). # Safe to re-run: existing database, user, config and registration are kept. set -euo pipefail VERSION="v0.7.7" SHA256="1f3b6953b4eae00d141ce706f6e97c1b5d0e50268a447bc605df2fa4f44c5d9d" PG_CT=109 SYNAPSE_CT=151 SYNAPSE_IP="10.10.10.29" # Only used if the database role does not exist yet; never printed. DB_PASSWORD="$(openssl rand -hex 24 /root/.mautrix-discord-role-existed else su postgres -c "psql -v ON_ERROR_STOP=1 -q" </dev/null echo "ok" PG ROLE_EXISTED=$(pct exec "$PG_CT" -- sh -c 'test -f /root/.mautrix-discord-role-existed && echo yes || echo no' /dev/null 2>&1 || useradd --system --home "$DIR" --shell /usr/sbin/nologin mautrix-discord mkdir -p "$DIR" /var/log/mautrix-discord curl -fsSL -o "$DIR/mautrix-discord.new" "https://github.com/mautrix/discord/releases/download/$VERSION/mautrix-discord-amd64" echo "$SHA256 $DIR/mautrix-discord.new" | sha256sum -c - chmod 755 "$DIR/mautrix-discord.new" mv "$DIR/mautrix-discord.new" "$DIR/mautrix-discord" if [ ! -f "$DIR/config.yaml" ]; then if [ "$ROLE_EXISTED" = yes ]; then echo "ERROR: DB role already existed but there is no config; set the password in $DIR/config.yaml by hand" >&2 exit 1 fi sed "s/DB_PASSWORD/$PW/" "$SRC/discord-bridge/config.yaml" > "$DIR/config.yaml" fi if [ ! -f "$DIR/registration.yaml" ]; then (cd "$DIR" && ./mautrix-discord -g -c config.yaml -r registration.yaml) fi chown -R mautrix-discord:mautrix-discord "$DIR" /var/log/mautrix-discord chmod 600 "$DIR/config.yaml" install -m 640 -o root -g matrix-synapse "$DIR/registration.yaml" /etc/matrix-synapse/discord-registration.yaml install -m 644 "$SRC/systemd/mautrix-discord.service" /etc/systemd/system/mautrix-discord.service systemctl daemon-reload HS=/etc/matrix-synapse/homeserver.yaml if ! grep -q discord-registration.yaml "$HS"; then cp "$HS" "$HS.bak-$(date +%Y%m%d%H%M%S)-discord" sed -i '/^ - \/etc\/matrix-synapse\/hookshot-registration.yaml/a \ - /etc/matrix-synapse/discord-registration.yaml' "$HS" fi grep -A3 '^app_service_config_files' "$HS" echo "ok" BR echo "== [3/3] Restart Synapse, start bridge ==" pct exec "$SYNAPSE_CT" -- bash -s <<'RS' set -euo pipefail systemctl restart matrix-synapse for i in $(seq 1 60); do curl -fsS -o /dev/null http://127.0.0.1:8008/health && break sleep 2 done curl -fsS http://127.0.0.1:8008/health && echo " (synapse healthy)" systemctl enable --now mautrix-discord sleep 5 systemctl --no-pager --lines=15 status mautrix-discord RS pct exec "$PG_CT" -- rm -f /root/.mautrix-discord-role-existed "