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/frontend/compute/clone.tsx
Views: 687
/*1Clone compute server config. Entirely done client side.23Main issue is DNS can't be the same.45In the future we will ALSO support a checkbox to clone the data too, but not yet.6*/78import { Alert, Modal } from "antd";9import { useState } from "react";10import ShowError from "@cocalc/frontend/components/error";11import Inline from "./inline";12import { createServer, getServers } from "./api";1314export default function Clone({ id, project_id, close }) {15const [error, setError] = useState<string>("");16const [loading, setLoading] = useState<boolean>(false);1718return (19<Modal20open21confirmLoading={loading}22onCancel={close}23onOk={async () => {24try {25setLoading(true);26await createClone({ id, project_id });27close();28} catch (err) {29setError(`${err}`);30} finally {31setLoading(false);32}33}}34title={35<>36Clone Compute Server <Inline id={id} />37</>38}39okText={40<>41Clon{loading ? "ing" : "e"} <Inline id={id} />42</>43}44>45<ShowError46error={error}47setError={setError}48style={{ marginBottom: "15px" }}49/>50This makes a new deprovisioned compute server that is configured as close51as possibleto this this compute server.{" "}52<Alert53showIcon54style={{ margin: "15px" }}55type="warning"56message="The underlying disk is not copied."57/>58After cloning the compute server, you can edit anything about its59configuration before starting it.60</Modal>61);62}6364async function createClone({65id,66project_id,67}: {68id: number;69project_id: string;70}) {71const servers = await getServers({ project_id });72const titles = new Set(servers.map((x) => x.title));73const allDns = new Set(74servers.filter((x) => x.configuration.dns).map((x) => x.configuration.dns),75);76let server;77let done = false;78for (const s of servers) {79if (s.id == id) {80server = s;81done = true;82break;83}84}85if (!done) {86throw Error(`no such compute server ${id}`);87}88let n = 1;89let title = `Clone of ${server.title}`;90if (titles.has(title)) {91while (titles.has(title + ` (${n})`)) {92n += 1;93}94title = title + ` (${n})`;95}96server.title = title;9798delete server.configuration.authToken;99100if (server.configuration.dns) {101n = 1;102while (allDns.has(server.configuration.dns + `-${n}`)) {103n += 1;104}105server.configuration.dns = server.configuration.dns + `-${n}`;106}107108await createServer({ ...server });109}110111112