feat: separate hub/runner service install options in menu

This commit is contained in:
i
2026-06-02 22:52:38 +08:00
parent 0dd33ea3c2
commit 5c92522135
2 changed files with 100 additions and 44 deletions
+37 -12
View File
@@ -551,13 +551,12 @@ TZ=Asia/Shanghai
# ─────────────────────────────────────────
# 创建开机自启计划任务
# ─────────────────────────────────────────
function Setup-AutoStart {
Write-Step "创建开机自启计划任务"
function Setup-AutoStartHub {
Write-Step "创建 hub 计划任务"
$hubScriptPath = Join-Path $script:INSTALL_DIR "start-hub.ps1"
$hubScript = @"
# HAPI Hub 启动脚本
# 请根据实际部署方式启动 Hub
# 配置文件: $($script:INSTALL_DIR)\.env
"@
$hubScript | Set-Content $hubScriptPath -Encoding UTF8
@@ -582,22 +581,31 @@ function Setup-AutoStart {
}
"@
$settingsJson | Set-Content (Join-Path $hapiHome "settings.json") -Encoding UTF8
}
function Setup-AutoStartRunner {
Write-Step "创建 runner 计划任务"
$hapiExe = (Get-Command hapi -ErrorAction SilentlyContinue).Source
if (-not $hapiExe) { $hapiExe = Join-Path $script:HAPI_BIN_DIR "hapi.exe" }
$trigger2 = New-ScheduledTaskTrigger -AtLogon
$action2 = New-ScheduledTaskAction -Execute "powershell.exe" `
$trigger = New-ScheduledTaskTrigger -AtLogon
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument "-WindowStyle Hidden -Command `"`& '$hapiExe' runner start`""
$settings2 = New-ScheduledTaskSettingsSet `
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
-StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Hours 72) -MultipleInstances IgnoreNew
$principal2 = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive
Register-ScheduledTask -TaskName "HAPI Runner" -Trigger $trigger2 -Action $action2 `
-Principal $principal2 -Settings $settings2 -Force | Out-Null
$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive
Register-ScheduledTask -TaskName "HAPI Runner" -Trigger $trigger -Action $action `
-Principal $principal -Settings $settings -Force | Out-Null
Write-Ok "HAPI Runner 计划任务已创建"
}
function Setup-AutoStart {
Setup-AutoStartHub
Setup-AutoStartRunner
}
# ─────────────────────────────────────────
# 禁用开机自启计划任务
# ─────────────────────────────────────────
@@ -801,8 +809,14 @@ function Main-Menu {
$hasService = $hubExists -or $runnerExists
$autostartEnabled = ($hubExists -and "$($script:STATUS_AUTOSTART_HUB)" -ne "Disabled") -or ($runnerExists -and "$($script:STATUS_AUTOSTART_RUNNER)" -ne "Disabled")
Write-Host " $i) 安装计划任务 (hub + runner)"
$options += "setup_autostart"
Write-Host " $i) 安装计划任务 - hub"
$options += "install_service_hub"
$i++
Write-Host " $i) 安装计划任务 - runner"
$options += "install_service_runner"
$i++
Write-Host " $i) 安装计划任务 - hub + runner"
$options += "install_service_both"
$i++
if ($autostartEnabled) {
@@ -860,7 +874,18 @@ function Main-Menu {
Collect-Status
Read-Host "按 Enter 继续..."
}
"setup_autostart" {
"install_service_hub" {
if (-not $script:STATUS_HUB_CONFIGURED) { Write-Warn "Hub 未配置,先执行配置"; Setup-Hub }
Setup-AutoStartHub
Collect-Status
Read-Host "按 Enter 继续..."
}
"install_service_runner" {
Setup-AutoStartRunner
Collect-Status
Read-Host "按 Enter 继续..."
}
"install_service_both" {
if (-not $script:STATUS_HUB_CONFIGURED) { Write-Warn "Hub 未配置,先执行配置"; Setup-Hub }
Setup-AutoStart
Collect-Status
+63 -32
View File
@@ -823,14 +823,13 @@ EOF
# ─────────────────────────────────────────
# 创建 systemd 服务
# ─────────────────────────────────────────
setup_systemd() {
step "创建开机自启服务"
setup_systemd_hub() {
step "创建 hub 开机自启服务"
local hapi_bin
hapi_bin=$(command -v hapi 2>/dev/null || echo "$HAPI_BIN_DIR/hapi")
if [[ "$IS_ROOT" == true ]]; then
# 系统级 systemd
cat > /etc/systemd/system/hapi-hub.service <<EOF
[Unit]
Description=HAPI Hub
@@ -847,7 +846,6 @@ TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload >>"$LOG_FILE" 2>&1
systemctl enable hapi-hub >>"$LOG_FILE" 2>&1
ok "hapi-hub.service 已启用"
@@ -859,30 +857,7 @@ EOF
"cliApiToken": "$CLI_TOKEN"
}
EOF
cat > /etc/systemd/system/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=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 已启用并启动"
else
# 用户级 systemd
local user_units="$HOME/.config/systemd/user"
mkdir -p "$user_units"
@@ -902,10 +877,42 @@ 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) 已启用"
fi
}
setup_systemd_runner() {
step "创建 runner 开机自启服务"
local hapi_bin
hapi_bin=$(command -v hapi 2>/dev/null || echo "$HAPI_BIN_DIR/hapi")
if [[ "$IS_ROOT" == true ]]; then
cat > /etc/systemd/system/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=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 已启用并启动"
else
local user_units="$HOME/.config/systemd/user"
mkdir -p "$user_units"
cat > "$user_units/hapi-runner.service" <<EOF
[Unit]
@@ -924,13 +931,17 @@ 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
}
setup_systemd() {
setup_systemd_hub
setup_systemd_runner
}
# ─────────────────────────────────────────
# 启用开机自启
# ─────────────────────────────────────────
@@ -1168,8 +1179,14 @@ main_menu() {
local autostart_enabled=false
[[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]] && autostart_enabled=true
echo " $i) 安装系统服务 (hub + runner)"
options+=("setup_autostart")
echo " $i) 安装系统服务 - hub"
options+=("install_service_hub")
((i++))
echo " $i) 安装系统服务 - runner"
options+=("install_service_runner")
((i++))
echo " $i) 安装系统服务 - hub + runner"
options+=("install_service_both")
((i++))
if [[ "$autostart_enabled" == true ]]; then
@@ -1234,7 +1251,21 @@ main_menu() {
collect_status
read -rp "按 Enter 继续..."
;;
setup_autostart)
install_service_hub)
if [[ "$STATUS_HUB_CONFIGURED" == false ]]; then
warn "Hub 未配置,先执行配置"
setup_hub || true
fi
setup_systemd_hub
collect_status
read -rp "按 Enter 继续..."
;;
install_service_runner)
setup_systemd_runner
collect_status
read -rp "按 Enter 继续..."
;;
install_service_both)
if [[ "$STATUS_HUB_CONFIGURED" == false ]]; then
warn "Hub 未配置,先执行配置"
setup_hub || true