singularity-forge/scripts/run-vega-source-server.mjs
Mikael Hugo 6d8fc62243
Some checks are pending
sf self-deploy / build, test, and publish server image (push) Waiting to run
sf self-deploy / deploy test and probe (push) Blocked by required conditions
sf self-deploy / promote prod (push) Blocked by required conditions
fix: use shared sf webserver project config
2026-05-17 22:09:28 +02:00

160 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
/**
* run-vega-source-server.mjs — start the source-mounted SF server container on
* vega without requiring Docker Compose.
*
* Purpose: keep the local production server containerized while using the live
* checkout at /home/mhugo/code/singularity-forge as the source of truth.
*
* Consumer: `npm run docker:vega:up` on vega.
*/
import { spawnSync } from "node:child_process";
import { homedir } from "node:os";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const root = resolve(fileURLToPath(new URL("..", import.meta.url)));
const image = process.env.SF_VEGA_IMAGE || "sf-source-server:vega";
const bind = process.env.SF_VEGA_BIND || "127.0.0.1";
const workspace = resolve(process.env.SF_WORKSPACE_DIR || root);
const workspacesRoot = resolve(process.env.SF_WORKSPACES_DIR || dirname(root));
const name = process.env.SF_VEGA_CONTAINER || "sf-server-vega";
const port = process.env.SF_VEGA_PORT || "4000";
const uid = process.env.PUID || String(process.getuid?.() ?? 1000);
const gid = process.env.PGID || String(process.getgid?.() ?? 1000);
const command = process.argv[2] ?? "up";
if (command === "--help" || command === "-h" || command === "help") {
process.stdout.write(`Usage:
npm run docker:vega:up
SF_WORKSPACE_DIR=/path/to/repo npm run docker:vega:up
node scripts/run-vega-source-server.mjs print
node scripts/run-vega-source-server.mjs logs
node scripts/run-vega-source-server.mjs down
Environment:
SF_WORKSPACE_DIR initial repo mounted at /workspace, defaults to this checkout
SF_WORKSPACES_DIR repo parent mounted at /workspaces, defaults to ../
SF_VEGA_BIND host bind address, defaults to 127.0.0.1
SF_VEGA_PORT shared webserver port, defaults to 4000
SF_VEGA_CONTAINER explicit container name
`);
process.exit(0);
}
if (command === "print") {
process.stdout.write(
JSON.stringify(
{
name,
image,
bind,
port,
workspace,
workspacesRoot,
sfSource: root,
},
null,
2,
) + "\n",
);
process.exit(0);
}
if (command === "logs") {
run("docker", ["logs", "-f", "--tail=200", name]);
process.exit(0);
}
if (command === "down") {
run("docker", ["rm", "-f", name]);
process.exit(0);
}
if (command !== "up") {
process.stderr.write(`Unknown command: ${command}\n`);
process.exit(2);
}
const allowedOrigins =
process.env.SF_WEB_ALLOWED_ORIGINS ||
`http://127.0.0.1:${port},http://localhost:${port}`;
run("docker", [
"build",
"-f",
"docker/Dockerfile.source-server",
"-t",
image,
".",
]);
spawnSync("docker", ["rm", "-f", name], { stdio: "ignore" });
run("docker", [
"run",
"-d",
"--name",
name,
"--restart",
"unless-stopped",
"--user",
`${uid}:${gid}`,
"-p",
`${bind}:${port}:4000`,
"-e",
"HOME=/home/node",
"-e",
"NODE_ENV=development",
"-e",
"SF_SOURCE_ROOT=/opt/sf",
"-e",
"SF_RUNTIME_SOURCE_ROOT=/opt/sf",
"-e",
"SF_RELEASE_MANIFEST=/opt/sf/dist/sf-release-manifest.json",
"-e",
`SF_WEB_PROJECT_CWD=${workspace}`,
"-e",
`SF_WORKSPACES_DIR=${workspacesRoot}`,
"-e",
"HOSTNAME=0.0.0.0",
"-e",
"PORT=4000",
"-e",
"SF_WEB_HOST=0.0.0.0",
"-e",
"SF_WEB_PORT=4000",
"-e",
`SF_WEB_ALLOWED_ORIGINS=${allowedOrigins}`,
"-e",
"SF_DEV_SERVER_WATCH=1",
"-v",
`${root}:/opt/sf`,
"-v",
`${workspace}:/workspace`,
"-v",
`${workspacesRoot}:/workspaces`,
"-v",
`${workspacesRoot}:${workspacesRoot}`,
"-v",
`${homedir()}/.sf:/home/node/.sf`,
"-v",
`${homedir()}/.gitconfig:/home/node/.gitconfig:ro`,
image,
"node",
"/opt/sf/dist/web/standalone/server.js",
]);
process.stdout.write(`${name} listening on ${bind}:${port}\n`);
process.stdout.write(`SF source: ${root}\n`);
process.stdout.write(`Initial workspace: ${workspace}\n`);
process.stdout.write(`Workspace parent: ${workspacesRoot}\n`);
function run(command, args) {
const result = spawnSync(command, args, {
cwd: root,
stdio: "inherit",
env: process.env,
});
if (result.status !== 0) process.exit(result.status ?? 1);
}