99 lines
3.0 KiB
Bash
Executable File
99 lines
3.0 KiB
Bash
Executable File
#!/bin/sh
|
||
set -eu
|
||
|
||
APP_NAME="led-manager"
|
||
APP_DIR="/opt/led-manager"
|
||
CONFIG_DIR="/etc/led-manager"
|
||
CONFIG_FILE="$CONFIG_DIR/config.json"
|
||
PORT="${PORT:-8088}"
|
||
HOST="${HOST:-0.0.0.0}"
|
||
COLORS="${COLORS:-red,green,blue}"
|
||
RAW_BASE="${RAW_BASE:-https://gitea.chickliu.fun/Hermes/led-manager-panel/raw/branch/main}"
|
||
|
||
need_root() {
|
||
if [ "$(id -u)" != "0" ]; then
|
||
echo "请用 root 运行:su - 或 sudo sh install.sh" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
has_cmd() { command -v "$1" >/dev/null 2>&1; }
|
||
|
||
download() {
|
||
url="$1"
|
||
out="$2"
|
||
if has_cmd curl; then
|
||
curl -fsSL "$url" -o "$out"
|
||
elif has_cmd wget; then
|
||
wget -qO "$out" "$url"
|
||
else
|
||
echo "缺少 curl/wget,无法下载 $url" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
install_python_if_possible() {
|
||
if has_cmd python3; then return 0; fi
|
||
echo "未发现 python3,尝试安装..."
|
||
if has_cmd apt-get; then
|
||
apt-get update
|
||
apt-get install -y python3 ca-certificates
|
||
else
|
||
echo "Debian 系统请先安装 python3:apt update && apt install -y python3" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
install_systemd_service() {
|
||
if ! has_cmd systemctl; then
|
||
echo "未发现 systemctl,已安装文件但不能配置开机启动。手动启动:"
|
||
echo "python3 $APP_DIR/led_panel.py --host $HOST --port $PORT --config $CONFIG_FILE"
|
||
exit 0
|
||
fi
|
||
|
||
echo "安装 systemd 服务:$APP_NAME"
|
||
download "$RAW_BASE/led-manager.service" "/etc/systemd/system/$APP_NAME.service"
|
||
# Patch host/port/config/colors in service if env overrides were provided.
|
||
sed -i "s#--host [^ ]* --port [0-9]* --config [^ ]*#--host $HOST --port $PORT --config $CONFIG_FILE --colors $COLORS#" "/etc/systemd/system/$APP_NAME.service" || true
|
||
if ! grep -q -- '--colors' "/etc/systemd/system/$APP_NAME.service"; then
|
||
sed -i "s#\(ExecStart=.*\)#\1 --colors $COLORS#" "/etc/systemd/system/$APP_NAME.service" || true
|
||
fi
|
||
systemctl daemon-reload
|
||
systemctl enable "$APP_NAME"
|
||
systemctl restart "$APP_NAME"
|
||
systemctl is-active "$APP_NAME" >/dev/null 2>&1 && echo "服务已启动:$APP_NAME" || systemctl status "$APP_NAME" --no-pager || true
|
||
}
|
||
|
||
need_root
|
||
install_python_if_possible
|
||
mkdir -p "$APP_DIR" "$CONFIG_DIR"
|
||
|
||
# Preserve existing persistent config across reinstall/update.
|
||
if [ ! -f "$CONFIG_FILE" ]; then
|
||
cat > "$CONFIG_FILE" <<'JSON'
|
||
{
|
||
"version": 1,
|
||
"leds": {}
|
||
}
|
||
JSON
|
||
fi
|
||
chmod 755 "$CONFIG_DIR" || true
|
||
chmod 644 "$CONFIG_FILE" || true
|
||
|
||
download "$RAW_BASE/led_panel.py" "$APP_DIR/led_panel.py"
|
||
chmod +x "$APP_DIR/led_panel.py"
|
||
|
||
# Debian-focused installer: always prefer systemd. OpenWrt is intentionally not handled here.
|
||
install_systemd_service
|
||
|
||
ipaddr="$(hostname -I 2>/dev/null | awk '{print $1}' || true)"
|
||
echo
|
||
echo "安装/更新完成。"
|
||
echo "服务:systemctl status $APP_NAME"
|
||
echo "配置:$CONFIG_FILE"
|
||
echo "访问地址: http://${ipaddr:-设备IP}:$PORT/"
|
||
echo "面板里点『保存持久化』后,重启系统会由 systemd 启动本项目并自动读取配置恢复 LED。"
|
||
echo "当前只显示/控制颜色:$COLORS"
|
||
echo "如需修改端口:PORT=8090 sh install.sh"
|
||
echo "如需显示全部 LED:COLORS= sh install.sh"
|