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/next/pages/api/v2/compute/get-serial-port-output.ts
Views: 687
1
/*
2
Get serial port output (since boot) for a compute server.
3
4
Any collaborator on the project containing the compute server has permission to get this serial output.
5
*/
6
7
import getAccountId from "lib/account/get-account";
8
import { getSerialPortOutput } from "@cocalc/server/compute/control";
9
import { getServerNoCheck } from "@cocalc/server/compute/get-servers";
10
import getParams from "lib/api/get-params";
11
import isCollaborator from "@cocalc/server/projects/is-collaborator";
12
13
import { apiRoute, apiRouteOperation } from "lib/api";
14
import {
15
GetComputeServerSerialPortOutputInputSchema,
16
GetComputeServerSerialPortOutputOutputSchema,
17
} from "lib/api/schema/compute/get-serial-port-output";
18
19
async function handle(req, res) {
20
try {
21
res.json(await get(req));
22
} catch (err) {
23
res.json({ error: `${err.message}` });
24
return;
25
}
26
}
27
28
async function get(req) {
29
const account_id = await getAccountId(req);
30
if (!account_id) {
31
throw Error("user must be signed in");
32
}
33
// id of the server
34
const { id } = getParams(req);
35
const server = await getServerNoCheck(id);
36
37
if (
38
!(await isCollaborator({
39
account_id,
40
project_id: server.project_id,
41
}))
42
) {
43
throw Error("must be a collaborator on project with compute server");
44
}
45
46
return await getSerialPortOutput({
47
id,
48
account_id: server.account_id, // actual compute server owner
49
});
50
}
51
52
export default apiRoute({
53
getSerialPortOutput: apiRouteOperation({
54
method: "POST",
55
openApiOperation: {
56
tags: ["Compute"],
57
},
58
})
59
.input({
60
contentType: "application/json",
61
body: GetComputeServerSerialPortOutputInputSchema,
62
})
63
.outputs([
64
{
65
status: 200,
66
contentType: "application/json",
67
body: GetComputeServerSerialPortOutputOutputSchema,
68
},
69
])
70
.handler(handle),
71
});
72
73