-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #894 from paritytech/refacto/native-resources
Refacto/native resources
- Loading branch information
Showing
5 changed files
with
251 additions
and
132 deletions.
There are no files selected for viewing
139 changes: 7 additions & 132 deletions
139
javascript/packages/orchestrator/src/providers/native/dynResourceDefinition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
javascript/packages/orchestrator/src/providers/native/resources/bootnode.resource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { makeDir } from "@zombienet/utils"; | ||
import { genCmd } from "../../../cmdGenerator"; | ||
import { NodeResource } from "./node.resource"; | ||
import { NodeSpec, Port, ProcessEnvironment, ZombieRole } from "./types"; | ||
|
||
export class BootNodeResource extends NodeResource { | ||
protected async createDirectories() { | ||
try { | ||
await makeDir(this.configPath, true); | ||
await makeDir(this.dataPath, true); | ||
} catch { | ||
throw new Error( | ||
`Error generating directories for ${this.nodeSetupConfig.name} resource`, | ||
); | ||
} | ||
} | ||
|
||
protected generateCommand() { | ||
return genCmd(this.nodeSetupConfig, this.configPath, this.dataPath, false); | ||
} | ||
|
||
protected getZombieRole(): ZombieRole { | ||
return "bootnode"; | ||
} | ||
|
||
protected generateNodeSpec( | ||
ports: Port[], | ||
command: string[], | ||
zombieRole: ZombieRole, | ||
env: ProcessEnvironment, | ||
): NodeSpec { | ||
return { | ||
metadata: { | ||
name: "bootnode", | ||
namespace: this.namespace, | ||
labels: { | ||
name: this.namespace, | ||
instance: "bootnode", | ||
"zombie-role": zombieRole, | ||
app: "zombienet", | ||
"zombie-ns": this.namespace, | ||
}, | ||
}, | ||
spec: { | ||
cfgPath: this.configPath, | ||
ports, | ||
command, | ||
env, | ||
}, | ||
}; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
javascript/packages/orchestrator/src/providers/native/resources/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { BootNodeResource } from "./bootnode.resource"; | ||
export { NodeResource } from "./node.resource"; |
153 changes: 153 additions & 0 deletions
153
javascript/packages/orchestrator/src/providers/native/resources/node.resource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { getRandomPort, makeDir } from "@zombienet/utils"; | ||
import { genCmd, genCumulusCollatorCmd } from "../../../cmdGenerator"; | ||
import { | ||
P2P_PORT, | ||
PROMETHEUS_PORT, | ||
RPC_HTTP_PORT, | ||
RPC_WS_PORT, | ||
} from "../../../constants"; | ||
import { Node, envVars } from "../../../types"; | ||
import { Client } from "../../client"; | ||
import { | ||
NodeSpec, | ||
Port, | ||
PortProperty, | ||
ProcessEnvironment, | ||
ZombieRole, | ||
} from "./types"; | ||
|
||
export class NodeResource { | ||
protected readonly configPath: string; | ||
protected readonly dataPath: string; | ||
private readonly relayDataPath: string; | ||
|
||
constructor( | ||
client: Client, | ||
protected readonly namespace: string, | ||
protected readonly nodeSetupConfig: Node, | ||
) { | ||
const nodeRootPath = `${client.tmpDir}/${this.nodeSetupConfig.name}`; | ||
this.configPath = `${nodeRootPath}/cfg`; | ||
this.dataPath = `${nodeRootPath}/data`; | ||
this.relayDataPath = `${nodeRootPath}/relay-data`; | ||
} | ||
|
||
public async generateSpec() { | ||
await this.createDirectories(); | ||
const ports = await this.generatePorts(); | ||
const command = await this.generateCommand(); | ||
const zombieRole = this.getZombieRole(); | ||
const env = this.getEnv(); | ||
const nodeManifest = this.generateNodeSpec(ports, command, zombieRole, env); | ||
|
||
return nodeManifest; | ||
} | ||
|
||
protected async createDirectories() { | ||
try { | ||
await makeDir(this.configPath, true); | ||
await makeDir(this.dataPath, true); | ||
await makeDir(this.relayDataPath, true); | ||
} catch { | ||
throw new Error( | ||
`Error generating directories for ${this.nodeSetupConfig.name} resource`, | ||
); | ||
} | ||
} | ||
|
||
private async portFromNodeSetupConfigOrDefault(portProperty: PortProperty) { | ||
if (this.nodeSetupConfig[portProperty]) { | ||
return this.nodeSetupConfig[portProperty]; | ||
} | ||
|
||
return getRandomPort(); | ||
} | ||
|
||
private async generatePorts(): Promise<Port[]> { | ||
return [ | ||
{ | ||
containerPort: PROMETHEUS_PORT, | ||
name: "prometheus", | ||
flag: "--prometheus-port", | ||
hostPort: await this.portFromNodeSetupConfigOrDefault("prometheusPort"), | ||
}, | ||
{ | ||
containerPort: RPC_HTTP_PORT, | ||
name: "rpc", | ||
flag: "--rpc-port", | ||
hostPort: await this.portFromNodeSetupConfigOrDefault("rpcPort"), | ||
}, | ||
{ | ||
containerPort: RPC_WS_PORT, | ||
name: "rpc-ws", | ||
flag: "--ws-port", | ||
hostPort: await this.portFromNodeSetupConfigOrDefault("wsPort"), | ||
}, | ||
{ | ||
containerPort: P2P_PORT, | ||
name: "p2p", | ||
flag: "--port", | ||
hostPort: await this.portFromNodeSetupConfigOrDefault("p2pPort"), | ||
}, | ||
]; | ||
} | ||
|
||
protected generateCommand() { | ||
if (this.nodeSetupConfig.zombieRole === "cumulus-collator") { | ||
return genCumulusCollatorCmd( | ||
this.nodeSetupConfig, | ||
this.configPath, | ||
this.dataPath, | ||
this.relayDataPath, | ||
false, | ||
); | ||
} | ||
|
||
return genCmd(this.nodeSetupConfig, this.configPath, this.dataPath, false); | ||
} | ||
|
||
protected getZombieRole(): ZombieRole { | ||
const { zombieRole, validator } = this.nodeSetupConfig; | ||
|
||
if (zombieRole) return zombieRole; | ||
|
||
return validator ? "authority" : "full-node"; | ||
} | ||
|
||
protected getEnv() { | ||
const { env } = this.nodeSetupConfig; | ||
|
||
return env.reduce((memo, item: envVars) => { | ||
memo[item.name] = item.value; | ||
return memo; | ||
}, {} as ProcessEnvironment); | ||
} | ||
|
||
protected generateNodeSpec( | ||
ports: Port[], | ||
command: string[], | ||
zombieRole: ZombieRole, | ||
env: ProcessEnvironment, | ||
): NodeSpec { | ||
return { | ||
metadata: { | ||
name: this.nodeSetupConfig.name, | ||
namespace: this.namespace, | ||
labels: { | ||
"zombie-role": zombieRole, | ||
app: "zombienet", | ||
"zombie-ns": this.namespace, | ||
name: this.namespace, | ||
instance: this.nodeSetupConfig.name, | ||
}, | ||
}, | ||
spec: { | ||
cfgPath: this.configPath, | ||
dataPath: this.dataPath, | ||
ports, | ||
command, | ||
env, | ||
}, | ||
}; | ||
} | ||
} |
Oops, something went wrong.