#!/usr/bin/env bash set -euo pipefail INSTALL_ROOT="${INSTALL_ROOT:-/opt/hardware-dashboard}" CONFIG_FILE="${CONFIG_FILE:-/etc/hardware-watch.json}" SYSTEMD_DIR="${SYSTEMD_DIR:-/etc/systemd/system}" if [[ "$(id -u)" -ne 0 ]]; then echo "必须使用 root 运行部署脚本。" >&2 exit 1 fi if [[ ! -f "${CONFIG_FILE}" ]]; then echo "缺少 ${CONFIG_FILE}。请先复制 config/hardware-watch.example.json 并填入当前服务器实际值。" >&2 exit 1 fi getent group hardware-watch >/dev/null || groupadd --system hardware-watch getent passwd hardware-dashboard >/dev/null || useradd --system --gid hardware-watch --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin hardware-dashboard install -d -m 0755 "${INSTALL_ROOT}/static" install -d -m 0750 /var/lib/hardware-dashboard /var/lib/hardware-watch /var/log/hardware-watch install -d -m 0755 /etc/systemd/journald.conf.d install -m 0644 server.py task_core.py taskd.py task_exec.py "${INSTALL_ROOT}/" install -m 0644 static/index.html static/app.js static/tasks.js static/style.css static/favicon.svg "${INSTALL_ROOT}/static/" install -m 0755 scripts/hwlog /usr/local/sbin/hwlog install -m 0640 config/hardware-watch.json "${CONFIG_FILE}" install -m 0644 deploy/systemd/*.service deploy/systemd/*.timer "${SYSTEMD_DIR}/" install -m 0644 deploy/journald/60-hardware-watch.conf /etc/systemd/journald.conf.d/60-hardware-watch.conf setfacl -m u:hardware-dashboard:rx /var/lib/hardware-watch /var/log/hardware-watch setfacl -d -m u:hardware-dashboard:rx /var/lib/hardware-watch /var/log/hardware-watch find /var/lib/hardware-watch /var/log/hardware-watch -maxdepth 1 -type f -exec setfacl -m u:hardware-dashboard:r {} + setfacl -m u:hardware-dashboard:r "${CONFIG_FILE}" systemctl daemon-reload systemctl enable --now hardware-watch.timer systemctl restart hardware-watch.service systemctl enable --now hardware-taskd.service systemctl restart hardware-dashboard.service echo "部署完成。首次初始化定时任务管理员:" echo "python3 ${INSTALL_ROOT}/taskd.py init-admin"