From 94918c92a0338d2539ba92d28effe791fd116bf8 Mon Sep 17 00:00:00 2001
From: Jeroen Dekkers <jeroen@dekkers.ch>
Date: Wed, 1 Nov 2023 16:22:12 +0100
Subject: [PATCH 1/2] Switch default to granian

---
 rocky/Dockerfile               |  2 +-
 rocky/debian/kat-rocky.service |  3 +--
 rocky/debian/postinst          | 17 ++++++-----------
 rocky/entrypoint.sh            |  6 +-----
 4 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/rocky/Dockerfile b/rocky/Dockerfile
index d13f512f702..255f1c43b7f 100644
--- a/rocky/Dockerfile
+++ b/rocky/Dockerfile
@@ -69,4 +69,4 @@ RUN export SECRET_KEY="secret" BYTES_API="http://bytes:8000" BYTES_PASSWORD="pas
 
 USER rocky
 
-CMD ["uwsgi", "--ini", "uwsgi.ini", "--wsgi-file", "rocky/wsgi.py"]
+CMD ["granian", "--interface", "wsgi", "rocky.wsgi:application", "--host", "0.0.0.0"]
diff --git a/rocky/debian/kat-rocky.service b/rocky/debian/kat-rocky.service
index 1430ffa7623..6bebab0c8ba 100644
--- a/rocky/debian/kat-rocky.service
+++ b/rocky/debian/kat-rocky.service
@@ -9,12 +9,11 @@ SyslogIdentifier=kat-rocky
 WorkingDirectory=/opt/venvs/kat-rocky/
 EnvironmentFile=/usr/lib/kat/rocky.defaults
 EnvironmentFile=/etc/kat/rocky.conf
-ExecStart=/opt/venvs/kat-rocky/bin/uwsgi --ini /etc/kat/rocky.uwsgi.ini
+ExecStart=/opt/venvs/kat-rocky/bin/granian --interface wsgi rocky.wsgi:application
 Restart=on-failure
 RestartSec=3s
 KillSignal=SIGQUIT
 KillMode=mixed
-Type=notify
 NotifyAccess=all
 
 [Install]
diff --git a/rocky/debian/postinst b/rocky/debian/postinst
index c7dbaad5496..bfe43a710f4 100755
--- a/rocky/debian/postinst
+++ b/rocky/debian/postinst
@@ -9,19 +9,14 @@ if [ "$1" = "configure" ]; then
         # Set SECRET_KEY if empty
         key=$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 64)
         sed -i "s/SECRET_KEY= *\$/SECRET_KEY=${key}/" /etc/kat/rocky.conf
-
-        # Only configure granian for new installs. Existing installations will
-        # switch in the next release.
-        mkdir -p /etc/systemd/system/kat-rocky.service.d
-        cat > /etc/systemd/system/kat-rocky.service.d/use-granian.conf << EOF
-# Do not customize this file because it will be automatically removed in the next release
-[Service]
-ExecStart=
-ExecStart=/opt/venvs/kat-rocky/bin/granian --interface wsgi rocky.wsgi:application
-Type=simple
-EOF
     fi
 
+    # Delete file and directory that was added in 1.13 to use granian only on
+    # new installs.
+    rm -f /etc/systemd/system/kat-rocky.service.d/use-granian.conf
+    if [ -d /etc/systemd/system/kat-rocky.service.d ]; then
+        rmdir --ignore-fail-on-non-empty /etc/systemd/system/kat-rocky.service.d
+    fi
 fi
 
 #DEBHELPER#
diff --git a/rocky/entrypoint.sh b/rocky/entrypoint.sh
index a4f25616119..54687790413 100755
--- a/rocky/entrypoint.sh
+++ b/rocky/entrypoint.sh
@@ -8,8 +8,4 @@ if [ "$DATABASE_MIGRATION" = "1" ] || [[ $DATABASE_MIGRATION == "true" ]]; then
     python manage.py migrate --noinput
 fi
 
-if [ "$1" == "uwsgi" ] && { [ "$USE_GRANIAN" = "1" ] || [[ $USE_GRANIAN == "true" ]]; }; then
-    exec granian --interface wsgi rocky.wsgi:application --host 0.0.0.0
-else
-    exec "$@"
-fi
+exec "$@"

From c823dd4b11df4680a8a6a5f9da4c1cb8f98b62ca Mon Sep 17 00:00:00 2001
From: Jeroen Dekkers <jeroen@dekkers.ch>
Date: Wed, 1 Nov 2023 16:26:21 +0100
Subject: [PATCH 2/2] Only try to remove file if directory exists

---
 rocky/debian/postinst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rocky/debian/postinst b/rocky/debian/postinst
index bfe43a710f4..6a616d82503 100755
--- a/rocky/debian/postinst
+++ b/rocky/debian/postinst
@@ -13,8 +13,8 @@ if [ "$1" = "configure" ]; then
 
     # Delete file and directory that was added in 1.13 to use granian only on
     # new installs.
-    rm -f /etc/systemd/system/kat-rocky.service.d/use-granian.conf
     if [ -d /etc/systemd/system/kat-rocky.service.d ]; then
+        rm -f /etc/systemd/system/kat-rocky.service.d/use-granian.conf
         rmdir --ignore-fail-on-non-empty /etc/systemd/system/kat-rocky.service.d
     fi
 fi