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