Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a node to network #1197

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/grid_client/scripts/add_node_to_network.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions packages/grid_client/src/high_level/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TwinDeployment {
public nodeId: number,
public network: Network | null = null,
public solutionProviderId: number | null = null,
public returnNetworkContracts = false,
) {}
}

Expand Down
51 changes: 51 additions & 0 deletions packages/grid_client/src/high_level/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Addr } from "netaddr";

import { DeploymentFactory, Network } from "../primitives";
import { WorkloadTypes, Znet } 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) {
const data = workload.data as Znet;
if (workload.type !== WorkloadTypes.network || !Addr(network.ipRange).contains(Addr(data.subnet))) {
continue;
}
workload.data = network.updateNetwork(data);
workload.version += 1;
break;
}
twinDeployments.push(new TwinDeployment(d, Operations.update, 0, 0, network, solutionProviderId, true));
}
return twinDeployments;
}
}

export { NetworkHL };
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -769,6 +777,7 @@ export {
SetServiceContractFeesModel,
SetServiceContractMetadataModel,
GetServiceContractModel,
NetworkAddNodeModel,
NetworkGetModel,
NodeGetModel,
SetDedicatedNodeExtraFeesModel,
Expand Down
22 changes: 20 additions & 2 deletions packages/grid_client/src/modules/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,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"];
}
Expand Down
Loading