#!/bin/sh # koha-generate-env -- Generate EnvironmentFile for systemd units # Usage: koha-generate-env # # 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 SERVICE="$1" INSTANCE="$2" if [ -z "$SERVICE" ] || [ -z "$INSTANCE" ]; then echo "Usage: koha-generate-env " >&2 exit 1 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 exit 1 fi # Safe xmlstarlet wrapper: returns empty string if key not found xml_val() { xmlstarlet sel -t -v "yazgfs/config/$1" "$CONF" 2>/dev/null || true } # 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=$(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" if [ -f "/etc/koha/sites/${INSTANCE}/plack.psgi" ]; then PSGIFILE="/etc/koha/sites/${INSTANCE}/plack.psgi" fi # 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" </dev/null || true) fi cat >"$ENVFILE" <"$ENVFILE" ;; esac chmod 640 "$ENVFILE" chown root:"${INSTANCE}-koha" "$ENVFILE"