install.sh: support update mode; auto-restart if service was active

- Detect fresh install vs update via systemctl is-enabled
- On update: pip install --upgrade picks up new dependency versions
- If service was active before running the script, restart it automatically
- Summary message adapts to install vs update context

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 15:50:06 +02:00
co-authored by Claude Sonnet 4.6
parent 5944b0a7a4
commit a4ce702ee4
+50 -24
View File
@@ -1,16 +1,15 @@
#!/usr/bin/env bash
# Install we_monitor as a systemd user service.
# Install or update we_monitor as a systemd user service.
#
# What this script does:
# 1. Creates ~/.config/we_monitor/ and a credentials template if missing
# 2. Writes a systemd user service unit
# 3. Enables the service (does not start it — edit credentials first)
#
# Usage:
# Fresh install:
# chmod +x install.sh
# ./install.sh
# # then edit ~/.config/we_monitor/credentials.json
# systemctl --user start we_monitor
#
# Update (code already pulled via git):
# ./install.sh
# (restarts the service automatically if it was running)
set -euo pipefail
@@ -25,7 +24,21 @@ LOG_DIR="$SCRIPT_DIR/logs"
VENV="$SCRIPT_DIR/.venv"
PYTHON="$VENV/bin/python"
echo "==> we_monitor installer"
# detect fresh install vs update
IS_UPDATE=false
WAS_ACTIVE=false
if systemctl --user is-enabled "$SERVICE_NAME" &>/dev/null; then
IS_UPDATE=true
if systemctl --user is-active "$SERVICE_NAME" &>/dev/null; then
WAS_ACTIVE=true
fi
fi
if $IS_UPDATE; then
echo "==> we_monitor updater"
else
echo "==> we_monitor installer"
fi
echo " Project : $SCRIPT_DIR"
echo " User : $USER"
echo ""
@@ -40,11 +53,11 @@ fi
echo "==> Installing / updating dependencies"
"$VENV/bin/pip" install --quiet --upgrade pip
"$VENV/bin/pip" install --quiet -r "$SCRIPT_DIR/requirements.txt"
"$VENV/bin/pip" install --quiet --upgrade -r "$SCRIPT_DIR/requirements.txt"
echo " Python : $PYTHON"
echo ""
# ── 2. credentials template ───────────────────────────────────────────────────
# ── 2. credentials template (only on fresh install) ───────────────────────────
mkdir -p "$CREDS_DIR"
chmod 700 "$CREDS_DIR"
@@ -108,24 +121,37 @@ else
echo " Run as root to fix: loginctl enable-linger $USER"
fi
# ── 6. reload and enable (but do NOT start yet) ───────────────────────────────
# ── 6. reload systemd and enable / restart ────────────────────────────────────
systemctl --user daemon-reload
systemctl --user enable "$SERVICE_NAME"
if $WAS_ACTIVE; then
systemctl --user restart "$SERVICE_NAME"
echo "==> Service restarted"
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Installation complete."
if $IS_UPDATE; then
echo " Update complete."
echo ""
if $WAS_ACTIVE; then
echo " Service was restarted automatically."
else
echo " Service was not running — start it manually if needed:"
echo " systemctl --user start $SERVICE_NAME"
fi
else
echo " Installation complete."
echo ""
echo " Next steps:"
echo " 1. Edit credentials (username, password, vin, interval_s, …):"
echo " \$EDITOR $CREDS_FILE"
echo ""
echo " 2. Start the service:"
echo " systemctl --user start $SERVICE_NAME"
fi
echo ""
echo " Next steps:"
echo " 1. Edit credentials (username, password, vin, interval_s, …):"
echo " \$EDITOR $CREDS_FILE"
echo ""
echo " 2. Start the service:"
echo " systemctl --user start $SERVICE_NAME"
echo ""
echo " 3. Check status:"
echo " systemctl --user status $SERVICE_NAME"
echo ""
echo " 4. Follow logs:"
echo " journalctl --user -u $SERVICE_NAME -f"
echo " Check status : systemctl --user status $SERVICE_NAME"
echo " Follow logs : journalctl --user -u $SERVICE_NAME -f"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"