-
Notifications
You must be signed in to change notification settings - Fork 8
/
zos.ts
302 lines (282 loc) · 11.5 KB
/
zos.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
import { RMB } from "../clients/rmb/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { DeploymentResultContracts, Operations, TwinDeployment } from "../high_level/models";
import { TwinDeploymentHandler } from "../high_level/twinDeploymentHandler";
import { DeploymentFactory } from "../primitives/deployment";
import { Nodes } from "../primitives/nodes";
import { Deployment } from "../zos";
import { WorkloadTypes } from "../zos/workload";
import {
GPUCardInfo,
NodeCPUTest,
NodeHealthCheck,
NodeIPerf,
NodeIPValidation,
PingNodeOptionsModel,
ZOSGetDeploymentModel,
ZOSModel,
ZOSNetworkInterfaces,
ZOSNetworkPublicConfig,
ZOSNodeModel,
ZOSNodePerfTestsResult,
ZOSNodeStatistics,
ZOSStoragePools,
ZOSVersionResultModel,
} from "./models";
import { checkBalance } from "./utils";
class Zos {
rmb: RMB;
capacity: Nodes;
/**
* Class representing the ZOS functionality.
*
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.rmb = new RMB(config.rmbClient);
this.capacity = new Nodes(this.config.graphqlURL, this.config.proxyURL, this.config.rmbClient);
}
// Helper function to filter the test by name
private filterTest<T>(response: T[], testName: string): T | undefined {
return response.find((obj: T) => (obj as any).name === testName);
}
/**
* Deploy a deployment on a node.
*
* @param {ZOSModel} options - The options for the deployment.
* @returns {Promise<DeploymentResultContracts>} - The result of the deployment.
* @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: ZOSModel): Promise<DeploymentResultContracts> {
// get node_id from the deployment
const node_id = options.node_id;
delete options.node_id;
const deploymentFactory = new DeploymentFactory(this.config);
const deployment = await deploymentFactory.fromObj(options);
let publicIps = 0;
for (const workload of deployment.workloads) {
if (workload.type === WorkloadTypes.ip && workload.data["v4"]) {
publicIps++;
}
}
console.log(`Deploying on node_id: ${node_id} with number of public IPs: ${publicIps}`);
const twinDeployment = new TwinDeployment(deployment, Operations.deploy, publicIps, node_id, deployment.metadata);
const twinDeploymentHandler = new TwinDeploymentHandler(this.config);
return await twinDeploymentHandler.handle([twinDeployment]);
}
/**
* Ping a node to check its availability.
*
* @param {PingNodeOptionsModel} options - The options for pinging the node.
* @returns {Promise<boolean>} - A boolean indicating if the node is reachable.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async pingNode(options: PingNodeOptionsModel): Promise<ZOSVersionResultModel> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.system.version", "", 10, 1);
}
/**
* Get deployment information based on the contract ID.
*
* @param {ZOSGetDeploymentModel} options - The options for getting deployment information.
* @returns {Promise<Deployment>} - A promise that resolves with the deployment information.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getDeployment(options: ZOSGetDeploymentModel): Promise<Deployment> {
const nodeId = await this.capacity.getNodeIdFromContractId(options.contractId, this.config.substrateURL);
const nodeTwinId = await this.capacity.getNodeTwinId(nodeId);
const payload = JSON.stringify({ contract_id: options.contractId });
return await this.rmb.request([nodeTwinId], "zos.deployment.get", payload);
}
/**
* Get statistics for a specific node.
*
* @param {ZOSNodeModel} options - The options for getting node statistics.
* @returns {Promise<ZOSNodeStatistics>} - A promise that resolves with the node statistics.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeStatistics(options: ZOSNodeModel): Promise<ZOSNodeStatistics> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.statistics.get", "");
}
/**
* Check if a node has a public IPv6 address.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating if the node has a public IPv6 address.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async hasPublicIPv6(options: ZOSNodeModel): Promise<boolean> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.has_ipv6", "");
}
/**
* List network interfaces for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<ZOSNetworkInterfaces>} - A promise that resolves with the network interfaces information.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async listNetworkInterfaces(options: ZOSNodeModel): Promise<ZOSNetworkInterfaces> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.interfaces", "");
}
/**
* List the public IP addresses associated with a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<string[]>} - A promise that resolves with the list of public IP addresses.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async listNetworkPublicIPs(options: ZOSNodeModel): Promise<string[]> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.list_public_ips", "");
}
/**
* Get the public network configuration for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<ZOSNetworkPublicConfig>} - A promise that resolves with the public network configuration.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNetworkPublicConfig(options: ZOSNodeModel): Promise<ZOSNetworkPublicConfig> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.public_config_get", "");
}
/**
* Get the storage pools information for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<ZOSStoragePools[]>} - A promise that resolves with the storage pools information.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getStoragePools(options: ZOSNodeModel): Promise<ZOSStoragePools[]> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.storage.pools", "");
}
/**
* Get GPU information for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<GPUCardInfo[]>} - A promise that resolves with an array of GPU card information.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeGPUInfo(options: ZOSNodeModel): Promise<GPUCardInfo[]> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.gpu.list", "");
}
/**
* Get performance tests for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<ZOSNodePerfTestsResult>} - A promise that resolves with the performance test results.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodePerfTests(options: ZOSNodeModel): Promise<ZOSNodePerfTestsResult> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
const result = new ZOSNodePerfTestsResult();
const response = await this.rmb.request([nodeTwinId], "zos.perf.get_all", "");
result.iperf = this.filterTest<NodeIPerf>(response, "iperf");
result.publicIPValidation = this.filterTest<NodeIPValidation>(response, "public-ip-validation");
result.healthCheck = this.filterTest<NodeHealthCheck>(response, "healthcheck");
result.cpuBenchmark = this.filterTest<NodeCPUTest>(response, "cpu-benchmark");
return result;
}
/**
* Get the IPerf test results for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<NodeIPerf>} - A promise that resolves with the IPerf test results.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeIPerfTest(options: ZOSNodeModel): Promise<NodeIPerf> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
const payload = JSON.stringify({ name: "iperf" });
return await this.rmb.request([nodeTwinId], "zos.perf.get", payload);
}
/**
* Get the IP validation test results for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<NodeIPValidation>} - A promise that resolves with the IP validation test results.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeIPValidation(options: ZOSNodeModel): Promise<NodeIPValidation> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
const payload = JSON.stringify({ name: "public-ip-validation" });
return await this.rmb.request([nodeTwinId], "zos.perf.get", payload);
}
/**
* Get CPU test results for a specific node.
*
* @param {ZOSNodeModel} options - The options containing the node ID.
* @returns {Promise<NodeCPUTest>} - A promise that resolves with the CPU test results.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeCPUTest(options: ZOSNodeModel): Promise<NodeCPUTest> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
const payload = JSON.stringify({ name: "cpu-benchmark" });
return await this.rmb.request([nodeTwinId], "zos.perf.get", payload);
}
}
export { Zos as zos };