fix: preserve systemd enable/disable state on package upgrade

The preremove script was unconditionally stopping and disabling the
service, which meant upgrades (dpkg -i new.deb) would disable the
service. Users had to manually re-enable after every upgrade.

Now:
- preremove: only stop+disable on actual removal (not upgrade)
  Checks $1 for "remove"/"purge" (deb) or "0" (rpm)
- postinstall: restart the service on upgrade if it was running,
  preserving enable/disable state. Only shows first-install
  instructions on initial install.

Tested with shellcheck.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-03-29 19:26:29 +02:00
commit 3341e9a818
2 changed files with 48 additions and 9 deletions

26
dist/postinstall.sh vendored
View file

@ -1,6 +1,9 @@
#!/bin/sh
# Post-install script for Favoritter .deb/.rpm package.
# Creates the system user and sets directory permissions.
# Creates the system user, sets directory permissions, and handles upgrades.
#
# Debian/Ubuntu: called with "configure" on install/upgrade.
# RPM: called with 1 on first install, 2+ on upgrade.
set -e
# Create system user if it doesn't exist.
@ -12,10 +15,25 @@ fi
install -d -o favoritter -g favoritter -m 0750 /var/lib/favoritter
install -d -o favoritter -g favoritter -m 0750 /var/lib/favoritter/uploads
# Reload systemd to pick up the service file.
# Reload systemd to pick up any service file changes.
if command -v systemctl >/dev/null 2>&1; then
systemctl daemon-reload
# On upgrade: restart the service if it was running.
# This picks up the new binary without losing enable/disable state.
if systemctl is-active --quiet favoritter 2>/dev/null; then
systemctl restart favoritter
fi
fi
echo "Favoritter installed. Configure /etc/favoritter/favoritter.env then run:"
echo " sudo systemctl enable --now favoritter"
# Only show the setup message on first install (not upgrades).
action="${1:-}"
case "$action" in
# Debian first install or RPM first install ($1=1).
configure|1)
if ! systemctl is-enabled --quiet favoritter 2>/dev/null; then
echo "Favoritter installed. Configure /etc/favoritter/favoritter.env then run:"
echo " sudo systemctl enable --now favoritter"
fi
;;
esac

27
dist/preremove.sh vendored
View file

@ -1,9 +1,30 @@
#!/bin/sh
# Pre-remove script for Favoritter .deb/.rpm package.
# Stops the service before package removal.
# Only stops and disables the service on actual removal, not on upgrade.
#
# Debian/Ubuntu: called with "remove" on uninstall, "upgrade" on upgrade.
# RPM (Fedora/RHEL): called with 0 on final removal, 1+ on upgrade.
set -e
if command -v systemctl >/dev/null 2>&1; then
action="${1:-}"
case "$action" in
# Debian: full removal.
remove|purge)
if command -v systemctl >/dev/null 2>&1; then
systemctl stop favoritter 2>/dev/null || true
systemctl disable favoritter 2>/dev/null || true
fi
fi
;;
# RPM: $1 is the number of remaining installations.
0)
if command -v systemctl >/dev/null 2>&1; then
systemctl stop favoritter 2>/dev/null || true
systemctl disable favoritter 2>/dev/null || true
fi
;;
# Debian "upgrade" or RPM "1+" — do nothing, the service stays running.
# The new postinstall will daemon-reload and restart.
*)
;;
esac