From e62ae1ac4c43cc423cbf395f53077a3771b3a71f Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 10:37:50 +0300 Subject: [PATCH 1/6] Add network high level --- packages/grid_client/src/high_level/models.ts | 1 + .../grid_client/src/high_level/network.ts | 53 +++++++++++++++++++ .../src/high_level/twinDeploymentHandler.ts | 2 + 3 files changed, 56 insertions(+) create mode 100644 packages/grid_client/src/high_level/network.ts diff --git a/packages/grid_client/src/high_level/models.ts b/packages/grid_client/src/high_level/models.ts index dba6f49986..ed6470a750 100644 --- a/packages/grid_client/src/high_level/models.ts +++ b/packages/grid_client/src/high_level/models.ts @@ -15,6 +15,7 @@ class TwinDeployment { public nodeId: number, public network: Network | null = null, public solutionProviderId: number | null = null, + public returnNetworkContracts = false, ) {} } diff --git a/packages/grid_client/src/high_level/network.ts b/packages/grid_client/src/high_level/network.ts new file mode 100644 index 0000000000..35b5e4c78c --- /dev/null +++ b/packages/grid_client/src/high_level/network.ts @@ -0,0 +1,53 @@ +import { Addr } from "netaddr"; + +import { DeploymentFactory, Network } from "../primitives"; +import { WorkloadTypes } from "../zos"; +import { HighLevelBase } from "./base"; +import { Operations, TwinDeployment } from "./models"; + +class NetworkHL extends HighLevelBase { + async addNode(networkName: string, ipRange: string, nodeId: number, solutionProviderId: number, description = "") { + const network = new Network(networkName, ipRange, this.config); + await network.load(); + const networkMetadata = JSON.stringify({ + type: "network", + name: networkName, + projectName: this.config.projectName, + }); + + const workload = await network.addNode(nodeId, networkMetadata, description); + if (!workload) { + throw Error(`Node ${nodeId} is already exist on network ${networkName}`); + } + + const twinDeployments: TwinDeployment[] = []; + const deploymentFactory = new DeploymentFactory(this.config); + const deployment = deploymentFactory.create([workload], 0, networkMetadata, description, 0); + twinDeployments.push( + new TwinDeployment(deployment, Operations.deploy, 0, nodeId, network, solutionProviderId, true), + ); + + if (!(await network.exists())) { + return twinDeployments; + } + // update network if it's already exist + for (const deployment of network.deployments) { + const d = await deploymentFactory.fromObj(deployment); + for (const workload of d["workloads"]) { + if ( + workload["type"] !== WorkloadTypes.network || + !Addr(network.ipRange).contains(Addr(workload["data"]["subnet"])) + ) { + continue; + } + workload.data = network.updateNetwork(workload["data"]); + workload.version += 1; + break; + } + twinDeployments.push(new TwinDeployment(d, Operations.update, 0, 0, network, solutionProviderId, true)); + } + return twinDeployments; + } +} + +export { NetworkHL }; diff --git a/packages/grid_client/src/high_level/twinDeploymentHandler.ts b/packages/grid_client/src/high_level/twinDeploymentHandler.ts index 0a00f9cd07..fb24b26108 100644 --- a/packages/grid_client/src/high_level/twinDeploymentHandler.ts +++ b/packages/grid_client/src/high_level/twinDeploymentHandler.ts @@ -515,6 +515,7 @@ class TwinDeploymentHandler { if (twinDeployment.deployment.challenge_hash() === contract.contractType.nodeContract.deploymentHash) { twinDeployment.deployment.contract_id = contract.contractId; if ( + twinDeployment.returnNetworkContracts || !( twinDeployment.deployment.workloads.length === 1 && twinDeployment.deployment.workloads[0].type === WorkloadTypes.network @@ -539,6 +540,7 @@ class TwinDeploymentHandler { if (twinDeployment.deployment.challenge_hash() === contract.contractType.nodeContract.deploymentHash) { twinDeployment.nodeId = contract.contractType.nodeContract.nodeId; if ( + twinDeployment.returnNetworkContracts || !( twinDeployment.deployment.workloads.length === 1 && twinDeployment.deployment.workloads[0].type === WorkloadTypes.network From 6c4fc0085c7310022792aa0bb57b856e54b4fe42 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 10:39:29 +0300 Subject: [PATCH 2/6] Support add node to network module --- packages/grid_client/src/modules/models.ts | 9 +++++++++ packages/grid_client/src/modules/networks.ts | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index fccfb67867..f2b74f009b 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -640,6 +640,14 @@ class pingFarmModel { @Expose() @IsInt() @IsNotEmpty() @Min(1) farmId: number; } +class NetworkAddNodeModel { + @Expose() @IsString() @IsNotEmpty() @IsAlphanumeric() @MaxLength(NameLength) name: string; + @Expose() @IsString() @IsNotEmpty() ipRange: string; + @Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number; + @Expose() @IsInt() @IsOptional() solutionProviderId?: number; + @Expose() @IsString() @IsOptional() description?: string; +} + class NetworkGetModel { @Expose() @IsString() @IsNotEmpty() @IsAlphanumeric() @MaxLength(NameLength) name: string; } @@ -769,6 +777,7 @@ export { SetServiceContractFeesModel, SetServiceContractMetadataModel, GetServiceContractModel, + NetworkAddNodeModel, NetworkGetModel, NodeGetModel, SetDedicatedNodeExtraFeesModel, diff --git a/packages/grid_client/src/modules/networks.ts b/packages/grid_client/src/modules/networks.ts index 433c22bfb1..6f09ff393a 100644 --- a/packages/grid_client/src/modules/networks.ts +++ b/packages/grid_client/src/modules/networks.ts @@ -3,14 +3,32 @@ import * as PATH from "path"; import { GridClientConfig } from "../config"; import { expose } from "../helpers/expose"; import { validateInput } from "../helpers/validator"; +import { NetworkHL } from "../high_level/network"; import { BaseModule } from "./base"; -import { NetworkGetModel } from "./models"; +import { NetworkAddNodeModel, NetworkGetModel } from "./models"; +import { checkBalance } from "./utils"; class NetworkModule extends BaseModule { moduleName = "networks"; + network: NetworkHL; constructor(public config: GridClientConfig) { super(config); + this.network = new NetworkHL(config); + } + + @expose + @validateInput + @checkBalance + async addNode(options: NetworkAddNodeModel) { + const twinDeployments = await this.network.addNode( + options.name, + options.ipRange, + options.nodeId, + options.solutionProviderId!, + options.description, + ); + return { contracts: await this.twinDeploymentHandler.handle(twinDeployments) }; } @expose From 04e89707bc239511f49d7d0f37a001c4f74f9af0 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 10:40:19 +0300 Subject: [PATCH 3/6] Add script for adding node to the network --- .../grid_client/scripts/add_node_to_network.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/grid_client/scripts/add_node_to_network.ts diff --git a/packages/grid_client/scripts/add_node_to_network.ts b/packages/grid_client/scripts/add_node_to_network.ts new file mode 100644 index 0000000000..670d1e87a2 --- /dev/null +++ b/packages/grid_client/scripts/add_node_to_network.ts @@ -0,0 +1,18 @@ +import { getClient } from "./client_loader"; +import { log } from "./utils"; + +async function main() { + const grid3 = await getClient(); + try { + // if the network is not created, it will create one and add this node to it. + const res = await grid3.networks.addNode({ + name: "wedtest", + ipRange: "10.249.0.0/16", + nodeId: 14, + }); + log(res); + } finally { + grid3.disconnect(); + } +} +main(); From a10ff57e371b12532a5303f0b773763989dce826 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 10:41:10 +0300 Subject: [PATCH 4/6] Rename get network config script --- .../scripts/{test_networks.ts => get_network_config.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/grid_client/scripts/{test_networks.ts => get_network_config.ts} (100%) diff --git a/packages/grid_client/scripts/test_networks.ts b/packages/grid_client/scripts/get_network_config.ts similarity index 100% rename from packages/grid_client/scripts/test_networks.ts rename to packages/grid_client/scripts/get_network_config.ts From c7491271863e02aed65ae6254e7e7bb2f5597636 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 11:20:32 +0300 Subject: [PATCH 5/6] Fix network path when getting network config --- packages/grid_client/src/modules/networks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/grid_client/src/modules/networks.ts b/packages/grid_client/src/modules/networks.ts index 6f09ff393a..a7aa908628 100644 --- a/packages/grid_client/src/modules/networks.ts +++ b/packages/grid_client/src/modules/networks.ts @@ -39,7 +39,7 @@ class NetworkModule extends BaseModule { @expose @validateInput async getWireGuardConfigs(options: NetworkGetModel) { - const path = PATH.join(this.getDeploymentPath(options.name), "info.json"); + const path = PATH.join(this.config.storePath, this.moduleName, options.name, "info.json"); const networkInfo = await this.backendStorage.load(path); return networkInfo["wireguardConfigs"]; } From b9fb8b873ec63c71895e35c4e91ab2b850d2a9a6 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 10 Oct 2023 11:27:25 +0300 Subject: [PATCH 6/6] Apply some formating --- packages/grid_client/src/high_level/network.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/grid_client/src/high_level/network.ts b/packages/grid_client/src/high_level/network.ts index 35b5e4c78c..ba5da767d3 100644 --- a/packages/grid_client/src/high_level/network.ts +++ b/packages/grid_client/src/high_level/network.ts @@ -1,7 +1,7 @@ import { Addr } from "netaddr"; import { DeploymentFactory, Network } from "../primitives"; -import { WorkloadTypes } from "../zos"; +import { WorkloadTypes, Znet } from "../zos"; import { HighLevelBase } from "./base"; import { Operations, TwinDeployment } from "./models"; @@ -33,14 +33,12 @@ class NetworkHL extends HighLevelBase { // update network if it's already exist for (const deployment of network.deployments) { const d = await deploymentFactory.fromObj(deployment); - for (const workload of d["workloads"]) { - if ( - workload["type"] !== WorkloadTypes.network || - !Addr(network.ipRange).contains(Addr(workload["data"]["subnet"])) - ) { + for (const workload of d.workloads) { + const data = workload.data as Znet; + if (workload.type !== WorkloadTypes.network || !Addr(network.ipRange).contains(Addr(data.subnet))) { continue; } - workload.data = network.updateNetwork(workload["data"]); + workload.data = network.updateNetwork(data); workload.version += 1; break; }