Files
NODEDC_DEVICE_CORE/infra/deploy-runner/build-device-edge-core-channel-bootstrap-artifact.mjs

290 lines
14 KiB
JavaScript

#!/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-edge-core-channel-bootstrap-20260811-017", ...extra] = process.argv.slice(2);
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) {
throw new Error("usage: build-device-edge-core-channel-bootstrap-artifact.mjs [patch-id]");
}
const upgradeV4 = patchId.startsWith("device-edge-core-channel-upgrade-v4-");
const upgradeV2 = !upgradeV4 && patchId.startsWith("device-edge-core-channel-upgrade-v2-");
const upgradeV1 = !upgradeV4 && !upgradeV2 && patchId.startsWith("device-edge-core-channel-upgrade-");
const upgrade = upgradeV1 || upgradeV2 || upgradeV4;
const coreDockerfile = await readFile(
resolve(devicePlaneRoot, "services/device-control-core/Dockerfile"),
"utf8",
);
if (
coreDockerfile.includes(
"COPY packages/infrastructure-telemetry-contract ./packages/infrastructure-telemetry-contract",
)
) {
throw new Error("historical_device_edge_core_channel_builder_has_advanced");
}
const descriptorPath = upgradeV4
? "deployment/device-edge-core-channel-upgrade-v4.json"
: upgradeV2
? "deployment/device-edge-core-channel-upgrade-v2.json"
: upgradeV1
? "deployment/device-edge-core-channel-upgrade-v1.json"
: "deployment/device-edge-core-channel-bootstrap-v1.json";
const composePath = upgradeV4
? "docker-compose.device-plane.yml"
: "docker-compose.device-edge-core-channel.yml";
const entries = [
".dockerignore",
"package.json",
"package-lock.json",
composePath,
"packages/device-protocol-contract",
"packages/device-edge-channel-contract",
"services/device-control-core",
descriptorPath,
];
const stage = await mkdtemp(join(tmpdir(), "nodedc-device-edge-core-channel-"));
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.transitionId !== "__PATCH_ID__") {
throw new Error("device_edge_core_channel_template_id_mismatch");
}
descriptor.transitionId = patchId;
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/sensitive-reference-management.mjs",
"services/device-control-core/src/device-gateway-core-runtime.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_edge_core_channel_staged_module_import_failed:${modulePath}:${imported.stderr || imported.stdout}`);
}
}
const compose = await readFile(join(payload, composePath), "utf8");
const commonComposeRequired = [
"device-control-core:",
];
const composeRequired = upgradeV4
? [
...commonComposeRequired,
" - device-plane-private",
" - device-plane-control",
]
: [
...commonComposeRequired,
"DEVICE_EDGE_CHANNEL_ENABLED: \"true\"",
"DEVICE_EDGE_CHANNEL_CORE_KEY_FILE: /run/nodedc-secrets/device-edge-channel/core-private-key.pem",
"DEVICE_EDGE_CHANNEL_CORE_CERTIFICATE_FILE: /run/nodedc-secrets/device-edge-channel/core-certificate.pem",
"DEVICE_EDGE_CHANNEL_TRUST_ROOT: /run/nodedc-secrets/device-edge-channel/peers",
"source: /volume1/docker/nodedc-device-plane/secrets/device-edge-channel/core-private-key.pem",
"source: /volume1/docker/nodedc-device-plane/secrets/device-edge-channel/core-certificate.pem",
"source: /volume1/docker/nodedc-device-plane/secrets/device-edge-channel/peers",
"name: nodedc-device-plane-egress",
];
for (const required of composeRequired) {
if (!compose.includes(required)) {
throw new Error(`device_edge_core_channel_compose_contract_missing:${required}`);
}
}
if (upgradeV4) {
const coreBlock = compose.split("\n device-control-core:", 2)[1]?.split("\n device-gateway:", 1)[0] || "";
const gatewayBlock = compose.split("\n device-gateway:", 2)[1]?.split("\nnetworks:", 1)[0] || "";
if (
!coreBlock.includes(" - device-plane-private")
|| coreBlock.includes(" - device-plane-control")
|| !gatewayBlock.includes(" - device-plane-private")
|| !gatewayBlock.includes(" - device-plane-control")
) {
throw new Error("device_edge_core_channel_v4_network_boundary_mismatch");
}
}
const composeForbidden = upgradeV4
? ["gw_priority:", "network_mode:", "privileged:"]
: [
"device-manager:",
"device-gateway:",
"device-postgres:",
"PRIVATE KEY",
"ports:",
"network_mode:",
"privileged:",
];
for (const forbidden of composeForbidden) {
if (compose.includes(forbidden)) {
throw new Error(`device_edge_core_channel_compose_boundary_violation:${forbidden}`);
}
}
const descriptor = JSON.parse(await readFile(join(payload, descriptorPath), "utf8"));
const descriptorContractMatches = upgradeV4
? (
descriptor.schemaVersion === "nodedc.device-plane.device-edge-core-channel-upgrade.v4"
&& descriptor.action === "upgrade"
&& descriptor.composeActivation === "replace-core-network-membership-with-private-plus-egress"
&& descriptor.identityRecovery === "forbidden-valid-existing-identity-required"
&& descriptor.endpointPolicy === "public-ipv4-standard-https-tcp-443-only"
&& descriptor.upgradePredecessor?.patchId === "device-edge-core-channel-upgrade-v2-20260812-021"
&& descriptor.upgradePredecessor?.artifactSha256 === "e40a6fd24edfecac09e42cd82635a77850541bcf047788db3e9c55d2b9e58867"
&& descriptor.failedAttempt?.patchId === "device-edge-core-channel-upgrade-v3-20260812-022"
&& descriptor.failedAttempt?.artifactSha256 === "9e2b409a4b2d19711db434e90d03ac8e3db77bd74949f83cace7949f33caf613"
&& descriptor.failedAttempt?.backupId === "device-plane-device-edge-core-channel-upgrade-v3-20260812-022-20260812-123620"
&& JSON.stringify(descriptor.coreNetworks) === JSON.stringify(["device-plane-private", "device-plane-egress"])
&& descriptor.removedCoreNetwork === "device-plane-control"
&& descriptor.composeCompatibility === "synology-compose-v2.20-no-gw-priority"
&& descriptor.edgeRegistrations === "preserved"
&& descriptor.rollback === "restore-upgrade-v2-021-source-and-preapply-core-runtime"
)
: upgradeV2
? (
descriptor.schemaVersion === "nodedc.device-plane.device-edge-core-channel-upgrade.v2"
&& descriptor.action === "upgrade"
&& descriptor.composeActivation === "preserve-dedicated-additive-override"
&& descriptor.identityRecovery === "forbidden-valid-existing-identity-required"
&& descriptor.endpointPolicy === "public-ipv4-standard-https-tcp-443-only"
&& descriptor.upgradePredecessor?.patchId === "device-edge-core-channel-upgrade-20260812-019"
&& descriptor.upgradePredecessor?.artifactSha256 === "8e9a220275959f378c1c4b00be5c7192e79afe2134eaab808a64e515870a8438"
&& descriptor.edgeRegistrations === "preserved"
&& descriptor.rollback === "restore-upgrade-019-source-and-preapply-core-runtime"
)
: upgradeV1
? (
descriptor.schemaVersion === "nodedc.device-plane.device-edge-core-channel-upgrade.v1"
&& descriptor.action === "upgrade"
&& descriptor.composeActivation === "preserve-dedicated-additive-override"
&& descriptor.identityRecovery === "forbidden-valid-existing-identity-required"
&& descriptor.endpointPolicy === "public-ipv4-standard-https-tcp-443-only"
&& descriptor.bootstrapPredecessor?.patchId === "device-edge-core-channel-bootstrap-20260812-018"
&& descriptor.bootstrapPredecessor?.artifactSha256 === "5598b7388b491fe524ab46038ce476482a93a6cf07d8ca5e00206c69ded02931"
)
: (
descriptor.schemaVersion === "nodedc.device-plane.device-edge-core-channel-bootstrap.v1"
&& descriptor.action === "activate"
&& descriptor.composeActivation === "dedicated-additive-override"
&& descriptor.identityRecovery === "exact-invalid-unexported-failed-predecessor-only"
);
if (
!descriptorContractMatches
|| descriptor.transitionId !== patchId
|| descriptor.service !== "device-control-core"
|| descriptor.tlsPurpose !== "clientAuth"
|| descriptor.direction !== "core-initiated"
|| descriptor.publicIngress !== "none-on-synology"
|| descriptor.commandTransport !== "disabled"
|| descriptor.gelios !== "untouched"
) {
throw new Error("device_edge_core_channel_bootstrap_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", "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");
}