176 lines
5.2 KiB
JavaScript
176 lines
5.2 KiB
JavaScript
#!/usr/bin/env node
|
|
import { createHash } from "node:crypto";
|
|
import { spawnSync } from "node:child_process";
|
|
import {
|
|
cp,
|
|
lstat,
|
|
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 sourceRoot = platformRoot;
|
|
const descriptorRelative =
|
|
"deployment/device-plane-b2-discovery-loopback-recovery-v1.json";
|
|
const artifactDir = resolve(
|
|
process.env.NODEDC_DEPLOY_ARTIFACT_DIR
|
|
|| resolve(scriptDir, "../deploy-artifacts"),
|
|
);
|
|
const [
|
|
patchId = "device-plane-b2-discovery-loopback-recovery-20260802-004",
|
|
...extra
|
|
] = process.argv.slice(2);
|
|
|
|
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) {
|
|
throw new Error(
|
|
"usage: "
|
|
+ "build-device-plane-b2-discovery-loopback-recovery-artifact.mjs "
|
|
+ "[patch-id]",
|
|
);
|
|
}
|
|
|
|
const files = [descriptorRelative];
|
|
const stage = await mkdtemp(
|
|
join(tmpdir(), "nodedc-device-plane-b2-loopback-recovery-"),
|
|
);
|
|
const payload = join(stage, "payload");
|
|
const target = join(
|
|
artifactDir,
|
|
`nodedc-device-plane-${patchId}.tgz`,
|
|
);
|
|
|
|
await assertRecoveryDescriptor();
|
|
|
|
try {
|
|
const source = resolve(sourceRoot, descriptorRelative);
|
|
const sourceStat = await lstat(source);
|
|
if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
|
|
throw new Error("device_plane_b2_recovery_descriptor_unsafe");
|
|
}
|
|
await mkdir(dirname(join(payload, descriptorRelative)), {
|
|
recursive: true,
|
|
});
|
|
await cp(source, join(payload, descriptorRelative), {
|
|
force: true,
|
|
verbatimSymlinks: true,
|
|
});
|
|
await writeFile(
|
|
join(stage, "manifest.env"),
|
|
`id=${patchId}\ncomponent=device-plane\ntype=app-overlay\n`,
|
|
"utf8",
|
|
);
|
|
await writeFile(
|
|
join(stage, "files.txt"),
|
|
`${files.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 digest = createHash("sha256")
|
|
.update(await readFile(target))
|
|
.digest("hex");
|
|
console.log(JSON.stringify({
|
|
ok: true,
|
|
patchId,
|
|
artifact: target,
|
|
sha256: digest,
|
|
component: "device-plane",
|
|
transition: "failed-b2-loopback-build-reconciliation",
|
|
entries: files,
|
|
build: [],
|
|
services: [],
|
|
preservedRuntime: [
|
|
"device-control-core",
|
|
"device-gateway",
|
|
"device-postgres",
|
|
"nodedc-device-plane-postgres-data",
|
|
"Gelios",
|
|
],
|
|
sourceAction: "publish-reconciliation-marker-only",
|
|
runtimeAction: "read-only-acceptance",
|
|
ingress: "disabled:127.0.0.1:9921/tcp:closed",
|
|
rollback: "marker-only-runtime-unchanged",
|
|
excluded: [
|
|
"application-source",
|
|
"compose",
|
|
"Dockerfile",
|
|
"secrets",
|
|
"runtime",
|
|
"database",
|
|
"Gelios",
|
|
],
|
|
}, null, 2));
|
|
} finally {
|
|
await rm(stage, { recursive: true, force: true });
|
|
}
|
|
|
|
async function assertRecoveryDescriptor() {
|
|
const descriptor = JSON.parse(await readFile(
|
|
resolve(sourceRoot, descriptorRelative),
|
|
"utf8",
|
|
));
|
|
const expected = {
|
|
schemaVersion: "nodedc.device-plane.b2-discovery-loopback-recovery.v1",
|
|
mode: "failed-b2-loopback-build-reconciliation",
|
|
failedPatchId: "device-plane-b2-discovery-loopback-20260801-003",
|
|
failedArtifactSha256:
|
|
"7273c5bf67fe6bc1f1da66ad726009240d39ee3aee58201b96c23d6f707a3d84",
|
|
failedBackupId:
|
|
"device-plane-device-plane-b2-discovery-loopback-20260801-003-20260802-154311",
|
|
sourceAction: "publish-reconciliation-marker-only",
|
|
runtimeAction: "read-only-acceptance",
|
|
preservedServices: [
|
|
"device-control-core",
|
|
"device-gateway",
|
|
"device-postgres",
|
|
],
|
|
expectedLoopbackPorts: [
|
|
"127.0.0.1:18120:18120",
|
|
"127.0.0.1:18121:18121",
|
|
],
|
|
closedPort: "127.0.0.1:9921/tcp",
|
|
databaseVolume: "nodedc-device-plane-postgres-data",
|
|
commandTransport: "disabled",
|
|
gelios: "untouched",
|
|
rollback: "marker-only-runtime-unchanged",
|
|
};
|
|
if (JSON.stringify(descriptor) !== JSON.stringify(expected)) {
|
|
throw new Error("device_plane_b2_recovery_descriptor_mismatch");
|
|
}
|
|
}
|
|
|
|
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");
|
|
}
|