From cf6f092cbbe8c80c5db7ac8e253d258330a7641b Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 25 May 2026 22:59:49 +0200 Subject: [PATCH] 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 --- install.sh | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100755 install.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..ce72c81 --- /dev/null +++ b/install.sh @@ -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" < 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 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" \ No newline at end of file