- Profiles pin each environment: POC (branch poc) and Fanyu (branch dev, 180.76.138.88, droboticflow-fanyu), with their Deployments by UID - Releases expect every Deployment of their profile; Fanyu's api image also runs its annotation processor and validator, each rolled back to its own image - Bindings may list several projects; each Runner has its own settings - fanyu-access: least-privilege identity and build-Pod admission policy in Fanyu's cluster, its token only in the executor and Fanyu's Runner - projects --replace: delete every project with all it holds, then create the POC and Fanyu projects, their environments and Runners - Ship the Runner's public keys in the ConfigMap, not a Mac path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
280 lines
12 KiB
JavaScript
280 lines
12 KiB
JavaScript
#!/usr/bin/env node
|
||
// Entry of WishForge's containers in the POC cluster (infra/images/cluster-poc.Dockerfile,
|
||
// docs/cluster-poc-deployment.md). Each role prepares the private files it needs from its own
|
||
// mounted Secret, waits for the services it depends on, and then runs in the foreground; a
|
||
// service that exits ends the container, which Kubernetes restarts. Importing it starts nothing.
|
||
import { spawn } from 'node:child_process';
|
||
import { randomUUID } from 'node:crypto';
|
||
import { chmod, copyFile, mkdir, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
|
||
import net from 'node:net';
|
||
import path from 'node:path';
|
||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||
import { setTimeout as delay } from 'node:timers/promises';
|
||
import { backupLoop } from './cluster-poc-backup.mjs';
|
||
|
||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||
const ROLES = {
|
||
platform: { env: '/etc/wishforge/env/platform.env', services: 'git,api,coordinator,deployer,web' },
|
||
executor: { env: '/etc/wishforge/env/executor.env', services: 'provisioner' },
|
||
runner: { env: '/etc/wishforge/env/runner.env' },
|
||
// Daily snapshots of the data (cluster-poc-backup.mjs); it reads no settings file.
|
||
backup: {},
|
||
};
|
||
|
||
/** A KEY=value file's settings, without changing this process's environment. */
|
||
export function parseEnvFile(text) {
|
||
const values = {};
|
||
for (const line of text.split('\n')) {
|
||
const match = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)$/.exec(line);
|
||
if (!match) continue;
|
||
let value = match[2].trim();
|
||
if (/^(['"]).*\1$/.test(value)) value = value.slice(1, -1);
|
||
values[match[1]] = value;
|
||
}
|
||
return values;
|
||
}
|
||
|
||
/** Host and port of the address a setting names: a URL, a host:port pair or a database URL. */
|
||
export function endpoint(address) {
|
||
const url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(address) ? address : `tcp://${address}`);
|
||
const port = Number(url.port || { 'http:': 80, 'https:': 443, 'postgresql:': 5432 }[url.protocol]);
|
||
if (!url.hostname || !Number.isInteger(port)) throw new Error(`Not a service address: ${address}`);
|
||
return { host: url.hostname.replace(/^\[|\]$/g, ''), port };
|
||
}
|
||
|
||
async function reachable({ host, port }) {
|
||
return new Promise(resolve => {
|
||
const socket = net.connect({ host, port, timeout: 2000 });
|
||
socket.once('connect', () => (socket.destroy(), resolve(true)));
|
||
socket.once('timeout', () => (socket.destroy(), resolve(false)));
|
||
socket.once('error', () => resolve(false));
|
||
});
|
||
}
|
||
async function waitFor(name, address, minutes = 10) {
|
||
const target = endpoint(address);
|
||
for (const until = Date.now() + minutes * 60_000; Date.now() < until; await delay(2000))
|
||
if (await reachable(target)) return;
|
||
throw new Error(`${name} at ${target.host}:${target.port} did not become reachable`);
|
||
}
|
||
async function waitForHttp(name, url, minutes = 10) {
|
||
for (const until = Date.now() + minutes * 60_000; Date.now() < until; await delay(2000)) {
|
||
const ok = await fetch(url, { signal: AbortSignal.timeout(3000) }).then(
|
||
response => response.ok,
|
||
() => false,
|
||
);
|
||
if (ok) return;
|
||
}
|
||
throw new Error(`${name} at ${url} did not become ready`);
|
||
}
|
||
|
||
/**
|
||
* A private copy, owned by this user and readable only by it, of a file Kubernetes mounted. It
|
||
* replaces the target in one step, so containers copying the same file at once never see half.
|
||
*/
|
||
export async function privateCopy(source, target) {
|
||
await mkdir(path.dirname(target), { recursive: true, mode: 0o700 });
|
||
const temporary = `${target}.${randomUUID()}.tmp`;
|
||
try {
|
||
await copyFile(source, temporary);
|
||
await chmod(temporary, 0o600);
|
||
await rename(temporary, target);
|
||
} finally {
|
||
await rm(temporary, { force: true });
|
||
}
|
||
}
|
||
const exists = location =>
|
||
stat(location).then(
|
||
() => true,
|
||
() => false,
|
||
);
|
||
|
||
/** The Claude CLI's home: its settings (the gateway, its key and models) from the mounted Secret. */
|
||
async function prepareClaudeHome(home) {
|
||
if (!home || !path.isAbsolute(home)) throw new Error('WF_CLAUDE_LOCAL_AUTH_HOME must be an absolute path');
|
||
await mkdir(path.join(home, '.claude'), { recursive: true, mode: 0o700 });
|
||
await chmod(home, 0o700);
|
||
await privateCopy('/etc/wishforge/claude/settings.json', path.join(home, '.claude/settings.json'));
|
||
}
|
||
|
||
/** The SSH home through which POC integration fetches and pushes: its deploy key and pinned host keys. */
|
||
async function prepareGitHome(home) {
|
||
const ssh = path.join(home, '.ssh');
|
||
await mkdir(ssh, { recursive: true, mode: 0o700 });
|
||
await chmod(home, 0o700);
|
||
await chmod(ssh, 0o700);
|
||
await privateCopy('/etc/wishforge/git/id_ed25519', path.join(ssh, 'id_ed25519'));
|
||
await privateCopy('/etc/wishforge/config/known_hosts', path.join(ssh, 'known_hosts'));
|
||
}
|
||
|
||
/** The bindings of a binding file: one, or a list of them. */
|
||
export const bindingsOf = text => [JSON.parse(text)].flat();
|
||
|
||
/**
|
||
* POC files the runtime only accepts as the operator's own: copied from the read-only mounts. A
|
||
* profile keeps its name (poc-profile.json, fanyu-profile.json); the bindings name theirs.
|
||
*/
|
||
async function preparePocFiles(env) {
|
||
const config = '/etc/wishforge/config';
|
||
if (env.WF_POC_PROFILE_FILE)
|
||
await privateCopy(path.join(config, path.basename(env.WF_POC_PROFILE_FILE)), env.WF_POC_PROFILE_FILE);
|
||
if (env.WF_LOCAL_POC_BINDING_FILE) {
|
||
await privateCopy(path.join(config, 'project.json'), env.WF_LOCAL_POC_BINDING_FILE);
|
||
// Every profile, bound or not yet: replacing the projects binds the ones it finds here.
|
||
for (const name of (await readdir(config)).filter(name => name.endsWith('-profile.json')))
|
||
await privateCopy(path.join(config, name), path.join(path.dirname(env.WF_LOCAL_POC_BINDING_FILE), name));
|
||
for (const binding of bindingsOf(await readFile(env.WF_LOCAL_POC_BINDING_FILE, 'utf8')))
|
||
await privateCopy(path.join(config, path.basename(binding.profileFile)), binding.profileFile);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* A project's protected install or verify commands, pointed at this image's Node and check script
|
||
* when they run the POC check from elsewhere (a project migrated from the Mac). Anything else is
|
||
* returned unchanged, the same array: commands a person chose, or ones already pointing here.
|
||
*/
|
||
export function clusterCheckCommands(
|
||
commands,
|
||
step,
|
||
node = process.execPath,
|
||
script = path.join(ROOT, 'scripts/poc-project-check.mjs'),
|
||
) {
|
||
const [argv, ...others] = commands ?? [];
|
||
const pocCheck =
|
||
!others.length && argv?.length === 3 && path.basename(argv[1]) === 'poc-project-check.mjs' && argv[2] === step;
|
||
return pocCheck && (argv[0] !== node || argv[1] !== script) ? [[node, script, step]] : commands;
|
||
}
|
||
|
||
/** Stays up, doing nothing, until Kubernetes stops the container; a signal handler alone keeps no process alive. */
|
||
export function idle() {
|
||
return new Promise(resolve => {
|
||
const timer = setInterval(() => undefined, 1 << 30);
|
||
process.once('SIGTERM', () => {
|
||
clearInterval(timer);
|
||
resolve(0);
|
||
});
|
||
});
|
||
}
|
||
|
||
function run(executable, args, env, { inherit = true } = {}) {
|
||
return new Promise((resolve, reject) => {
|
||
const child = spawn(executable, args, { cwd: ROOT, env, stdio: inherit ? 'inherit' : 'ignore' });
|
||
const forward = signal => child.kill(signal);
|
||
process.on('SIGTERM', forward);
|
||
process.on('SIGINT', forward);
|
||
child.once('error', reject);
|
||
child.once('exit', (code, signal) => {
|
||
process.off('SIGTERM', forward);
|
||
process.off('SIGINT', forward);
|
||
resolve(code ?? (signal ? 128 : 1));
|
||
});
|
||
});
|
||
}
|
||
const base = () => ({
|
||
PATH: process.env.PATH,
|
||
HOME: process.env.HOME,
|
||
LANG: 'C.UTF-8',
|
||
TMPDIR: process.env.TMPDIR || '/tmp',
|
||
...Object.fromEntries(
|
||
['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY'].filter(key => process.env[key]).map(key => [key, process.env[key]]),
|
||
),
|
||
});
|
||
|
||
async function platform(file) {
|
||
const env = parseEnvFile(await readFile(file, 'utf8'));
|
||
await waitFor('PostgreSQL', env.DATABASE_ADMIN_URL);
|
||
await waitFor('Temporal', env.TEMPORAL_ADDRESS);
|
||
await waitFor('object storage', env.S3_ENDPOINT);
|
||
const migrated = await run(process.execPath, ['--import', 'tsx', 'apps/api/src/db/migrate.ts'], {
|
||
...base(),
|
||
DATABASE_ADMIN_URL: env.DATABASE_ADMIN_URL,
|
||
WF_DATABASE_APP_PASSWORD: env.WF_DATABASE_APP_PASSWORD,
|
||
WF_DEPLOYMENT_MODE: 'development',
|
||
});
|
||
if (migrated !== 0) throw new Error('Database migration failed');
|
||
await preparePocFiles(env);
|
||
if (env.WF_LOCAL_POC_BINDING_FILE) {
|
||
const pointed = await run(process.execPath, ['--import', 'tsx', 'scripts/cluster-poc-project.ts'], {
|
||
...base(),
|
||
DATABASE_URL: env.DATABASE_URL,
|
||
WF_LOCAL_POC_BINDING_FILE: env.WF_LOCAL_POC_BINDING_FILE,
|
||
});
|
||
if (pointed !== 0) throw new Error('Could not point the POC project’s checks at this image');
|
||
}
|
||
if (!(await exists(env.WF_LOCAL_ACCESS_FILE))) {
|
||
const created = await run(process.execPath, ['scripts/setup-local-access.mjs'], {
|
||
...base(),
|
||
WF_LOCAL_ACCESS_FILE: env.WF_LOCAL_ACCESS_FILE,
|
||
});
|
||
if (created !== 0) throw new Error('Could not create the local administrator credentials');
|
||
}
|
||
await prepareClaudeHome(env.WF_CLAUDE_LOCAL_AUTH_HOME);
|
||
return run(process.execPath, ['scripts/dev.mjs'], {
|
||
...base(),
|
||
WF_LOCAL_ENV_FILE: file,
|
||
WF_LOCAL_SERVICES: ROLES.platform.services,
|
||
WF_LOCAL_REUSE_SERVICES: 'false',
|
||
});
|
||
}
|
||
|
||
async function executor(file) {
|
||
const env = parseEnvFile(await readFile(file, 'utf8'));
|
||
await prepareClaudeHome(env.WF_CLAUDE_LOCAL_AUTH_HOME);
|
||
await preparePocFiles(env);
|
||
if (env.WF_LOCAL_POC_BINDING_FILE)
|
||
for (const binding of bindingsOf(await readFile(env.WF_LOCAL_POC_BINDING_FILE, 'utf8'))) {
|
||
await prepareGitHome(binding.gitHome);
|
||
await mkdir(binding.stateDirectory, { recursive: true, mode: 0o700 });
|
||
}
|
||
await mkdir(env.WF_LOCAL_RUN_ROOT, { recursive: true, mode: 0o700 });
|
||
await waitForHttp('the control plane', `${env.WF_CONTROL_PLANE_URL}/readyz`);
|
||
return run(process.execPath, ['scripts/dev.mjs'], {
|
||
...base(),
|
||
WF_LOCAL_ENV_FILE: file,
|
||
WF_LOCAL_SERVICES: ROLES.executor.services,
|
||
WF_LOCAL_REUSE_SERVICES: 'false',
|
||
});
|
||
}
|
||
|
||
async function runner(file) {
|
||
// A second environment's Runner (WF_ROLE_ENV_FILE) is set up once its project exists.
|
||
if (!(await exists(file))) {
|
||
console.log(`Runner not configured (${path.basename(file)} missing); idling.`);
|
||
return idle();
|
||
}
|
||
const env = parseEnvFile(await readFile(file, 'utf8'));
|
||
await preparePocFiles(env);
|
||
await mkdir(env.WF_RUNNER_STATE_DIR, { recursive: true, mode: 0o700 });
|
||
await waitForHttp('the control plane', `${env.WF_CONTROL_PLANE_URL}/readyz`);
|
||
// A rehearsal deployment that must never act on the POC keeps its Runner idle.
|
||
if (process.env.WF_RUNNER_DISABLED === 'true') {
|
||
console.log('Runner disabled for this deployment (WF_RUNNER_DISABLED=true); idling.');
|
||
return idle();
|
||
}
|
||
return run(process.execPath, [`--env-file=${file}`, '--import', 'tsx', 'apps/runner/src/main.ts'], base());
|
||
}
|
||
|
||
export async function main(argv = process.argv.slice(2)) {
|
||
const role = argv[0];
|
||
if (argv.length !== 1 || !ROLES[role])
|
||
throw new Error(`Usage: cluster-poc-entry.mjs ${Object.keys(ROLES).join('|')}`);
|
||
// While data is restored, the database, Temporal and object storage run and nothing writes to them.
|
||
if (process.env.WF_MAINTENANCE === 'true') {
|
||
console.log(`${role}: maintenance (WF_MAINTENANCE=true); not starting.`);
|
||
return idle();
|
||
}
|
||
if (role === 'backup') return backupLoop();
|
||
const file = process.env.WF_ROLE_ENV_FILE || ROLES[role].env;
|
||
if (role === 'platform') return platform(file);
|
||
if (role === 'executor') return executor(file);
|
||
return runner(file);
|
||
}
|
||
|
||
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url)
|
||
main().then(
|
||
code => process.exit(code),
|
||
error => {
|
||
console.error(`[cluster-poc-entry] ${error.message}`);
|
||
process.exit(1);
|
||
},
|
||
);
|