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