From 83751f37cabc5b214f4296f2618e490086396dfb Mon Sep 17 00:00:00 2001
From: a <a@trwnh.com>
Date: Wed, 13 Nov 2024 23:36:53 +0000
Subject: [PATCH] Add systemd service with webhook notify on start/stop

---
 README.md            | 40 ++++++++++++++++++++++++++++------------
 contrib/notify.sh    |  7 +++++++
 contrib/umi@.service | 15 +++++++++++++++
 3 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 contrib/notify.sh
 create mode 100644 contrib/umi@.service

diff --git a/README.md b/README.md
index a5c4499..521c7c0 100644
--- a/README.md
+++ b/README.md
@@ -22,33 +22,49 @@ Copy `.env.example` to `.env`:
   - Settings > Advanced > Developer Mode: enabled
   - Right click the guild and select "Copy ID"
 
+- `DISCORD_STATUS_WEBHOOK_URL` (optional) is used to monitor when the bot fails
+  - Channel Settings > Integrations > New Webhook (don't forget to save changes)
+  - Copy Webhook URL
+
 - `DISCORD_BOT_ADMIN_ROLE_ID` for reloading cogs
 - `DISCORD_BOT_ADMIN_USER_ID` for reloading cogs
 - `DISCORD_VCJOIN_CHANNEL` for the VCJoin cog
 
 ## Setup
 
-### ...with PDM
+### with systemd
 
 ```sh
-pdm install --prod
-pdm run python app.py
-```
+mkdir -p /srv/discord
+cd /srv/discord
 
-### ...with requirements.txt and virtualenv
+sudo useradd --system -s /srv/discord discord
+sudo chown -R discord:discord /srv/discord
 
-```sh
-virtualenv .venv
-source .venv/bin/activate
-pip install -r requirements.txt
-python app.py
+sudo -Hu discord git clone https://git.trwnh.com/a/umi.git
+
+python -m venv .venv
+.venv/bin/pip install -r umi/requirements.txt
+
+sudo cp umi/umi@.service /etc/systemd/system/umi@.service
+sudo systemctl daemon-reload
+sudo systemctl start umi@umi
 ```
 
 ### ...with Docker Compose
 
 ```sh
-docker compose build
-docker compose up -d
+mkdir -p /srv/discord
+cd /srv/discord
+
+sudo useradd --system -s /srv/discord discord
+sudo chown -R discord:discord /srv/discord
+
+sudo -Hu discord git clone https://git.trwnh.com/a/umi.git
+
+cd umi
+sudo docker compose build
+sudo docker compose up -d
 ```
 
 ## Log into YouTube
diff --git a/contrib/notify.sh b/contrib/notify.sh
new file mode 100644
index 0000000..9aeb7b7
--- /dev/null
+++ b/contrib/notify.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/bash
+
+curl \
+        -X POST \
+        -H "Content-Type: application/json" \
+        -d '{"username": "system", "content": "'"$1"'"}' \
+        $DISCORD_STATUS_WEBHOOK_URL
diff --git a/contrib/umi@.service b/contrib/umi@.service
new file mode 100644
index 0000000..22abbf2
--- /dev/null
+++ b/contrib/umi@.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Discord bot for logging VC joins and playing music
+After=network.target
+
+[Service]
+Restart=on-failure
+User=discord
+WorkingDirectory=/srv/discord/%I
+EnvironmentFile=/srv/discord/%I/.env
+ExecStart=/srv/discord/.venv/bin/python -u app.py
+ExecStopPost=/srv/discord/%I/notify.sh "umi instance stopped: `%i`"
+ExecStartPost=/srv/discord/%I/notify.sh "umi instance started: `%i`"
+
+[Install]
+WantedBy=multi-user.target