Files
PMDB-Connector/install.sh
T

136 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
echo "Checking reachability of PMDB public route..."
if curl -s --head --request GET https://pmdb.myowntools.org | grep "200 OK" > /dev/null; then
echo "Public route is reachable."
else
echo "WARNING: Public route is not reachable. Installation might fail or connector won't start."
fi
set -e
PROFILE=$1
if [ -z "$PROFILE" ]; then
echo "Usage: ./install.sh <profile> [--yes-deps]"
exit 1
fi
# B-7 Consent-Ebene: optionale Dependencies werden nur mit ausdruecklicher
# Zustimmung (--yes-deps) nachinstalliert. Ohne Flag bleibt der Venv-Check
# beobachtend (kein Install ins Gateway-venv).
YES_DEPS=0
for _arg in "$@"; do
[ "$_arg" = "--yes-deps" ] && YES_DEPS=1
done
echo "Installing PMDB SSE Connector for profile: $PROFILE"
# 1. Venv
VENV_PATH="$HOME/pmdb-sse-connector/venv"
python3 -m venv $VENV_PATH
$VENV_PATH/bin/pip install --upgrade pip
# 2. Systemd User Unit
UNIT_FILE="$HOME/.config/systemd/user/pmdb-sse-connector@${PROFILE}.service"
mkdir -p $(dirname $UNIT_FILE)
cat <<EOF > $UNIT_FILE
[Unit]
Description=PMDB SSE Connector for %i
After=network-online.target
[Service]
Type=simple
WorkingDirectory=$HOME/pmdb-sse-connector
ExecStart=$VENV_PATH/bin/python3 connector.py --profile %i
Restart=always
RestartSec=10
Environment=PYTHONUNBUFFERED=1
KillMode=process
Environment=PMDB_RUN_TIMEOUT_S=300
Environment=PATH=$HOME/.hermes/tools/python-3.14.7+20260901-linux-x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[Install]
WantedBy=default.target
EOF
# R6-2b: Verdrahtung versionieren — Drop-in (override.conf) mit dem %i-Vertrag
# (PROFILE=NAME, PROFILE_DIR=Pfad) wird MIT erzeugt. Ohne Drop-in zerstoert ein
# kuenftiger install.sh-Lauf den produktiven Weg (Basis-Unit ohne Verdrahtung).
# Inhalt = versionierte Kopie von systemd/pmdb-sse-connector@.service.d/override.conf.
DROPIN_DIR="$HOME/.config/systemd/user/pmdb-sse-connector@.service.d"
mkdir -p "$DROPIN_DIR"
cat <<'DROPIN_EOF' > "$DROPIN_DIR/override.conf"
[Service]
Environment=PROFILE=%i
Environment=PROFILE_DIR=$HOME/pmdb-sse-connector
ExecStartPost=$HOME/pmdb-sse-connector/scripts/post_venv_check.sh
DROPIN_EOF
# 3. Watchdog (quoted heredoc: keine Shell-Expansion im Zieldatei-Inhalt)
WATCHDOG_PATH="/usr/local/bin/pmdb-connector-watchdog.sh"
sudo tee "$WATCHDOG_PATH" > /dev/null <<'WATCHDOG_EOF'
#!/bin/bash
# F-XXX (P-NEW-F-XXX-HBSHARE): Heartbeat-Datei PRO Profil (Suffix).
HB_FILE="/tmp/pmdb-sse-hb-$1"
if [ ! -f "$HB_FILE" ]; then
systemctl --user restart pmdb-sse-connector@$1
exit 0
fi
LAST_HB=$(cat "$HB_FILE")
NOW=$(date +%s)
# %% = kuerzt am ERSTEN Punkt (kein Float-Rest bei konkatenierten Timestamps).
DIFF=$((NOW - ${LAST_HB%%.*}))
if [ "$DIFF" -gt 300 ]; then
systemctl --user restart pmdb-sse-connector@$1
fi
WATCHDOG_EOF
sudo chmod +x "$WATCHDOG_PATH"
# 4. Watchdog Timer (systemd-timer)
TIMER_FILE="$HOME/.config/systemd/user/pmdb-sse-connector@${PROFILE}.timer"
cat <<EOF > $TIMER_FILE
[Unit]
Description=Watchdog Timer for PMDB SSE Connector %i
[Timer]
OnCalendar=*:0/1
Unit=pmdb-sse-connector@%i.service
[Install]
WantedBy=timers.target
EOF
systemctl --user enable pmdb-sse-connector@${PROFILE}.timer
# Token Check (Fail-fast)
TOKEN_FILE="$HOME/.hermes/mcp_tokens.json"
if [ ! -f "$TOKEN_FILE" ]; then
echo "-----------------------------------------------------------------------"
echo "ERROR: MCP Token file not found at $TOKEN_FILE"
echo "The service will crash cyclically without a valid token."
echo "Please create the file with the following structure:"
echo "{"
echo " "agenten": {"
echo " "$PROFILE": { "token": "your-token-here" }"
echo " }"
echo "}"
echo "-----------------------------------------------------------------------"
exit 1
fi
# 5. Activation
systemctl --user daemon-reload
systemctl --user enable pmdb-sse-connector@${PROFILE}.service
systemctl --user enable pmdb-sse-connector@${PROFILE}.timer
systemctl --user restart pmdb-sse-connector@${PROFILE}.service
# B-8: Trigger post-install venv check (Consent-gated: YES_DEPS)
export PROFILE=${PROFILE}
export YES_DEPS=${YES_DEPS}
$HOME/pmdb-sse-connector/scripts/post_venv_check.sh
echo "Installation complete. Service is running."