Files
inquiry_robot/deploy/_remote_recover_wwwroot.ps1
T

114 lines
4.5 KiB
PowerShell

# 续作:Nginx 卡住后的恢复 + 从 E:\wwwroot 起智能体
# 禁止动 C:\yutongda 现网指针;现网 8080 勿停。
$ErrorActionPreference = "Continue"
$Dst = "E:\wwwroot"
$AgentHome = "E:\wwwroot\releases\scaffold-agent-20260912-152641\agent"
$NginxConf = "C:\nginx-1.18.0\myconf\ai.conf"
$Stamp = Get-Date -Format "yyyyMMdd-HHmmss"
Write-Output "=== ensure nginx root ==="
if (Test-Path $NginxConf) {
$t = Get-Content $NginxConf -Raw
if ($t -notmatch 'root E:/wwwroot/admin/dist;') {
Copy-Item $NginxConf "$NginxConf.bak-fix-$Stamp" -Force
$t2 = $t -replace 'root\s+C:/ytd-test/admin/dist;', 'root E:/wwwroot/admin/dist;'
$t2 = $t2 -replace 'root\s+C:\\ytd-test\\admin\\dist;', 'root E:/wwwroot/admin/dist;'
Set-Content -Path $NginxConf -Value $t2 -Encoding UTF8
}
Select-String -Path $NginxConf -Pattern '^\s*root ' | ForEach-Object { $_.Line.Trim() }
}
Write-Output "=== bounce nginx (avoid hung Restart-Service) ==="
# 强制结束卡住的 nginx,再用 bounce / 启动服务
Get-Process nginx -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
try {
if (Test-Path 'C:\yutongda\bounce_nginx.ps1') {
& 'C:\yutongda\bounce_nginx.ps1'
Write-Output "BOUNCE_OK"
} else {
Start-Service Nginx1.18 -ErrorAction SilentlyContinue
& 'C:\nginx-1.18.0\nginx.exe'
Write-Output "NGINX_EXE_STARTED"
}
} catch {
Write-Output "BOUNCE_ERR=$_"
}
Start-Sleep -Seconds 2
Get-Service Nginx1.18 -ErrorAction SilentlyContinue | Format-List Name, Status
Get-Process nginx -ErrorAction SilentlyContinue | Select-Object Id, ProcessName | Format-Table -AutoSize
Write-Output "=== ensure run-agent-http.cmd ==="
New-Item -ItemType Directory -Force -Path "$Dst\ops", "$Dst\logs" | Out-Null
$runCmd = @"
@echo off
cd /d "$AgentHome"
set YTD_ENV=test
set AGENT_HTTP_PORT=8910
:loop
"$AgentHome\.venv\Scripts\python.exe" -m agent.http_app >> "$Dst\logs\agent-http.out.log" 2>> "$Dst\logs\agent-http.err.log"
echo agent exited, restart in 3s >> "$Dst\logs\agent-http.err.log"
timeout /t 3 /nobreak >nul
goto loop
"@
Set-Content -Path "$Dst\ops\run-agent-http.cmd" -Value $runCmd -Encoding ASCII
Write-Output "=== stop old 8910 if any ==="
Get-NetTCPConnection -LocalPort 8910 -State Listen -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty OwningProcess -Unique |
ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue }
Write-Output "=== start agent ==="
if (-not (Test-Path "$AgentHome\.venv\Scripts\python.exe")) {
Write-Output "VENV_MISSING"
Get-ChildItem "$AgentHome" -ErrorAction SilentlyContinue | Select-Object Name
} else {
$wmi = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
CommandLine = "cmd.exe /c `"$Dst\ops\run-agent-http.cmd`""
}
Write-Output "WMI_Return=$($wmi.ReturnValue) ProcessId=$($wmi.ProcessId)"
Start-Sleep -Seconds 7
}
Write-Output "=== health ==="
try {
$h = Invoke-WebRequest -Uri "http://127.0.0.1:8910/health" -UseBasicParsing -TimeoutSec 8
Write-Output "HEALTH=$($h.StatusCode) $($h.Content)"
} catch {
Write-Output "HEALTH_FAIL=$_"
if (Test-Path "$Dst\logs\agent-http.err.log") { Get-Content "$Dst\logs\agent-http.err.log" -Tail 50 }
}
Write-Output "=== ports ==="
foreach ($p in 8910,8080,8810,8180,8913) {
$c = Get-NetTCPConnection -LocalPort $p -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1
if ($c) { "$p LISTEN pid=$($c.OwningProcess)" } else { "$p down" }
}
Write-Output "=== pointer ==="
$listen = Get-NetTCPConnection -LocalPort 8910 -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1
$pointer = @{
releaseId = "scaffold-agent-20260912-152641"
releaseRoot = "E:\wwwroot\releases\scaffold-agent-20260912-152641"
agentHome = $AgentHome
root = $Dst
migratedFrom = "C:\ytd-test"
updatedAt = (Get-Date).ToString("o")
pid = if ($listen) { $listen.OwningProcess } else { 0 }
} | ConvertTo-Json
Set-Content -Path "$Dst\ops\active-release.json" -Value $pointer -Encoding UTF8
Get-Content "$Dst\ops\active-release.json"
Write-Output "=== retire old if health ok and still present ==="
if ((Test-Path 'C:\ytd-test') -and (Get-NetTCPConnection -LocalPort 8910 -State Listen -EA SilentlyContinue)) {
Rename-Item 'C:\ytd-test' "ytd-test.migrated-to-E-wwwroot-$Stamp"
Write-Output "OLD_RENAMED"
}
Write-Output "=== public-local smoke ==="
cmd /c "curl.exe -sk -H `"Host: ai.ytd-scm.com`" https://127.0.0.1/agent/health"
Write-Output ""
cmd /c "curl.exe -sk -o NUL -w root:%{http_code} -H `"Host: ai.ytd-scm.com`" https://127.0.0.1/"
Write-Output ""
Write-Output "RECOVER_DONE"