Files
NODEDC_PLATFORM/infra/deploy-runner/build-platform-device-core-hub-trust-artifact.mjs
T

73 lines
3.7 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 platformRoot = resolve(scriptDir, "../..");
const artifactDir = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(scriptDir, "../deploy-artifacts"));
const [patchId = "platform-device-core-hub-trust-20260810-001", ...extra] = process.argv.slice(2);
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) throw new Error("usage: build-platform-device-core-hub-trust-artifact.mjs [patch-id]");
const entries = [
"platform/docker-compose.platform-http.yml",
"platform/deployment/device-core-hub-trust-v1.json",
];
const sources = new Map([
[entries[0], resolve(platformRoot, "infra/synology/docker-compose.platform-http.yml")],
[entries[1], resolve(platformRoot, "infra/deployment/device-core-hub-trust-v1.json")],
]);
await buildArtifact({ patchId, component: "platform", entries, sources });
async function buildArtifact({ patchId, component, entries, sources }) {
const stage = await mkdtemp(join(tmpdir(), "nodedc-platform-device-core-trust-"));
const payload = join(stage, "payload");
const target = join(artifactDir, `nodedc-${component}-${patchId}.tgz`);
try {
await mkdir(payload, { recursive: true });
for (const entry of entries) {
const destination = join(payload, entry);
await mkdir(dirname(destination), { recursive: true });
await cp(sources.get(entry), destination, { force: true });
}
const compose = await readFile(join(payload, entries[0]), "utf8");
for (const required of [
"NODEDC_DEVICE_CORE_INTERNAL_TOKEN_FILE: /run/nodedc-secrets/device-core-internal-token",
"source: /volume1/docker/nodedc-platform/secrets/device-core-internal-token",
"create_host_path: false",
]) if (!compose.includes(required)) throw new Error(`hub_trust_compose_contract_missing:${required}`);
await writeFile(join(stage, "manifest.env"), `id=${patchId}\ncomponent=${component}\ntype=app-overlay\n`, "utf8");
await writeFile(join(stage, "files.txt"), `${entries.join("\n")}\n`, "utf8");
await mkdir(artifactDir, { recursive: true });
canonicalTar(target, stage);
const sha256 = createHash("sha256").update(await readFile(target)).digest("hex");
console.log(JSON.stringify({ ok: true, patchId, component, artifact: target, sha256, entries, services: ["launcher"] }, null, 2));
} finally {
await rm(stage, { recursive: true, force: true });
}
}
function canonicalTar(target, stage) {
const result = spawnSync("python3", ["-c", canonicalTarScript(), target, stage], { encoding: "utf8" });
if (result.status !== 0) throw new Error(`tar_failed:${result.stderr || result.stdout}`);
}
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");
}