Files
NODEDC_DEVICE_CORE/infra/deploy-runner/build-device-control-core-release-v3-reconciliation-artifact.mjs

115 lines
4.6 KiB
JavaScript

#!/usr/bin/env node
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const sourceRoot = resolve(scriptDir, "../..");
const artifactDir = resolve(
process.env.NODEDC_DEPLOY_ARTIFACT_DIR
|| resolve(scriptDir, "../deploy-artifacts"),
);
const [
patchId = "device-control-core-release-v3-reconciliation-20260822-042",
...extra
] = process.argv.slice(2);
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) {
throw new Error(
"usage: build-device-control-core-release-v3-reconciliation-artifact.mjs [patch-id]",
);
}
const entry = "deployment/device-control-core-release-v3-reconciliation-v1.json";
const stage = await mkdtemp(join(tmpdir(), "nodedc-control-core-v3-reconciliation-"));
const payload = join(stage, "payload");
const target = resolve(artifactDir, `nodedc-device-plane-${patchId}.tgz`);
try {
const descriptor = JSON.parse(await readFile(resolve(sourceRoot, entry), "utf8"));
const expected = {
schemaVersion: "nodedc.device-plane.device-control-core-release-v3-reconciliation.v1",
mode: "failed-release-v3-exact-preapply-image-restore",
failedPatchId: "device-control-core-release-v3-20260822-040",
failedArtifactSha256:
"08448a56cdf391076f92c5242e368fd0033b420167874645eebfa1f22184ee92",
failedArtifact:
"nodedc-device-plane-device-control-core-release-v3-20260822-040.tgz.20260822-184245",
backupId:
"device-plane-device-control-core-release-v3-20260822-040-20260822-184245",
predecessorPatchId: "device-control-core-release-v2-20260822-038",
predecessorArtifactSha256:
"e2d062b82b022dba662522b5d6e192026ac964d78950d903295ca3cbbc95ab28",
preapplyImageId:
"sha256:31d35733ee46225b487c0f02a7b52d4ba2d13f5b99f6a717b7f5e6f5460b412a",
sourceAction: "accept-byte-exact-restored-preapply-source",
runtimeAction: "retag-exact-preapply-image+recreate-device-control-core-only",
preservedServices: [
"device-manager",
"device-gateway",
"device-postgres",
"device-backhaul-target",
],
databaseVolume: "nodedc-device-plane-postgres-data",
publicIngress: "disabled",
edgeChannel: "core-initiated-pinned-mtls-registered-edges-only",
commandTransport: "typed-service-ping-v1",
gelios: "untouched-legacy-only",
rollback: "marker+exact-preapply-image-runtime",
};
if (JSON.stringify(descriptor) !== JSON.stringify(expected)) {
throw new Error("device_control_core_v3_reconciliation_descriptor_mismatch");
}
await mkdir(dirname(join(payload, entry)), { recursive: true });
await cp(resolve(sourceRoot, entry), join(payload, entry), { force: true });
await writeFile(
join(stage, "manifest.env"),
`id=${patchId}\ncomponent=device-plane\ntype=app-overlay\n`,
"utf8",
);
await writeFile(join(stage, "files.txt"), `${entry}\n`, "utf8");
await mkdir(artifactDir, { recursive: true });
const tar = spawnSync(
"python3",
["-c", canonicalTarScript(), target, stage],
{ encoding: "utf8", maxBuffer: 16 * 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: [entry],
build: [],
services: ["device-control-core"],
transition: descriptor.mode,
runtimeAction: descriptor.runtimeAction,
preservedServices: descriptor.preservedServices,
}, null, 2));
} finally {
await rm(stage, { recursive: true, force: true });
}
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");
}