Creates .venv/ with python3 -m venv if not present, then installs/ updates requirements.txt into it before setting up the systemd service. The service ExecStart always uses the venv Python. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
124 lines
4.3 KiB
Bash
Executable File
124 lines
4.3 KiB
Bash
Executable File
#!/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"
|
|
|
|
VENV="$SCRIPT_DIR/.venv"
|
|
PYTHON="$VENV/bin/python"
|
|
|
|
echo "==> we_monitor installer"
|
|
echo " Project : $SCRIPT_DIR"
|
|
echo " User : $USER"
|
|
echo ""
|
|
|
|
# ── 1. virtual environment ────────────────────────────────────────────────────
|
|
if [[ ! -x "$PYTHON" ]]; then
|
|
echo "==> Creating virtual environment : $VENV"
|
|
python3 -m venv "$VENV"
|
|
else
|
|
echo "==> Virtual environment exists : $VENV"
|
|
fi
|
|
|
|
echo "==> Installing / updating dependencies"
|
|
"$VENV/bin/pip" install --quiet --upgrade pip
|
|
"$VENV/bin/pip" install --quiet -r "$SCRIPT_DIR/requirements.txt"
|
|
echo " Python : $PYTHON"
|
|
echo ""
|
|
|
|
# ── 2. 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
|
|
|
|
# ── 3. log directory ──────────────────────────────────────────────────────────
|
|
mkdir -p "$LOG_DIR"
|
|
echo "==> Log directory : $LOG_DIR"
|
|
|
|
# ── 4. 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"
|
|
|
|
# ── 5. 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
|
|
|
|
# ── 6. 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 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |