Files
scadaproj/docs/deploy/2026-11-19-qa-nam-ldap/otopcua-swap2.ps1
T

51 lines
4.1 KiB
PowerShell

# QA window on ONE node, started with the service RUNNING (pack §4 QA steps 2/3): Invoke-OtOpcUaInstallSwap.ps1
# -Carry appsettings*.json,logs -NoStart runs -Scope Outside before its own stop, stops, swaps, -Scope InstallRoot
# reapply+verify, leaves it STOPPED. Then back up data\ + pki\ (stopped => consistent SQLite triplets). Lines UTC-stamped.
$ErrorActionPreference = 'Stop'
function Out-ZbStamped { process { '{0:HH:mm:ss.fff}Z {1}' -f (Get-Date).ToUniversalTime(), "$_" } }
try {
$stageDir = 'E:\psrem-stage\otopcua-cdf433a3'
$backupDir = "$stageDir\backup"
$candidate = 'E:\ApiInstall\OtOpcUa\app.new-cdf433a3'
$prevInstall = 'E:\ApiInstall\OtOpcUa\app.bak.20261119-pre-cdf433a3'
"node=$env:COMPUTERNAME" | Out-ZbStamped
if (-not (Test-Path -LiteralPath $candidate)) { throw "candidate missing" }
if (Test-Path -LiteralPath $prevInstall) { throw "prev path already exists: $prevInstall" }
if ((Get-Service OtOpcUaHost).Status -ne 'Running') { throw "service not running; the pack starts the swap with the service up" }
'swap invoked (service running)' | Out-ZbStamped
Set-Location "$stageDir\install"
$swapFilter = 'does not inherit the worker deny|grants nothing to Everyone|^\s*(@\{|-DenyPaths\.|to -DenyPaths\.|add it to -DenyPaths\.|RESTRICTED, or add)'
try {
& .\Invoke-OtOpcUaInstallSwap.ps1 -InstallRoot E:\ApiInstall\OtOpcUa\app -Candidate $candidate -PreviousInstall $prevInstall `
-Carry 'appsettings*.json','logs' -NoStart *>&1 | ForEach-Object { "$_" } | Where-Object { $_ -notmatch $swapFilter } | Out-ZbStamped
} catch {
# vq04 2026-11-13: the move right after the stop hit "file in use" (host/worker still exiting after SCM said Stopped).
# Retry once only when nothing moved: service Stopped, app\ intact, candidate still staged, no PreviousInstall.
("first swap attempt failed: {0}" -f $_.Exception.Message) | Out-ZbStamped
if ((Get-Service OtOpcUaHost).Status -ne 'Stopped' -or (Test-Path -LiteralPath $prevInstall) -or -not (Test-Path -LiteralPath $candidate) -or
-not (Test-Path -LiteralPath 'E:\ApiInstall\OtOpcUa\app\OtOpcUa.Host.dll')) { throw "state not retryable: $($_.Exception.Message)" }
$waitUntil = (Get-Date).AddSeconds(30)
while ((Get-Date) -lt $waitUntil -and (Get-Process -Name 'OtOpcUa.Host','ZB.MOM.WW.OtOpcUa.ScriptWorker' -ErrorAction SilentlyContinue)) { Start-Sleep -Milliseconds 500 }
$left = @(Get-Process -Name 'OtOpcUa.Host','ZB.MOM.WW.OtOpcUa.ScriptWorker' -ErrorAction SilentlyContinue)
("host/worker processes left: {0}" -f (($left | ForEach-Object { "$($_.ProcessName)#$($_.Id)" }) -join ', ')) | Out-ZbStamped
foreach ($p in $left) { Stop-Process -Id $p.Id -Force }
Start-Sleep -Seconds 2
'retrying the swap (service stopped)' | Out-ZbStamped
& .\Invoke-OtOpcUaInstallSwap.ps1 -InstallRoot E:\ApiInstall\OtOpcUa\app -Candidate $candidate -PreviousInstall $prevInstall `
-Carry 'appsettings*.json','logs' -NoStart *>&1 | ForEach-Object { "$_" } | Where-Object { $_ -notmatch $swapFilter } | Out-ZbStamped
}
("swap returned, service={0}" -f (Get-Service OtOpcUaHost).Status) | Out-ZbStamped
if ((Get-Service OtOpcUaHost).Status -ne 'Stopped') { throw "service not stopped after the -NoStart swap" }
if (-not (Test-Path -LiteralPath $prevInstall)) { throw "swap did not move the live folder" }
if (Get-Process -Name 'OtOpcUa.Host' -ErrorAction SilentlyContinue) { throw "OtOpcUa.Host still running" }
$workerProc = Get-Process -Name 'ZB.MOM.WW.OtOpcUa.ScriptWorker' -ErrorAction SilentlyContinue
if ($workerProc) { Start-Sleep 3; if (Get-Process -Name 'ZB.MOM.WW.OtOpcUa.ScriptWorker' -ErrorAction SilentlyContinue) { throw "ScriptWorker still running" } }
foreach ($dirName in 'data','pki') {
& robocopy.exe "E:\ApiInstall\OtOpcUa\$dirName" "$backupDir\$dirName-pre-cdf433a3" /E /COPY:DAT /R:1 /W:1 /NFL /NDL /NJH /NP | Out-Null
if ($LASTEXITCODE -ge 8) { throw "$dirName backup robocopy failed $LASTEXITCODE" }
}
"data + pki backup ok -> $backupDir" | Out-ZbStamped
("staged ProductVersion=" + (Get-Item 'E:\ApiInstall\OtOpcUa\app\OtOpcUa.Host.dll').VersionInfo.ProductVersion) | Out-ZbStamped
exit 0
} catch { Write-Output "SWAP FAILED: $($_.Exception.Message)"; Write-Output "service=$((Get-Service OtOpcUaHost).Status)"; exit 1 }