Path: blob/master/src/packages/frontend/compute/clone.tsx
1503 views
/*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, getServersById } from "./api";13import type { ComputeServerUserInfo } from "@cocalc/util/db-schema/compute-servers";1415export default function Clone({ id, close }) {16const [error, setError] = useState<string>("");17const [loading, setLoading] = useState<boolean>(false);1819return (20<Modal21width={700}22open23confirmLoading={loading}24onCancel={close}25onOk={async () => {26try {27setLoading(true);28await createClone({ id });29close();30} catch (err) {31setError(`${err}`);32} finally {33setLoading(false);34}35}}36title={37<>38Clone Compute Server <Inline id={id} />39</>40}41okText={42<>43Clon{loading ? "ing" : "e"} <Inline id={id} />44</>45}46>47<ShowError48error={error}49setError={setError}50style={{ marginBottom: "15px" }}51/>52This makes a new deprovisioned compute server that is configured as close53as possibleto this this compute server.{" "}54<Alert55showIcon56style={{ margin: "15px" }}57type="warning"58message="The underlying disk is not copied."59/>60After cloning the compute server, you can edit anything about its61configuration before starting it.62</Modal>63);64}6566export async function cloneConfiguration({67id,68noChange,69}: {70id: number;71noChange?: boolean;72}) {73const servers = await getServersById({ ids: [id] });74if (servers.length == 0) {75throw Error(`no such compute server ${id}`);76}77const server = servers[0] as ComputeServerUserInfo;78if (!noChange) {79let n = 1;80let title = `Clone of ${server.title}`;81const titles = new Set(servers.map((x) => x.title));82if (titles.has(title)) {83while (titles.has(title + ` (${n})`)) {84n += 1;85}86title = title + ` (${n})`;87}88server.title = title;89}9091delete server.configuration.authToken;92return server;93}9495async function createClone({ id }: { id: number }) {96const server = await cloneConfiguration({ id });97await createServer({ ...server });98}99100101