Files
QSfera/Server/scripts/install-rpi-production-monitoring.sh
T
2026-06-09 21:26:17 +03:00

63 lines
1.8 KiB
Bash

#!/usr/bin/env sh
set -eu
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
SERVER_DIR="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)"
SYSTEMD_DIR="${SYSTEMD_DIR:-/etc/systemd/system}"
ENV_DIR="${ENV_DIR:-/etc/qsfera}"
ENV_FILE="${ENV_FILE:-$ENV_DIR/rpi-production-check.env}"
RUN_NOW="${RUN_NOW:-true}"
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
else
SUDO="${SUDO:-sudo}"
fi
run_privileged() {
if [ -n "$SUDO" ]; then
"$SUDO" "$@"
else
"$@"
fi
}
install_unit() {
source_path="$1"
target_path="$2"
run_privileged install -m 0644 "$source_path" "$target_path"
}
tmp_env="$(mktemp)"
cleanup() {
rm -f "$tmp_env"
}
trap cleanup EXIT INT TERM
run_privileged install -d -m 0755 "$SYSTEMD_DIR" "$ENV_DIR"
if [ ! -f "$ENV_FILE" ]; then
{
echo "QSFERA_SERVER_DIR=$SERVER_DIR"
grep -v '^QSFERA_SERVER_DIR=' "$SERVER_DIR/systemd/rpi-production-check.env.example"
} >"$tmp_env"
run_privileged install -m 0640 "$tmp_env" "$ENV_FILE"
echo "Created $ENV_FILE"
else
echo "Keeping existing $ENV_FILE"
fi
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check.service" "$SYSTEMD_DIR/qsfera-rpi-production-check.service"
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check.timer" "$SYSTEMD_DIR/qsfera-rpi-production-check.timer"
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check-alert@.service" "$SYSTEMD_DIR/qsfera-rpi-production-check-alert@.service"
run_privileged systemctl daemon-reload
run_privileged systemctl enable --now qsfera-rpi-production-check.timer
if [ "$RUN_NOW" = "true" ]; then
run_privileged systemctl start qsfera-rpi-production-check.service
fi
run_privileged systemctl --no-pager status qsfera-rpi-production-check.timer
run_privileged systemctl --no-pager show qsfera-rpi-production-check.service -p Result -p ExecMainStatus -p ActiveState