9706: Stop systemd services when their packages are uninstalled.
authorTom Clegg <tom@curoverse.com>
Mon, 15 Aug 2016 20:10:29 +0000 (16:10 -0400)
committerTom Clegg <tom@curoverse.com>
Tue, 23 Aug 2016 17:28:18 +0000 (13:28 -0400)
build/go-package-scripts/postinst
build/go-package-scripts/prerm [new file with mode: 0755]
build/run-library.sh

index 3adb0fc04882c5c1e6000902f658b50bbda8237a..8559a97d4421e36674cce48a09d64182746284b7 100755 (executable)
@@ -15,12 +15,14 @@ case "${1}" in
             eval "$(systemctl -p UnitFileState show "$systemd_unit")"
             case "$UnitFileState" in
                 disabled)
-                    systemctl enable "$systemd_unit"
-                    # Failing to start the service is not a package error,
-                    # so don't let an error here propagate up.
+                    # Failing to enable or start the service is not a
+                    # package error, so don't let errors here
+                    # propagate up.
+                    systemctl enable "$systemd_unit" || true
                     systemctl start "$systemd_unit" || true
                     ;;
                 enabled)
+                    systemctl daemon-reload || true
                     systemctl reload-or-try-restart "$systemd_unit" || true
                     ;;
             esac
diff --git a/build/go-package-scripts/prerm b/build/go-package-scripts/prerm
new file mode 100755 (executable)
index 0000000..4e1399d
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+# NOTE: This package name detection will only work on Debian.
+# If this prerm script ever starts doing work on Red Hat,
+# we'll need to adapt this code accordingly.
+script="$(basename "$0")"
+pkg="${script%.prerm}"
+systemd_unit="${pkg}.service"
+
+case "${1}" in
+    remove)
+        if [ -e /run/systemd/system ]; then
+            systemctl stop "$systemd_unit" || true
+            systemctl disable "$systemd_unit" || true
+        fi
+        ;;
+esac
index 9140ac1611bd05041bd99314015468497bc8cb5c..485e0d2974484c0e0927e69df36c97dd5dd9b97d 100755 (executable)
@@ -104,8 +104,10 @@ package_go_binary() {
     declare -a switches=()
     systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
     if [[ -e "${systemd_unit}" ]]; then
-        switches+=("${systemd_unit}=/lib/systemd/system/${prog}.service")
-        switches+=(--after-install "$WORKSPACE/build/go-package-scripts/postinst")
+        switches+=(
+            --after-install "$WORKSPACE/build/go-package-scripts/postinst"
+            --before-remove "$WORKSPACE/build/go-package-scripts/prerm"
+            "${systemd_unit}=/lib/systemd/system/${prog}.service")
     fi
     switches+=("$WORKSPACE/$license_file=/usr/share/doc/$prog/$license_file")