#!/bin/bash set -euo pipefail [ "$(id -u)" -eq 0 ] || { echo 'Run as root on the sensor' >&2; exit 1; } STAGE=$(cd "$(dirname "$0")/.." && pwd) python3 -c 'import yaml, requests' [ -s /etc/leaf-controller/env ] || { echo 'Create root-only /etc/leaf-controller/env with LEAF_VYOS_KEY first' >&2; exit 1; } [ "$(findmnt -n -o FSTYPE --target /var/log/suricata-leaf)" = tmpfs ] || { echo 'Provision the sensor log tmpfs first' >&2; exit 1; } if systemctl is-active --quiet suricata.service; then echo 'The stock Suricata service is active; stop it before installing a second sensor.' >&2 exit 1 fi systemctl stop leaf-eve-rotate.timer leaf-eve-rotate.service 2>/dev/null || true systemctl stop suricata-leaf.service leaf-controller.service 2>/dev/null || true BACKUP=/var/backups/leaf-proto/$(date +%Y%m%d-%H%M%S) install -d -m 700 "$BACKUP" items=() for item in etc/suricata-leaf etc/leaf-controller opt/leaf-controller home/yuru/leaf-proto etc/systemd/system/leaf-controller.service etc/systemd/system/suricata-leaf.service etc/systemd/system/leaf-eve-rotate.service etc/systemd/system/leaf-eve-rotate.timer etc/tmpfiles.d/leaf-suricata.conf; do if [ -e "/$item" ]; then items+=("$item"); fi done tar -czf "$BACKUP/sensor-files.tar.gz" -C / "${items[@]}" python3 - "$BACKUP" <<'PY' import sqlite3,sys,pathlib p=pathlib.Path('/var/lib/leaf-controller/state.sqlite') if p.exists(): with sqlite3.connect(p) as source, sqlite3.connect(sys.argv[1]+'/state.sqlite') as target: source.backup(target) PY rollback() { rc=$? trap - ERR echo "Installation failed; restoring previous files from $BACKUP" >&2 systemctl stop suricata-leaf leaf-controller leaf-eve-rotate.timer 2>/dev/null || true for unit in leaf-controller.service suricata-leaf.service leaf-eve-rotate.service leaf-eve-rotate.timer; do if ! tar -tzf "$BACKUP/sensor-files.tar.gz" | grep -qx "etc/systemd/system/$unit"; then systemctl disable "$unit" 2>/dev/null || true rm -f "/etc/systemd/system/$unit" fi done tar -xzf "$BACKUP/sensor-files.tar.gz" -C / if [ -f "$BACKUP/state.sqlite" ]; then install -o leafctrl -g leafctrl -m 640 "$BACKUP/state.sqlite" /var/lib/leaf-controller/state.sqlite rm -f /var/lib/leaf-controller/state.sqlite-wal /var/lib/leaf-controller/state.sqlite-shm fi systemctl daemon-reload systemctl start leaf-controller suricata-leaf || true exit "$rc" } trap rollback ERR getent group leafctrl >/dev/null || groupadd --system leafctrl id leafctrl >/dev/null 2>&1 || useradd --system --gid leafctrl --home-dir /nonexistent --shell /usr/sbin/nologin leafctrl install -d -o leafctrl -g leafctrl -m 750 /var/lib/leaf-controller /var/log/leaf-controller chown -R leafctrl:leafctrl /var/lib/leaf-controller /var/log/leaf-controller find /var/lib/leaf-controller /var/log/leaf-controller -type f -exec chmod 640 {} + install -d -m 755 /opt/leaf-controller/controller cp -a "$STAGE/controller/leaf_controller" /opt/leaf-controller/controller/ chown -R root:root /opt/leaf-controller chmod -R a+rX /opt/leaf-controller install -d -o root -g leafctrl -m 750 /etc/leaf-controller # Preserve site policy and TLS settings on upgrades; seed only a new install. if [ ! -s /etc/leaf-controller/config.yaml ]; then install -m 644 "$STAGE/config/prototype-vyos.yaml" /etc/leaf-controller/config.yaml fi install -d -m 755 /opt/leaf-controller/tools install -m 755 "$STAGE/tests/live_helper.py" /opt/leaf-controller/tools/leaf-live-helper.py install -m 755 "$STAGE/scripts/rotate-eve.py" "$STAGE/scripts/rotate-vyos-key.py" "$STAGE/scripts/vyos_show.py" "$STAGE/scripts/vyos_smoke.py" "$STAGE/scripts/vyos_ctl.py" "$STAGE/scripts/probe-vyos-read.py" /opt/leaf-controller/tools/ install -m 644 "$STAGE/suricata/leaf-eve-rotate.service" "$STAGE/suricata/leaf-eve-rotate.timer" /etc/systemd/system/ install -m 644 "$STAGE/controller/leaf-controller.service" /etc/systemd/system/leaf-controller.service install -d -m 755 /etc/suricata-leaf/rules install -m 644 "$STAGE/suricata/suricata-leaf.yaml" /etc/suricata-leaf/suricata-leaf.yaml install -m 644 "$STAGE/suricata/rules/leaf-local.rules" /etc/suricata-leaf/rules/leaf-local.rules install -m 644 "$STAGE/suricata/suricata-leaf.service" /etc/systemd/system/suricata-leaf.service # tmpfs mount stays in place; recreate permissions after reboot via tmpfiles. printf '%s\n' 'd /var/log/suricata-leaf 0750 root leafctrl -' \ 'd /run/suricata 0770 root leafctrl -' > /etc/tmpfiles.d/leaf-suricata.conf systemd-tmpfiles --create /etc/tmpfiles.d/leaf-suricata.conf chown root:leafctrl /var/log/suricata-leaf chmod 750 /var/log/suricata-leaf find /var/log/suricata-leaf -maxdepth 1 -type f -exec chgrp leafctrl {} + find /var/log/suricata-leaf -maxdepth 1 -type f -exec chmod 640 {} + systemctl daemon-reload systemctl enable suricata-leaf.service leaf-controller.service leaf-eve-rotate.timer systemctl start suricata-leaf.service leaf-controller.service leaf-eve-rotate.timer sleep 2 systemctl is-active --quiet suricata-leaf.service systemctl is-active --quiet leaf-controller.service trap - ERR printf 'Backup: %s\n' "$BACKUP" systemctl --no-pager --full status suricata-leaf.service leaf-controller.service