Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/info-json.ts
1447 views
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
import basePath from "@cocalc/backend/base-path";
11
import { infoJson, project_id, username } from "./data";
12
import { getOptions } from "./init-program";
13
import { getLogger } from "./logger";
14
15
let INFO: {
16
project_id: string;
17
location: { host: string; username: string };
18
base_path: string;
19
base_url: string; // backward compat
20
};
21
22
export { INFO };
23
24
export default async function init() {
25
const winston = getLogger("info-json init");
26
winston.info("initializing the info.json file...");
27
let host: string;
28
if (process.env.HOST != null) {
29
host = process.env.HOST;
30
} else if (getOptions().kucalc) {
31
// what we want for the Google Compute engine deployment
32
// earlier, there was eth0, but newer Ubuntu's on GCP have ens4
33
const nics = networkInterfaces();
34
const mynic = nics.eth0 ?? nics.ens4;
35
host = mynic?.[0].address ?? "127.0.0.1";
36
} else {
37
// for a single machine (e.g., cocalc-docker)
38
host = "127.0.0.1";
39
}
40
INFO = {
41
project_id,
42
location: { host, username },
43
base_path: basePath,
44
base_url: basePath, // for backwards compat userspace code
45
};
46
await writeFile(infoJson, JSON.stringify(INFO));
47
winston.info(`Successfully wrote "${infoJson}"`);
48
}
49
50