Files
muSRFlow/scripts/ci-runtime-registry.js
T

155 lines
9.5 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { Transform } = require('stream');
const { pipeline } = require('stream/promises');
const { publishRegistryFiles } = require('./ci-publish-packages');
function validateDescriptor(descriptor) {
const { runtime, artifact } = descriptor;
if (descriptor.schemaVersion !== 1 || descriptor.kind !== 'musrflow-runtime-download'
|| runtime?.platform !== 'linux' || runtime?.arch !== 'x64'
|| typeof runtime.runtimeId !== 'string' || !runtime.runtimeId
|| !/^[0-9a-f]{40}$/.test(runtime.build?.sourceRevision || '') || runtime.build?.sourceDirty !== false
|| typeof runtime.root?.version !== 'string'
|| artifact?.format !== 'tar.gz' || artifact.rootDirectory !== 'runtime'
|| !/^[A-Za-z0-9][A-Za-z0-9._-]*\.tar\.gz$/.test(artifact.filename || '')
|| !/^[0-9a-f]{64}$/.test(artifact.sha256 || '')
|| !Number.isSafeInteger(artifact.size) || artifact.size <= 0 || artifact.size > 8 * 1024 ** 3) {
throw new Error('Expected a clean, source-pinned Linux x64 runtime bundle');
}
}
function selectRuntime(configFile, env = process.env) {
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
if (config.schemaVersion !== 1 || !Object.hasOwn(config.runtimes || {}, 'linux-x64')) throw new Error('Invalid runtime pin configuration');
const pin = config.runtimes['linux-x64'];
// An explicit manual override wins; the old repository variable is used
// only until the first reviewed registry pin has been committed.
const override = env.RUNTIME_RUN_OVERRIDE || (!pin && env.LEGACY_RUNTIME_RUN_ID);
if (override) {
if (!/^[1-9][0-9]*$/.test(override)) throw new Error('Runtime workflow run ID must be numeric');
console.log('Using Actions runtime artifact ' + override + '; its retention limit still applies');
return { source: 'artifact', runId: override };
}
if (!pin) throw new Error('No approved runtime pin. Run Publish runtime and commit its runtime-source.json, or supply a manual runtime_run_id');
if (!/^[0-9a-f]{64}$/.test(pin.descriptorSha256 || '') || !/^[0-9a-f]{64}$/.test(pin.archiveSha256 || '')) {
throw new Error('Invalid runtime pin checksums');
}
console.log('Using pinned runtime: ' + pin.runtimeId);
return { source: 'registry', runId: '', pin };
}
async function publishRuntime(directory, env = process.env, request = fetch) {
const descriptors = [];
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const file = path.join(directory, entry.name);
if (entry.isFile() && entry.name === 'runtime-download.json') descriptors.push(file);
if (entry.isDirectory() && fs.existsSync(path.join(file, 'runtime-download.json'))) descriptors.push(path.join(file, 'runtime-download.json'));
}
if (descriptors.length !== 1) throw new Error('Expected exactly one runtime bundle');
const descriptorFile = descriptors[0];
const bytes = fs.readFileSync(descriptorFile);
if (bytes.length > 1024 ** 2) throw new Error('Runtime descriptor is too large');
const descriptor = JSON.parse(bytes);
validateDescriptor(descriptor);
const { runtime, artifact } = descriptor;
const bundle = path.dirname(descriptorFile);
const archive = path.join(bundle, artifact.filename);
if (!fs.lstatSync(archive).isFile() || fs.statSync(archive).size !== artifact.size) throw new Error('Runtime archive size mismatch');
const hash = crypto.createHash('sha256');
for await (const chunk of fs.createReadStream(archive)) hash.update(chunk);
if (hash.digest('hex') !== artifact.sha256) throw new Error('Runtime archive checksum mismatch');
if (fs.readFileSync(archive + '.sha256', 'utf8').trim() !== `${artifact.sha256} ${artifact.filename}`) throw new Error('Runtime checksum sidecar mismatch');
// The content hash includes the manifest, source revision, build options,
// base image, architecture, and tool files, not the GUI release version.
const version = `linux-x64-${artifact.sha256}`;
await publishRegistryFiles(bundle, { 'musrflow-runtime-bundle': [artifact.filename, artifact.filename + '.sha256', 'runtime-download.json'] }, version, env, request);
const owner = env.GITEA_REPOSITORY.split('/')[0];
return { schemaVersion: 1, runtimes: { 'linux-x64': {
descriptorUrl: `${env.GITEA_SERVER_URL.replace(/\/$/, '')}/api/packages/${owner}/generic/musrflow-runtime-bundle/${version}/runtime-download.json`,
descriptorSha256: crypto.createHash('sha256').update(bytes).digest('hex'),
archiveSha256: artifact.sha256, runtimeId: runtime.runtimeId,
sourceRevision: runtime.build.sourceRevision, rootVersion: runtime.root.version
} } };
}
async function downloadRuntime(configFile, destination, env = process.env, request = fetch) {
const selected = selectRuntime(configFile, { ...env, RUNTIME_RUN_OVERRIDE: '', LEGACY_RUNTIME_RUN_ID: '' });
const pin = selected.pin;
const url = new URL(pin.descriptorUrl);
const server = new URL(env.GITEA_SERVER_URL);
const owner = String(env.GITEA_REPOSITORY || '').split('/')[0];
const expectedPath = `${server.pathname.replace(/\/$/, '')}/api/packages/${owner}/generic/musrflow-runtime-bundle/linux-x64-${pin.archiveSha256}/runtime-download.json`;
if (url.protocol !== 'https:' || url.origin !== server.origin || url.pathname !== expectedPath
|| url.username || url.password || url.search || url.hash) throw new Error('Pinned runtime must use this Gitea owner and its content-addressed registry path');
if (!env.RUNTIME_TOKEN) throw new Error('A package read token is required');
const output = path.resolve(destination);
if (fs.existsSync(output)) throw new Error('Runtime download destination already exists');
fs.mkdirSync(path.dirname(output), { recursive: true });
const stage = fs.mkdtempSync(path.join(path.dirname(output), '.runtime-download-'));
let descriptor;
try {
for (const kind of ['descriptor', 'archive']) {
const filename = kind === 'descriptor' ? 'runtime-download.json' : descriptor.artifact.filename;
const expectedHash = kind === 'descriptor' ? pin.descriptorSha256 : pin.archiveSha256;
const limit = kind === 'descriptor' ? 1024 ** 2 : descriptor.artifact.size;
const target = kind === 'descriptor' ? url : new URL(filename, url);
const response = await request(target.href, {
headers: { Authorization: `token ${env.RUNTIME_TOKEN}` }, redirect: 'error',
signal: AbortSignal.timeout(30 * 60 * 1000)
});
if (response.status !== 200) throw new Error(`Download runtime ${kind}: HTTP ${response.status}`);
let size = 0;
const hash = crypto.createHash('sha256');
const verifier = new Transform({ transform(chunk, encoding, callback) {
size += chunk.length;
if (size > limit) return callback(new Error('Runtime download exceeds expected size'));
hash.update(chunk);
callback(null, chunk);
} });
await pipeline(response.body, verifier, fs.createWriteStream(path.join(stage, filename), { flags: 'wx' }));
if (hash.digest('hex') !== expectedHash || (kind === 'archive' && size !== limit)) throw new Error(`Runtime ${kind} checksum or size mismatch`);
if (kind === 'descriptor') {
descriptor = JSON.parse(fs.readFileSync(path.join(stage, filename), 'utf8'));
validateDescriptor(descriptor);
const runtime = descriptor.runtime;
if (descriptor.artifact.sha256 !== pin.archiveSha256 || runtime.runtimeId !== pin.runtimeId
|| runtime.build.sourceRevision !== pin.sourceRevision || runtime.root.version !== pin.rootVersion) {
throw new Error('Runtime identity does not match the committed pin');
}
}
}
fs.writeFileSync(path.join(stage, descriptor.artifact.filename + '.sha256'), `${pin.archiveSha256} ${descriptor.artifact.filename}\n`);
fs.renameSync(stage, output);
return output;
} finally {
fs.rmSync(stage, { recursive: true, force: true });
}
}
module.exports = { selectRuntime, publishRuntime, downloadRuntime };
if (require.main === module) {
(async () => {
const [command, source, destination] = process.argv.slice(2);
if (command === 'select') {
if (!process.env.GITHUB_ENV) throw new Error('GITHUB_ENV is required');
const selected = selectRuntime(source);
fs.appendFileSync(process.env.GITHUB_ENV, `RUNTIME_SOURCE=${selected.source}\nRUNTIME_RUN_ID=${selected.runId}\n`);
} else if (command === 'publish') {
const pin = await publishRuntime(source);
fs.writeFileSync(destination || 'runtime-source.json', JSON.stringify(pin, null, 2) + '\n');
console.log('Review and commit the generated runtime-source.json as packaging/runtime/runtime-source.json');
} else if (command === 'download') {
if (!destination) throw new Error('A download destination is required');
await downloadRuntime(source, destination);
} else {
throw new Error('Usage: ci-runtime-registry.js select <pin> | publish <bundle> <candidate-pin> | download <pin> <directory>');
}
})().catch(error => { console.error(error.message); process.exitCode = 1; });
}