-
Notifications
You must be signed in to change notification settings - Fork 7
/
zos.ts
110 lines (96 loc) · 3.94 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
import { RMB } from "../clients/rmb/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { Operations, TwinDeployment } from "../high_level/models";
import { TwinDeploymentHandler } from "../high_level/twinDeploymentHandler";
import { DeploymentFactory } from "../primitives/deployment";
import { Nodes } from "../primitives/nodes";
import { WorkloadTypes } from "../zos/workload";
import { PingNodeOptionsModel, ZOSGetDeploymentModel, ZOSModel, ZOSNodeModel } from "./models";
import { checkBalance } from "./utils";
class Zos {
rmb: RMB;
capacity: Nodes;
constructor(public config: GridClientConfig) {
this.rmb = new RMB(config.rmbClient);
this.capacity = new Nodes(this.config.graphqlURL, this.config.proxyURL, this.config.rmbClient);
}
@expose
@validateInput
@checkBalance
async deploy(options: ZOSModel) {
// 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);
const twinDeploymentHandler = new TwinDeploymentHandler(this.config);
return await twinDeploymentHandler.handle([twinDeployment]);
}
@expose
@validateInput
async pingNode(options: PingNodeOptionsModel): Promise<boolean> {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.system.version", "", 20, 1);
}
@expose
@validateInput
async getDeployment(options: ZOSGetDeploymentModel) {
const nodeId = await this.capacity.getNodeIdFromContractId(options.contractId, this.config.mnemonic);
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);
}
@expose
@validateInput
async getNodeStatistics(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.statistics.get", "");
}
@expose
@validateInput
async hasPublicIPv6(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.has_ipv6", "");
}
@expose
@validateInput
async listNetworkInterfaces(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.interfaces", "");
}
@expose
@validateInput
async listNetworkPublicIPs(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.list_public_ips", "");
}
@expose
@validateInput
async getNetworkPublicConfig(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.network.public_config_get", "");
}
@expose
@validateInput
async getStoragePools(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.storage.pools", "");
}
@expose
@validateInput
async getNodeGPUInfo(options: ZOSNodeModel) {
const nodeTwinId = await this.capacity.getNodeTwinId(options.nodeId);
return await this.rmb.request([nodeTwinId], "zos.gpu.list", "");
}
}
export { Zos as zos };