Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/compute/scripts.ts
Views: 687
/*1Returns a bash script that when run as root starts2a compute server and connects it to a project.34This is meant to be used for on prem compute servers,5hence it includes installing the /cocalc code and the "user" user.6*/78import {9getStartupScript,10getStopScript,11getDeprovisionScript,12} from "@cocalc/server/compute/control";13import { getAccountWithApiKey as getProjectIdWithApiKey } from "@cocalc/server/api/manage";14import getParams from "lib/api/get-params";15import getPool from "@cocalc/database/pool";1617import { apiRoute, apiRouteOperation } from "lib/api";18import {19ComputeServerScriptsInputSchema,20ComputeServerScriptsOutputSchema,21} from "lib/api/schema/compute/scripts";2223async function handle(req, res) {24try {25res.send(await get(req));26} catch (err) {27res.json({ error: `${err.message}` });28return;29}30}3132export async function get(req) {33const { api_key, id: id0, action } = getParams(req);34// use api_key to get project, and also verify access:35const id = parseInt(id0);36return await getScript({ api_key, id, action });37}3839export async function getScript({40api_key,41id,42action,43}: {44api_key: string;45id: number;46action: "start" | "stop" | "deprovision";47}): Promise<string> {48const project_id = await getProjectIdWithApiKey(api_key);49if (!project_id) {50throw Error("api_key must be a valid project api key");51}52const { rows } = await getPool().query(53"SELECT COUNT(*) AS count FROM compute_servers WHERE id=$1 AND project_id=$2",54[id, project_id],55);56if (rows[0]?.count != 1) {57throw Error(`no compute server with id=${id} in project with this api key`);58}59if (action == "start") {60return await getStartupScript({61id,62api_key,63installUser: true,64});65} else if (action == "stop") {66return await getStopScript({67id,68api_key,69});70} else if (action == "deprovision") {71return await getDeprovisionScript({72id,73api_key,74});75} else {76throw Error(`unknown action=${action}`);77}78}7980export default apiRoute({81scripts: apiRouteOperation({82method: "POST",83openApiOperation: {84tags: ["Compute"],85},86})87.input({88contentType: "application/json",89body: ComputeServerScriptsInputSchema,90})91.outputs([92{93status: 200,94contentType: "text/plain",95body: ComputeServerScriptsOutputSchema,96},97])98.handler(handle),99});100101102