|
| 1 | +/* |
| 2 | +Runner based on podman. |
| 3 | +*/ |
| 4 | + |
| 5 | +import getLogger from "@cocalc/backend/logger"; |
| 6 | +import { nodePath } from "./mounts"; |
| 7 | +import { isValidUUID } from "@cocalc/util/misc"; |
| 8 | +import { ensureConfFilesExists, setupDataPath, writeSecretToken } from "./util"; |
| 9 | +import { getEnvironment } from "./env"; |
| 10 | +import { mkdir } from "fs/promises"; |
| 11 | +import { spawn } from "node:child_process"; |
| 12 | +import { type Configuration } from "./types"; |
| 13 | +export { type Configuration }; |
| 14 | +import { getCoCalcMounts } from "./mounts"; |
| 15 | +import { mountHome, setQuota } from "./filesystem"; |
| 16 | +import { executeCode } from "@cocalc/backend/execute-code"; |
| 17 | + |
| 18 | +const logger = getLogger("project-runner:podman"); |
| 19 | +const children: { [project_id: string]: any } = {}; |
| 20 | + |
| 21 | +const GRACE_PERIOD = 2000; |
| 22 | + |
| 23 | +const DEFAULT_IMAGE = "ubuntu:25.04"; |
| 24 | + |
| 25 | +export async function start({ |
| 26 | + project_id, |
| 27 | + config, |
| 28 | +}: { |
| 29 | + project_id: string; |
| 30 | + config?: Configuration; |
| 31 | +}) { |
| 32 | + if (!isValidUUID(project_id)) { |
| 33 | + throw Error("start: project_id must be valid"); |
| 34 | + } |
| 35 | + logger.debug("start", { project_id, config: { ...config, secret: "xxx" } }); |
| 36 | + if (children[project_id] != null && children[project_id].exitCode == null) { |
| 37 | + logger.debug("start -- already running"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const home = await mountHome(project_id); |
| 42 | + await mkdir(home, { recursive: true }); |
| 43 | + await ensureConfFilesExists(home); |
| 44 | + const env = getEnvironment({ |
| 45 | + project_id, |
| 46 | + env: config?.env, |
| 47 | + HOME: "/home/user", |
| 48 | + }); |
| 49 | + await setupDataPath(home); |
| 50 | + if (config?.secret) { |
| 51 | + await writeSecretToken(home, config.secret); |
| 52 | + } |
| 53 | + |
| 54 | + if (config?.disk) { |
| 55 | + // TODO: maybe this should be done in parallel with other things |
| 56 | + // to make startup time slightly faster (?) -- could also be incorporated |
| 57 | + // into mount. |
| 58 | + await setQuota(project_id, config.disk); |
| 59 | + } |
| 60 | + |
| 61 | + const args: string[] = ["run", "--rm", "--network=host", "--user=0:0"]; |
| 62 | + const cmd = "podman"; |
| 63 | + const script = "/cocalc/src/packages/project/bin/cocalc-project.js"; |
| 64 | + |
| 65 | + args.push("--hostname", `project-${project_id}`); |
| 66 | + args.push("--name", `project-${project_id}`); |
| 67 | + |
| 68 | + const mounts = getCoCalcMounts(); |
| 69 | + for (const path in mounts) { |
| 70 | + args.push("-v", `${path}:${mounts[path]}:ro`); |
| 71 | + } |
| 72 | + args.push("-v", `${home}:${env.HOME}`); |
| 73 | + for (const name in env) { |
| 74 | + args.push("-e", `${name}=${env[name]}`); |
| 75 | + } |
| 76 | + |
| 77 | + args.push(config?.image ?? DEFAULT_IMAGE); |
| 78 | + args.push(nodePath); |
| 79 | + args.push(script, "--init", "project_init.sh"); |
| 80 | + |
| 81 | + console.log(`${cmd} ${args.join(" ")}`); |
| 82 | + logger.debug(`${cmd} ${args.join(" ")}`); |
| 83 | + |
| 84 | + const child = spawn(cmd, args); |
| 85 | + children[project_id] = child; |
| 86 | + |
| 87 | + child.stdout.on("data", (chunk: Buffer) => { |
| 88 | + logger.debug(`project_id=${project_id}.stdout: `, chunk.toString()); |
| 89 | + }); |
| 90 | + child.stderr.on("data", (chunk: Buffer) => { |
| 91 | + logger.debug(`project_id=${project_id}.stderr: `, chunk.toString()); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +export async function stop({ project_id }) { |
| 96 | + if (!isValidUUID(project_id)) { |
| 97 | + throw Error("stop: project_id must be valid"); |
| 98 | + } |
| 99 | + logger.debug("stop", { project_id }); |
| 100 | + const child = children[project_id]; |
| 101 | + if (child != null && child.exitCode == null) { |
| 102 | + try { |
| 103 | + await podman([ |
| 104 | + "stop", |
| 105 | + "-t", |
| 106 | + `${GRACE_PERIOD / 1000}`, |
| 107 | + `project-${project_id}`, |
| 108 | + ]); |
| 109 | + await podman(["rm", `project-${project_id}`]); |
| 110 | + } catch {} |
| 111 | + delete children[project_id]; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +async function podman(args: string[]) { |
| 116 | + return await executeCode({ command: "podman", args, err_on_exit: true }); |
| 117 | +} |
| 118 | + |
| 119 | +async function state(project_id) { |
| 120 | + const { stdout } = await podman([ |
| 121 | + "ps", |
| 122 | + "--filter", |
| 123 | + `name=project-${project_id}`, |
| 124 | + "--format", |
| 125 | + "{{.State}}", |
| 126 | + ]); |
| 127 | + return stdout.trim() == "running" ? "running" : "opened"; |
| 128 | +} |
| 129 | + |
| 130 | +export async function status({ project_id }) { |
| 131 | + if (!isValidUUID(project_id)) { |
| 132 | + throw Error("status: project_id must be valid"); |
| 133 | + } |
| 134 | + logger.debug("status", { project_id }); |
| 135 | + // TODO |
| 136 | + return { state: await state(project_id), ip: "127.0.0.1" }; |
| 137 | +} |
| 138 | + |
| 139 | +export async function close() { |
| 140 | + const v: any[] = []; |
| 141 | + for (const project_id in children) { |
| 142 | + logger.debug(`killing project_id=${project_id}`); |
| 143 | + v.push(stop({ project_id })); |
| 144 | + delete children[project_id]; |
| 145 | + } |
| 146 | + await Promise.all(v); |
| 147 | +} |
| 148 | + |
| 149 | +// important because it kills all |
| 150 | +// the processes that were spawned |
| 151 | +process.once("exit", close); |
| 152 | +["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => { |
| 153 | + process.once(sig, () => { |
| 154 | + process.exit(); |
| 155 | + }); |
| 156 | +}); |
0 commit comments