Add install.sh: systemd user service setup

Creates ~/.config/we_monitor/credentials.json template (mode 600),
writes ~/.config/systemd/user/we_monitor.service using the project
venv Python, enables linger for boot-time start, and enables the
service. Does not start until credentials are edited.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 22:59:49 +02:00
co-authored by Claude Sonnet 4.6
parent a01ae714c8
commit cf6f092cbb
Executable
+115
View File
@@ -0,0 +1,115 @@
#!/usr/bin/env bash
# Install 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:
# chmod +x install.sh
# ./install.sh
# # then edit ~/.config/we_monitor/credentials.json
# systemctl --user start we_monitor
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICE_NAME="we_monitor"
CREDS_DIR="$HOME/.config/we_monitor"
CREDS_FILE="$CREDS_DIR/credentials.json"
SYSTEMD_DIR="$HOME/.config/systemd/user"
SERVICE_FILE="$SYSTEMD_DIR/$SERVICE_NAME.service"
LOG_DIR="$SCRIPT_DIR/logs"
# Prefer project venv, fall back to system python3
if [[ -x "$SCRIPT_DIR/.venv/bin/python" ]]; then
PYTHON="$SCRIPT_DIR/.venv/bin/python"
else
PYTHON="$(command -v python3)"
fi
echo "==> we_monitor installer"
echo " Project : $SCRIPT_DIR"
echo " Python : $PYTHON"
echo " User : $USER"
echo ""
# ── 1. credentials template ───────────────────────────────────────────────────
mkdir -p "$CREDS_DIR"
chmod 700 "$CREDS_DIR"
if [[ ! -f "$CREDS_FILE" ]]; then
cat > "$CREDS_FILE" <<'EOF'
{
"username": "me@example.com",
"password": "secret",
"vin": "",
"domains": ["charging", "measurements", "readiness"]
}
EOF
chmod 600 "$CREDS_FILE"
echo "==> Created credentials template : $CREDS_FILE"
echo " *** Edit this file before starting the service! ***"
else
echo "==> Credentials file exists : $CREDS_FILE"
fi
# ── 2. log directory ──────────────────────────────────────────────────────────
mkdir -p "$LOG_DIR"
echo "==> Log directory : $LOG_DIR"
# ── 3. systemd user service ───────────────────────────────────────────────────
mkdir -p "$SYSTEMD_DIR"
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=WeConnect vehicle data collector
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=$SCRIPT_DIR
ExecStart=$PYTHON $SCRIPT_DIR/collect.py -c $CREDS_FILE
Restart=on-failure
RestartSec=60
StandardOutput=journal
StandardError=journal
SyslogIdentifier=$SERVICE_NAME
[Install]
WantedBy=default.target
EOF
echo "==> Installed service unit : $SERVICE_FILE"
# ── 4. enable linger (service survives logout / starts at boot) ───────────────
if loginctl enable-linger "$USER" 2>/dev/null; then
echo "==> Linger enabled for $USER (service starts at boot)"
else
echo " Note: could not enable linger — service runs only while logged in"
echo " Run as root to fix: loginctl enable-linger $USER"
fi
# ── 5. reload and enable (but do NOT start yet) ───────────────────────────────
systemctl --user daemon-reload
systemctl --user enable "$SERVICE_NAME"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Installation complete."
echo ""
echo " Next steps:"
echo " 1. Edit credentials:"
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 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"