From b31754c4aeba61de73f69d0784afdc5b824ca3bd Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 3 Sep 2020 15:10:26 +0200
Subject: [PATCH] debian: use uwsgi to serve app (#29297)

---
 debian/authentic2-multitenant-uwsgi.ini | 45 +++++++++++++++++++++++++
 debian/authentic2-multitenant.install   |  1 +
 debian/authentic2-multitenant.service   | 12 +++----
 debian/authentic2-uwsgi.ini             | 45 +++++++++++++++++++++++++
 debian/authentic2.install               |  1 +
 debian/authentic2.service               | 12 +++----
 debian/control                          |  7 ++--
 7 files changed, 107 insertions(+), 16 deletions(-)
 create mode 100644 debian/authentic2-multitenant-uwsgi.ini
 create mode 100644 debian/authentic2-uwsgi.ini

diff --git a/debian/authentic2-multitenant-uwsgi.ini b/debian/authentic2-multitenant-uwsgi.ini
new file mode 100644
index 00000000..dfca8074
--- /dev/null
+++ b/debian/authentic2-multitenant-uwsgi.ini
@@ -0,0 +1,45 @@
+[uwsgi]
+auto-procname = true
+procname-prefix-spaced = authentic2-multitenant
+strict = true
+
+plugin = python3
+single-interpreter = true
+module = authentic2.wsgi:application
+need-app = true
+
+http-socket = /run/authentic2-multitenant/authentic2-multitenant.sock
+chmod-socket = 666
+vacuum = true
+
+master = true
+enable-threads = true
+harakiri = 120
+
+processes = 500
+
+plugin = cheaper_busyness
+cheaper-algo = busyness
+cheaper = 5
+cheaper-initial = 10
+cheaper-overload = 5
+cheaper-step = 10
+cheaper-busyness-multiplier = 30
+cheaper-busyness-min = 20
+cheaper-busyness-max = 70
+cheaper-busyness-backlog-alert = 16
+cheaper-busyness-backlog-step = 2
+
+max-requests = 500
+max-worker-lifetime = 7200
+
+buffer-size = 32768
+
+py-tracebacker = /run/authentic2-multitenant/py-tracebacker.sock.
+stats = /run/authentic2-multitenant/stats.sock
+
+ignore-sigpipe = true
+
+if-file = /etc/authentic2-multitenant/uwsgi-local.ini
+  include = /etc/authentic2-multitenant/uwsgi-local.ini
+endif =
diff --git a/debian/authentic2-multitenant.install b/debian/authentic2-multitenant.install
index 6200681c..3bbc9722 100644
--- a/debian/authentic2-multitenant.install
+++ b/debian/authentic2-multitenant.install
@@ -1,3 +1,4 @@
 debian/multitenant/authentic2-multitenant-manage /usr/bin
 debian/multitenant/config.py                     /etc/authentic2-multitenant
 debian/multitenant/debian_config.py              /usr/lib/authentic2-multitenant
+debian/authentic2-multitenant-uwsgi.ini          /etc/authentic2-multitenant
diff --git a/debian/authentic2-multitenant.service b/debian/authentic2-multitenant.service
index b1bbac5b..e058c11f 100644
--- a/debian/authentic2-multitenant.service
+++ b/debian/authentic2-multitenant.service
@@ -10,18 +10,16 @@ User=authentic-multitenant
 Group=authentic-multitenant
 ExecStartPre=/usr/bin/authentic2-multitenant-manage migrate_schemas --noinput
 ExecStartPre=/usr/bin/authentic2-multitenant-manage collectstatic --noinput
-ExecStart=/usr/bin/gunicorn3 \
-          --bind unix:/run/%p/%p.sock \
-          --worker-class=sync \
-          --workers 5 \
-          --timeout=60 \
-          --name %p \
-          authentic2.wsgi:application
+ExecStart=/usr/bin/uwsgi --ini /etc/%p/%p-uwsgi.ini
 ExecReload=/bin/kill -HUP $MAINPID
+KillSignal=SIGQUIT
 TimeoutStartSec=0
 PrivateTmp=true
 Restart=on-failure
 RuntimeDirectory=%p
+Type=notify
+StandardError=syslog
+NotifyAccess=all
 
 [Install]
 WantedBy=multi-user.target
diff --git a/debian/authentic2-uwsgi.ini b/debian/authentic2-uwsgi.ini
new file mode 100644
index 00000000..7b51ac26
--- /dev/null
+++ b/debian/authentic2-uwsgi.ini
@@ -0,0 +1,45 @@
+[uwsgi]
+auto-procname = true
+procname-prefix-spaced = authentic2
+strict = true
+
+plugin = python3
+single-interpreter = true
+module = authentic2.wsgi:application
+need-app = true
+
+http-socket = /run/authentic2/authentic2.sock
+chmod-socket = 666
+vacuum = true
+
+master = true
+enable-threads = true
+harakiri = 120
+
+processes = 500
+
+plugin = cheaper_busyness
+cheaper-algo = busyness
+cheaper = 5
+cheaper-initial = 10
+cheaper-overload = 5
+cheaper-step = 10
+cheaper-busyness-multiplier = 30
+cheaper-busyness-min = 20
+cheaper-busyness-max = 70
+cheaper-busyness-backlog-alert = 16
+cheaper-busyness-backlog-step = 2
+
+max-requests = 500
+max-worker-lifetime = 7200
+
+buffer-size = 32768
+
+py-tracebacker = /run/authentic2/py-tracebacker.sock.
+stats = /run/authentic2/stats.sock
+
+ignore-sigpipe = true
+
+if-file = /etc/authentic2/uwsgi-local.ini
+  include = /etc/authentic2/uwsgi-local.ini
+endif =
diff --git a/debian/authentic2.install b/debian/authentic2.install
index a608b265..bc9c7a08 100644
--- a/debian/authentic2.install
+++ b/debian/authentic2.install
@@ -3,3 +3,4 @@ debian/authentic2-manage              /usr/bin
 debian/conf/nginx-example.conf        /usr/share/authentic2
 debian/debian_config.py               /usr/lib/authentic2
 debian/launch-authentic2.sh           /usr/lib/authentic2
+debian/authentic2-uwsgi.ini           /etc/authentic2
diff --git a/debian/authentic2.service b/debian/authentic2.service
index c85309d7..514f5099 100644
--- a/debian/authentic2.service
+++ b/debian/authentic2.service
@@ -10,18 +10,16 @@ User=authentic2
 Group=authentic2
 ExecStartPre=/usr/bin/authentic2-manage migrate --noinput
 ExecStartPre=/usr/bin/authentic2-manage collectstatic --noinput
-ExecStart=/usr/lib/authentic2/launch-authentic2.sh \
-          --bind unix:/run/%p/%p.sock \
-          --worker-class=sync \
-          --workers 5 \
-          --timeout=60 \
-          --name %p \
-          authentic2.wsgi:application
+ExecStart=/usr/bin/uwsgi --ini /etc/%p/%p-uwsgi.ini
 ExecReload=/bin/kill -HUP $MAINPID
+KillSignal=SIGQUIT
 TimeoutStartSec=0
 PrivateTmp=true
 Restart=on-failure
 RuntimeDirectory=%p
+Type=notify
+StandardError=syslog
+NotifyAccess=all
 
 [Install]
 WantedBy=multi-user.target
diff --git a/debian/control b/debian/control
index 26fdf096..9cf6c50e 100644
--- a/debian/control
+++ b/debian/control
@@ -55,7 +55,9 @@ Architecture: all
 Depends: ${misc:Depends}, adduser,
     python3-authentic2 (= ${binary:Version}),
     python3-psycopg2,
-    gunicorn3, dbconfig-common,
+    uwsgi,
+    uwsgi-plugin-python3,
+    dbconfig-common,
     debconf | debconf-2.0, ucf
 Recommends: postgresql-client
 Suggests: nginx, postgresql
@@ -75,7 +77,8 @@ Depends: ${misc:Depends}, adduser,
     python3-django-tenant-schemas (>= 1.5.2.1),
     python3-hobo,
     python3-jwcrypto (>= 0.4.2),
-    gunicorn3,
+    uwsgi,
+    uwsgi-plugin-python3,
     debconf | debconf-2.0, ucf
 Recommends: postgresql-client
 Suggests: nginx, postgresql
-- 
2.20.1

