Skip to content

Commit

Permalink
feat(bdk): bdk quorum cluster delete network
Browse files Browse the repository at this point in the history
  • Loading branch information
kidneyweakx committed Mar 5, 2024
1 parent b9588e2 commit a1cb771
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 15 deletions.
50 changes: 50 additions & 0 deletions src/quorum/command/cluster/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Argv } from 'yargs'
import config from '../../config'
import Cluster from '../../service/cluster'
import { onCancel } from '../../../util/error'
import prompts from 'prompts'
import ora from 'ora'

export const command = 'delete'

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

interface OptType {
interactive: boolean
}

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

export const handler = async () => {
const cluster = new Cluster(config)

const confirm: boolean = await (async () => {
const fileList = cluster.getHelmChartFiles()
if (fileList.length !== 0) {
const confirmDelete = (await prompts({
type: 'confirm',
name: 'value',
message: '⚠️ Detecting quorum nodes already exists. The following processes will remove all existing files. Continue?',
initial: false,
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora('Quorum Network 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!')
}
}
5 changes: 3 additions & 2 deletions src/quorum/instance/infra/InfraRunner.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DockerRunCommandType } from '../../model/type/docker.type'
import { K8SRunCommandType } from '../../model/type/kubernetes.type'
import { K8SRunCommandType, ClusterDeleteType } from '../../model/type/kubernetes.type'
/**
* Infra return type when direct use docker
*/
Expand Down Expand Up @@ -33,7 +33,8 @@ export interface KubernetesInfraRunner<T> {
createDeploymentAndService(payload: K8SRunCommandType): Promise<T>
createTemplate(payload: K8SRunCommandType): Promise<T>
wait(job: string, namespace: string): Promise<T>
deleteDeploymentAndService(payload: K8SRunCommandType): Promise<T>
deleteDeploymentAndService(payload: ClusterDeleteType): Promise<T>
listAllRelease(namespace: string): Promise<T>
}

// Strategy
Expand Down
21 changes: 16 additions & 5 deletions src/quorum/instance/infra/kubernetes/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { logger } from '../../../../util/logger'
import { spawn } from 'child_process'
import config from '../../../config'
import { DockerResultType, KubernetesInfraRunner } from '../InfraRunner.interface'
import { K8SRunCommandType } from '../../../model/type/kubernetes.type'
import { ClusterDeleteType, K8SRunCommandType } from '../../../model/type/kubernetes.type'

export class Runner implements KubernetesInfraRunner<DockerResultType> {
public createDeploymentAndService = async (payload: K8SRunCommandType): Promise<DockerResultType> => {
Expand All @@ -26,14 +26,25 @@ export class Runner implements KubernetesInfraRunner<DockerResultType> {
return { stdout: helmOutput }
}

public wait = async (job: string, namespace: string): Promise<DockerResultType> => {
const k8sOutput = await this.runKubectl(['wait', '--for=condition=complete', job, '-n', namespace, '--timeout=600s'])
public wait = async (job: string, namespace: string, timeoutSecond = 300): Promise<DockerResultType> => {
const k8sOutput = await this.runKubectl([
'wait',
'--for=condition=complete',
job,
'-n',
namespace,
`--timeout=${timeoutSecond.toString()}s`])
return { stdout: k8sOutput }
}

public deleteDeploymentAndService = async (payload: K8SRunCommandType): Promise<DockerResultType> => {
public listAllRelease = async (namespace: string): Promise<DockerResultType> => {
const helmOutput = await this.runHelm(['list', '--short', '--namespace', namespace])
return { stdout: helmOutput }
}

public deleteDeploymentAndService = async (payload: ClusterDeleteType): Promise<DockerResultType> => {
await this.runHelm(
['delete',
['uninstall',
payload.name,
'--namespace', payload.namespace])
return { stdout: '' }
Expand Down
16 changes: 15 additions & 1 deletion src/quorum/instance/kubernetesCluster.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractInstance } from './Instance.abstract'
import { logger } from '../../util'
import { K8SRunCommandType } from '../model/type/kubernetes.type'
import { K8SRunCommandType, ClusterDeleteType } from '../model/type/kubernetes.type'

export default class KubernetesInstance extends AbstractInstance {
public async install (payload: K8SRunCommandType) {
Expand All @@ -17,6 +17,20 @@ export default class KubernetesInstance extends AbstractInstance {
}
}

public async delete (payload: ClusterDeleteType) {
logger.debug('Kubernetes instance delete')
if (this.kubernetesInfra !== undefined) {
return await this.kubernetesInfra.deleteDeploymentAndService(payload)
}
}

public async listAllRelease (namespace: string) {
logger.debug('Kubernetes instance listAllRelease')
if (this.kubernetesInfra !== undefined) {
return await this.kubernetesInfra.listAllRelease(namespace)
}
}

public async wait (job: string, namespace: string) {
logger.debug('Kubernetes instance wait')
if (this.kubernetesInfra !== undefined) {
Expand Down
4 changes: 4 additions & 0 deletions src/quorum/model/type/kubernetes.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface K8SRunCommandType {
ignoreError?: boolean
}

export interface ClusterDeleteType {
name: string
namespace: string
}
export interface ClusterCreateType extends NetworkCreateType {
provider: string
region?: string
Expand Down
2 changes: 1 addition & 1 deletion src/quorum/model/yaml/helm-chart/genesisYaml.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HelmChartYaml from './helmChartYaml'

class GenesisConfigYaml extends HelmChartYaml {
public setGenesis (chainID: number, nodeCount: number, alloc: any[]) {
public setGenesis (chainID: number, nodeCount: number) {
this.setQuorumFlags({
privacy: false,
removeKeysOnDelete: false,
Expand Down
2 changes: 0 additions & 2 deletions src/quorum/model/yaml/kubernetes/ingressYaml.ts

This file was deleted.

25 changes: 21 additions & 4 deletions src/quorum/service/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export default class Cluster extends AbstractService {
* @description Use helm create quorum template
*/
public async apply (networkCreateConfig: ClusterCreateType, spinner: Ora): Promise<void> {
const { provider, region, chainId, validatorNumber, memberNumber, alloc } = networkCreateConfig
const { provider, region, chainId, validatorNumber, memberNumber } = networkCreateConfig
// create genesis and account
const k8s = new KubernetesInstance(this.config, this.infra, this.kubernetesInfra)
this.bdkFile.checkHelmChartPath()
const genesisYaml = new GenesisConfigYaml()
genesisYaml.setProvider(provider, region)
genesisYaml.setGenesis(chainId, validatorNumber, alloc)
genesisYaml.setGenesis(chainId, validatorNumber)

this.bdkFile.createGenesisChartValues(genesisYaml)
// custom namespace
Expand Down Expand Up @@ -74,12 +74,12 @@ export default class Cluster extends AbstractService {
clusterGenerateConfig: ClusterGenerateType,
networkCreateConfig: ClusterCreateType,
): Promise<void> {
const { provider, region, chainId, validatorNumber, memberNumber, alloc } = networkCreateConfig
const { provider, region, chainId, validatorNumber, memberNumber } = networkCreateConfig
this.bdkFile.checkHelmChartPath()
// create genesis and account
const genesisYaml = new GenesisConfigYaml()
genesisYaml.setProvider(provider, region)
genesisYaml.setGenesis(chainId, validatorNumber, alloc)
genesisYaml.setGenesis(chainId, validatorNumber)

this.bdkFile.createGenesisChartValues(genesisYaml)

Expand Down Expand Up @@ -110,6 +110,23 @@ export default class Cluster extends AbstractService {
this.exportChartTar()
}

/**
* @description Delete all quorum deployment and service
*/
public async delete (): Promise<void> {
const k8s = new KubernetesInstance(this.config, this.infra, this.kubernetesInfra)
const releases = await this.getAllHelmRelease()
releases.forEach(async (release: string) => {
await k8s.delete({ name: release, namespace: 'quorum' })
})
}

private async getAllHelmRelease () {
const k8s = new KubernetesInstance(this.config, this.infra, this.kubernetesInfra)
const releases = await k8s.listAllRelease('quorum') as DockerResultType
return releases.stdout.split('\n').slice(1)
}

public getHelmChartFiles (): string[] {
return this.bdkFile.getHelmChartValuesFiles()
}
Expand Down

0 comments on commit a1cb771

Please sign in to comment.