Files
stack/scripts/pi-fit-image.mjs

73 lines
4.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
// #1873 pi-unpin S5: the image fit step. The agent image installs the pi package-lock.json resolves
// (npm ci), and nothing pins its version. This step runs the fit check (packages/harness/src/
// pi-fit.mjs, as sessions run it) on that pi before the image is built, so a lock that resolves an
// unfit pi fails the build. scripts/build.sh, scripts/release.sh package, scripts/verify.sh and the
// gate's image preparation run it first.
//
// It runs on the host, not as a Containerfile RUN step: the fit check runs pi inside bwrap, and
// docker build's default seccomp profile refuses the namespaces bwrap needs. The host copy stands for
// the image's: npm ci installs both from the same lock entry, and this step refuses unless the
// checkout's node_modules holds exactly that entry (version, resolved and integrity, as npm recorded
// them in node_modules/.package-lock.json, and the installed package.json's version).
// It runs pi on the host's node, which can be a different version from the image's (the
// Containerfile's FROM). Running it on the image's node is left for later.
//
// node scripts/pi-fit-image.mjs exit 0: passed; exit 1: one line on stderr, unfit or can't tell
import { readFileSync, realpathSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { lockedPi, PI_LOCK_KEY } from "./image-tag.mjs";
import { locate } from "../packages/plugins/harness/pi/binary.mjs";
import { levels } from "../packages/plugins/harness/pi/index.mjs";
import { piFit } from "../packages/harness/src/pi-fit.mjs";
// The name refusal lines use for this pi. Never a path.
export const LABEL = "the lock's pi";
const RUN_NPM_CI = "run npm ci --ignore-scripts in the repository first";
const readJson = (path) => { try { return JSON.parse(readFileSync(path, "utf8")); } catch { return null; } };
const real = (path) => { try { return realpathSync(path); } catch { return null; } };
// The lookup's answer for the pi npm installed from the lock in root, relabelled, or
// { bin: null, problem }. lookup: binary.mjs's locate (tests pass a stand-in).
export function lockedFound(root, { lookup = locate } = {}) {
let locked;
try { locked = lockedPi(root); } catch (e) { return { bin: null, problem: e.message }; }
const installed = readJson(join(root, "node_modules", ".package-lock.json"))?.packages?.[PI_LOCK_KEY];
if (!installed || installed.version !== locked.version || installed.resolved !== locked.resolved || installed.integrity !== locked.integrity) {
return { bin: null, problem: `the installed pi isn't the one package-lock.json resolves: ${RUN_NPM_CI}` };
}
const dir = join(root, PI_LOCK_KEY);
if (readJson(join(dir, "package.json"))?.version !== locked.version) {
return { bin: null, problem: `the installed pi's package.json doesn't carry the lock's version: ${RUN_NPM_CI}` };
}
const found = lookup({ MOSAIC_PI_BIN: join(root, "node_modules", ".bin", "pi") });
if (!found?.bin) return { bin: null, problem: `the lookup refused the lock's pi: ${RUN_NPM_CI}` };
if (found.pkg !== real(dir)) return { bin: null, problem: `node_modules/.bin/pi isn't the lock's pi package: ${RUN_NPM_CI}` };
return { ...found, label: LABEL, locked: locked.version };
}
// The fit check's answer for root's locked pi: { ok: true, version, ... } or { ok: false, message }.
// fit: pi-fit.mjs's piFit (tests may pass a stand-in).
export async function imageFit(root, { lookup = locate, fit = piFit } = {}) {
const found = lockedFound(root, { lookup });
if (!found.bin) return { ok: false, message: `pi (${LABEL}) can't be checked: ${found.problem}` };
const { locked, ...pi } = found;
const result = await fit(pi, { levels: levels() });
return result?.ok === true ? { ...result, locked } : { ok: false, message: typeof result?.message === "string" ? result.message : `pi (${LABEL}) can't be checked: the check gave no answer` };
}
const self = fileURLToPath(import.meta.url);
let main = false;
try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {}
if (main) {
const result = await imageFit(join(self, "..", ".."));
if (result.ok) {
process.stdout.write(`pi-fit-image: pi ${result.locked} (${LABEL}) passed the fit check\n`);
} else {
process.stderr.write(`pi-fit-image: ${result.message.split("\n")[0]}\n`);
process.exit(1);
}
}