Files
inquiry_robot/deploy/_remote_cleanup_old_releases.ps1
T

120 lines
4.0 KiB
PowerShell

# Cleanup old releases under E:\wwwroot; keep current targets only
$ErrorActionPreference = 'Stop'
$Root = 'E:\wwwroot'
function Get-JunctionTarget([string]$link) {
if (-not (Test-Path $link)) { return $null }
$item = Get-Item $link -Force
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
cmd /c ("dir /AL `"$link`"") | Out-Null
# Resolve via Get-Item Target if available
try {
if ($item.Target -and $item.Target.Count -gt 0) { return [string]$item.Target[0] }
} catch {}
}
# Fallback: read active-release.json
return $null
}
$ptrPath = Join-Path $Root 'ops\active-release.json'
$keepAgent = $null
$keepAdmin = $null
if (Test-Path $ptrPath) {
$ptr = Get-Content $ptrPath -Raw | ConvertFrom-Json
if ($ptr.agentHome) { $keepAgent = [string]$ptr.agentHome }
if ($ptr.adminRelease) { $keepAdmin = [string]$ptr.adminRelease }
}
$agentCurrent = Join-Path $Root 'agent\current'
$adminCurrent = Join-Path $Root 'admin\current'
$t1 = Get-JunctionTarget $agentCurrent
$t2 = Get-JunctionTarget $adminCurrent
if ($t1) { $keepAgent = $t1 }
if ($t2) { $keepAdmin = $t2 }
# Resolve junction via fsutil / dir listing
function Resolve-Link([string]$p) {
if (-not (Test-Path $p)) { return $null }
$out = cmd /c ("fsutil reparsepoint query `"$p`"") 2>$null
# Alternative: (Get-Item).Target
$i = Get-Item $p -Force
if ($i.LinkType -eq 'Junction' -or ($i.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
if ($i.Target) { return ([string]$i.Target[0]) }
}
return $null
}
$ka = Resolve-Link $agentCurrent
$kb = Resolve-Link $adminCurrent
if ($ka) { $keepAgent = $ka }
if ($kb) { $keepAdmin = $kb }
Write-Output ('keepAgent=' + $keepAgent)
Write-Output ('keepAdmin=' + $keepAdmin)
$removed = @()
# agent releases: E:\wwwroot\agent\releases\<id>\agent or <id>
$agentRelRoot = Join-Path $Root 'agent\releases'
if (Test-Path $agentRelRoot) {
Get-ChildItem $agentRelRoot -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer } | ForEach-Object {
$rel = $_.FullName
$agentHome = Join-Path $rel 'agent'
$isKeep = $false
if ($keepAgent) {
if ($keepAgent -eq $agentHome -or $keepAgent -eq $rel -or $keepAgent.StartsWith($rel + '\')) { $isKeep = $true }
}
if (-not $isKeep) {
Write-Output ('REMOVE agent ' + $rel)
Remove-Item $rel -Recurse -Force
$removed += $rel
} else {
Write-Output ('KEEP agent ' + $rel)
}
}
}
$adminRelRoot = Join-Path $Root 'admin\releases'
if (Test-Path $adminRelRoot) {
Get-ChildItem $adminRelRoot -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer } | ForEach-Object {
$rel = $_.FullName
$dist = Join-Path $rel 'dist'
$isKeep = $false
if ($keepAdmin) {
if ($keepAdmin -eq $dist -or $keepAdmin -eq $rel -or $keepAdmin.StartsWith($rel + '\')) { $isKeep = $true }
}
if (-not $isKeep) {
Write-Output ('REMOVE admin ' + $rel)
Remove-Item $rel -Recurse -Force
$removed += $rel
} else {
Write-Output ('KEEP admin ' + $rel)
}
}
}
# old env backups older than keep recent 2
Get-ChildItem $Root -Filter '.env.bak*' -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | ForEach-Object {
Write-Output ('REMOVE bak ' + $_.Name)
Remove-Item $_.FullName -Force
$removed += $_.FullName
}
# incoming zips older
$incoming = 'C:\Users\ytd_extdev_0903\incoming'
if (Test-Path $incoming) {
Get-ChildItem $incoming -Include '*.zip','jeecg-system-start*.jar' -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-1) } |
ForEach-Object {
Write-Output ('REMOVE incoming ' + $_.Name)
Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
}
}
Write-Output ('removed_count=' + $removed.Count)
Write-Output 'CLEANUP_OK'
# ports sanity: must not touch yutongda
foreach ($x in 8080,8180,8910) {
$c = Get-NetTCPConnection -LocalPort $x -State Listen -EA SilentlyContinue | Select-Object -First 1
if ($c) { Write-Output ("port $x LISTEN") } else { Write-Output ("port $x down") }
}