#!/usr/bin/env node import { createHash } from "node:crypto"; import { spawnSync } from "node:child_process"; import { cp, lstat, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join, relative, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; const scriptDir = dirname(fileURLToPath(import.meta.url)); const platformRoot = resolve(scriptDir, "../.."); const devicePlaneRoot = platformRoot; const managerRoot = resolve(platformRoot, "apps/device-manager"); const artifactDir = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(scriptDir, "../deploy-artifacts")); const [patchId = "device-manager-release-v3-20260812-026", ...extra] = process.argv.slice(2); if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) throw new Error("usage: build-device-manager-control-plane-artifact.mjs [patch-id]"); const descriptorPath = patchId.startsWith("device-manager-release-v4-") ? "deployment/device-manager-release-v4.json" : patchId.startsWith("device-manager-release-v3-") ? "deployment/device-manager-release-v3.json" : "deployment/device-manager-release-v1.json"; const isV3 = descriptorPath.endsWith("release-v3.json"); const isV4 = descriptorPath.endsWith("release-v4.json"); const isManagerOnly = isV3 || isV4; const composeSource = resolve(devicePlaneRoot, "docker-compose.device-manager.yml"); const composeSourceSha256 = createHash("sha256").update(await readFile(composeSource)).digest("hex"); if (!isV4 && composeSourceSha256 !== "4954120aaddc999798b64c304d8cf692b79714feb727d873117bd1f3434e865e") { throw new Error("historical_device_manager_compose_has_advanced"); } const entries = isManagerOnly ? [ "docker-compose.device-manager.yml", "services/device-manager", descriptorPath, ] : [ ".dockerignore", "package.json", "package-lock.json", "docker-compose.device-manager.yml", "packages/device-protocol-contract", "packages/device-edge-channel-contract", "packages/arusnavi-b2-adapter", "services/device-control-core", "services/device-gateway/package.json", "services/device-edge-relay/package.json", "services/device-manager", descriptorPath, ]; const stage = await mkdtemp(join(tmpdir(), "nodedc-device-manager-control-plane-")); const payload = join(stage, "payload"); const target = join(artifactDir, `nodedc-device-plane-${patchId}.tgz`); try { const build = spawnSync("npm", ["run", "build", "--workspace", "@nodedc/device-manager"], { cwd: platformRoot, encoding: "utf8", maxBuffer: 64 * 1024 * 1024, }); if (build.status !== 0) throw new Error(`device_manager_build_failed:${build.stderr || build.stdout}`); await mkdir(payload, { recursive: true }); for (const entry of entries) { if (entry === descriptorPath) { const descriptor = JSON.parse(await readFile(resolve(devicePlaneRoot, entry), "utf8")); if (descriptor.releaseId !== "__PATCH_ID__") { throw new Error("device_manager_release_template_id_mismatch"); } descriptor.releaseId = patchId; const destination = join(payload, entry); await mkdir(dirname(destination), { recursive: true }); await writeFile(destination, `${JSON.stringify(descriptor, null, 2)}\n`, "utf8"); continue; } if (entry === "services/device-manager") { const destination = join(payload, entry); await mkdir(destination, { recursive: true }); await copySafe(resolve(devicePlaneRoot, "services/device-manager/Dockerfile"), join(destination, "Dockerfile"), devicePlaneRoot); await copySafe(resolve(managerRoot, "server"), join(destination, "server"), managerRoot); await copySafe(resolve(managerRoot, "dist"), join(destination, "dist"), managerRoot); continue; } await copySafe(resolve(devicePlaneRoot, entry), join(payload, entry), devicePlaneRoot); } if (!isManagerOnly) { await validateDockerCopySources( join(payload, "services/device-control-core/Dockerfile"), payload, ); } await validateDockerCopySources( join(payload, "services/device-manager/Dockerfile"), join(payload, "services/device-manager"), ); for (const modulePath of isManagerOnly ? [] : [ "services/device-control-core/src/sensitive-reference-management.mjs", "services/device-control-core/src/device-gateway-core-runtime.mjs", "packages/device-edge-channel-contract/src/index.mjs", ]) { const coreImport = spawnSync( process.execPath, [ "--input-type=module", "--eval", `import(${JSON.stringify(pathToFileURL(join(payload, modulePath)).href)})`, ], { cwd: payload, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, }, ); if (coreImport.status !== 0) { throw new Error( `device_control_core_staged_module_import_failed:${modulePath}:${coreImport.stderr || coreImport.stdout}`, ); } } const compose = await readFile(join(payload, "docker-compose.device-manager.yml"), "utf8"); for (const required of [ "device-manager:", "DEVICE_MANAGEMENT_API_ENABLED: \"true\"", "NODEDC_LAUNCHER_INTERNAL_TOKEN_FILE: /run/nodedc-secrets/device-core-internal-token", "NODEDC_DEVICE_CORE_TOKEN_FILE: /run/nodedc-secrets/management-core-token", "name: nodedc-platform_edge", ...(isV4 ? [ "NODEDC_DEVICE_MANAGER_PRESENTATION_PATH: /var/lib/nodedc-device-manager/device-manager-presentation.json", "NODEDC_DEVICE_MANAGER_MEDIA_ROOT: /var/lib/nodedc-device-manager/media", "source: /volume1/docker/nodedc-device-plane/data/device-manager", "target: /var/lib/nodedc-device-manager", "read_only: false", "create_host_path: false", ] : []), ]) if (!compose.includes(required)) throw new Error(`device_manager_compose_contract_missing:${required}`); for (const forbidden of [ "NODEDC_INTERNAL_ACCESS_TOKEN:", "NODEDC_PLATFORM_SERVICE_TOKEN:", "PRIVATE KEY", "DEVICE_EDGE_CHANNEL_", "device-edge-channel/", "nodedc-device-plane-egress", "0.0.0.0:18122", "0.0.0.0:9921:9921", "- \"9921:9921\"", ]) { if (compose.includes(forbidden)) throw new Error(`device_manager_compose_boundary_violation:${forbidden}`); } const descriptor = JSON.parse(await readFile(join(payload, descriptorPath), "utf8")); const predecessor = descriptor.predecessor; const commonContractInvalid = ( descriptor.releaseId !== patchId || !["activate", "upgrade"].includes(descriptor.action) || !predecessor || !["reconciliation", "release"].includes(predecessor.kind) || !/^[A-Za-z0-9._-]{1,96}$/.test(predecessor.patchId || "") || !/^[a-f0-9]{64}$/.test(predecessor.artifactSha256 || "") || (descriptor.action === "activate") !== (predecessor.kind === "reconciliation") || descriptor.healthGate !== (isV4 ? "bounded-container-grace+core-contract+persistent-data" : "bounded-container-grace+core-contract") || descriptor.rollback !== (isV4 ? "restore-preapply-snapshot-preserve-manager-data" : "restore-preapply-snapshot") ); if (commonContractInvalid) throw new Error("device_manager_activation_successor_contract_mismatch"); if (descriptorPath.endsWith("release-v4.json")) { if ( descriptor.schemaVersion !== "nodedc.device-plane.device-manager-release.v4" || descriptor.commandTransport !== "typed-service-ping-v1" || descriptor.commandCatalog !== "allowlisted-adapter-typed-commands-only" || descriptor.credentialBoundary !== "transient-core-memory-then-single-pinned-mtls-command-envelope-to-edge-never-persisted-never-logged-never-returned" || descriptor.controlCorePredecessor?.patchId !== "device-control-core-release-v2-20260821-030" || descriptor.controlCorePredecessor?.artifactSha256 !== "8459521a662541a5a87cb0188991cdcfb51727427db8ec2a232ce4846bfc3454" || descriptor.edgeChannelPredecessor?.patchId !== "device-edge-core-channel-upgrade-v4-20260812-023" || descriptor.edgeChannelPredecessor?.artifactSha256 !== "c10d5b6b7d55ab239f85b6c8130e34ce9f84985e3b46e6e5534733156c7982fc" || descriptor.presentationPersistence !== "runner-managed-host-data-bind" || descriptor.presentationDataHostPath !== "/volume1/docker/nodedc-device-plane/data/device-manager" || descriptor.presentationDataContainerPath !== "/var/lib/nodedc-device-manager" || descriptor.presentationDataOwnership !== "uid-1000-gid-1000-mode-0750" || descriptor.presentationDataLifecycle !== "preserve-across-manager-recreate-and-source-rollback" || descriptor.presentationPath !== "/var/lib/nodedc-device-manager/device-manager-presentation.json" || descriptor.mediaRoot !== "/var/lib/nodedc-device-manager/media" || descriptor.defaultAccentHex !== "#f5f5f5" || descriptor.rollback !== "restore-preapply-snapshot-preserve-manager-data" || descriptor.gelios !== "untouched-legacy-only" ) throw new Error("device_manager_v4_persistence_contract_mismatch"); } else if (descriptorPath.endsWith("release-v3.json")) { if ( descriptor.schemaVersion !== "nodedc.device-plane.device-manager-release.v3" || descriptor.commandTransport !== "typed-service-ping-v1" || descriptor.commandCatalog !== "allowlisted-adapter-typed-commands-only" || descriptor.credentialBoundary !== "transient-core-memory-then-single-pinned-mtls-command-envelope-to-edge-never-persisted-never-logged-never-returned" || descriptor.controlCorePredecessor?.patchId !== "device-control-core-release-v2-20260821-030" || descriptor.controlCorePredecessor?.artifactSha256 !== "8459521a662541a5a87cb0188991cdcfb51727427db8ec2a232ce4846bfc3454" || descriptor.edgeChannelPredecessor?.patchId !== "device-edge-core-channel-upgrade-v4-20260812-023" || descriptor.edgeChannelPredecessor?.artifactSha256 !== "c10d5b6b7d55ab239f85b6c8130e34ce9f84985e3b46e6e5534733156c7982fc" || descriptor.edgeChannel !== "preserve-active-v4-core-initiated-pinned-mtls" || descriptor.edgeChannelEgress !== "preserve-dedicated-core-only-bridge-no-host-ingress-public-ipv4-tcp-443-only" || descriptor.gelios !== "untouched-legacy-only" ) throw new Error("device_manager_v3_typed_command_contract_mismatch"); } else if ( descriptor.schemaVersion !== "nodedc.device-plane.device-manager-release.v1" || descriptor.commandTransport !== "disabled" || descriptor.gelios !== "untouched" ) { throw new Error("device_manager_v1_contract_mismatch"); } await writeFile(join(stage, "manifest.env"), `id=${patchId}\ncomponent=device-plane\ntype=app-overlay\n`, "utf8"); await writeFile(join(stage, "files.txt"), `${entries.join("\n")}\n`, "utf8"); await mkdir(artifactDir, { recursive: true }); const tar = spawnSync("python3", ["-c", canonicalTarScript(), target, stage], { encoding: "utf8", maxBuffer: 128 * 1024 * 1024 }); if (tar.status !== 0) throw new Error(`tar_failed:${tar.stderr || tar.stdout}`); const sha256 = createHash("sha256").update(await readFile(target)).digest("hex"); console.log(JSON.stringify({ ok: true, patchId, component: "device-plane", artifact: target, sha256, entries, services: isManagerOnly ? ["device-manager"] : ["device-control-core", "device-manager"], preserved: ["device-postgres", "device-gateway", "device-backhaul-target", "Gelios"], }, null, 2)); } finally { await rm(stage, { recursive: true, force: true }); } async function copySafe(source, destination, sourceBoundary) { const sourceStat = await lstat(source); if (sourceStat.isSymbolicLink()) throw new Error(`source_symlink_rejected:${relative(sourceBoundary, source)}`); if (sourceStat.isFile()) { if (source.endsWith(".test.mjs") || source.endsWith(".map")) return; await mkdir(dirname(destination), { recursive: true }); await cp(source, destination, { force: true, verbatimSymlinks: true }); return; } if (!sourceStat.isDirectory()) throw new Error(`source_type_rejected:${source}`); await mkdir(destination, { recursive: true }); for (const entry of await readdir(source, { withFileTypes: true })) { if ([".DS_Store", ".git", "node_modules", "test"].includes(entry.name) || entry.name.startsWith(".env")) continue; await copySafe(join(source, entry.name), join(destination, entry.name), sourceBoundary); } } async function validateDockerCopySources(dockerfilePath, buildContext) { const dockerfile = await readFile(dockerfilePath, "utf8"); for (const [index, rawLine] of dockerfile.split("\n").entries()) { const line = rawLine.trim(); if (!/^COPY\s+/i.test(line)) continue; if (line.endsWith("\\") || /^COPY\s+\[/i.test(line)) { throw new Error(`unsupported_docker_copy_syntax:${dockerfilePath}:${index + 1}`); } const tokens = line.split(/\s+/).slice(1); while (tokens[0]?.startsWith("--")) tokens.shift(); if (tokens.length < 2) { throw new Error(`invalid_docker_copy:${dockerfilePath}:${index + 1}`); } for (const source of tokens.slice(0, -1)) { if (/[*?[\]{}]/.test(source)) { throw new Error(`docker_copy_glob_rejected:${dockerfilePath}:${index + 1}:${source}`); } const resolvedSource = resolve(buildContext, source); const relativeSource = relative(buildContext, resolvedSource); if (!relativeSource || relativeSource.startsWith("..") || resolve(buildContext, relativeSource) !== resolvedSource) { throw new Error(`docker_copy_source_outside_context:${dockerfilePath}:${index + 1}:${source}`); } try { await lstat(resolvedSource); } catch (error) { if (error?.code === "ENOENT") { throw new Error(`docker_copy_source_missing:${dockerfilePath}:${index + 1}:${source}`); } throw error; } } } } function canonicalTarScript() { return [ "import gzip,io,pathlib,sys,tarfile", "root=pathlib.Path(sys.argv[2])", "with open(sys.argv[1],'wb') as out:", " with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:", " with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:", " for top in ('manifest.env','files.txt','payload'):", " p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])", " for x in paths:", " info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix()); info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644", " with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)", ].join("\n"); }