fix: support OpenWrt 25 install script (#1658)

This commit is contained in:
Hintay
2026-05-21 05:31:05 +09:00
parent 56389cba93
commit 7db6b97925
6 changed files with 328 additions and 16 deletions
+32
View File
@@ -176,6 +176,38 @@ rc-update add nginx-ui default
:::
### OpenWrt
If your system uses OpenWrt init scripts, please use the following `/etc/init.d` commands to control it:
::: code-group
```shell [Start]
/etc/init.d/nginx-ui start
```
```shell [Stop]
/etc/init.d/nginx-ui stop
```
```shell [Restart]
/etc/init.d/nginx-ui restart
```
```shell [Show Status]
/etc/init.d/nginx-ui status
```
```shell [Enable at Boot]
/etc/init.d/nginx-ui enable
```
```shell [Disable at Boot]
/etc/init.d/nginx-ui disable
```
:::
### Init.d
If your system uses traditional init.d scripts, please use the following commands to control it:
+32
View File
@@ -172,6 +172,38 @@ rc-update add nginx-ui default
:::
### OpenWrt
如果您的系统使用 OpenWrt init 脚本,请使用以下 `/etc/init.d` 命令对其进行控制:
::: code-group
```shell [启动]
/etc/init.d/nginx-ui start
```
```shell [停止]
/etc/init.d/nginx-ui stop
```
```shell [重启]
/etc/init.d/nginx-ui restart
```
```shell [显示状态]
/etc/init.d/nginx-ui status
```
```shell [开机启动]
/etc/init.d/nginx-ui enable
```
```shell [禁用开机启动]
/etc/init.d/nginx-ui disable
```
:::
### Init.d
如果您的系统使用传统的 init.d 脚本,请使用以下命令对其进行控制:
+32
View File
@@ -172,6 +172,38 @@ rc-update add nginx-ui default
:::
### OpenWrt
如果您的系統使用 OpenWrt init 指令碼,請使用以下 `/etc/init.d` 指令控制:
::: code-group
```shell [啟動]
/etc/init.d/nginx-ui start
```
```shell [停止]
/etc/init.d/nginx-ui stop
```
```shell [重啟]
/etc/init.d/nginx-ui restart
```
```shell [顯示狀態]
/etc/init.d/nginx-ui status
```
```shell [開機啟動]
/etc/init.d/nginx-ui enable
```
```shell [停用開機啟動]
/etc/init.d/nginx-ui disable
```
:::
### Init.d
如果您的系統使用傳統的 init.d 指令碼,請使用以下指令控制:
+164 -16
View File
@@ -10,8 +10,10 @@ ServicePath="/etc/systemd/system/nginx-ui.service"
InitPath="/etc/init.d/nginx-ui"
# OpenRC Path
OpenRCPath="/etc/init.d/nginx-ui"
# OpenWrt Path
OpenWrtPath="/etc/init.d/nginx-ui"
# Service Type (systemd, openrc, initd)
# Service Type (systemd, openrc, openwrt, initd)
SERVICE_TYPE=''
# Latest release version
@@ -43,6 +45,9 @@ RPROXY=$GH_PROXY
# --purge
PURGE='0'
# Test root for detection tests
DETECT_ROOT=''
# Font color
FontBlack="\033[30m";
FontRed="\033[31m";
@@ -58,6 +63,33 @@ curl_with_retry() {
$(type -P curl) -x "${PROXY}" -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
root_path() {
if [[ -n "$DETECT_ROOT" ]]; then
echo "${DETECT_ROOT}$1"
else
echo "$1"
fi
}
command_exists() {
local command_name="$1"
if [[ -z "$DETECT_ROOT" ]]; then
type -P "$command_name" >/dev/null 2>&1
return
fi
[[ -x "${DETECT_ROOT}/bin/${command_name}" ]] || \
[[ -x "${DETECT_ROOT}/sbin/${command_name}" ]] || \
[[ -x "${DETECT_ROOT}/usr/bin/${command_name}" ]] || \
[[ -x "${DETECT_ROOT}/usr/sbin/${command_name}" ]]
}
is_openwrt() {
grep -qi '^ID=.*openwrt' "$(root_path /etc/os-release)" 2>/dev/null || \
[[ -f "$(root_path /etc/openwrt_release)" ]]
}
## Demo function for processing parameters
judgment_parameters() {
while [[ "$#" -gt '0' ]]; do
@@ -155,7 +187,7 @@ check_if_running_as_root() {
}
identify_the_operating_system_and_architecture() {
if [[ "$(uname)" == 'Linux' ]]; then
if [[ "$(uname)" == 'Linux' || -n "$DETECT_ROOT" ]]; then
case "$(uname -m)" in
'i386' | 'i686')
MACHINE='32'
@@ -168,11 +200,11 @@ identify_the_operating_system_and_architecture() {
;;
'armv6l')
MACHINE='arm32-v6'
grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
grep Features "$(root_path /proc/cpuinfo)" | grep -qw 'vfp' || MACHINE='arm32-v5'
;;
'armv7' | 'armv7l')
MACHINE='arm32-v7a'
grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
grep Features "$(root_path /proc/cpuinfo)" | grep -qw 'vfp' || MACHINE='arm32-v5'
;;
'armv8' | 'aarch64')
MACHINE='arm64-v8a'
@@ -182,30 +214,30 @@ identify_the_operating_system_and_architecture() {
exit 1
;;
esac
if [[ ! -f '/etc/os-release' ]]; then
if [[ ! -f "$(root_path /etc/os-release)" ]]; then
echo -e "${FontRed}error: Don't use outdated Linux distributions.${FontSuffix}"
exit 1
fi
if [[ "$(type -P apt)" ]]; then
if command_exists apt; then
PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
PACKAGE_MANAGEMENT_REMOVE='apt purge'
elif [[ "$(type -P dnf)" ]]; then
elif command_exists dnf; then
PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
PACKAGE_MANAGEMENT_REMOVE='dnf remove'
elif [[ "$(type -P yum)" ]]; then
elif command_exists yum; then
PACKAGE_MANAGEMENT_INSTALL='yum -y install'
PACKAGE_MANAGEMENT_REMOVE='yum remove'
elif [[ "$(type -P zypper)" ]]; then
elif command_exists zypper; then
PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
PACKAGE_MANAGEMENT_REMOVE='zypper remove'
elif [[ "$(type -P pacman)" ]]; then
elif command_exists pacman; then
PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
elif [[ "$(type -P opkg)" ]]; then
elif command_exists opkg; then
PACKAGE_MANAGEMENT_INSTALL='opkg install'
PACKAGE_MANAGEMENT_REMOVE='opkg remove'
elif [[ "$(type -P apk)" ]]; then
elif command_exists apk; then
PACKAGE_MANAGEMENT_INSTALL='apk add --no-cache'
PACKAGE_MANAGEMENT_REMOVE='apk del'
else
@@ -215,11 +247,13 @@ identify_the_operating_system_and_architecture() {
# Do not combine this judgment condition with the following judgment condition.
## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
if is_openwrt; then
SERVICE_TYPE='openwrt'
elif [[ -f "$(root_path /.dockerenv)" ]] || grep -q 'docker\|lxc' "$(root_path /proc/1/cgroup)" && command_exists systemctl; then
SERVICE_TYPE='systemd'
elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
elif [[ -d "$(root_path /run/systemd/system)" ]] || grep -q systemd <(ls -l "$(root_path /sbin/init)" 2>/dev/null); then
SERVICE_TYPE='systemd'
elif [[ "$(type -P rc-update)" ]] || [[ "$(type -P apk)" ]]; then
elif command_exists rc-update || command_exists apk; then
SERVICE_TYPE='openrc'
else
SERVICE_TYPE='initd'
@@ -243,6 +277,15 @@ install_software() {
fi
}
test_detect() {
DETECT_ROOT="$1"
identify_the_operating_system_and_architecture
echo "MACHINE=$MACHINE"
echo "PACKAGE_MANAGEMENT_INSTALL=$PACKAGE_MANAGEMENT_INSTALL"
echo "PACKAGE_MANAGEMENT_REMOVE=$PACKAGE_MANAGEMENT_REMOVE"
echo "SERVICE_TYPE=$SERVICE_TYPE"
}
get_latest_version() {
# Get latest release version number
local latest_release
@@ -340,6 +383,8 @@ install_service() {
install_systemd_service
elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
install_openrc_service
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
install_openwrt_service
else
install_initd_service
fi
@@ -384,6 +429,25 @@ install_openrc_service() {
OPENRC='1'
}
install_openwrt_service() {
local openwrt_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.openwrt"
echo "Downloading Nginx UI OpenWrt init.d file: $openwrt_download_link"
if ! curl_with_retry -R -H 'Cache-Control: no-cache' -L -o "$OpenWrtPath" "$openwrt_download_link"; then
echo -e "${FontRed}error: Download OpenWrt init.d file failed! Please check your network or try again.${FontSuffix}"
return 1
fi
chmod 755 "$OpenWrtPath"
echo "info: OpenWrt init.d service file has been installed successfully!"
echo -e "${FontGreen}note: The OpenWrt service is installed to '$OpenWrtPath'.${FontSuffix}"
cat_file_with_name "$OpenWrtPath"
"$OpenWrtPath" enable
OPENWRT='1'
}
install_initd_service() {
# Download init.d script
local initd_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.init"
@@ -459,6 +523,20 @@ start_nginx_ui() {
exit 1
fi
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
# Check if service is already running
if "$OpenWrtPath" status >/dev/null 2>&1; then
echo 'info: Nginx UI service is already running.'
else
"$OpenWrtPath" start
sleep 1s
if "$OpenWrtPath" status >/dev/null 2>&1; then
echo 'info: Start the Nginx UI service.'
else
echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
exit 1
fi
fi
else
# init.d
$InitPath start
@@ -524,6 +602,16 @@ check_nginx_ui_status() {
else
return 2 # not installed
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
if [[ -f "$OpenWrtPath" ]]; then
if "$OpenWrtPath" status >/dev/null 2>&1 || [[ -n "$(pidof nginx-ui)" ]]; then
return 0 # running
else
return 1 # not running
fi
else
return 2 # not installed
fi
else
# init.d
if [[ -f "$InitPath" ]]; then
@@ -557,6 +645,15 @@ restart_nginx_ui() {
echo -e "${FontRed}error: Failed to restart the Nginx UI service.${FontSuffix}"
exit 1
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
"$OpenWrtPath" restart
sleep 1s
if "$OpenWrtPath" status >/dev/null 2>&1; then
echo 'info: Restart the Nginx UI service.'
else
echo -e "${FontRed}error: Failed to restart the Nginx UI service.${FontSuffix}"
exit 1
fi
else
# init.d
$InitPath restart
@@ -581,6 +678,11 @@ stop_nginx_ui() {
echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
exit 1
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
if ! "$OpenWrtPath" stop; then
echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
exit 1
fi
else
# init.d
if ! $InitPath stop; then
@@ -630,6 +732,33 @@ remove_nginx_ui() {
# Remove from runlevels
rc-update del nginx-ui default 2>/dev/null || true
if ! ("rm" -r $delete_files 2>/dev/null); then
echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
exit 1
else
for file in $delete_files
do
[[ -e "$file" ]] && echo "removed: $file"
done
echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
echo 'info: Nginx UI has been removed.'
if [[ "$PURGE" -eq '0' ]]; then
echo 'info: If necessary, manually delete the configuration and log files.'
echo "info: e.g., $DataPath ..."
fi
exit 0
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]] && ([[ -f "$OpenWrtPath" ]] || [[ -f "/usr/local/bin/nginx-ui" ]]); then
if [[ -f "$OpenWrtPath" ]] && "$OpenWrtPath" status >/dev/null 2>&1; then
stop_nginx_ui
fi
delete_files="/usr/local/bin/nginx-ui $OpenWrtPath"
if [[ "$PURGE" -eq '1' ]]; then
[[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
fi
[[ -f "$OpenWrtPath" ]] && "$OpenWrtPath" disable 2>/dev/null || true
if ! ("rm" -r $delete_files 2>/dev/null); then
echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
exit 1
@@ -709,6 +838,11 @@ show_help() {
}
main() {
if [[ "${NGINX_UI_INSTALL_TESTING:-}" == "1" && "${1:-}" == "__test_detect" ]]; then
test_detect "$2"
exit 0
fi
check_if_running_as_root
identify_the_operating_system_and_architecture
judgment_parameters "$@"
@@ -722,7 +856,7 @@ main() {
TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
# Auto install OpenRC on Alpine Linux if needed
if [[ "$(type -P apk)" ]]; then
if [[ "$SERVICE_TYPE" == "openrc" ]] && [[ "$(type -P apk)" ]]; then
install_software 'openrc' 'openrc'
fi
install_software 'curl' 'curl'
@@ -750,6 +884,8 @@ main() {
echo "installed: ${ServicePath}"
elif [[ "$SERVICE_TYPE" == "openrc" && "$OPENRC" -eq '1' ]]; then
echo "installed: ${OpenRCPath}"
elif [[ "$SERVICE_TYPE" == "openwrt" && "$OPENWRT" -eq '1' ]]; then
echo "installed: ${OpenWrtPath}"
elif [[ "$SERVICE_TYPE" == "initd" && "$INITD" -eq '1' ]]; then
echo "installed: ${InitPath}"
fi
@@ -779,6 +915,8 @@ main() {
systemctl enable nginx-ui
elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
rc-update add nginx-ui default
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
"$OpenWrtPath" enable
fi
else
# Service is not installed, start it and enable
@@ -803,6 +941,16 @@ main() {
else
echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
fi
elif [[ "$SERVICE_TYPE" == "openwrt" ]]; then
"$OpenWrtPath" start
"$OpenWrtPath" enable
sleep 1s
if "$OpenWrtPath" status >/dev/null 2>&1; then
echo "info: Started and enabled the Nginx UI service on OpenWrt."
print_install_secret
else
echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
fi
elif [[ "$SERVICE_TYPE" == "initd" ]]; then
$InitPath start
sleep 1s
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh /etc/rc.common
START=99
STOP=10
USE_PROCD=1
NAME="nginx-ui"
PROG="/usr/local/bin/nginx-ui"
CONFIG="/usr/local/etc/nginx-ui/app.ini"
start_service() {
procd_open_instance
procd_set_param command "$PROG" --config "$CONFIG"
procd_set_param respawn
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}
service_triggers() {
procd_add_reload_trigger "$CONFIG"
}
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
INSTALL_SCRIPT="$ROOT_DIR/install.sh"
assert_contains() {
local haystack="$1"
local needle="$2"
local message="$3"
if [[ "$haystack" != *"$needle"* ]]; then
echo "not ok - $message"
echo "expected to find: $needle"
echo "actual output:"
echo "$haystack"
exit 1
fi
}
test_openwrt_with_apk_is_not_detected_as_openrc() {
local sandbox output
sandbox="$(mktemp -d)"
trap 'rm -rf "$sandbox"' RETURN
mkdir -p "$sandbox/bin" "$sandbox/etc" "$sandbox/proc/1" "$sandbox/sbin"
cat > "$sandbox/etc/os-release" <<'EOF'
NAME="OpenWrt"
ID="openwrt"
VERSION_ID="25.10"
EOF
: > "$sandbox/etc/openwrt_release"
: > "$sandbox/proc/cpuinfo"
: > "$sandbox/proc/1/cgroup"
ln -s /bin/true "$sandbox/bin/apk"
output="$(NGINX_UI_INSTALL_TESTING=1 "$INSTALL_SCRIPT" __test_detect "$sandbox" 2>&1)"
assert_contains "$output" "PACKAGE_MANAGEMENT_INSTALL=apk add --no-cache" "OpenWrt 25 should use apk for packages"
assert_contains "$output" "SERVICE_TYPE=openwrt" "OpenWrt 25 should use OpenWrt service type"
}
test_openwrt_with_apk_is_not_detected_as_openrc
echo "ok - install.sh detects OpenWrt 25 apk as openwrt"