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:
+50
-24
@@ -1,16 +1,15 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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:
|
# Fresh install:
|
||||||
# 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:
|
|
||||||
# chmod +x install.sh
|
# chmod +x install.sh
|
||||||
# ./install.sh
|
# ./install.sh
|
||||||
# # then edit ~/.config/we_monitor/credentials.json
|
# # then edit ~/.config/we_monitor/credentials.json
|
||||||
# systemctl --user start we_monitor
|
# systemctl --user start we_monitor
|
||||||
|
#
|
||||||
|
# Update (code already pulled via git):
|
||||||
|
# ./install.sh
|
||||||
|
# (restarts the service automatically if it was running)
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
@@ -25,7 +24,21 @@ LOG_DIR="$SCRIPT_DIR/logs"
|
|||||||
VENV="$SCRIPT_DIR/.venv"
|
VENV="$SCRIPT_DIR/.venv"
|
||||||
PYTHON="$VENV/bin/python"
|
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 " Project : $SCRIPT_DIR"
|
||||||
echo " User : $USER"
|
echo " User : $USER"
|
||||||
echo ""
|
echo ""
|
||||||
@@ -40,11 +53,11 @@ fi
|
|||||||
|
|
||||||
echo "==> Installing / updating dependencies"
|
echo "==> Installing / updating dependencies"
|
||||||
"$VENV/bin/pip" install --quiet --upgrade pip
|
"$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 " Python : $PYTHON"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# ── 2. credentials template ───────────────────────────────────────────────────
|
# ── 2. credentials template (only on fresh install) ───────────────────────────
|
||||||
mkdir -p "$CREDS_DIR"
|
mkdir -p "$CREDS_DIR"
|
||||||
chmod 700 "$CREDS_DIR"
|
chmod 700 "$CREDS_DIR"
|
||||||
|
|
||||||
@@ -108,24 +121,37 @@ else
|
|||||||
echo " Run as root to fix: loginctl enable-linger $USER"
|
echo " Run as root to fix: loginctl enable-linger $USER"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── 6. reload and enable (but do NOT start yet) ───────────────────────────────
|
# ── 6. reload systemd and enable / restart ────────────────────────────────────
|
||||||
systemctl --user daemon-reload
|
systemctl --user daemon-reload
|
||||||
systemctl --user enable "$SERVICE_NAME"
|
systemctl --user enable "$SERVICE_NAME"
|
||||||
|
|
||||||
|
if $WAS_ACTIVE; then
|
||||||
|
systemctl --user restart "$SERVICE_NAME"
|
||||||
|
echo "==> Service restarted"
|
||||||
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
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 ""
|
||||||
echo " Next steps:"
|
echo " Check status : systemctl --user status $SERVICE_NAME"
|
||||||
echo " 1. Edit credentials (username, password, vin, interval_s, …):"
|
echo " Follow logs : journalctl --user -u $SERVICE_NAME -f"
|
||||||
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 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||||
Reference in New Issue
Block a user