Files
scadaproj/docs/deploy/2026-11-19-qa-nam-ldap/sb-qa.tmpl.ps1
T

132 lines
8.5 KiB
PowerShell

# ScadaBridge QA (vq03/vq04) 061432c8 -> 2e0ce81e (#144/#145, no migration) + NAM AD LDAP, one window. Rendered per call: ACTION / TS.
# prep | backup (vq03) | stop-site | stop-central | swap | start-central | start-site | verify | rollback-swap
# Derived from ScadaBridge deploy-prep/2026-11-15-143/qa.tmpl.ps1 (migrate dropped; the swap also installs the per-node NAM
# appsettings.Central.json and removes the GLAuth password env entry from the Central service).
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$Action = '__ACTION__'
$ts = '__TS__'
$sha = '2e0ce81e'
$fromSha = '061432c8'
$zipSha256 = '6C706B0A76B7E9EC7CA00E12CEA19C5D86814BEAF66B541F527CBFC3D6572E23'
$stageDir = "E:\psrem-stage\sb-$sha"
$stage = "$stageDir\unpacked"
$root = 'E:\ApiInstall\ScadaBridge'
$log = "$root\_DEPLOY-NOTES-$sha-$ts.txt"
$settings = 'appsettings.json','appsettings.Central.json','appsettings.Site.json'
$centralKey = 'HKLM:\SYSTEM\CurrentControlSet\Services\ScadaBridge-Central'
$namCentral = "$stageDir\appsettings.Central.json.nam-" + $env:COMPUTERNAME.Substring($env:COMPUTERNAME.Length-4).ToLower()
$bak = "F:\MSSQL\BACKUP\ScadaBridge_pre-$($sha)_$ts.bak"
function Say($m) { $line = "[{0:HH:mm:ss.fff}] {1} {2}" -f (Get-Date), $env:COMPUTERNAME, $m; $line; Add-Content -Path $log -Value $line }
function Is-Dropped($e) { $e -like 'ScadaBridge__Security__Ldap__ServiceAccountPassword=*' }
function Digest($entries) {
$h = [System.Security.Cryptography.SHA256]::Create(); $o = @()
foreach ($e in $entries) { $i = $e.IndexOf('='); if ($i -lt 1) { $o += '<unparsable>'; continue }
$v = $e.Substring($i+1); $x = -join ($h.ComputeHash([Text.Encoding]::UTF8.GetBytes($v)) | % { $_.ToString('x2') })
$o += ("{0} sha256:{1} len={2}" -f $e.Substring(0,$i), $x.Substring(0,12), $v.Length) }
$h.Dispose(); $o | Sort-Object
}
function Wait-Ready($url, $seconds) {
$deadline = (Get-Date).AddSeconds($seconds)
while ((Get-Date) -lt $deadline) { try { if ((Invoke-WebRequest -UseBasicParsing $url -TimeoutSec 5).StatusCode -eq 200) { return $true } } catch {}; Start-Sleep 2 }
return $false
}
function Stop-Svc($s) {
if ((Get-Service $s).Status -ne 'Stopped') { Say "stopping $s"; Stop-Service $s -Force; (Get-Service $s).WaitForStatus('Stopped','00:02:00') }
Say "$s stopped"
}
function Sql-Init {
$envC = @((Get-ItemProperty $centralKey).Environment)
$cs = ($envC | Where-Object { $_ -like 'ScadaBridge__Database__ConfigurationDb=*' }) -replace '^[^=]*=',''
$script:sb = New-Object System.Data.SqlClient.SqlConnectionStringBuilder $cs
$script:db = $sb['Database']; if (-not $db) { $script:db = $sb.InitialCatalog }
if ($db -ne 'ScadaBridge') { throw "unexpected catalog" }
$env:SQLCMDPASSWORD = $sb.Password
}
function Sql-Scalar($q) { $r = & sqlcmd -S $sb.DataSource -d $db -U $sb.UserID -C -h -1 -W -b -Q "SET NOCOUNT ON; $q"; if ($LASTEXITCODE -ne 0) { throw "sql failed" }; ($r | Where-Object { $_ -and $_.Trim() } | Select-Object -First 1).Trim() }
$schemaQ = "SELECT CONCAT((SELECT COUNT(*) FROM __EFMigrationsHistory),'|',(SELECT MAX(MigrationId) FROM __EFMigrationsHistory),'|',(SELECT COUNT_BIG(*) FROM dbo.AuditLog))"
switch ($Action) {
'prep' {
Say "PREP $fromSha -> $sha"
if ((Get-FileHash "$stageDir\scadabridge-$sha.zip" -Algorithm SHA256).Hash -ne $zipSha256) { throw "zip hash mismatch" }
if (-not (Test-Path $namCentral)) { throw "missing $namCentral" }
Get-Content $namCentral -Raw | ConvertFrom-Json | Out-Null
if (Test-Path $stage) { Remove-Item $stage -Recurse -Force }
Expand-Archive "$stageDir\scadabridge-$sha.zip" $stage
$sv = (Get-Item "$stage\app\ZB.MOM.WW.ScadaBridge.Host.exe").VersionInfo.ProductVersion
$sc = (Get-Item "$stage\cli\scadabridge.exe").VersionInfo.ProductVersion
if ($sv -notlike "*+$sha*" -or $sc -notlike "*+$sha*") { throw "staged exe $sv / $sc" }
$lv = (Get-Item "$root\app\ZB.MOM.WW.ScadaBridge.Host.exe").VersionInfo.ProductVersion
if ($lv -notlike "*+$fromSha*") { throw "live exe $lv" }
Say "staged $sv; live $lv; app files $((Get-ChildItem "$stage\app" -Recurse -File).Count), cli $((Get-ChildItem "$stage\cli" -Recurse -File).Count)"
foreach ($s in 'ScadaBridge-Central','ScadaBridge-Site') {
& reg.exe export "HKLM\SYSTEM\CurrentControlSet\Services\$s" "$stageDir\_envbak-$s-$ts.reg" /y | Out-Null
if ($LASTEXITCODE -ne 0) { throw "reg export $s failed" }
$e = @((Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\$s").Environment)
Say ("$s env entries {0}" -f $e.Count); Digest $e | % { Say " env $s $_" }
}
$keep = New-Item -ItemType Directory -Force -Path "$root\_appsettings-keep-$ts"
foreach ($f in $settings) { Copy-Item "$root\app\$f" "$keep\$f"; Say ("keep {0} sha256 {1}" -f $f, (Get-FileHash "$keep\$f" -Algorithm SHA256).Hash) }
}
'backup' {
Sql-Init
$pre = Sql-Scalar $schemaQ; Say "schema before: $pre"
if (-not $pre.StartsWith('65|')) { throw "expected 65 migrations" }
& sqlcmd -S $sb.DataSource -d master -U $sb.UserID -C -b -Q "BACKUP DATABASE [$db] TO DISK = N'$bak' WITH COPY_ONLY, CHECKSUM, COMPRESSION, INIT" | Out-Null
if ($LASTEXITCODE -ne 0) { throw "backup failed" }
& sqlcmd -S $sb.DataSource -d master -U $sb.UserID -C -b -Q "RESTORE VERIFYONLY FROM DISK = N'$bak' WITH CHECKSUM" | Out-Null
if ($LASTEXITCODE -ne 0) { throw "verifyonly failed" }
Say "backup $bak VERIFYONLY ok"
}
'stop-site' { Stop-Svc 'ScadaBridge-Site' }
'stop-central' {
Stop-Svc 'ScadaBridge-Central'
$deadline = (Get-Date).AddSeconds(30)
while ((Get-Process -Name 'ZB.MOM.WW.ScadaBridge.Host' -ErrorAction SilentlyContinue) -and (Get-Date) -lt $deadline) { Start-Sleep 1 }
if (Get-Process -Name 'ZB.MOM.WW.ScadaBridge.Host' -ErrorAction SilentlyContinue) { throw "Host.exe still running" }
Say "no Host.exe remains"
}
'swap' {
if (Get-Process -Name 'ZB.MOM.WW.ScadaBridge.Host' -ErrorAction SilentlyContinue) { throw "Host.exe running" }
Rename-Item "$root\app" "app.bak.$ts"; Rename-Item "$root\cli" "cli.bak.$ts"
Say "renamed app/cli -> *.bak.$ts"
Copy-Item "$stage\app" "$root\app" -Recurse
Copy-Item "$stage\cli" "$root\cli" -Recurse
$keep = "$root\_appsettings-keep-$ts"
foreach ($f in $settings) {
Copy-Item "$keep\$f" "$root\app\$f" -Force
if ((Get-FileHash "$keep\$f" -Algorithm SHA256).Hash -ne (Get-FileHash "$root\app\$f" -Algorithm SHA256).Hash) { throw "appsettings mismatch $f" }
Say "restored $f (sha256 match)"
}
Copy-Item $namCentral "$root\app\appsettings.Central.json" -Force
Say ("installed NAM appsettings.Central.json sha256 {0}" -f (Get-FileHash "$root\app\appsettings.Central.json" -Algorithm SHA256).Hash)
Say ("live Host " + (Get-Item "$root\app\ZB.MOM.WW.ScadaBridge.Host.exe").VersionInfo.ProductVersion + " / CLI " + (Get-Item "$root\cli\scadabridge.exe").VersionInfo.ProductVersion)
$before = @((Get-ItemProperty $centralKey).Environment)
$keepEnv = @($before | Where-Object { -not (Is-Dropped $_) })
Set-ItemProperty -Path $centralKey -Name Environment -Value ([string[]]$keepEnv) -Type MultiString
$after = @((Get-ItemProperty $centralKey).Environment)
foreach ($e in $keepEnv) { if ($after -notcontains $e) { throw "env entry changed" } }
if (@($after | ? { Is-Dropped $_ }).Count -ne 0) { throw "GLAuth password entry still present" }
Say ("central env entries {0} -> {1}" -f $before.Count, $after.Count)
}
'start-central' { Say "starting ScadaBridge-Central"; Start-Service 'ScadaBridge-Central'; $ok = Wait-Ready 'http://localhost:8085/health/ready' 180; Say "central ready=$ok"; if (-not $ok) { throw "central not ready" } }
'start-site' { Say "starting ScadaBridge-Site"; Start-Service 'ScadaBridge-Site'; $ok = Wait-Ready 'http://localhost:8084/health/ready' 180; Say "site ready=$ok"; if (-not $ok) { throw "site not ready" } }
'verify' {
foreach ($s in 'ScadaBridge-Central','ScadaBridge-Site') { Say ("$s " + (Get-Service $s).Status) }
if ($env:COMPUTERNAME -eq 'WONDER-APP-VQ03') { Sql-Init; Say ("schema now: " + (Sql-Scalar $schemaQ)) }
}
'rollback-swap' {
foreach ($s in 'ScadaBridge-Site','ScadaBridge-Central') { Stop-Svc $s }
if (Test-Path "$root\app") { Rename-Item "$root\app" "app.failed.$ts" }
if (Test-Path "$root\cli") { Rename-Item "$root\cli" "cli.failed.$ts" }
Rename-Item "$root\app.bak.$ts" 'app'; Rename-Item "$root\cli.bak.$ts" 'cli'
& reg.exe import "$stageDir\_envbak-ScadaBridge-Central-$ts.reg" | Out-Null
Say "rolled back binaries + Central env (services left STOPPED; start Central vq03, vq04, then Sites)"
}
default { throw "bad action $Action" }
}
Remove-Item Env:SQLCMDPASSWORD -ErrorAction SilentlyContinue
Say "$Action OK"
exit 0