fix: support user-level systemd for non-root (status, setup, enable, disable)
This commit is contained in:
+133
-111
@@ -824,15 +824,14 @@ EOF
|
||||
# 创建 systemd 服务
|
||||
# ─────────────────────────────────────────
|
||||
setup_systemd() {
|
||||
if [[ "$IS_ROOT" != true ]]; then
|
||||
warn "非 root 用户无法设置系统级开机自启"
|
||||
warn "可以使用用户级 systemd,但需要手动配置"
|
||||
return 1
|
||||
fi
|
||||
step "创建开机自启服务"
|
||||
|
||||
step "创建 systemd 开机自启服务"
|
||||
local hapi_bin
|
||||
hapi_bin=$(command -v hapi 2>/dev/null || echo "$HAPI_BIN_DIR/hapi")
|
||||
|
||||
cat > /etc/systemd/system/hapi-hub.service <<EOF
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
# 系统级 systemd
|
||||
cat > /etc/systemd/system/hapi-hub.service <<EOF
|
||||
[Unit]
|
||||
Description=HAPI Hub
|
||||
After=network-online.target
|
||||
@@ -849,22 +848,19 @@ TimeoutStartSec=0
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
systemctl daemon-reload >>"$LOG_FILE" 2>&1
|
||||
systemctl enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service 已启用"
|
||||
systemctl daemon-reload >>"$LOG_FILE" 2>&1
|
||||
systemctl enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service 已启用"
|
||||
|
||||
local hapi_bin
|
||||
hapi_bin=$(command -v hapi 2>/dev/null || echo "$HAPI_BIN_DIR/hapi")
|
||||
|
||||
mkdir -p /root/.hapi
|
||||
cat > /root/.hapi/settings.json <<EOF
|
||||
mkdir -p /root/.hapi
|
||||
cat > /root/.hapi/settings.json <<EOF
|
||||
{
|
||||
"apiUrl": "http://localhost:3006",
|
||||
"cliApiToken": "$CLI_TOKEN"
|
||||
}
|
||||
EOF
|
||||
|
||||
cat > /etc/systemd/system/hapi-runner.service <<EOF
|
||||
cat > /etc/systemd/system/hapi-runner.service <<EOF
|
||||
[Unit]
|
||||
Description=HAPI Runner
|
||||
After=network-online.target hapi-hub.service
|
||||
@@ -882,34 +878,92 @@ StandardError=journal
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
systemctl enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service 已启用并启动"
|
||||
systemctl enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service 已启用并启动"
|
||||
else
|
||||
# 用户级 systemd
|
||||
local user_units="$HOME/.config/systemd/user"
|
||||
mkdir -p "$user_units"
|
||||
|
||||
cat > "$user_units/hapi-hub.service" <<EOF
|
||||
[Unit]
|
||||
Description=HAPI Hub
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
RemainAfterExit=yes
|
||||
WorkingDirectory=$INSTALL_DIR
|
||||
ExecStart=$hapi_bin hub start
|
||||
ExecStop=$hapi_bin hub stop
|
||||
TimeoutStartSec=0
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
EOF
|
||||
|
||||
systemctl --user daemon-reload >>"$LOG_FILE" 2>&1
|
||||
systemctl --user enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service (user) 已启用"
|
||||
|
||||
cat > "$user_units/hapi-runner.service" <<EOF
|
||||
[Unit]
|
||||
Description=HAPI Runner
|
||||
After=network-online.target hapi-hub.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=$hapi_bin runner start
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
TimeoutStopSec=30
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
EOF
|
||||
|
||||
systemctl --user enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl --user start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service (user) 已启用并启动"
|
||||
fi
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# 启用开机自启
|
||||
# ─────────────────────────────────────────
|
||||
enable_autostart() {
|
||||
if [[ "$IS_ROOT" != true ]]; then
|
||||
warn "非 root 用户无法设置系统级开机自启"
|
||||
return 1
|
||||
fi
|
||||
|
||||
step "启用开机自启"
|
||||
|
||||
if [[ -f /etc/systemd/system/hapi-hub.service ]]; then
|
||||
systemctl enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-hub.service 已启用"
|
||||
fi
|
||||
|
||||
if [[ -f /etc/systemd/system/hapi-runner.service ]]; then
|
||||
systemctl enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service 已启用"
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
if [[ -f /etc/systemd/system/hapi-hub.service ]]; then
|
||||
systemctl enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-hub.service 已启用"
|
||||
fi
|
||||
if [[ -f /etc/systemd/system/hapi-runner.service ]]; then
|
||||
systemctl enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service 已启用"
|
||||
else
|
||||
warn "hapi-runner.service 不存在,请先安装 HAPI CLI 并配置 Hub"
|
||||
fi
|
||||
else
|
||||
warn "hapi-runner.service 不存在,请先安装 HAPI CLI 并配置 Hub"
|
||||
local user_units="$HOME/.config/systemd/user"
|
||||
if [[ -f "$user_units/hapi-hub.service" ]]; then
|
||||
systemctl --user enable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
systemctl --user start hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-hub.service (user) 已启用"
|
||||
fi
|
||||
if [[ -f "$user_units/hapi-runner.service" ]]; then
|
||||
systemctl --user enable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
systemctl --user start hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
ok "hapi-runner.service (user) 已启用"
|
||||
else
|
||||
warn "hapi-runner.service 不存在,请先安装 HAPI CLI 并配置 Hub"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -917,23 +971,31 @@ enable_autostart() {
|
||||
# 禁用开机自启
|
||||
# ─────────────────────────────────────────
|
||||
disable_autostart() {
|
||||
if [[ "$IS_ROOT" != true ]]; then
|
||||
warn "非 root 用户无法修改系统级开机自启"
|
||||
return 1
|
||||
fi
|
||||
|
||||
step "禁用开机自启"
|
||||
|
||||
if [[ -f /etc/systemd/system/hapi-hub.service ]]; then
|
||||
systemctl stop hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl disable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service 已禁用"
|
||||
fi
|
||||
|
||||
if [[ -f /etc/systemd/system/hapi-runner.service ]]; then
|
||||
systemctl stop hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl disable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-runner.service 已禁用"
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
if [[ -f /etc/systemd/system/hapi-hub.service ]]; then
|
||||
systemctl stop hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl disable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service 已禁用"
|
||||
fi
|
||||
if [[ -f /etc/systemd/system/hapi-runner.service ]]; then
|
||||
systemctl stop hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl disable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-runner.service 已禁用"
|
||||
fi
|
||||
else
|
||||
local user_units="$HOME/.config/systemd/user"
|
||||
if [[ -f "$user_units/hapi-hub.service" ]]; then
|
||||
systemctl --user stop hapi-hub >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl --user disable hapi-hub >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-hub.service (user) 已禁用"
|
||||
fi
|
||||
if [[ -f "$user_units/hapi-runner.service" ]]; then
|
||||
systemctl --user stop hapi-runner >>"$LOG_FILE" 2>&1 || true
|
||||
systemctl --user disable hapi-runner >>"$LOG_FILE" 2>&1
|
||||
ok "hapi-runner.service (user) 已禁用"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -1005,6 +1067,16 @@ collect_status() {
|
||||
STATUS_AUTOSTART_RUNNER_ENABLED=$(systemctl is-enabled hapi-runner 2>/dev/null || echo "unknown")
|
||||
STATUS_AUTOSTART_RUNNER_ACTIVE=$(systemctl is-active hapi-runner 2>/dev/null || echo "unknown")
|
||||
fi
|
||||
else
|
||||
local user_units="$HOME/.config/systemd/user"
|
||||
if [[ -f "$user_units/hapi-hub.service" ]]; then
|
||||
STATUS_AUTOSTART_HUB_ENABLED=$(systemctl --user is-enabled hapi-hub 2>/dev/null || echo "unknown")
|
||||
STATUS_AUTOSTART_HUB_ACTIVE=$(systemctl --user is-active hapi-hub 2>/dev/null || echo "unknown")
|
||||
fi
|
||||
if [[ -f "$user_units/hapi-runner.service" ]]; then
|
||||
STATUS_AUTOSTART_RUNNER_ENABLED=$(systemctl --user is-enabled hapi-runner 2>/dev/null || echo "unknown")
|
||||
STATUS_AUTOSTART_RUNNER_ACTIVE=$(systemctl --user is-active hapi-runner 2>/dev/null || echo "unknown")
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -1093,18 +1165,16 @@ main_menu() {
|
||||
fi
|
||||
|
||||
echo ""
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
if [[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]]; then
|
||||
echo " $i) 禁用开机自启"
|
||||
options+=("disable_autostart")
|
||||
else
|
||||
echo " $i) 启用开机自启"
|
||||
options+=("enable_autostart")
|
||||
fi
|
||||
((i++))
|
||||
local has_autostart=false
|
||||
[[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]] && has_autostart=true
|
||||
if [[ "$has_autostart" == true ]]; then
|
||||
echo " $i) 禁用开机自启"
|
||||
options+=("disable_autostart")
|
||||
else
|
||||
echo -e " ${DIM}开机自启 (需要 root)${NC}"
|
||||
echo " $i) 设置开机自启"
|
||||
options+=("setup_autostart")
|
||||
fi
|
||||
((i++))
|
||||
|
||||
echo ""
|
||||
echo " $i) 刷新状态"
|
||||
@@ -1158,7 +1228,7 @@ main_menu() {
|
||||
collect_status
|
||||
read -rp "按 Enter 继续..."
|
||||
;;
|
||||
enable_autostart)
|
||||
setup_autostart|enable_autostart)
|
||||
if [[ "$STATUS_HUB_CONFIGURED" == false ]]; then
|
||||
warn "Hub 未配置,先执行配置"
|
||||
setup_hub || true
|
||||
@@ -1309,61 +1379,13 @@ perform_install() {
|
||||
|
||||
if [[ "$ok" != true ]]; then
|
||||
err "HAPI CLI 安装失败"
|
||||
return 1
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$SKIP_HUB" != true ]]; then
|
||||
setup_hub
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
setup_systemd
|
||||
fi
|
||||
setup_systemd
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
perform_upgrade() {
|
||||
resolve_install_source
|
||||
resolve_remote_version
|
||||
|
||||
local cmp
|
||||
cmp=$(compare_versions "$INSTALLED_VERSION" "$REMOTE_VERSION")
|
||||
if [[ "$cmp" != "2" ]]; then
|
||||
ok "当前已是最新版本 (v$INSTALLED_VERSION)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "升级: v$INSTALLED_VERSION -> v$REMOTE_VERSION"
|
||||
|
||||
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
|
||||
install_nodejs || true
|
||||
fi
|
||||
|
||||
local ok=false
|
||||
case "$SELECTED_SOURCE" in
|
||||
npm) install_from_npm && ok=true || ok=false ;;
|
||||
github-official)
|
||||
if install_from_github "$GITHUB_OFFICIAL_REPO"; then
|
||||
ok=true
|
||||
else
|
||||
warn "官方仓库无匹配 asset,尝试本仓库..."
|
||||
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
|
||||
fi
|
||||
;;
|
||||
github-fork) install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false ;;
|
||||
esac
|
||||
|
||||
if [[ "$ok" != true ]]; then
|
||||
err "HAPI CLI 升级失败"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ "$SKIP_HUB" != true ]]; then
|
||||
setup_hub
|
||||
if [[ "$IS_ROOT" == true ]]; then
|
||||
setup_systemd
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user