CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/info-json.ts
Views: 687
1
/*
2
Responsible for creating the info.json file that's in the
3
temporary store (e.g., ~/.smc) for the project. It is used
4
by various scripts and programs to get basic information
5
about the project.
6
*/
7
8
import { writeFile } from "node:fs/promises";
9
import { networkInterfaces } from "node:os";
10
11
import basePath from "@cocalc/backend/base-path";
12
import { infoJson, project_id, username } from "./data";
13
import { getOptions } from "./init-program";
14
import { getLogger } from "./logger";
15
16
let INFO: {
17
project_id: string;
18
location: { host: string; username: string };
19
base_path: string;
20
base_url: string; // backward compat
21
};
22
23
export { INFO };
24
25
export default async function init() {
26
const winston = getLogger("info-json init");
27
winston.info("initializing the info.json file...");
28
let host: string;
29
if (process.env.HOST != null) {
30
host = process.env.HOST;
31
} else if (getOptions().kucalc) {
32
// what we want for the Google Compute engine deployment
33
// earlier, there was eth0, but newer Ubuntu's on GCP have ens4
34
const nics = networkInterfaces();
35
const mynic = nics.eth0 ?? nics.ens4;
36
host = mynic?.[0].address ?? "127.0.0.1";
37
} else {
38
// for a single machine (e.g., cocalc-docker)
39
host = "127.0.0.1";
40
}
41
INFO = {
42
project_id,
43
location: { host, username },
44
base_path: basePath,
45
base_url: basePath, // for backwards compat userspace code
46
};
47
await writeFile(infoJson, JSON.stringify(INFO));
48
winston.info(`Successfully wrote "${infoJson}"`);
49
}
50
51