@cocalc/project
Version:
CoCalc: project daemon
128 lines • 4.68 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanup = exports.set_extra_env = exports.configure = exports.is_free_project = exports.getProjectConfig = exports.DEFAULT_FREE_PROCS_NICENESS = void 0;
/*
This configures the project hub based on an environment variable or other data.
*/
const debug_1 = __importDefault(require("debug"));
const fs_1 = require("fs");
const os_1 = require("os");
const L = (0, debug_1.default)("project:project-setup");
// const { callback2: cb2 } = require("@cocalc/util/async-utils");
// const { execute_code } = require("@cocalc/backend/misc_node");
// 19 is the minimum, we keep it 1 above that.
exports.DEFAULT_FREE_PROCS_NICENESS = 18;
function getProjectConfig() {
const conf_enc = process.env.COCALC_PROJECT_CONFIG;
if (conf_enc == null) {
return null;
}
try {
L(`configure(${conf_enc.slice(0, 30)}...)`);
const conf_raw = Buffer.from(conf_enc, "base64").toString("utf8");
return JSON.parse(conf_raw);
}
catch (err) {
// we report and ignore errors
L(`ERROR parsing COCALC_PROJECT_CONFIG -- '${conf_enc}' -- ${err}`);
return null;
}
}
exports.getProjectConfig = getProjectConfig;
// this is for kucalc projects only
function is_free_project() {
const conf = getProjectConfig();
const ifp = conf?.quota?.member_host === false;
L(`is_free_project: ${ifp}`);
return ifp;
}
exports.is_free_project = is_free_project;
function configure() {
if (is_free_project()) {
L(`member_host is false -- renicing everything`);
(0, os_1.setPriority)(process.pid, exports.DEFAULT_FREE_PROCS_NICENESS);
}
}
exports.configure = configure;
// Contains additional environment variables. Base 64 encoded JSON of {[key:string]:string}.
function set_extra_env() {
sage_aarch64_hack();
if (!process.env.COCALC_EXTRA_ENV) {
L("set_extra_env: nothing provided");
return;
}
const ret = {};
try {
const env64 = process.env.COCALC_EXTRA_ENV;
const raw = Buffer.from(env64, "base64").toString("utf8");
L(`set_extra_env: ${raw}`);
const data = JSON.parse(raw);
if (typeof data === "object") {
for (let k in data) {
const v = data[k];
if (typeof v !== "string" || v.length === 0) {
L(`set_extra_env: ignoring key ${k}, value is not a string or has length 0`);
continue;
}
// this is the meat of all this – this should happen after cleanup()!
process.env[k] = v;
ret[k] = v;
}
}
}
catch (err) {
// we report and ignore errors
L(`ERROR set_extra_env -- cannot process '${process.env.COCALC_EXTRA_ENV}' -- ${err}`);
}
return ret;
}
exports.set_extra_env = set_extra_env;
// this should happen before set_extra_env
function cleanup() {
// clean environment to get rid of nvm and other variables
if (process.env.PATH == null)
return;
process.env.PATH = process.env.PATH.split(":")
.filter((x) => !x.startsWith("/cocalc/nvm"))
.join(":");
const envrm = [
"DATA",
"BASE_PATH",
"NODE_PATH",
"NODE_ENV",
"NODE_VERSION",
"NVM_CD_FLAGS",
"NVM_DIR",
"NVM_BIN",
"DEBUG",
"PATH_COCALC",
"COCALC_ROOT",
];
envrm.forEach((name) => delete process.env[name]);
// Also get rid of any npm_ vars that get set due to how the project server
// is started. This is mainly an issue with cocalc-docker.
for (const key in process.env) {
if (key.startsWith("npm_"))
delete process.env[key];
}
}
exports.cleanup = cleanup;
// See https://github.com/opencv/opencv/issues/14884
// Importing Sage in various situations, e.g., as is done for sage server,
// is fundamentally broken on aarch64 linux due to this issue. Yes, I explained
// this on sage-devel, but nobody understood.
// It's also important to NOT do this hack if you're not on aarch64!
function sage_aarch64_hack() {
const LD_PRELOAD = "/usr/lib/aarch64-linux-gnu/libgomp.so.1";
if (process.arch == "arm64" && (0, fs_1.existsSync)(LD_PRELOAD)) {
process.env.LD_PRELOAD = LD_PRELOAD;
}
}
//# sourceMappingURL=project-setup.js.map