Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
20 KiB
Deploy / Upgrade Runbook: NSSM-wrapped Gateway Service (Server + x86 Worker)
Written for mxaccessgw#207 (archreview R21-O31 / cycle-1 TST-10). There is no deploy script in this repo; this runbook is the procedure. It is assembled only from what the repo contains (
docs/GatewayConfiguration.md,docs/Authentication.md,docs/Diagnostics.md,docs/runbooks/*,docs/plans/2026-08-07-*,docs/superpowers/plans/2026-10-19-ackmsg-as-operator.md,src/.../appsettings.json,scripts/ci/) cross-checked againstscadaproj/deployment.md§5 and §7.1.Verification legend. Statements are either backed by a cited repo file, or tagged [UNVERIFIED] (comes from deploy notes / other repos / memory and could not be confirmed from this repo). Treat every [UNVERIFIED] item as "confirm on the box before relying on it".
Scope of remote execution. On the wonder boxes (
wonder-app-vd03DEV,wonder-app-vq03/vq04QA) remote command execution is permitted for the on-box steps of a deploy the owner has asked for, and nothing else (CLAUDE.md"Wonder box remote access" note; the umbrellascadaprojindex carries the current rule). No ad-hoc diagnostics over remote exec: use SMB file reads, HTTP probes and direct SQL. Confirm the current rule before starting; this repo's note is dated 2026-09-11 and the umbrella index has since relaxed it to deploy-only [UNVERIFIED from this repo].
0. What is being deployed
Two artifacts, deployed together because they share a wire protocol (WorkerProtocolVersion,
appsettings.json MxGateway:Protocol):
| Artifact | Build | Runs as |
|---|---|---|
Server (x64 .NET 10, gRPC :5120 h2c + Blazor dashboard :5130) |
dotnet publish src/ZB.MOM.WW.MxGateway.Server -c Release (framework-dependent in the windev flow; self-contained is the alternative if the box lacks a .NET 10 runtime [UNVERIFIED]) |
The NSSM service MxAccessGw |
| Worker (x86 .NET Framework 4.8, one process per gateway session, owns the MXAccess COM STA) | dotnet build src/ZB.MOM.WW.MxGateway.Worker/ZB.MOM.WW.MxGateway.Worker.csproj -p:Platform=x86 (CLAUDE.md) - Windows only; builds on windev, not on macOS/Linux (scripts/ci/README.md) |
Child of the Server, launched from MxGateway:Worker:ExecutablePath. Not a service of its own. |
The Server finds the Worker through MxGateway:Worker:ExecutablePath
(docs/GatewayConfiguration.md, default is a repo-relative bin\x86\Release path, so deployed hosts
override it via the NSSM env var MxGateway__Worker__ExecutablePath).
Service name MxAccessGw and the windev layout (C:\publish\mxaccessgw\Server-YYYYMMDD,
C:\publish\mxaccessgw\Worker, build clone C:\build\mxaccessgw-ci) are from
docs/superpowers/plans/2026-10-19-ackmsg-as-operator.md Task "Deploy" and
docs/runbooks/IdentifyingADeployedBuild.md. The wonder boxes use E:\ApiInstall\... per
scadaproj/deployment.md §7.1; the exact MxAccessGw subfolder names there are [UNVERIFIED].
Configuration is delivered as NSSM AppEnvironmentExtra environment variables
(Kestrel__Endpoints__*, MxGateway__Authentication__SqlitePath, MxGateway__Worker__ExecutablePath,
ASPNETCORE_ENVIRONMENT/DOTNET_ENVIRONMENT, ZB_SECRETS_MASTER_KEY, the API-key pepper,
MxGateway__Ldap__ServiceAccountPassword), not by editing appsettings.json
(docs/GatewayConfiguration.md, "Kestrel", "Secrets Master Key").
1. State that MUST survive the swap
The upgrade renames/replaces the application directory. Anything stored inside it is lost. The validator therefore rejects stores inside the content root (see the 2026-08-09 incident below).
| State | Setting | Default location (Windows) | Notes |
|---|---|---|---|
| API-key store (SQLite) | MxGateway:Authentication:SqlitePath (NSSM: MxGateway__Authentication__SqlitePath) |
C:\ProgramData\MxGateway\gateway-auth.db |
Must be rooted; validator also rejects a path inside the content root. Back up the -wal/-shm siblings with it. |
| API-key pepper | secret MxGateway:ApiKeyPepper (PepperSecretName); lives outside the DB, in NSSM env / Secrets store |
n/a | Without the same pepper every issued mxgw_... token fails verification. Never in the DB (docs/Authentication.md). Back it up in the org vault. |
| Secrets store (SQLite) | Secrets:SqlitePath (NSSM: Secrets__SqlitePath) |
C:\ProgramData\MxGateway\mxgateway-secrets.db |
Must be absolute AND outside the content root; fail-closed with no opt-out (docs/GatewayConfiguration.md L349; scadaproj Secrets notes). |
| Secrets master key (KEK) | ZB_SECRETS_MASTER_KEY in NSSM AppEnvironmentExtra |
n/a | Losing it makes the store unrecoverable; no RewrapAll yet (docs/GatewayConfiguration.md, "Rotation / DR"). Back it up in the vault, not just on the box. |
| Galaxy snapshot cache | MxGateway:Galaxy:SnapshotCachePath |
C:\ProgramData\MxGateway\galaxy-snapshot.json |
Cache only; rejected inside content root so it survives deploys. Losing it is harmless. |
| TLS self-signed cert | Tls:SelfSignedCertPath |
C:\ProgramData\MxGateway\certs\gateway-selfsigned.pfx |
If lost, clients pinned to the old cert see a changed cert. |
| NSSM env block | nssm get MxAccessGw AppEnvironmentExtra |
NSSM registry config | File copies do not touch it, but nssm set AppEnvironmentExtra replaces the whole block. |
Local appsettings*.json overrides in the deploy dir |
n/a | deploy dir | Copy the live ones aside and restore them after the swap (the windev plan copies appsettings.json / appsettings.Development.json from the current dir). |
Incident that motivates the rule (2026-08-09, per docs/GatewayConfiguration.md L127): the
API-key store lived inside the application directory; the upgrade renamed that directory to
Server.bak.*, silently starting an empty store, and no gRPC client could authenticate for two
days. The SQLite DB must also never be opened with a host-side sqlite3 while the service runs
(copy the file triplet and query the copy; cross-repo lesson, [UNVERIFIED from this repo]).
2. Traps (read before touching anything)
OrphanWorkerTerminatorkills live workers if a second gateway starts. On startup the Server enumerates processes named after the configured Worker exe (ZB.MOM.WW.MxGateway.Worker) and kills every one whose path equals the configuredExecutablePath- or whose path it cannot read, which is the normal case for an x86 process seen from an x64 gateway, so it falls back to image-name match only (src/ZB.MOM.WW.MxGateway.Server/Workers/OrphanWorkerTerminator.cs). It skips only its own PID's children logic (currentProcessId), not another gateway's workers. Consequence: starting a second gateway process on the same box (a manualdotnet ...Server.dllorServer.exewhile the service is up, a second NSSM service, a "test" start of the new build before stopping the old one, or a rollback started while the new one still runs) terminates every live session's Worker and all MXAccess subscriptions with it. Rule: exactly one gateway process per box at any time; stop the service and confirm the process has exited before starting anything else. Never smoke-test the new build by launching it beside the running service. (On QA, vq03 and vq04 are independent boxes with independent gateways; this trap is per-box, not per-pair.)- A gateway restart does not reattach workers (
OrphanWorkerTerminatorsummary). Plan for every client session to drop and for clients to reopen sessions. Session-resilience reattach is opt-in design only (docs/plans/2026-06-15-session-resilience-design.md); do not assume it is on. - Secrets store path.
Secrets:SqlitePathmust be absolute and outside the content root or the host fails closed at startup. A store left at an in-tree path is exactly what the swap removes. Also: thesecretCLI and the gateway must use the sameSecrets__SqlitePath, or the CLI seeds a different store and the gateway then fails closed as if the secret were never set (docs/GatewayConfiguration.mdL381-395). nssm set ... AppEnvironmentExtrareplaces the whole block.nssm getfirst, save the output (redactPassword=/key values in anything you paste), then re-include every entry (docs/GatewayConfiguration.mdL370-374;docs/runbooks/SEC-36-ldap-credential-rotation.md). Thenssm set MxAccessGw Application/AppDirectorycommands used for repointing do not touch the env block.- Worker bitness. The Worker must be built x86 (
-p:Platform=x86) andRequiredArchitecturestaysX86. The Server (x64) cannot load MXAccess COM. Build on windev (ssh windevflow,scripts/ci/windev-worker-ci.ps1), not on a Mac. Deploying a Worker from a different commit than the Server risks a protocol mismatch; build both from the same SHA. - Environment name gates security hard-stops (
docs/GatewayConfiguration.mdL317-335):Dashboard:DisableLoginand plaintext LDAP are refused only when the environment name is exactlyProduction. Do not changeASPNETCORE_ENVIRONMENT/DOTNET_ENVIRONMENTduring an upgrade; a host that boots under a different label silently loses those guards, and a host that was permissive may refuse to boot if you "fix" the label. - Version stamp. Do not trust
InformationalVersionfor builds from 2026-07-09 to 2026-08-10 (docs/runbooks/IdentifyingADeployedBuild.md). Record the SHA yourself in the deploy notes. - Stuck
STOP_PENDING. Stopping the service can hang inSTOP_PENDING(workers must exit withinWorker:ShutdownTimeoutSeconds, default 10). Observed on the QA boxes per deploy notes [UNVERIFIED from this repo]; if it hangs, wait, then kill the Server process by PID. Do not start the new build untilGet-Processshows noZB.MOM.WW.MxGateway.Serverand noZB.MOM.WW.MxGateway.Worker.
3. Preflight (no changes yet)
- Decide the target SHA on
origin/main; confirm CI is green for it (windows-x86job runs the Worker build +Worker.Testson windev;nightly-windevruns the live tier,scripts/ci/README.md). - Read the current state of the service (read-only):
Note the entry count and names of the env block; verify the same count after the start.
nssm get MxAccessGw Application nssm get MxAccessGw AppDirectory nssm get MxAccessGw AppParameters nssm get MxAccessGw AppEnvironmentExtra # save to the deploy notes; redact secrets - Confirm the effective store paths from the env block:
MxGateway__Authentication__SqlitePath,Secrets__SqlitePath,MxGateway__Worker__ExecutablePath. If either store path is missing from the env block it is on the code default (C:\ProgramData\MxGateway\...), which is fine; if it is under the application directory, stop and move it first (see section 1). - Confirm the pepper and
ZB_SECRETS_MASTER_KEYare present in the env block (names only). - Read the dashboard Settings page (
/settingson the dashboard port,:5130) and save the effective config; it now includes the heartbeat check interval, stuck ceiling, event-channel full-mode timeout and replay byte cap. Compare after the upgrade to catch config drift. - Check the box is quiet: dashboard sessions list, or
GET /health/active(themxaccess-sessionscheck reportstotal/ready/faulted/starting/closing). Agree a window with clients (OtOpcUaGalaxyDriver, ScadaBridge MxGateway adapter) - their sessions will drop. - Check the new build's required config: diff the repo
src/ZB.MOM.WW.MxGateway.Server/appsettings.jsonand the changelog for new mandatory options, anddocs/GatewayConfiguration.mdfor validator changes (the validator can refuse to boot on a bad value, which is a rollback trigger). - Free disk for a full copy of the deploy dir and the stores (see backup).
- Check auth-store schema compatibility: the Server migrates the API-key DB on start
(
Authentication:RunMigrationsOnStartup: true) via theZB.MOM.WW.Authpackage. If the new build bumps the Auth package, the schema can migrate forward and the old binary may not read it back (see the 2026-08-07 follow-up plan'sAuthStoreMigrationExceptioncheck). Treat the DB backup as the rollback for a schema bump.
4. Build (on windev; no deploy yet)
windev flow from docs/superpowers/plans/2026-10-19-ackmsg-as-operator.md:
git -C C:\build\mxaccessgw-ci fetch origin
git -C C:\build\mxaccessgw-ci worktree add C:\build\mxgw-deploy <sha> # detached worktree, host checkout untouched
cd C:\build\mxgw-deploy
dotnet publish src\ZB.MOM.WW.MxGateway.Server -c Release -o C:\publish\mxaccessgw\Server-YYYYMMDD
dotnet build src\ZB.MOM.WW.MxGateway.Worker\ZB.MOM.WW.MxGateway.Worker.csproj -c Release -p:Platform=x86
# stage the Worker output (bin\x86\Release) into a temp dir, e.g. C:\publish\mxaccessgw\Worker-new
Remove the worktree afterwards (git worktree remove); an absent C:\build\mxgw-deploy is the
normal end state (IdentifyingADeployedBuild.md). Whether the Worker is built with dotnet build
or dotnet publish, and its exact output folder, should be matched to the existing Worker folder on
the host [UNVERIFIED]. Note: the Worker is net48 and must be built where the net48 targeting
pack and MSBuild are present (windev; scadaproj notes say Build Tools 2022 + net48 reference
assemblies verified 2026-09-10 [UNVERIFIED from this repo]).
For the wonder boxes the staged output is copied over SMB (UNC copy is always allowed) into a
staging directory such as E:\psrem-stage\<app>-<sha>\ per scadaproj/deployment.md §7.1
([UNVERIFIED] path convention); the on-box steps below are then the only remote-exec part.
5. Backup
With the service still running copy nothing live; do these after stopping (section 6 step 1), or copy the stores with the service stopped. Back up:
- The current deploy directory (
Server-YYYYMMDDorServer) to a siblingServer.bak.<timestamp>(the conventionIdentifyingADeployedBuild.mdreads as a deploy history chain), and the currentWorkerdirectory toWorker.bak-<timestamp>[-label]. gateway-auth.dbplusgateway-auth.db-wal/-shmif present, andmxgateway-secrets.db(plus its-wal/-shm), to*.bak-<label>next to the originals. Never delete or move the live DB.- The saved
AppEnvironmentExtratext and thenssm get Application/AppDirectoryvalues (for rollback repointing). - Off-box: the KEK and pepper in the org vault (not on the box only).
- Record the old and new SHA, timestamps and backup paths in a
_DEPLOY-NOTES-<timestamp>.txtnext to the install (the convention seen on wonder perdeployment.md§7.1, [UNVERIFIED]).
6. Stop / swap / start
Do this on one box at a time; QA vq03 and vq04 are independent gateways, so roll them one after
the other (deployment.md §7.1 records the 2026-09-10 round as "rolling, one stop per box").
- Stop and confirm gone.
Both lists must be empty before continuing (traps 1 and 8). If a Worker is left over, the next start will kill it as an orphan (by design) - but it signals an unclean stop worth noting.
nssm stop MxAccessGw # or Stop-Service MxAccessGw -Force Get-Process ZB.MOM.WW.MxGateway.Server, ZB.MOM.WW.MxGateway.Worker -ErrorAction SilentlyContinue - Backup (section 5) now that nothing holds the stores open.
- Swap the Server. Preferred: deploy to a new dated directory and repoint NSSM, which leaves the
old directory intact as the rollback target:
(
Copy-Item <old>\appsettings.json, <old>\appsettings.Development.json <new>\ # preserve live file config nssm set MxAccessGw Application <new>\ZB.MOM.WW.MxGateway.Server.exe nssm set MxAccessGw AppDirectory <new>2026-10-19-ackmsg-as-operator.md;IdentifyingADeployedBuild.md"Deployment-side naming".) Alternative used on the wonder boxes: renameapptoapp.bak.<timestamp>and copy the new files intoapp(deployment.md§7.1; [UNVERIFIED]). Either way, restore any host-specificappsettings*.jsonbyte-identical; do not edit the NSSM env block unless the release notes require it. - Swap the Worker. Rename
WorkertoWorker.bak-<timestamp>, copy the new x86 Worker build in its place, and make sureMxGateway__Worker__ExecutablePathpoints at it (the path is matched case-insensitively by the orphan terminator, so keep one canonical spelling). - Verify the env block is untouched:
nssm get MxAccessGw AppEnvironmentExtraentry count equals the preflight count. - Start (
nssm start MxAccessGw). Exactly once; do not retry-start concurrently with anything else.
7. Post-deploy verification
All of this can be done over HTTP/gRPC from another machine or from the box without further remote exec beyond the deploy window.
- Service
RUNNINGand stable for at least 60 s (no restart loop); the Application event log has a clean host start and no newAuthStoreMigrationException/Hosting failed to startafter the start time (2026-08-07-followups-windev-ldapfixtures-runners.mdTask 2). - Health:
GET http://<host>:5130/healthz(live),/health/ready(auth-storecheck),/health/active(mxaccess-sessions). These endpoints are anonymous by design (GatewayApplication.cs,docs/Diagnostics.md). Zero sessions is Healthy, so green does not prove the Worker/COM path works. - Prove the Worker path, since an idle health probe does not: open a gRPC session with an existing
API key (any of the clients, or the
mxaCLI) and do a read of a known tag. This also proves the API-key store and pepper survived: amxgw_...token issued before the upgrade must authenticate. Negative control: a wrong token must be rejected (UNAUTHENTICATED). - Dashboard: login works (or the localhost anonymous policy applies),
/settingsmatches the preflight capture, the version/Worker protocol line is right. - Identify the deployed build by behaviour, not the version stamp where it is unreliable
(
IdentifyingADeployedBuild.md): a plainWritereturningstatuses[0]proves a Worker at or past53f69cd. - Check the Server log for
Terminated orphan worker process- expected only if the previous stop was unclean. - Check downstream clients reconnected (OtOpcUa
GalaxyDriver; ScadaBridge MxGateway adapter) and, on a pair of QA boxes, repeat sections 5-7 for the second box. - Write the deploy note: SHA, backup paths, env-entry count, evidence for each check above.
8. Rollback
Triggers: service will not stay RUNNING, host fails validator at start, health red beyond the grace
window, API keys no longer authenticate, or a Worker session cannot be opened.
nssm stop MxAccessGw; confirm no Server/Worker process (trap 1: never start the old build while the new one is still up).- Repoint:
nssm set MxAccessGw Application <old>\...Server.exeandAppDirectory <old>, or renameapp.bak.<ts>back toapp, and restoreWorker.bak-<ts>overWorker. - If the API-key DB was migrated (schema bump, trap in section 3 step 9) or damaged, restore
gateway-auth.dbfrom the backup (restore the-wal/-shmtogether or none). Otherwise leave the live store alone: the env-block stores are outside the swap and need no restore. nssm get AppEnvironmentExtraentry count equals the preflight count; if you changed it, restore the saved block (all entries).nssm start MxAccessGw; repeat section 7 steps 1-3.- Report: what failed, the log excerpt, backup paths used.
9. Open gaps (not verifiable from this repo)
- Exact install paths on the wonder boxes (
E:\ApiInstall\MxAccessGw\...), the staging convention and the_DEPLOY-NOTESfile format - taken fromscadaproj/deployment.md, whose live content could not be cross-checked here. - Whether the Server on each box is framework-dependent or self-contained (a box without .NET 10 needs self-contained; the 2026-10-19 windev plan says framework-dependent).
- The
STOP_PENDINGbehaviour and the NSSM service account (LocalSystemperdeployment.md). - NSSM install script: none exists in the repo - installing a new box (service creation,
AppExit, recovery, log redirection settings) is not covered here; the only reference isdeployment.md's statement that VQ04 was cloned from VQ03's NSSMAppEnvironmentExtraconfig. - Automating this runbook as a script was the other half of the original finding; not done here.