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

81 lines
7.0 KiB
PowerShell

# QA OtOpcUa upgrade c9640f5c -> cdf433a3, phase 1 on ONE node (service keeps running): protected backup dir, registry
# export, script + zip hashes, expand to app.new-cdf433a3 + deps check, env NAMES/SHA prefixes (+ no OpcUa__Redundancy* /
# PeerApplicationUris*), pre-window -Verify with the new installer, swap -WhatIf. On vq03 only (-DbBackup): ConfigDb
# COPY_ONLY+CHECKSUM+COMPRESSION backup + RESTORE VERIFYONLY + migration state. Secrets never printed.
param([switch]$DbBackup)
$ErrorActionPreference = 'Stop'
try {
$stageDir = 'E:\psrem-stage\otopcua-cdf433a3'
$backupDir = "$stageDir\backup"
$candidate = 'E:\ApiInstall\OtOpcUa\app.new-cdf433a3'
$svcKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\OtOpcUaHost'
. "$stageDir\envdigest.ps1"
Write-Output "node=$env:COMPUTERNAME"
if (-not (Test-Path -LiteralPath $backupDir)) { New-Item -ItemType Directory -Force -Path $backupDir | Out-Null }
& icacls.exe $backupDir /inheritance:r /grant:r 'SYSTEM:(OI)(CI)(F)' 'BUILTIN\Administrators:(OI)(CI)(F)' | Out-Null
if ($LASTEXITCODE -ne 0) { throw "icacls backup dir failed $LASTEXITCODE" }
Write-Output "backup dir $backupDir (protected: SYSTEM + Administrators)"
& reg.exe export 'HKLM\SYSTEM\CurrentControlSet\Services\OtOpcUaHost' "$backupDir\OtOpcUaHost-pre-cdf433a3.reg" /y | Out-Null
if ($LASTEXITCODE -ne 0) { throw "reg export failed $LASTEXITCODE" }
Write-Output "reg export ok"
$expectScripts = @{ 'Install-ScriptWorkerIsolation.ps1' = 'BB31D2759D0DE29292C123D74860226ED37AF554F2C8FE6871312C7E81F7C05A';
'ScriptWorkerIsolation.psm1' = '4C1C9D0B7086D66C3977E013933018D7495C14DB56E53701633688B10AB9D32C';
'Invoke-OtOpcUaInstallSwap.ps1' = '10C07BD0F527BDBDACC2B94605FB410996F0B9446390F88A8DF5C725AB0C7C6F' }
foreach ($scriptName in $expectScripts.Keys) {
if ((Get-FileHash -LiteralPath "$stageDir\install\$scriptName" -Algorithm SHA256).Hash -ne $expectScripts[$scriptName]) { throw "script hash mismatch $scriptName" }
}
Write-Output "install scripts: 3/3 hashes match the pack"
$wantHash = ((Get-Content -LiteralPath "$stageDir\otopcua-cdf433a3.zip.sha256" -Raw).Trim() -split '\s+')[0]
$gotHash = (Get-FileHash -LiteralPath "$stageDir\otopcua-cdf433a3.zip" -Algorithm SHA256).Hash
if ($wantHash -ne $gotHash) { throw "zip hash mismatch want=$wantHash got=$gotHash" }
if (Test-Path -LiteralPath $candidate) { Remove-Item -LiteralPath $candidate -Recurse -Force }
Expand-Archive -LiteralPath "$stageDir\otopcua-cdf433a3.zip" -DestinationPath $candidate
$fileCount = (Get-ChildItem -LiteralPath $candidate -Recurse -File | Measure-Object).Count
Write-Output ("expanded files={0} (expect 1410) zip hash ok {1}" -f $fileCount, $gotHash.Substring(0,12))
if ($fileCount -ne 1410) { throw "file count mismatch" }
$verInfo = (Get-Item -LiteralPath "$candidate\OtOpcUa.Host.dll").VersionInfo.ProductVersion
Write-Output "candidate ProductVersion=$verInfo"
if ($verInfo -notlike '1.0.0+cdf433a3512353de*') { throw "unexpected ProductVersion" }
$depsText = Get-Content -LiteralPath "$candidate\OtOpcUa.Host.deps.json" -Raw
foreach ($pkg in 'ZB.MOM.WW.LocalDb/0.3.8','ZB.MOM.WW.LocalDb.Replication/0.3.8','ZB.MOM.WW.HistorianGateway.Client/1.1.0') {
if ($depsText -notmatch [regex]::Escape("`"$pkg`"")) { throw "deps.json lacks $pkg" }
}
Write-Output "deps.json: LocalDb + .Replication 0.3.8, HistorianGateway.Client 1.1.0"
$envLines = @((Get-ItemProperty $svcKeyPath).Environment)
Show-ZbEnvDigest $envLines 'pre'
$envTable = @{}
foreach ($envLine in $envLines) { $eqPos = $envLine.IndexOf('='); if ($eqPos -gt 0) { $envTable[$envLine.Substring(0,$eqPos)] = $envLine.Substring($eqPos+1) } }
foreach ($flagName in 'Scripting__Runner','Scripting__Runner__Seams__0','Scripting__Runner__Seams__1','Scripting__Runner__Seams__2','Scripting__Worker__Identity','Scripting__Worker__PeCacheDir','OtOpcUa__Telemetry__Exporter','OtOpcUa__Telemetry__OtlpEndpoint') {
if ($envTable.ContainsKey($flagName)) { Write-Output " flag $flagName=$($envTable[$flagName])" } else { Write-Output " flag $flagName=<absent>" }
}
$redundancyNames = @($envTable.Keys | Where-Object { $_ -like 'OpcUa__Redundancy*' -or $_ -like 'OpcUa__PeerApplicationUris*' })
Write-Output (" OpcUa__Redundancy*/PeerApplicationUris* entries: {0}" -f $redundancyNames.Count)
# QA runs Warm since 2026-11-13: redundancy keys are expected (recorded above, not refused)
$appsettingsHits = @(Get-ChildItem -LiteralPath 'E:\ApiInstall\OtOpcUa\app' -Filter 'appsettings*.json' | Select-String -Pattern 'Redundancy|PeerApplicationUris')
Write-Output (" appsettings*.json Redundancy/PeerApplicationUris hits: {0}" -f $appsettingsHits.Count)
if ($DbBackup) {
$connBuilder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($envTable['ConnectionStrings__ConfigDb'])
$sqlcmdExe = (Get-Command sqlcmd.exe -ErrorAction SilentlyContinue).Source
if (-not $sqlcmdExe) { $sqlcmdExe = 'C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE' }
$env:SQLCMDPASSWORD = $connBuilder.Password
try {
& $sqlcmdExe -S $connBuilder.DataSource -U $connBuilder.UserID -C -I -b -d master -W -h -1 -Q "BACKUP DATABASE [OtOpcUaConfig] TO DISK = N'OtOpcUaConfig_pre-cdf433a3_20261119.bak' WITH COPY_ONLY, INIT, CHECKSUM, COMPRESSION; RESTORE VERIFYONLY FROM DISK = N'OtOpcUaConfig_pre-cdf433a3_20261119.bak' WITH CHECKSUM; SELECT 'db backup + verify ok';"
if ($LASTEXITCODE -ne 0) { throw "db backup failed $LASTEXITCODE" }
& $sqlcmdExe -S $connBuilder.DataSource -U $connBuilder.UserID -C -I -b -d $connBuilder.InitialCatalog -W -h -1 -Q "SET NOCOUNT ON; SELECT 'migrations=' + CAST(COUNT(*) AS varchar(10)) + ' last=' + MAX(MigrationId) FROM __EFMigrationsHistory; SELECT 'snapshot=' + CAST(snapshot_isolation_state_desc AS varchar(20)) FROM sys.databases WHERE name = DB_NAME(); SELECT TOP 1 'latest sealed=' + LOWER(CONVERT(varchar(36),DeploymentId)) + ' rev=' + RevisionHash FROM dbo.Deployment WHERE Status = 2 ORDER BY SealedAtUtc DESC;"
if ($LASTEXITCODE -ne 0) { throw "migration query failed $LASTEXITCODE" }
} finally { Remove-Item Env:SQLCMDPASSWORD -ErrorAction SilentlyContinue; Remove-Variable connBuilder -ErrorAction SilentlyContinue }
}
Set-Location "$stageDir\install"
Write-Output '--- pre-window -Verify (new installer, service up)'
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\Install-ScriptWorkerIsolation.ps1 -Verify *>&1 | ForEach-Object { "$_" } | Where-Object { $_ -notmatch 'does not inherit the worker deny|grants nothing to Everyone|^\s*(@\{|-DenyPaths\.|to -DenyPaths\.|add it to -DenyPaths\.|RESTRICTED, or add)' }
$verifyExit = $LASTEXITCODE
Write-Output "pre-window verify exit=$verifyExit"
if ($verifyExit -ne 0) { throw "pre-window verify failed" }
Write-Output '--- swap -WhatIf (preflight only)'
& .\Invoke-OtOpcUaInstallSwap.ps1 -InstallRoot E:\ApiInstall\OtOpcUa\app -Candidate $candidate -PreviousInstall E:\ApiInstall\OtOpcUa\app.bak.20261119-pre-cdf433a3 -Carry 'appsettings*.json','logs' -WhatIf *>&1 | ForEach-Object { "$_" }
Write-Output ("E: free {0} GB" -f [int]((Get-PSDrive E).Free/1GB))
Write-Output "service=$((Get-Service OtOpcUaHost).Status)"
exit 0
} catch { Write-Output "PREPARE FAILED: $($_.Exception.Message)"; exit 1 }