Skip to content

Commit c837e0e

Browse files
committed
refactor project runner code into new package
- this compiles, but not tested at all and I don't expect it to work at all yet, of course
1 parent a61a0d7 commit c837e0e

File tree

22 files changed

+547
-416
lines changed

22 files changed

+547
-416
lines changed

src/packages/pnpm-lock.yaml

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@cocalc/project-runner",
3+
"version": "0.1.0",
4+
"description": "CoCalc Project Runner",
5+
"exports": {
6+
"./run": "./dist/run/index.js"
7+
},
8+
"scripts": {
9+
"preinstall": "npx only-allow pnpm",
10+
"build": "pnpm exec tsc --build",
11+
"tsc": "pnpm exec tsc --pretty --preserveWatchOutput",
12+
"depcheck": "pnpx depcheck",
13+
"clean": "rm -rf node_modules dist"
14+
},
15+
"files": ["dist/**", "README.md", "package.json"],
16+
"author": "SageMath, Inc.",
17+
"keywords": ["utilities", "btrfs", "cocalc"],
18+
"license": "SEE LICENSE.md",
19+
"dependencies": {
20+
"@cocalc/backend": "workspace:*",
21+
"@cocalc/conat": "workspace:*",
22+
"@cocalc/util": "workspace:*",
23+
"package-directory": "^8.1.0"
24+
},
25+
"devDependencies": {
26+
"@types/jest": "^30.0.0",
27+
"@types/node": "^18.16.14"
28+
},
29+
"repository": {
30+
"type": "git",
31+
"url": "https://github.com/sagemathinc/cocalc"
32+
},
33+
"homepage": "https://github.com/sagemathinc/cocalc/tree/master/src/packages/project-runner"
34+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { conatServer } from "@cocalc/backend/data";
2+
import { join } from "node:path";
3+
4+
export function dataPath(HOME: string): string {
5+
return join(HOME, ".cache", "cocalc");
6+
}
7+
8+
// see also packages/project/secret-token.ts
9+
export function secretTokenPath(HOME: string) {
10+
const data = dataPath(HOME);
11+
return join(data, "secret-token");
12+
}
13+
14+
const ENV_VARS_DELETE = [
15+
"PGDATA",
16+
"PGHOST",
17+
"PGUSER",
18+
"PGDATABASE",
19+
"PROJECTS",
20+
"BASE_PATH",
21+
"PORT",
22+
"DATA",
23+
"LOGS",
24+
"PWD",
25+
"LINES",
26+
"COLUMNS",
27+
"LS_COLORS",
28+
"INIT_CWD",
29+
"DEBUG_FILE",
30+
"SECRETS",
31+
] as const;
32+
33+
function sanitizedEnv(env: { [key: string]: string | undefined }): {
34+
[key: string]: string;
35+
} {
36+
const env2 = { ...env };
37+
// Remove some potentially confusing env variables
38+
for (const key of ENV_VARS_DELETE) {
39+
delete env2[key];
40+
}
41+
// Comment about stripping things starting with /root:
42+
// These tend to creep in as npm changes, e.g., 'npm_config_userconfig' is
43+
// suddenly /root/.npmrc, and due to permissions this will break starting
44+
// projects with a mysterious "exit code 243" and no further info, which
45+
// is really hard to track down.
46+
for (const key in env2) {
47+
if (
48+
key.startsWith("npm_") ||
49+
key.startsWith("COCALC_") ||
50+
key.startsWith("CONAT_") ||
51+
key.startsWith("PNPM_") ||
52+
key.startsWith("__NEXT") ||
53+
key.startsWith("NODE_") ||
54+
env2[key]?.startsWith("/root") ||
55+
env2[key] == null
56+
) {
57+
delete env2[key];
58+
}
59+
}
60+
return env2 as { [key: string]: string };
61+
}
62+
63+
export function getEnvironment({
64+
HOME,
65+
project_id,
66+
env: extra,
67+
}: {
68+
HOME: string;
69+
project_id: string;
70+
env?: { [key: string]: string };
71+
}): { [key: string]: string } {
72+
const extra_env: string = Buffer.from(JSON.stringify(extra ?? {})).toString(
73+
"base64",
74+
);
75+
76+
// we only support "user" as the username here:
77+
const USER = "user";
78+
const DATA = dataPath(HOME);
79+
80+
return {
81+
...sanitizedEnv(process.env),
82+
...{
83+
HOME,
84+
DATA,
85+
LOGS: DATA,
86+
// DEBUG: so interesting stuff gets logged, but not too much unless we really need it.
87+
DEBUG: "cocalc:*,-cocalc:silly:*",
88+
// important to explicitly set the COCALC_ vars since server env has own in a project
89+
COCALC_PROJECT_ID: project_id,
90+
COCALC_USERNAME: USER,
91+
USER,
92+
COCALC_EXTRA_ENV: extra_env,
93+
PATH: `${HOME}/bin:${HOME}/.local/bin:${process.env.PATH}`,
94+
CONAT_SERVER: conatServer,
95+
COCALC_SECRET_TOKEN: secretTokenPath(HOME),
96+
},
97+
};
98+
}

0 commit comments

Comments
 (0)