From 3be4f0a5fdf777701f2fb4e802d400dcb3eaeded Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 8 Jul 2019 21:41:09 +0200
Subject: [PATCH] debian: switch to uwsgi (#13060)

---
 debian/control       |  3 ++-
 debian/fargo.init    | 48 +++++++++-----------------------------------
 debian/fargo.install |  1 +
 debian/fargo.service | 14 ++++++-------
 debian/uwsgi.ini     | 26 ++++++++++++++++++++++++
 5 files changed, 45 insertions(+), 47 deletions(-)
 create mode 100644 debian/uwsgi.ini

diff --git a/debian/control b/debian/control
index a9ebe95..13211bd 100644
--- a/debian/control
+++ b/debian/control
@@ -27,7 +27,8 @@ Depends: ${misc:Depends},
     python-django-tenant-schemas,
     python-psycopg2,
     python-django-mellon,
-    gunicorn
+    uwsgi,
+    uwsgi-plugin-python
 Recommends: nginx, graphicsmagick
 Suggests: postgresql
 Description: Fargo Document Box
diff --git a/debian/fargo.init b/debian/fargo.init
index e553d2e..0cba62f 100644
--- a/debian/fargo.init
+++ b/debian/fargo.init
@@ -16,14 +16,12 @@
 PATH=/sbin:/usr/sbin:/bin:/usr/bin
 DESC="Document Box"
 NAME=fargo
-DAEMON=/usr/bin/gunicorn
+DAEMON=/usr/bin/uwsgi
 RUN_DIR=/run/$NAME
 PIDFILE=$RUN_DIR/$NAME.pid
 LOG_DIR=/var/log/$NAME
 SCRIPTNAME=/etc/init.d/$NAME
 BIND=unix:$RUN_DIR/$NAME.sock
-WORKERS=5
-TIMEOUT=30
 
 FARGO_SETTINGS_FILE=/usr/lib/$NAME/debian_config.py
 MANAGE_SCRIPT="/usr/bin/$NAME-manage"
@@ -37,17 +35,10 @@ GROUP=$NAME
 # Read configuration variable file if it is present
 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
 
-DAEMON_ARGS=${DAEMON_ARGS:-"--pid $PIDFILE \
---user $USER --group $GROUP \
---daemon \
---access-logfile $LOG_DIR/gunicorn-access.log \
---log-file $LOG_DIR/gunicorn-error.log \
---bind=$BIND  \
---workers=$WORKERS \
---worker-class=sync \
---timeout=$TIMEOUT \
---name $NAME \
-$NAME.wsgi:application"}
+DAEMON_ARGS=${DAEMON_ARGS:-"--pidfile=$PIDFILE
+--uid $USER --gid $GROUP
+--ini /etc/$NAME/uwsgi.ini
+--daemonize /var/log/uwsgi.$NAME.log"}
 
 # Load the VERBOSE setting and other rcS variables
 . /lib/init/vars.sh
@@ -73,9 +64,7 @@ do_start()
     #   0 if daemon has been started
     #   1 if daemon was already running
     #   2 if daemon could not be started
-    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
-        || return 1
-    start-stop-daemon --start --quiet --exec $DAEMON -- \
+    start-stop-daemon --start --quiet --user $USER --exec $DAEMON -- \
         $DAEMON_ARGS \
         || return 2
 }
@@ -90,32 +79,16 @@ do_stop()
     #   1 if daemon was already stopped
     #   2 if daemon could not be stopped
     #   other if a failure occurred
-    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
-    RETVAL="$?"
-    [ "$RETVAL" = 2 ] && return 2
-    # Wait for children to finish too if this is a daemon that forks
-    # and if the daemon is only ever run from this initscript.
-    # If the above conditions are not satisfied then add some other code
-    # that waits for the process to drop all resources that could be
-    # needed by services started subsequently.  A last resort is to
-    # sleep for some time.
-    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
-    [ "$?" = 2 ] && return 2
-    # Many daemons don't delete their pidfiles when they exit.
+    $DAEMON --stop $PIDFILE
     rm -f $PIDFILE
-    return "$RETVAL"
+    return 0 # hopefully
 }
 
 #
 # Function that sends a SIGHUP to the daemon/service
 #
 do_reload() {
-    #
-    # If the daemon can reload its configuration without
-    # restarting (for example, when it is sent a SIGHUP),
-    # then implement that here.
-    #
-    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name `basename $DAEMON`
+    $DAEMON --reload $PIDFILE
     return 0
 }
 
@@ -131,7 +104,6 @@ do_collectstatic() {
     log_action_msg "done"
 }
 
-
 case "$1" in
   start)
     log_daemon_msg "Starting $DESC " "$NAME"
@@ -152,7 +124,7 @@ case "$1" in
     esac
     ;;
   status)
-    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
+    status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
     ;;
   reload|force-reload)
     #
diff --git a/debian/fargo.install b/debian/fargo.install
index 557879b..b352b81 100644
--- a/debian/fargo.install
+++ b/debian/fargo.install
@@ -1,4 +1,5 @@
 debian/fargo-manage       /usr/bin
 debian/fargo.service      /lib/systemd/system
 debian/settings.py        /etc/fargo
+debian/uwsgi.ini          /etc/fargo
 debian/debian_config.py   /usr/lib/fargo/
diff --git a/debian/fargo.service b/debian/fargo.service
index 0dfbbd5..0e8d753 100644
--- a/debian/fargo.service
+++ b/debian/fargo.service
@@ -1,6 +1,6 @@
 [Unit]
 Description=Fargo
-After=network.target postgresql.service
+After=network.target syslog.target postgresql.service
 Wants=postgresql.service
 
 [Service]
@@ -10,18 +10,16 @@ User=%p
 Group=%p
 ExecStartPre=/usr/bin/fargo-manage migrate_schemas --noinput
 ExecStartPre=/usr/bin/fargo-manage collectstatic --noinput
-ExecStart=/usr/bin/gunicorn \
-          --bind unix:/run/%p/%p.sock \
-          --worker-class=sync \
-          --workers 5 \
-          --timeout=30 \
-          --name %p \
-          %p.wsgi:application
+ExecStart=/usr/bin/uwsgi --ini /etc/%p/uwsgi.ini
 ExecReload=/bin/kill -HUP $MAINPID
+KillSignal=SIGQUIT
 TimeoutStartSec=0
 PrivateTmp=true
 Restart=on-failure
 RuntimeDirectory=fargo
+Type=notify
+StandardError=syslog
+NotifyAccess=all
 
 [Install]
 WantedBy=multi-user.target
diff --git a/debian/uwsgi.ini b/debian/uwsgi.ini
new file mode 100644
index 0000000..98e642f
--- /dev/null
+++ b/debian/uwsgi.ini
@@ -0,0 +1,26 @@
+[uwsgi]
+auto-procname = true
+procname-prefix-spaced = fargo
+
+plugin = python
+module = fargo.wsgi:application
+
+http-socket = /run/fargo/fargo.sock
+chmod-socket = 666
+vacuum = true
+
+master = true
+processes = 5
+harakiri = 120
+enable-threads = true
+
+buffer-size = 32768
+
+py-tracebacker = /run/fargo/py-tracebacker.sock.
+stats = /run/fargo/stats.sock
+
+ignore-sigpipe = true
+
+if-file = /etc/fargo/uwsgi-local.ini
+  include = /etc/fargo/uwsgi-local.ini
+endif =
-- 
2.20.1

