diff --git a/debian/scripts/koha-generate-env b/debian/scripts/koha-generate-env index fe05cfdadc..9f008d2ae4 100755 --- a/debian/scripts/koha-generate-env +++ b/debian/scripts/koha-generate-env @@ -2,8 +2,11 @@ # koha-generate-env -- Generate EnvironmentFile for systemd units # Usage: koha-generate-env # -# Reads koha-conf.xml and writes /run/koha//.env -# with the parameters that the corresponding systemd unit needs. +# Reads koha-conf.xml and /etc/default/koha-common and writes +# /run/koha//.env with the parameters the matching +# systemd unit needs. The units run it as root (ExecStartPre=+) so it can +# read the instance's koha-conf.xml, then pick the file up through +# EnvironmentFile= before ExecStart runs. set -e @@ -18,6 +21,8 @@ fi CONF="/etc/koha/sites/${INSTANCE}/koha-conf.xml" ENVDIR="/run/koha/${INSTANCE}" ENVFILE="${ENVDIR}/${SERVICE}.env" +DEFAULTS="/etc/default/koha-common" +KOHA_HOME="/usr/share/koha" if [ ! -f "$CONF" ]; then echo "Error: $CONF not found" >&2 @@ -29,12 +34,31 @@ xml_val() { xmlstarlet sel -t -v "yazgfs/config/$1" "$CONF" 2>/dev/null || true } -mkdir -p "$ENVDIR" +# systemd parses the env file line by line and the units pass most values +# as a single argument, so a value must fit on one line and must not +# contain the quote characters used below. +clean() { + printf '%s' "$1" | tr -d '\n\r"\\' +} + +# number_or : keep only if it is a plain integer +number_or() { + case "$1" in + '' | *[!0-9]*) printf '%s' "$2" ;; + *) printf '%s' "$1" ;; + esac +} + +# koha-create-dirs normally creates this; make sure it exists with the +# ownership the instance user expects if we got here first. +if [ ! -d "$ENVDIR" ]; then + install -d -o "${INSTANCE}-koha" -g "${INSTANCE}-koha" "$ENVDIR" +fi case "$SERVICE" in plack) - WORKERS=$(xml_val plack_workers) - MAX_REQUESTS=$(xml_val plack_max_requests) + WORKERS=$(number_or "$(xml_val plack_workers)" 2) + MAX_REQUESTS=$(number_or "$(xml_val plack_max_requests)" 50) DEBUG_MODE=$(xml_val debug_mode) PSGIFILE="/etc/koha/plack.psgi" @@ -42,69 +66,99 @@ case "$SERVICE" in PSGIFILE="/etc/koha/sites/${INSTANCE}/plack.psgi" fi - if [ "$DEBUG_MODE" = "1" ] || [ "$DEBUG_MODE" = "true" ]; then + # Same rule as is_debug_mode in koha-functions.sh: anything set + # and not "0" means development. + if [ -n "$DEBUG_MODE" ] && [ "$DEBUG_MODE" != "0" ]; then ENVIRONMENT="development" else ENVIRONMENT="deployment" fi - cat > "$ENVFILE" <"$ENVFILE" < "$ENVFILE" <"$ENVFILE" < "$ENVFILE" <"$ENVFILE" < "$ENVFILE" <"$ENVFILE" </dev/null || true) + # Additional options: /etc/default/koha-common wins, then the + # z3950_responder_options element of the responder's config.xml, + # as in the SysV path. + Z3950_ADDITIONAL_OPTS="" + if [ -f "$DEFAULTS" ]; then + . "$DEFAULTS" + fi + if [ -z "$Z3950_ADDITIONAL_OPTS" ] && [ -f "${CONFIGDIR}/config.xml" ]; then + Z3950_ADDITIONAL_OPTS=$(xmlstarlet sel -t -v 'yazgfs/config/z3950_responder_options' "${CONFIGDIR}/config.xml" 2>/dev/null || true) fi - cat > "$ENVFILE" <"$ENVFILE" <"$ENVFILE" + ;; esac chmod 640 "$ENVFILE" diff --git a/debian/systemd/koha-common.service b/debian/systemd/koha-common.service index 57aacd5204..8369a57b13 100644 --- a/debian/systemd/koha-common.service +++ b/debian/systemd/koha-common.service @@ -1,16 +1,21 @@ [Unit] -Description=Koha ILS - All instances (compat) +Description=Koha ILS - All instances (compat wrapper around koha.target) Documentation=man:koha-common(8) -BindsTo=koha.target -After=koha.target +# This unit exists so that "systemctl start|stop|restart koha-common" (the +# old "service koha-common ..." habit) keeps working. It deliberately has NO +# dependency on koha.target: with BindsTo=/After= its own stop job would be +# ordered against the koha.target stop job that ExecStop= waits for, and the +# stop would hang until TimeoutStopSec. It is a command shim only; manage +# koha.target or the per-instance units for anything else. [Service] Type=oneshot RemainAfterExit=yes +SyslogIdentifier=koha-common # Instances can switch to Elasticsearch after their units were enabled, so # re-discover ES indexers on every start, like the SysV init script did. ExecStartPre=-/usr/sbin/koha-systemd-ctl sync-es-indexers -ExecStart=/bin/true +ExecStart=/bin/systemctl start koha.target ExecStop=/bin/systemctl stop koha.target [Install]