Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/exec_shell_code.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// import { getLogger } from "@cocalc/backend/logger";
7
// const winston = getLogger('exec-shell-code')
8
9
import { abspath } from "@cocalc/backend/misc_node";
10
import { CoCalcSocket } from "@cocalc/backend/tcp/enable-messaging-protocol";
11
import * as message from "@cocalc/util/message";
12
import { getLogger } from "./logger";
13
import execCode from "@cocalc/project/browser-websocket/exec-code";
14
import type { ExecuteCodeOutput } from "@cocalc/util/types/execute-code";
15
16
const { debug: D } = getLogger("exec_shell_code");
17
18
export async function exec_shell_code(socket: CoCalcSocket, mesg) {
19
//winston.debug("project_exec: #{misc.to_json(mesg)} in #{process.cwd()}")
20
if (mesg.command === "smc-jupyter") {
21
socket.write_mesg(
22
"json",
23
message.error({ id: mesg.id, error: "do not run smc-jupyter directly" }),
24
);
25
return;
26
}
27
28
D(`command=${mesg.command} args=${mesg.args} path=${mesg.path}`);
29
30
try {
31
const ret = handleExecShellCode(mesg);
32
socket.write_mesg("json", message.project_exec_output(ret));
33
} catch (err) {
34
let error = `Error executing command '${mesg.command}' with args '${mesg.args}' -- ${err}`;
35
if (error.indexOf("Connection refused") !== -1) {
36
error +=
37
"-- Email [email protected] if you need full internet access, which is disabled by default.";
38
}
39
// Too annoying and doesn't work.
40
//if error.indexOf("=") != -1
41
// error += "-- This is a BASH terminal, not a Sage worksheet. For Sage, use +New and create a Sage worksheet."
42
const err_mesg = message.error({
43
id: mesg.id,
44
error,
45
});
46
socket.write_mesg("json", err_mesg);
47
}
48
}
49
50
export async function handleExecShellCode(mesg) {
51
const out = await execCode({
52
path: !!mesg.compute_server_id ? mesg.path : abspath(mesg.path ?? ""),
53
...mesg,
54
});
55
let ret: ExecuteCodeOutput & { id: string } = {
56
id: mesg.id,
57
type: "blocking",
58
stdout: out?.stdout,
59
stderr: out?.stderr,
60
exit_code: out?.exit_code,
61
};
62
if (out?.type === "async") {
63
// extra fields for ExecuteCodeOutputAsync
64
ret = {
65
...ret,
66
...out, // type=async, pid, status, job_id, stats, ...
67
};
68
}
69
return ret;
70
}
71
72