import { spawn, type ChildProcess } from "node:child_process"; import { randomBytes } from "node:crypto"; import { existsSync } from "node:fs"; import { mkdir } from "node:fs/promises"; import { createRequire } from "node:module"; import { createServer } from "node:net"; import os from "node:os"; import path from "node:path"; import { setTimeout as delay } from "node:timers/promises"; export const MINIO_RELEASE = "RELEASE.2025-04-22T22-12-26Z"; export const DEFAULT_MINIO_BINARY = path.join(os.homedir(), ".cache", "paperclip", "tools", "minio", "minio"); export function minioBinary(environment: NodeJS.ProcessEnv = process.env): string | null { const candidate = environment.RESTART_CHAOS_MINIO_BIN || DEFAULT_MINIO_BINARY; return existsSync(candidate) ? candidate : null; } export async function freeLoopbackPort(): Promise { const server = createServer(); await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); }); const port = (server.address() as { port: number }).port; await new Promise(resolve => server.close(() => resolve())); return port; } export async function launchMinio(input: { binary: string; dataDir: string; address: string; home: string; accessKeyId: string; secretAccessKey: string; readyTimeoutMs?: number }): Promise<() => Promise> { const consolePort = await freeLoopbackPort(); const child: ChildProcess = spawn(input.binary, ["server", input.dataDir, "--address", input.address, "--console-address", `127.0.0.1:${consolePort}`, "--quiet"], { env: { PATH: process.env.PATH ?? "", HOME: input.home, MINIO_ROOT_USER: input.accessKeyId, MINIO_ROOT_PASSWORD: input.secretAccessKey, MINIO_BROWSER: "off", MINIO_UPDATE: "off" }, stdio: ["ignore", "ignore", "pipe"], }); let stderr = ""; let spawnError: Error | null = null; child.on("error", error => { spawnError = error; }); child.stderr?.on("data", chunk => { stderr = `${stderr}${chunk}`.slice(-4_000); }); const stop = async () => { if (spawnError || child.pid === undefined || child.exitCode !== null || child.signalCode !== null) return; const exited = new Promise(resolve => child.once("exit", () => resolve())); child.kill("SIGTERM"); await Promise.race([exited, delay(10_000).then(() => { child.kill("SIGKILL"); return exited; })]); }; const endpoint = `http://${input.address}`; const deadline = Date.now() + (input.readyTimeoutMs ?? 30_000); for (;;) { if (spawnError) throw new Error(`MinIO could not start from ${input.binary}: ${(spawnError as Error).message}`); if (child.exitCode !== null) throw new Error(`MinIO exited before it was ready (${child.exitCode}): ${stderr}`); const attempt = AbortSignal.timeout(Math.max(1, Math.min(2_000, deadline - Date.now()))); if (await fetch(`${endpoint}/minio/health/live`, { signal: attempt }).then(response => response.ok).catch(() => false)) return stop; if (Date.now() > deadline) { await stop(); throw new Error(`MinIO did not become ready: ${stderr}`); } await delay(200); } } export interface ObjectStore { endpoint: string; bucket: string; region: string; accessKeyId: string; secretAccessKey: string; dataDir: string; stop(): Promise; } export function objectStoreServerEnvironment(store: Pick, endpoint: string, prefix: string): Record { return { PAPERCLIP_STORAGE_PROVIDER: "s3", PAPERCLIP_STORAGE_S3_BUCKET: store.bucket, PAPERCLIP_STORAGE_S3_REGION: store.region, PAPERCLIP_STORAGE_S3_ENDPOINT: endpoint, PAPERCLIP_STORAGE_S3_FORCE_PATH_STYLE: "true", PAPERCLIP_STORAGE_S3_PREFIX: prefix, AWS_ACCESS_KEY_ID: store.accessKeyId, AWS_SECRET_ACCESS_KEY: store.secretAccessKey, AWS_SESSION_TOKEN: "", AWS_CREDENTIAL_EXPIRATION: "", AWS_EC2_METADATA_DISABLED: "true", }; } export async function startMinio(input: { binary: string; root: string; bucket?: string; readyTimeoutMs?: number }): Promise { const dataDir = path.join(input.root, "object-store"); await mkdir(dataDir, { recursive: true, mode: 0o700 }); const port = await freeLoopbackPort(); const accessKeyId = `chaos${randomBytes(6).toString("hex")}`; const secretAccessKey = randomBytes(24).toString("hex"); const endpoint = `http://127.0.0.1:${port}`; const stop = await launchMinio({ binary: input.binary, dataDir, address: `127.0.0.1:${port}`, home: input.root, accessKeyId, secretAccessKey, ...(input.readyTimeoutMs ? { readyTimeoutMs: input.readyTimeoutMs } : {}) }); const bucket = input.bucket ?? `chaos-${randomBytes(4).toString("hex")}`; const region = "us-east-1"; const { S3Client, CreateBucketCommand } = createRequire(new URL("../../server/package.json", import.meta.url))("@aws-sdk/client-s3"); const client = new S3Client({ endpoint, region, forcePathStyle: true, credentials: { accessKeyId, secretAccessKey } }); try { await client.send(new CreateBucketCommand({ Bucket: bucket })); } catch (error) { await stop(); throw error; } finally { client.destroy(); } return { endpoint, bucket, region, accessKeyId, secretAccessKey, dataDir, stop }; }