Skip to content

Commit

Permalink
fix: Add network type support and refactor network operations
Browse files Browse the repository at this point in the history
- Add NetworkType to network-related commands and services
- Refactor network naming conventions throughout the codebase
- Implement cluster deletion functionality
- Update explorer, backup, and network command handlers
- Modify Kubernetes runner and infrastructure interfaces
- Adjust default network configuration and Kubernetes types
  • Loading branch information
a7351220 committed Sep 24, 2024
1 parent 57a57d3 commit de83e93
Show file tree
Hide file tree
Showing 26 changed files with 169 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/eth/command/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argv } from 'yargs'

export const command = 'backup'

export const desc = '管理 Eth backup 的指令'
export const desc = '管理 Eth Network backup 的指令'

export const builder = (yargs: Argv) => {
return yargs.commandDir('backup').demandCommand()
Expand Down
2 changes: 1 addition & 1 deletion src/eth/command/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argv } from 'yargs'

export const command = 'cluster'

export const desc = '管理 Eth cluster 的指令'
export const desc = '管理 Eth Network cluster 的指令'

export const builder = (yargs: Argv) => {
return yargs.commandDir('cluster').demandCommand()
Expand Down
15 changes: 8 additions & 7 deletions src/eth/command/cluster/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import ora from 'ora'
import { getNetworkTypeChoices, NetworkType } from '../../config/network.type'
export const command = 'apply'

export const desc = '產生 Network Cluster 所需的相關設定檔案並建立網路'
export const desc = '產生 Eth Network Cluster 所需的相關設定檔案並建立網路'

interface OptType {
interactive: boolean
}

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk quorum cluster apply --interactive', 'Cathay BDK 互動式問答')
.example('bdk eth network cluster apply --interactive', 'Cathay BDK 互動式問答')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
}
export const handler = async (argv: Arguments<OptType>) => {
Expand All @@ -33,6 +33,7 @@ export const handler = async (argv: Arguments<OptType>) => {
},
]) as { networkType: NetworkType }

const networkTypeWithBigFirstLetter = networkType.charAt(0).toUpperCase() + networkType.slice(1)
const cluster = new Cluster(config, networkType)
const wallet = new Wallet()

Expand All @@ -42,11 +43,11 @@ export const handler = async (argv: Arguments<OptType>) => {
const confirmDelete = (await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ Detecting cluster already exists. The following processes will remove all existing files. Continue?',
message: `⚠️ Detecting ${networkTypeWithBigFirstLetter} cluster already exists. The following processes will remove all existing files. Continue?`,
initial: false,
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora(`${networkType} Cluster Delete ...`).start()
const spinner = ora(`${networkTypeWithBigFirstLetter} Cluster Delete ...`).start()
cluster.removeHelmChartFiles()
spinner.succeed('Remove all existing files!')
}
Expand Down Expand Up @@ -168,12 +169,12 @@ export const handler = async (argv: Arguments<OptType>) => {
return { provider, region, chainId, validatorNumber, memberNumber, alloc, isBootNode, bootNodeList, networkType }
} else {
const { address, privateKey } = wallet.createWalletAddress(WalletType.ETHEREUM)
const config = defaultNetworkConfig(address, privateKey)
const config = defaultNetworkConfig(address, privateKey, networkType)
return { ...config, provider: 'local', networkType }
}
})()
const spinner = ora(`${networkType} Cluster Apply ...`).start()
const spinner = ora(`${networkTypeWithBigFirstLetter} Cluster Apply ...`).start()
await cluster.apply(clusterCreate, spinner)
spinner.succeed(`${networkType} Cluster Apply Successfully!`)
spinner.succeed(`${networkTypeWithBigFirstLetter} Cluster Apply Successfully!`)
}
}
28 changes: 16 additions & 12 deletions src/eth/command/cluster/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,50 @@ import Cluster from '../../service/cluster'
import { onCancel } from '../../../util/error'
import prompts from 'prompts'
import ora from 'ora'
import { getNetworkTypeChoices, NetworkType } from '../../config/network.type'

export const command = 'delete'

export const desc = '刪除現有的 Quorum Cluster 網路'
export const desc = '刪除現有的 Eth Network Cluster 網路'

interface OptType {
interactive: boolean
}

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk quorum cluster delete', 'Cathay BDK 互動式問答')
.example('bdk eth network cluster delete', 'Cathay BDK 互動式問答')
}

export const handler = async () => {
const cluster = new Cluster(config, 'quorum')
const { networkType } = await prompts([{
type: 'select',
name: 'networkType',
message: 'What is your network?',
choices: getNetworkTypeChoices(),
}]) as { networkType: NetworkType }

const networkTypeWithBigFirstLetter = networkType.charAt(0).toUpperCase() + networkType.slice(1)
const cluster = new Cluster(config, networkType)
const confirm: boolean = await (async () => {
const fileList = cluster.getHelmChartFiles()
if (fileList.length !== 0) {
const confirmDelete = (await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ Detecting quorum cluster already exists. The following processes will remove all existing files. Continue?',
message: `⚠️ Detecting ${networkTypeWithBigFirstLetter} cluster already exists. The following processes will remove all existing files. Continue?`,
initial: false,
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora('Quorum Cluster Create ...').start()
cluster.removeHelmChartFiles()
spinner.succeed('Remove all existing files!')
}
return confirmDelete
} else {
return true
}
})()

if (confirm) {
const spinner = ora('Deployments Under Namespace Quorum Delete ...').start()
await cluster.delete()
spinner.succeed('Quorum Cluster Delete Successfully!')
const spinner = ora(`${networkTypeWithBigFirstLetter} Cluster Delete ...`).start()
await cluster.delete(networkType)
cluster.removeHelmChartFiles()
spinner.succeed(`${networkTypeWithBigFirstLetter} Cluster Delete Successfully!`)
}
}
16 changes: 9 additions & 7 deletions src/eth/command/cluster/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import { getNetworkTypeChoices, NetworkType } from '../../config/network.type'

export const command = 'generate'

export const desc = '產生 Network Cluster 所需的相關設定檔案'
export const desc = '產生 Eth Network Cluster 所需的相關設定檔案'

interface OptType {
interactive: boolean
}

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk network cluster generate --interactive', 'Cathay BDK 互動式問答')
.example('bdk eth network cluster generate --interactive', 'Cathay BDK 互動式問答')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
}

Expand All @@ -34,6 +34,8 @@ export const handler = async (argv: Arguments<OptType>) => {
choices: getNetworkTypeChoices(),
},
]) as { networkType: NetworkType }

const networkTypeWithBigFirstLetter = networkType.charAt(0).toUpperCase() + networkType.slice(1)
const cluster = new Cluster(config, networkType)
const wallet = new Wallet()

Expand All @@ -43,11 +45,11 @@ export const handler = async (argv: Arguments<OptType>) => {
const confirmDelete = (await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ Detecting cluster already exists. The following processes will remove all existing files. Continue?',
message: `⚠️ Detecting ${networkTypeWithBigFirstLetter} cluster already exists. The following processes will remove all existing files. Continue?`,
initial: false,
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora(`${networkType} Cluster Delete ...`).start()
const spinner = ora(`${networkTypeWithBigFirstLetter} Cluster Delete ...`).start()
cluster.removeHelmChartFiles()
spinner.succeed('Remove all existing files!')
}
Expand Down Expand Up @@ -195,13 +197,13 @@ export const handler = async (argv: Arguments<OptType>) => {
return { provider, region, chainId, validatorNumber, memberNumber, alloc, isBootNode, bootNodeList, networkType }
} else {
const { address, privateKey } = wallet.createWalletAddress(WalletType.ETHEREUM)
const config = defaultNetworkConfig(address, privateKey)
const config = defaultNetworkConfig(address, privateKey, networkType)
return { ...config, provider: 'local', networkType }
}
})()

const spinner = ora(`${networkType} Cluster Generate ...`).start()
const spinner = ora(`${networkTypeWithBigFirstLetter} Cluster Generate ...`).start()
await cluster.generate(clusterGenerate, networkCreate)
spinner.succeed(`${networkType} Cluster Generate Successfully!`)
spinner.succeed(`${networkTypeWithBigFirstLetter} Cluster Generate Successfully!`)
}
}
2 changes: 1 addition & 1 deletion src/eth/command/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argv } from 'yargs'

export const command = 'explorer'

export const desc = '管理 Eth explorer 的指令'
export const desc = '管理 Eth Network explorer 的指令'

export const builder = (yargs: Argv) => {
return yargs.commandDir('explorer').demandCommand()
Expand Down
17 changes: 12 additions & 5 deletions src/eth/command/explorer/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import prompts from 'prompts'
import ora from 'ora'
import { ExplorerCreateType } from '../../model/type/explorer.type'
import Backup from '../../service/backup'
import { getNetworkTypeChoices } from '../../config/network.type'

export const command = 'create'

Expand All @@ -22,9 +23,15 @@ export const builder = (yargs: Argv<OptType>) => {
}

export const handler = async (argv: Arguments<OptType>) => {
const explorer = new Explorer(config, 'quorum')
const backup = new Backup(config, 'quorum')

const { networkType } = await prompts([{
type: 'select',
name: 'networkType',
message: 'What is yours network?',
choices: getNetworkTypeChoices(),
}])
const networkTypeWithBigFirstLetter = networkType.charAt(0).toUpperCase() + networkType.slice(1)
const explorer = new Explorer(config, networkType)
const backup = new Backup(config, networkType)
const getBackupItems = backup.getExportItems()
const explorerCreate: ExplorerCreateType = await (async () => {
if (argv.interactive) {
Expand Down Expand Up @@ -70,7 +77,7 @@ export const handler = async (argv: Arguments<OptType>) => {
}
})()

const spinner = ora('Quorum Explorer Create ...').start()
const spinner = ora(`${networkTypeWithBigFirstLetter} Explorer Create ...`).start()
await explorer.create(explorerCreate)
spinner.succeed(`Quorum Explorer Create Successfully! Host at: http://localhost:${explorerCreate.port}`)
spinner.succeed(`${networkTypeWithBigFirstLetter} Explorer Create Successfully! Host at: http://localhost:${explorerCreate.port}`)
}
20 changes: 15 additions & 5 deletions src/eth/command/explorer/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ import Explorer from '../../service/explorer'
import { onCancel } from '../../../util'
import prompts from 'prompts'
import ora from 'ora'
import { getNetworkTypeChoices } from '../../config/network.type'

export const command = 'delete'

export const desc = '刪除現有的 Quorum Explorer.'
export const desc = '刪除現有的 Eth Network Explorer.'

export const builder = {}

export const handler = async () => {
const explorer = new Explorer(config, 'quorum')
const { networkType } = await prompts([
{
type: 'select',
name: 'networkType',
message: 'What is your network?',
choices: getNetworkTypeChoices(),
},
])
const networkTypeWithBigFirstLetter = networkType.charAt(0).toUpperCase() + networkType.slice(1)
const explorer = new Explorer(config, networkType)

let confirmDelete = true

const response = await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ The following processes will remove all explorer files. Confirm to delete Quorum Explorer?',
message: `⚠️ The following processes will remove all explorer files. Confirm to delete ${networkTypeWithBigFirstLetter} Explorer?`,
initial: false,
}, { onCancel })

confirmDelete = response.value

if (confirmDelete) {
const spinner = ora('Quorum Explorer Delete ...').start()
const spinner = ora(`${networkType} Explorer Delete ...`).start()
await explorer.delete()
spinner.succeed('Quorum Explorer Delete Successfully!')
spinner.succeed(`${networkTypeWithBigFirstLetter} Explorer Delete Successfully!`)
}
}
2 changes: 1 addition & 1 deletion src/eth/command/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argv } from 'yargs'

export const command = 'network'

export const desc = '管理 Eth network 的指令'
export const desc = '管理 Eth Network network 的指令'

export const builder = (yargs: Argv) => {
return yargs.commandDir('network').demandCommand()
Expand Down
8 changes: 4 additions & 4 deletions src/eth/command/network/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { getNetworkTypeChoices } from '../../config/network.type'

export const command = 'add'

export const desc = '新增 Quorum Node'
export const desc = '新增 Eth Network Node'

interface OptType {
interactive: boolean
}

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk quorum network add --interactive', 'Cathay BDK 互動式問答')
.example('bdk eth network add --interactive', 'Cathay BDK 互動式問答')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
}

Expand Down Expand Up @@ -64,11 +64,11 @@ export const handler = async (argv: Arguments) => {
if (connectOption === 'local') {
if (nodeOption === 'validator') {
const spinner = ora(`${networkTypeWithBigFirstLetter} Network Add ...`).start()
const validatorNum = await network.addValidatorLocal()
const validatorNum = await network.addValidatorLocal(networkType)
spinner.succeed(`${networkTypeWithBigFirstLetter} Network Add Validator${validatorNum} Successfully!`)
} else {
const spinner = ora(`${networkTypeWithBigFirstLetter} Network Add ...`).start()
const memberNum = await network.addMemberLocal()
const memberNum = await network.addMemberLocal(networkType)
spinner.succeed(`${networkTypeWithBigFirstLetter} Network Add Member${memberNum} Successfully!`)
}
} else if (connectOption === 'remote') {
Expand Down
24 changes: 18 additions & 6 deletions src/eth/command/network/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ import prompts from 'prompts'
import Network from '../../service/network'
import { onCancel, ParamsError, ProcessError } from '../../../util/error'
import ora from 'ora'
import { getNetworkTypeChoices } from '../../config/network.type'

export const command = 'check'

export const desc = '確認 Quorum Node 資訊'
export const desc = '確認 Eth Network Node 資訊'

interface OptType {
interactive: boolean
}

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk quorum network check --interactive', 'Cathay BDK 互動式問答')
.example('bdk eth network check --interactive', 'Cathay BDK 互動式問答')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
}

export const handler = async (argv: Arguments<OptType>) => {
const network = new Network(config, 'quorum')
const { networkType } = await prompts([
{
type: 'select',
name: 'networkType',
message: 'What is your network?',
choices: getNetworkTypeChoices().map(choice => ({
title: choice.title,
value: choice.title,
})),
},
])
const network = new Network(config, networkType)

if (argv.interactive) {
const node: string = await (async () => {
Expand Down Expand Up @@ -53,10 +65,10 @@ export const handler = async (argv: Arguments<OptType>) => {
},
], { onCancel })

const spinner = ora('Quorum Network Check ...').start()
const spinner = ora(`${networkType} Network Check ...`).start()
const result = await network.checkNode(node, method)
spinner.succeed(`Quorum Network Check Result: ${result}`)
spinner.succeed('Quorum Network Check Successfully!')
spinner.succeed(`${networkType} Network Check Result: ${result}`)
spinner.succeed(`${networkType} Network Check Successfully!`)
} else {
throw new ParamsError('Invalid params: Required parameter missing')
}
Expand Down
2 changes: 1 addition & 1 deletion src/eth/command/network/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const handler = async (argv: Arguments<OptType>) => {
return { networkType, chainId, validatorNumber, memberNumber, alloc, isBootNode, bootNodeList }
} else {
const { address, privateKey } = wallet.createWalletAddress(WalletType.ETHEREUM)
return defaultNetworkConfig(address, privateKey)
return defaultNetworkConfig(address, privateKey, networkType)
}
}
)()
Expand Down
2 changes: 1 addition & 1 deletion src/eth/command/network/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const handler = async () => {
const response = await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ The following processes will remove all network files. Confirm to delete Quorum Network?',
message: `⚠️ The following processes will remove all network files. Confirm to delete ${networkTypeWithBigFirstLetter} Network?`,
initial: false,
}, { onCancel })

Expand Down
Loading

0 comments on commit de83e93

Please sign in to comment.