Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/data.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Paths to temporary files used by the project.
8
*/
9
10
import { join } from "path";
11
import { data } from "@cocalc/backend/data";
12
import { is_valid_uuid_string } from "@cocalc/util/misc";
13
import { pidFilename } from "@cocalc/util/project-info";
14
15
export const infoJson = join(data, "info.json");
16
export const hubPortFile = join(data, "hub-server.port");
17
export const apiServerPortFile = join(data, "api-server.port");
18
export const browserPortFile = join(data, "browser-server.port");
19
export const projectPidFile = join(data, pidFilename);
20
export const startTimestampFile = join(data, "start-timestamp.txt");
21
export const sessionIDFile = join(data, "session-id.txt");
22
export const rootSymlink = join(data, "root");
23
export const SSH_LOG = join(data, "sshd.log");
24
export const SSH_ERR = join(data, "sshd.err");
25
export const compute_server_id = parseInt(process.env.COMPUTE_SERVER_ID ?? "0");
26
export const sageServerPaths = {
27
log: join(data, "sage_server", "sage_server.log"),
28
pid: join(data, "sage_server", "sage_server.pid"),
29
port: join(data, "sage_server", "sage_server.port"),
30
} as const;
31
32
// secret token must be after compute_server_id is set, since it uses it.
33
export { secretToken } from "./secret-token";
34
35
// note that the "username" need not be the output of `whoami`, e.g.,
36
// when using a cc-in-cc dev project where users are "virtual".
37
function getIDs() {
38
let project_id;
39
if (process.env.COCALC_PROJECT_ID) {
40
project_id = process.env.COCALC_PROJECT_ID;
41
} else {
42
if (!process.env.HOME) {
43
throw Error("HOME not defined, so no way to determine project_id");
44
}
45
const v = process.env.HOME.split("/");
46
project_id = v[v.length - 1];
47
if (!is_valid_uuid_string(project_id)) {
48
throw Error("unable to determine project_id from HOME directory path");
49
}
50
}
51
// Throw in some consistency checks:
52
if (!is_valid_uuid_string(project_id)) {
53
throw Error(`project_id=${project_id} is not a valid UUID`);
54
}
55
const username = process.env.COCALC_USERNAME ?? project_id.replace(/-/g, "");
56
return { project_id, username };
57
}
58
59
export const { project_id, username } = getIDs();
60
61