#!/bin/sh # RPM %post — post-install/upgrade scriptlet (spec §7). set -eu STORE_DIR="/var/lib/fenris" STORE_DB="${STORE_DIR}/observations.db" STORE_BAK="${STORE_DIR}/observations.db.bak" RUNTIME_PYTHON="/usr/bin/python3" VENDOR_DIR="/opt/fenris/vendor" # Detect init system if [ -d /run/systemd/system ] || [ "$(cat /proc/1/comm 2>/dev/null)" = "systemd" ]; then INIT_SYSTEM="systemd" else INIT_SYSTEM="runit" fi if [ "$1" -eq 1 ]; then # Fresh install if [ "${INIT_SYSTEM}" = "systemd" ]; then systemd-sysusers || true systemd-tmpfiles --create || true systemctl daemon-reload || true else # runit: create group, set directory permissions groupadd -f fenris install -d -o root -g fenris -m 2750 "${STORE_DIR}" 2>/dev/null || true # Mark runit service as dormant (down) for fresh install if [ -d /etc/sv/fenris-collect ] && [ ! -e /var/service/fenris-collect ]; then touch /etc/sv/fenris-collect/down fi # Ensure log directory exists mkdir -p /var/log/fenris-collect chown fenris:fenris /var/log/fenris-collect 2>/dev/null || true fi elif [ "$1" -ge 2 ]; then # Upgrade — snapshot, migration, init-system-aware reload if [ -f "${STORE_DB}" ]; then cp "${STORE_DB}" "${STORE_BAK}" 2>/dev/null || true fi if [ -d "${VENDOR_DIR}" ] && [ -f "${STORE_DB}" ]; then PYTHONPATH="${VENDOR_DIR}" "${RUNTIME_PYTHON}" -c " from fenris.store import migrate_to_latest from pathlib import Path n = migrate_to_latest(Path('${STORE_DB}')) print(f'Fenris migration: {n} step(s) applied') if n else None " 2>&1 || echo "Fenris: migration skipped (store not yet initialized)" fi if [ "${INIT_SYSTEM}" = "systemd" ]; then # Capture running unit content BEFORE daemon-reload (spec §7) RUNNING_UNITS="" for unit in fenris-collect.timer; do if systemctl is-active --quiet "${unit}" 2>/dev/null; then RUNNING_UNITS="${RUNNING_UNITS} ${unit}" fi done systemctl daemon-reload 2>/dev/null || true # Restart timer only if unit contents changed AND active for unit in ${RUNNING_UNITS}; do OLD_CONTENT="$(mktemp)" NEW_PATH="/usr/lib/systemd/system/${unit}" systemctl cat "${unit}" > "${OLD_CONTENT}" 2>/dev/null || true if ! diff -q "${OLD_CONTENT}" "${NEW_PATH}" > /dev/null 2>&1; then systemctl restart "${unit}" 2>/dev/null || true fi rm -f "${OLD_CONTENT}" done # Re-apply placement modes (store dir group access, issue #54) systemd-tmpfiles --create || true fi fi