Koha-clms/debian/koha-systemd.postinst

133 lines
5.2 KiB
Bash

#!/bin/sh
set -e
DEFAULTS=/etc/default/koha-common
# On a systemd host the daemons started by /etc/init.d/koha-common live in
# the cgroup of the koha-common.service that systemd-sysv-generator created
# for the init script. By the time this postinst runs, the init script has
# already been removed by koha-common's rm_conffile and the koha-* helpers
# already dispatch to systemd, so neither of them can stop those daemons
# any more. Kill the legacy cgroup, then sweep whatever still holds a pid
# file in the instances' run directories (daemon(1) supervisors, starman,
# the Z39.50 responder).
stop_legacy_daemons() {
if systemctl is-active --quiet koha-common.service 2>/dev/null; then
# The generated unit has KillMode=process; a plain stop would leave
# the daemon(1) supervisors and their children running.
systemctl kill --signal=SIGTERM koha-common.service 2>/dev/null || true
systemctl stop koha-common.service 2>/dev/null || true
fi
for pidfile in /run/koha/*/*.pid; do
[ -f "$pidfile" ] || continue
pid=$(cat "$pidfile" 2>/dev/null) || continue
case "$pid" in
'' | *[!0-9]*) continue ;;
esac
kill -TERM "$pid" 2>/dev/null || true
done
# Wait (briefly) for them to release their sockets and pid files.
i=0
while [ "$i" -lt 10 ]; do
alive=0
for pidfile in /run/koha/*/*.pid; do
[ -f "$pidfile" ] || continue
pid=$(cat "$pidfile" 2>/dev/null) || continue
case "$pid" in
'' | *[!0-9]*) continue ;;
esac
if kill -0 "$pid" 2>/dev/null; then
alive=1
fi
done
[ "$alive" -eq 0 ] && break
sleep 1
i=$((i + 1))
done
}
# First install (migration from the SysV-managed layout): enable the units
# that correspond to what the init script used to start for each enabled
# instance, then start everything.
enable_existing_instances() {
USE_INDEXER_DAEMON="yes"
if [ -f "$DEFAULTS" ]; then
. "$DEFAULTS"
fi
plack_instances=$(koha-list --enabled --plack 2>/dev/null || true)
z3950_instances=$(koha-list --enabled --z3950 2>/dev/null || true)
es_instances=$(koha-list --enabled --elasticsearch 2>/dev/null || true)
for instance in $(koha-list --enabled 2>/dev/null); do
# Always on
systemctl enable "koha-create-dirs@${instance}.service" 2>/dev/null || true
systemctl enable "koha-zebra@${instance}.service" 2>/dev/null || true
systemctl enable "koha-worker@${instance}.service" 2>/dev/null || true
systemctl enable "koha-worker-long_tasks@${instance}.service" 2>/dev/null || true
systemctl enable "koha@${instance}.target" 2>/dev/null || true
# Plack: the init script started it for "koha-list --enabled --plack"
# (Apache configuration), so do the same rather than for everyone.
if echo "$plack_instances" | grep -qx "$instance"; then
systemctl enable "koha-plack@${instance}.service" 2>/dev/null || true
fi
# SIP: read the SysV-era flag file DIRECTLY here, not via
# koha-list --sip / is_sip_enabled. The latter consults
# systemctl is-enabled on systemd hosts, which is "disabled" for
# every instance during this very first install. The flag file is
# the only signal for "did this instance have SIP enabled in the
# SysV-era state we are migrating from?".
if [ -e "/var/lib/koha/${instance}/sip.enabled" ]; then
systemctl enable "koha-sip@${instance}.service" 2>/dev/null || true
fi
if echo "$z3950_instances" | grep -qx "$instance"; then
systemctl enable "koha-z3950@${instance}.service" 2>/dev/null || true
fi
# Indexing: the ES indexer for Elasticsearch instances, otherwise
# the Zebra indexer daemon if /etc/default/koha-common asks for it,
# which is what the init script did.
if echo "$es_instances" | grep -qx "$instance"; then
systemctl enable "koha-es-indexer@${instance}.service" 2>/dev/null || true
elif [ "$USE_INDEXER_DAEMON" = "yes" ]; then
systemctl enable "koha-indexer@${instance}.service" 2>/dev/null || true
fi
done
systemctl enable koha.target 2>/dev/null || true
systemctl enable koha-common.service 2>/dev/null || true
}
case "$1" in
configure)
if [ -z "$2" ]; then
stop_legacy_daemons
fi
systemctl daemon-reload || true
if [ -z "$2" ]; then
enable_existing_instances
deb-systemd-invoke start koha.target koha-common.service >/dev/null || true
else
# Upgrade: restart the running per-component units so they pick
# up new code and unit files. deb-systemd-invoke takes unit
# names, not patterns, so enumerate them first.
units=$(systemctl list-units --plain --no-legend --state=active 'koha-*@*.service' 2>/dev/null | awk '{print $1}')
if [ -n "$units" ]; then
# shellcheck disable=SC2086
deb-systemd-invoke try-restart $units >/dev/null || true
fi
fi
;;
esac
#DEBHELPER#
exit 0