feat: persist led config on debian startup

This commit is contained in:
2026-05-15 15:25:40 +08:00
parent 45aaba0c8a
commit ac75a5f9b7
5 changed files with 203 additions and 44 deletions
+35 -34
View File
@@ -3,6 +3,8 @@ 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}"
RAW_BASE="${RAW_BASE:-https://gitea.chickliu.fun/Hermes/led-manager-panel/raw/branch/main}"
@@ -32,60 +34,59 @@ download() {
install_python_if_possible() {
if has_cmd python3; then return 0; fi
echo "未发现 python3,尝试安装..."
if has_cmd opkg; then
opkg update
opkg install python3
elif has_cmd apt-get; then
if has_cmd apt-get; then
apt-get update
apt-get install -y python3
apt-get install -y python3 ca-certificates
else
echo "未发现 opkg/apt-get,请先手动安装 python3" >&2
echo "Debian 系统请先安装 python3apt update && apt install -y python3" >&2
exit 1
fi
}
install_openwrt_service() {
echo "检测到 OpenWrt/procd,安装 /etc/init.d/$APP_NAME"
download "$RAW_BASE/led-manager.openwrt.init" "/etc/init.d/$APP_NAME"
chmod +x "/etc/init.d/$APP_NAME"
# Patch host/port in init script if env overrides were provided.
sed -i "s/^HOST=.*/HOST=$HOST/" "/etc/init.d/$APP_NAME" || true
sed -i "s/^PORT=.*/PORT=$PORT/" "/etc/init.d/$APP_NAME" || true
"/etc/init.d/$APP_NAME" enable
"/etc/init.d/$APP_NAME" restart || "/etc/init.d/$APP_NAME" start
"/etc/init.d/$APP_NAME" status || true
}
install_systemd_service() {
echo "检测到 systemd,安装 led-manager.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 in service if env overrides were provided.
sed -i "s/--host [^ ]* --port [0-9]*/--host $HOST --port $PORT/" "/etc/systemd/system/$APP_NAME.service" || true
# Patch host/port/config in service if env overrides were provided.
sed -i "s#--host [^ ]* --port [0-9]* --config [^ ]*#--host $HOST --port $PORT --config $CONFIG_FILE#" "/etc/systemd/system/$APP_NAME.service" || true
systemctl daemon-reload
systemctl enable --now "$APP_NAME"
systemctl enable "$APP_NAME"
systemctl restart "$APP_NAME"
systemctl status "$APP_NAME" --no-pager || true
}
need_root
install_python_if_possible
mkdir -p "$APP_DIR"
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"
if [ -x /sbin/procd ] || [ -d /etc/rc.d ] && has_cmd procd; then
install_openwrt_service
elif has_cmd systemctl && [ -d /run/systemd/system ]; then
install_systemd_service
elif [ -d /etc/init.d ] && [ -f /etc/openwrt_release ]; then
install_openwrt_service
else
echo "未检测到 systemd/procd,已安装到 $APP_DIR。手动启动:"
echo "python3 $APP_DIR/led_panel.py --host $HOST --port $PORT"
fi
# 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 "安装/更新完成。"
echo "服务:systemctl status $APP_NAME"
echo "配置:$CONFIG_FILE"
echo "访问地址: http://${ipaddr:-设备IP}:$PORT/"
echo "面板里点『保存持久化』后,重启系统会由 systemd 启动本项目并自动读取配置恢复 LED。"
echo "如需修改端口:PORT=8090 sh install.sh"