Source: spamassassin
Severity: wishlist
Tags: patch

The attached patch adds the following:

* Add sa-update.service and sa-update.timer
* Move common cronjob functions to /etc/spamassassin/spamassassin.cron.common
* Add check to original cronjob to only run when not using systemd
* Add documentation to spamassassin.default on how to enable sa-update on 
systems
  using systemd

Thanks,
Daniel

-- System Information:
Debian Release: 9.0
  APT prefers unstable-debug
  APT policy: (500, 'unstable-debug'), (500, 'unstable'), (103, 
'experimental'), (1, 'experimental-debug')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 4.9.0-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
>From 9cb3d7cd25aa3ab2c7cf19001727117e4baa9e7e Mon Sep 17 00:00:00 2001
From: Daniel Schaal <daniel@schaal.email>
Date: Fri, 27 Jan 2017 14:47:44 +0100
Subject: [PATCH] Add systemd timer as cronjob replacement

* Add sa-update.service and sa-update.timer
* Move common cronjob functions to /etc/spamassassin/spamassassin.cron.common
* Add check to original cronjob to only run when not using systemd
* Add documentation to spamassassin.default on how to enable sa-update on 
systems
  using systemd
---
 debian/rules                    |  5 +++
 debian/sa-update.service        |  8 ++++
 debian/sa-update.timer          | 10 +++++
 debian/spamassassin.cron.common | 84 +++++++++++++++++++++++++++++++++++++++++
 debian/spamassassin.cron.daily  | 74 +-----------------------------------
 debian/spamassassin.default     |  5 ++-
 debian/spamassassin.dirs        |  1 +
 7 files changed, 114 insertions(+), 73 deletions(-)
 create mode 100644 debian/sa-update.service
 create mode 100644 debian/sa-update.timer
 create mode 100644 debian/spamassassin.cron.common

diff --git a/debian/rules b/debian/rules
index 5797ac3..be8acb4 100755
--- a/debian/rules
+++ b/debian/rules
@@ -114,6 +114,10 @@ install-spamassassin: build-indep
 
        install -o root -g root -m644 debian/GPG.KEY 
debian/spamassassin/usr/share/spamassassin/GPG.KEY
 
+       install -o root -g root -m644 debian/sa-update.timer 
debian/spamassassin/lib/systemd/system/sa-update.timer
+       install -o root -g root -m644 debian/sa-update.service 
debian/spamassassin/lib/systemd/system/sa-update.service
+       install -o root -g root -m755 debian/spamassassin.cron.common 
debian/spamassassin/etc/spamassassin/spamassassin.cron.common
+
        -rmdir -p debian/spamassassin/usr/lib/perl5
 
        cp ldap/README 
debian/spamassassin/usr/share/doc/spamassassin/README.ldap
@@ -126,6 +130,7 @@ binary-indep: build-indep install-indep
        dh_installman -i sa-awl.1p sa-check_spamd.1p
        dh_installdocs -i
        dh_systemd_enable --no-enable
+       dh_systemd_enable --no-enable --package=spamassassin sa-update.timer
        dh_installexamples -i
        dh_installinit -i -- defaults 19 21
        dh_installcron -i
diff --git a/debian/sa-update.service b/debian/sa-update.service
new file mode 100644
index 0000000..2e23fed
--- /dev/null
+++ b/debian/sa-update.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Update spamassassin rules
+ConditionFileIsExecutable=/usr/bin/sa-update
+ConditionFileIsExecutable=/usr/bin/gpg
+
+[Service]
+Type=oneshot
+ExecStart=/etc/spamassassin/spamassassin.cron.common
\ No newline at end of file
diff --git a/debian/sa-update.timer b/debian/sa-update.timer
new file mode 100644
index 0000000..d94dcc1
--- /dev/null
+++ b/debian/sa-update.timer
@@ -0,0 +1,10 @@
+[Unit]
+Description=Update spamassassin rules daily
+
+[Timer]
+OnCalendar=daily
+RandomizedDelaySec=3600
+Persistent=true
+
+[Install]
+WantedBy=timers.target
\ No newline at end of file
diff --git a/debian/spamassassin.cron.common b/debian/spamassassin.cron.common
new file mode 100644
index 0000000..7543b4c
--- /dev/null
+++ b/debian/spamassassin.cron.common
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+# If there's a problem with the ruleset or configs, print the output
+# of spamassassin --lint (which will typically get emailed to root)
+# and abort.
+die_with_lint() {
+    env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
+        --chuid debian-spamd:debian-spamd --start \
+        --exec /usr/bin/spamassassin -- -D --lint 2>&1
+    exit 1
+}
+
+do_compile() {
+# Compile rules if the required tools are available. Prior to version
+# 3.3.2-8, there was an additional check to verify that an sa-compile
+# run had previously been executed by hand. With sa-learn now
+# distributed in a separate, optional, package, this check is no
+# longer necessary.
+    if [ -x /usr/bin/re2c -a -x /usr/bin/sa-compile ]; then
+        env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
+            --chuid debian-spamd:debian-spamd --start \
+            --exec /usr/bin/sa-compile -- --quiet
+
+        # Fixup perms -- group and other should be able to
+        # read and execute, but never write.  Works around
+        # sa-compile's failure to obey umask.
+        chmod -R go-w,go+rX /var/lib/spamassassin/compiled
+    fi
+}
+
+# Tell a running spamd to reload its configs and rules.
+reload() {
+    # Reload
+    if [ -d /run/systemd/system ]; then
+        systemctl --quiet try-reload-or-restart spamassassin
+    else
+        if which invoke-rc.d >/dev/null 2>&1; then
+            invoke-rc.d --quiet spamassassin status > /dev/null && \
+            invoke-rc.d spamassassin reload > /dev/null
+        else
+            invoke-rc.d --quiet spamassassin status > /dev/null && \
+            /etc/init.d/spamassassin reload > /dev/null
+        fi
+    fi
+
+    if [ -d /etc/spamassassin/sa-update-hooks.d ]; then
+        run-parts --lsbsysinit /etc/spamassassin/sa-update-hooks.d
+    fi
+}
+
+# Update
+umask 022
+env -i LANG="$LANG" PATH="$PATH" http_proxy="$http_proxy" \
+    start-stop-daemon --chuid debian-spamd:debian-spamd --start \
+    --exec /usr/bin/sa-update -- \
+    --gpghomedir /var/lib/spamassassin/sa-update-keys 2>&1
+
+case $? in
+    0)
+        # got updates!
+        env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
+            --chuid debian-spamd:debian-spamd --start \
+            --exec /usr/bin/spamassassin -- --lint 2>&1 || die_with_lint
+        do_compile
+        reload
+        ;;
+    1)
+        # no updates
+        exit 0
+        ;;
+    2)
+        # lint failed!
+        die_with_lint
+        ;;
+    *)
+        echo "sa-update failed for unknown reasons" 1>&2
+        ;;
+esac
+
+# Local variables:
+# mode: shell-script
+# tab-width: 4
+# indent-tabs-mode: nil
+# end:
diff --git a/debian/spamassassin.cron.daily b/debian/spamassassin.cron.daily
index 1b42ac8..cf4af2b 100644
--- a/debian/spamassassin.cron.daily
+++ b/debian/spamassassin.cron.daily
@@ -14,53 +14,10 @@ test -x /usr/bin/sa-update || exit 0
 test -x /etc/init.d/spamassassin || exit 0
 command -v gpg > /dev/null || exit 0
 
-if [ "$CRON" = "0" ] ; then
+if [ "$CRON" = "0" -o -d /run/systemd/system ] ; then
     exit 0
 fi
 
-# If there's a problem with the ruleset or configs, print the output
-# of spamassassin --lint (which will typically get emailed to root)
-# and abort.
-die_with_lint() {
-    env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
-        --chuid debian-spamd:debian-spamd --start \
-        --exec /usr/bin/spamassassin -- -D --lint 2>&1
-    exit 1
-}
-
-do_compile() {
-# Compile rules if the required tools are available. Prior to version
-# 3.3.2-8, there was an additional check to verify that an sa-compile
-# run had previously been executed by hand. With sa-learn now
-# distributed in a separate, optional, package, this check is no
-# longer necessary.
-    if [ -x /usr/bin/re2c -a -x /usr/bin/sa-compile ]; then
-        env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
-            --chuid debian-spamd:debian-spamd --start \
-            --exec /usr/bin/sa-compile -- --quiet
-
-        # Fixup perms -- group and other should be able to
-        # read and execute, but never write.  Works around
-        # sa-compile's failure to obey umask.
-        chmod -R go-w,go+rX /var/lib/spamassassin/compiled
-    fi
-}
-
-# Tell a running spamd to reload its configs and rules.
-reload() {
-    # Reload
-    if which invoke-rc.d >/dev/null 2>&1; then
-        invoke-rc.d --quiet spamassassin status > /dev/null && \
-          invoke-rc.d spamassassin reload > /dev/null
-    else
-        invoke-rc.d --quiet spamassassin status > /dev/null && \
-          /etc/init.d/spamassassin reload > /dev/null
-    fi
-    if [ -d /etc/spamassassin/sa-update-hooks.d ]; then
-        run-parts --lsbsysinit /etc/spamassassin/sa-update-hooks.d
-    fi
-}
-
 # Sleep for up to 3600 seconds if not running interactively
 if [ ! -t 0 ]; then
     RANGE=3600
@@ -69,34 +26,7 @@ if [ ! -t 0 ]; then
     sleep $number
 fi
 
-# Update
-umask 022
-env -i LANG="$LANG" PATH="$PATH" http_proxy="$http_proxy" \
-    start-stop-daemon --chuid debian-spamd:debian-spamd --start \
-    --exec /usr/bin/sa-update -- \
-    --gpghomedir /var/lib/spamassassin/sa-update-keys 2>&1
-
-case $? in
-    0)
-        # got updates!
-        env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
-            --chuid debian-spamd:debian-spamd --start \
-            --exec /usr/bin/spamassassin -- --lint 2>&1 || die_with_lint
-        do_compile
-        reload
-        ;;
-    1)
-        # no updates
-        exit 0
-        ;;
-    2)
-        # lint failed!
-        die_with_lint
-        ;;
-    *)
-        echo "sa-update failed for unknown reasons" 1>&2
-        ;;
-esac
+. /etc/spamassassin/spamassassin.cron.common
 
 # Local variables:
 # mode: shell-script
diff --git a/debian/spamassassin.default b/debian/spamassassin.default
index 1e957b7..a8bfe68 100644
--- a/debian/spamassassin.default
+++ b/debian/spamassassin.default
@@ -29,6 +29,9 @@ PIDFILE="/var/run/spamd.pid"
 #NICE="--nicelevel 15"
 
 # Cronjob
+# If you're using systemd (default for jessie), the CRON setting is
+# not used. Instead, enable the cronjob by issuing:
+# systemctl enable sa-update.timer
 # Set to anything but 0 to enable the cron job to automatically update
-# spamassassin's rules on a nightly basis
+# spamassassin's rules on a nightly basis on systems using sysvinit
 CRON=0
diff --git a/debian/spamassassin.dirs b/debian/spamassassin.dirs
index 6b080c8..ff7d6e3 100644
--- a/debian/spamassassin.dirs
+++ b/debian/spamassassin.dirs
@@ -3,6 +3,7 @@ etc/init.d
 etc/default
 etc/spamassassin
 etc/spamassassin/sa-update-hooks.d
+lib/systemd/system
 usr/share/doc/spamassassin
 usr/share/doc/spamassassin/rules
 usr/share/spamassassin
-- 
2.11.0

Reply via email to