Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/compute/clone.tsx
1503 views
1
/*
2
Clone compute server config. Entirely done client side.
3
4
Main issue is DNS can't be the same.
5
6
In the future we will ALSO support a checkbox to clone the data too, but not yet.
7
*/
8
9
import { Alert, Modal } from "antd";
10
import { useState } from "react";
11
import ShowError from "@cocalc/frontend/components/error";
12
import Inline from "./inline";
13
import { createServer, getServersById } from "./api";
14
import type { ComputeServerUserInfo } from "@cocalc/util/db-schema/compute-servers";
15
16
export default function Clone({ id, close }) {
17
const [error, setError] = useState<string>("");
18
const [loading, setLoading] = useState<boolean>(false);
19
20
return (
21
<Modal
22
width={700}
23
open
24
confirmLoading={loading}
25
onCancel={close}
26
onOk={async () => {
27
try {
28
setLoading(true);
29
await createClone({ id });
30
close();
31
} catch (err) {
32
setError(`${err}`);
33
} finally {
34
setLoading(false);
35
}
36
}}
37
title={
38
<>
39
Clone Compute Server <Inline id={id} />
40
</>
41
}
42
okText={
43
<>
44
Clon{loading ? "ing" : "e"} <Inline id={id} />
45
</>
46
}
47
>
48
<ShowError
49
error={error}
50
setError={setError}
51
style={{ marginBottom: "15px" }}
52
/>
53
This makes a new deprovisioned compute server that is configured as close
54
as possibleto this this compute server.{" "}
55
<Alert
56
showIcon
57
style={{ margin: "15px" }}
58
type="warning"
59
message="The underlying disk is not copied."
60
/>
61
After cloning the compute server, you can edit anything about its
62
configuration before starting it.
63
</Modal>
64
);
65
}
66
67
export async function cloneConfiguration({
68
id,
69
noChange,
70
}: {
71
id: number;
72
noChange?: boolean;
73
}) {
74
const servers = await getServersById({ ids: [id] });
75
if (servers.length == 0) {
76
throw Error(`no such compute server ${id}`);
77
}
78
const server = servers[0] as ComputeServerUserInfo;
79
if (!noChange) {
80
let n = 1;
81
let title = `Clone of ${server.title}`;
82
const titles = new Set(servers.map((x) => x.title));
83
if (titles.has(title)) {
84
while (titles.has(title + ` (${n})`)) {
85
n += 1;
86
}
87
title = title + ` (${n})`;
88
}
89
server.title = title;
90
}
91
92
delete server.configuration.authToken;
93
return server;
94
}
95
96
async function createClone({ id }: { id: number }) {
97
const server = await cloneConfiguration({ id });
98
await createServer({ ...server });
99
}
100
101