-
Notifications
You must be signed in to change notification settings - Fork 8
/
machines.ts
411 lines (392 loc) · 14.7 KB
/
machines.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { GridClientErrors, ValidationError } from "@threefold/types";
import { Addr } from "netaddr";
import { GridClientConfig } from "../config";
import { events } from "../helpers/events";
import { expose } from "../helpers/expose";
import { ZmachineData } from "../helpers/types";
import { validateInput } from "../helpers/validator";
import { VMHL } from "../high_level/machine";
import { DeploymentResultContracts, TwinDeployment } from "../high_level/models";
import { Network } from "../primitives/network";
import { Deployment } from "../zos";
import { WorkloadTypes } from "../zos/workload";
import { BaseModule } from "./base";
import { AddMachineModel, DeleteMachineModel, MachinesDeleteModel, MachinesGetModel, MachinesModel } from "./models";
import { checkBalance } from "./utils";
class MachinesModule extends BaseModule {
moduleName = "machines";
workloadTypes = [
WorkloadTypes.zmachine,
WorkloadTypes.zmount,
WorkloadTypes.volume,
WorkloadTypes.qsfs,
WorkloadTypes.ip,
WorkloadTypes.ipv4,
WorkloadTypes.zlogs,
]; // TODO: remove deprecated
vm: VMHL;
/**
* The MachinesModule class is responsible for managing virtual machine deployments.
* It extends the BaseModule class and provides methods to deploy, list, get, update, add, and delete machines.
*
* Initializes the VMHL instance with the provided configuration.
*
* @param {GridClientConfig} config - The configuration object for the GridClient.
*/
constructor(public config: GridClientConfig) {
super(config);
this.vm = new VMHL(config);
}
/**
* Creates a deployment for a set of machines.
*
* @param {MachinesModel} options - The options for creating the machine deployment.
* @returns {Promise<[TwinDeployment[], Network, string]>} - A promise that resolves to an array of twin deployments, the network, and the WireGuard configuration string.
*/
async _createDeployment(options: MachinesModel): Promise<[TwinDeployment[], Network, string]> {
const network = new Network(options.network.name, options.network.ip_range, this.config);
await network.load();
let twinDeployments: TwinDeployment[] = [];
let wireguardConfig = "";
const contractMetadata = JSON.stringify({
version: 3,
type: "vm",
name: options.name,
projectName: this.config.projectName || `vm/${options.name}`,
});
const machines_names: string[] = [];
for (const machine of options.machines) {
if (machines_names.includes(machine.name))
throw new ValidationError(`Another machine with the same name ${machine.name} already exists.`);
machines_names.push(machine.name);
const [TDeployments, wgConfig] = await this.vm.create(
machine.name,
machine.node_id,
machine.flist,
machine.cpu,
machine.memory,
machine.rootfs_size,
machine.disks!,
machine.public_ip,
machine.public_ip6!,
machine.planetary,
machine.mycelium,
machine.myceliumSeed!,
network,
options.network.myceliumSeeds!,
machine.entrypoint,
machine.env,
contractMetadata,
options.metadata,
options.description,
machine.qsfs_disks,
this.config.projectName,
options.network.addAccess,
options.network.accessNodeId,
machine.ip,
machine.corex,
machine.solutionProviderId!,
machine.zlogsOutput,
machine.gpus,
);
twinDeployments = twinDeployments.concat(TDeployments);
if (wgConfig) {
wireguardConfig = wgConfig;
}
}
return [twinDeployments, network, wireguardConfig];
}
/**
* Deploys a machine or a set of machines.
*
* @param {MachinesModel} options - The options for deploying the machine(s).
* @returns {Promise<{ contracts: any; wireguard_config: string }>} - A promise that resolves to the deployment contracts and WireGuard configuration.
*
* @example
*
* const networkOptions = {
* name: "wedtest",
* ip_range: "10.249.0.0/16",
* };
*
* const options: MachinesModel = {
* name: "test",
* network: networkOptions,
* machines: [...],
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.deploy(options);
* console.log(result.contracts);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance before proceeding.
*/
@expose
@validateInput
@checkBalance
async deploy(options: MachinesModel): Promise<{ contracts: any; wireguard_config: string }> {
if (await this.exists(options.name)) {
throw new ValidationError(`Another machine deployment with the same name ${options.name} already exists.`);
}
events.emit("logs", `Start creating the machine deployment with name ${options.name}`);
const [twinDeployments, , wireguardConfig] = await this._createDeployment(options);
const contracts = await this.twinDeploymentHandler.handle(twinDeployments);
await this.save(options.name, contracts);
return { contracts: contracts, wireguard_config: wireguardConfig };
}
/**
* Lists all machine deployments.
*
* @returns {Promise<string[]>} - A promise that resolves to a list of the names of machine contracts.
*
* @example
*
* const machineModel = new MachinesModule()
* const result = await machineModel.list();
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
*/
@expose
async list(): Promise<string[]> {
return await this._list();
}
/**
* Retrieves the object representation of a machine deployment.
*
* @param {string} deploymentName - The name of the deployment to retrieve.
* @returns {Promise<ZmachineData[]>} - A promise that resolves to the object representation of the machine deployment.
*
* @example
*
* const machineModel = new MachinesModule()
* const result = await machineModel.getObj("testName");
* console.log(result);
*
*/
async getObj(deploymentName: string): Promise<ZmachineData[]> {
const deployments = await this._get(deploymentName);
const workloads = await this._getWorkloadsByTypes(deploymentName, deployments, [WorkloadTypes.zmachine]);
const promises = workloads.map(
async workload => await this._getZmachineData(deploymentName, deployments, workload),
);
return await Promise.all(promises);
}
/**
* Retrieves a specific machine deployment by name.
*
* @param {MachinesGetModel} options - The options containing the name of the deployment to retrieve.
* @returns {Promise<Deployment[]>} - A promise that resolves to an array of the machine deployments.
*
* @example
*
* const options: MachinesGetModel = {
* name: "test",
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.get(options);
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get(options: MachinesGetModel): Promise<Deployment[]> {
return await this._get(options.name);
}
/**
* Method to delete a machine deployment after validating input and checking balance.
* Emits a log event before deleting the machine deployment.
*
* @param options The options for deleting the machine deployment.
* @returns A promise that resolves to an object with information about created, deleted, and updated deployments.
*
* @example
*
* const options: MachinesDeleteModel = {
* name: "test",
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.delete(options);
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance before proceeding.
*/
@expose
@validateInput
@checkBalance
async delete(options: MachinesDeleteModel) {
events.emit("logs", `Start deleting the machine deployment with name ${options.name}`);
return await this._delete(options.name);
}
/**
* Method to update a machine deployment after validating input and checking balance.
*
* @throws {ValidationError} - Throws a ValidationError if the machine with the provided name does not exist.
* @throws {WorkloadUpdateError} - Throws a WorkloadUpdateError if trying to change the network name or IP range.
*
* @param {MachinesModel} options - The options for updating the machine deployment.
* @returns {Promise<{ contracts: DeploymentResultContracts }>} - A promise that resolves after updating the machine deployment.
*
* @example
*
* const options: MachinesModel = {
* name: "test",
* network: networkOptions,
* machines: [...],
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.update(options);
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance before proceeding.
*/
@expose
@validateInput
@checkBalance
async update(options: MachinesModel): Promise<{ contracts: DeploymentResultContracts }> {
if (!(await this.exists(options.name))) {
throw new ValidationError(`There is no machine with the name: ${options.name}`);
}
const oldDeployments = await this._get(options.name);
const workload = (await this._getWorkloadsByTypes(options.name, oldDeployments, [WorkloadTypes.zmachine]))[0];
const networkName = workload.data["network"].interfaces[0].network;
const networkIpRange = Addr(workload.data["network"].interfaces[0].ip).mask(16).toString();
if (networkName !== options.network.name || networkIpRange !== options.network.ip_range) {
throw new GridClientErrors.Workloads.WorkloadUpdateError("Network name and ip_range can't be changed.");
}
const [twinDeployments, network] = await this._createDeployment(options);
return await this._update(this.vm, options.name, oldDeployments, twinDeployments, network);
}
/**
* Method to add a new machine to an existing deployment after validating input and checking balance.
* Emits a log event before adding the machine to the deployment.
*
* @param {AddMachineModel} options - The options for adding the machine to the deployment.
* @returns {Promise<DeploymentResultContracts>} - A promise that resolves to an object with information about created, deleted, and updated deployments.
*
* @example
*
* const options: AddMachineModel = {
* deployment_name: "deploymentName",
* myceliumNetworkSeed: "seedValue",
* // Add other required options here
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.add_machine(options);
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance before proceeding.
*/
@expose
@validateInput
@checkBalance
async add_machine(options: AddMachineModel): Promise<{ contracts: DeploymentResultContracts }> {
if (!(await this.exists(options.deployment_name))) {
throw new ValidationError(`There are no machine deployments with the name: ${options.deployment_name}.`);
}
const oldDeployments = await this._get(options.deployment_name);
if (this.workloadExists(options.name, oldDeployments))
throw new ValidationError(
`There is another machine with the same name "${options.name}" in the same deployment ${options.deployment_name}.`,
);
events.emit("logs", `Start adding machine: ${options.name} to deployment: ${options.deployment_name}`);
const workload = (
await this._getWorkloadsByTypes(options.deployment_name, oldDeployments, [WorkloadTypes.zmachine])
)[0];
const networkName = workload.data["network"].interfaces[0].network;
const networkIpRange = Addr(workload.data["network"].interfaces[0].ip).mask(16).toString();
const network = new Network(networkName, networkIpRange, this.config);
await network.load();
const contractMetadata = JSON.stringify({
version: 3,
type: "vm",
name: options.deployment_name,
projectName: this.config.projectName || `vm/${options.name}`,
});
const [twinDeployments] = await this.vm.create(
options.name,
options.node_id,
options.flist,
options.cpu,
options.memory,
options.rootfs_size,
options.disks!,
options.public_ip,
options.public_ip6!,
options.planetary,
options.mycelium,
options.myceliumSeed!,
network,
[{ nodeId: options.node_id, seed: options.myceliumNetworkSeed! }],
options.entrypoint,
options.env,
contractMetadata,
workload.metadata,
workload.description,
options.qsfs_disks,
this.config.projectName,
false,
0,
options.ip,
options.corex,
options.solutionProviderId!,
options.zlogsOutput,
options.gpus,
);
return await this._add(options.deployment_name, options.node_id, oldDeployments, twinDeployments, network);
}
/**
* Method to delete a machine deployment after validating input and checking balance.
* Emits a log event before deleting the machine deployment.
*
* @param {DeleteMachineModel} options - The options for deleting the machine deployment.
* @returns {Promise<DeploymentResultContracts>} - A promise that resolves to an object with information about created, deleted, and updated deployments.
*
* @example
*
* const options: DeleteMachineModel = {
* name: "test",
* deployment_name: "deploymentName",
* };
*
* const machineModel = new MachinesModule()
* const result = await machineModel.delete_machine(options);
* console.log(result);
*
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance before proceeding.
*/
@expose
@validateInput
@checkBalance
async delete_machine(options: DeleteMachineModel): Promise<DeploymentResultContracts> {
if (!(await this.exists(options.deployment_name))) {
throw new ValidationError(`There are no machine deployments with the name: ${options.deployment_name}.`);
}
events.emit("logs", `Start deleting machine: ${options.name} from deployment: ${options.deployment_name}`);
return await this._deleteInstance(this.vm, options.deployment_name, options.name);
}
}
export { MachinesModule as machines };