#!/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 artifactDir = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(scriptDir, "../deploy-artifacts")); const [ patchId = "device-control-core-release-v2-20260812-025", predecessorPatchId, predecessorSha256, ...extra ] = process.argv.slice(2); if ( extra.length || !/^device-control-core-release-[A-Za-z0-9._-]{1,67}$/.test(patchId) || ((predecessorPatchId === undefined) !== (predecessorSha256 === undefined)) || ( predecessorPatchId !== undefined && ( !/^device-control-core-release-[A-Za-z0-9._-]{1,67}$/.test(predecessorPatchId) || predecessorPatchId === patchId || !/^[0-9a-f]{64}$/.test(predecessorSha256) ) ) ) { throw new Error( "usage: build-device-control-core-release-artifact.mjs " + "[device-control-core-release-id] [predecessor-release-id predecessor-sha256]", ); } const isV2 = patchId.startsWith("device-control-core-release-v2-"); const expectedV2Predecessor = Object.freeze({ patchId: predecessorPatchId ?? "device-control-core-release-20260812-024", artifactSha256: predecessorSha256 ?? "a289e909283109642e6bba3d9822a31f63423cfe0bbcd52705979681bd2bc793", }); const descriptorPath = isV2 ? "deployment/device-control-core-release-v2.json" : "deployment/device-control-core-release-v1.json"; const entries = [ ".dockerignore", "package.json", "package-lock.json", "packages/device-protocol-contract", "packages/device-edge-channel-contract", "services/device-control-core", descriptorPath, ]; const stage = await mkdtemp(join(tmpdir(), "nodedc-device-control-core-release-")); const payload = join(stage, "payload"); const target = join(artifactDir, `nodedc-device-plane-${patchId}.tgz`); try { 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_control_core_release_template_id_mismatch"); } descriptor.releaseId = patchId; if (predecessorPatchId !== undefined) { descriptor.predecessor = { kind: "release", patchId: predecessorPatchId, artifactSha256: predecessorSha256, }; } const destination = join(payload, entry); await mkdir(dirname(destination), { recursive: true }); await writeFile(destination, `${JSON.stringify(descriptor, null, 2)}\n`, "utf8"); continue; } await copySafe(resolve(devicePlaneRoot, entry), join(payload, entry), devicePlaneRoot); } await validateDockerCopySources( join(payload, "services/device-control-core/Dockerfile"), payload, ); for (const modulePath of [ "services/device-control-core/src/device-gateway-core-runtime.mjs", "packages/device-protocol-contract/src/index.mjs", "packages/device-edge-channel-contract/src/index.mjs", ]) { const imported = spawnSync( process.execPath, ["--input-type=module", "--eval", `import(${JSON.stringify(pathToFileURL(join(payload, modulePath)).href)})`], { cwd: payload, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }, ); if (imported.status !== 0) { throw new Error(`device_control_core_release_staged_module_import_failed:${modulePath}:${imported.stderr || imported.stdout}`); } } const descriptor = JSON.parse(await readFile(join(payload, descriptorPath), "utf8")); if ( descriptor.schemaVersion !== `nodedc.device-plane.device-control-core-release.${isV2 ? "v2" : "v1"}` || descriptor.releaseId !== patchId || descriptor.action !== "upgrade" || descriptor.service !== "device-control-core" || descriptor.composeActivation !== "preserve-active-v4-topology" || descriptor.identityRecovery !== "forbidden-valid-existing-identity-required" || descriptor.tlsPurpose !== "clientAuth" || descriptor.direction !== "core-initiated" || descriptor.endpointPolicy !== "public-ipv4-standard-https-tcp-443-only" || JSON.stringify(descriptor.coreNetworks) !== JSON.stringify(["device-plane-private", "device-plane-egress"]) || descriptor.publicIngress !== "none-on-synology" || descriptor.edgeRegistrations !== "preserved" || descriptor.commandTransport !== (isV2 ? "typed-service-ping-v1" : "disabled") || descriptor.gelios !== (isV2 ? "untouched-legacy-only" : "untouched") || descriptor.rollback !== "restore-preapply-source-and-core-runtime" || ( isV2 && ( 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.predecessor?.patchId !== expectedV2Predecessor.patchId || descriptor.predecessor?.artifactSha256 !== expectedV2Predecessor.artifactSha256 ) ) ) { throw new Error("device_control_core_release_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: ["device-control-core"], preserved: ["device-manager", "device-gateway", "device-postgres", "device-backhaul-target", "edge registrations", "mTLS identity", "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"); }