Skip to content

Commit

Permalink
feat(cli): use prompts and progress stats for relay command (#598)
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
  • Loading branch information
leninmehedy authored Dec 5, 2023
1 parent 4eedfc1 commit 2974c8f
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 149 deletions.
5 changes: 1 addition & 4 deletions fullstack-network-manager/src/commands/base.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'
import { MissingArgumentError } from '../core/errors.mjs'
import { ShellRunner } from '../core/shell_runner.mjs'
import * as flags from './flags.mjs'

export class BaseCommand extends ShellRunner {
async prepareChartPath (config, chartRepo, chartName) {
if (!config) throw new MissingArgumentError('config is required')
async prepareChartPath (chartDir, chartRepo, chartName) {
if (!chartRepo) throw new MissingArgumentError('chart repo name is required')
if (!chartName) throw new MissingArgumentError('chart name is required')

const chartDir = this.configManager.flagValue(config, flags.chartDirectory)
if (chartDir) {
const chartPath = `${chartDir}/${chartName}`
await this.helm.dependency('update', chartPath)
Expand Down
176 changes: 137 additions & 39 deletions fullstack-network-manager/src/commands/chart.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import chalk from 'chalk'
import { Listr } from 'listr2'
import { FullstackTestingError } from '../core/errors.mjs'
import { BaseCommand } from './base.mjs'
import * as flags from './flags.mjs'
import * as paths from 'path'
import { constants } from '../core/index.mjs'
import * as prompts from './prompts.mjs'

export class ChartCommand extends BaseCommand {
prepareValuesFiles (valuesFile) {
Expand All @@ -19,13 +20,8 @@ export class ChartCommand extends BaseCommand {
return valuesArg
}

prepareValuesArg (config) {
const valuesFile = this.configManager.flagValue(config, flags.valuesFile)
const deployMirrorNode = this.configManager.flagValue(config, flags.deployMirrorNode)
const deployHederaExplorer = this.configManager.flagValue(config, flags.deployHederaExplorer)

prepareValuesArg (chartDir, valuesFile, deployMirrorNode, deployHederaExplorer) {
let valuesArg = ''
const chartDir = this.configManager.flagValue(config, flags.chartDirectory)
if (chartDir) {
valuesArg = `-f ${chartDir}/fullstack-deployment/values.yaml`
}
Expand All @@ -37,56 +33,149 @@ export class ChartCommand extends BaseCommand {
return valuesArg
}

async installFSTChart (config) {
try {
const namespace = this.configManager.flagValue(config, flags.namespace)
const valuesArg = this.prepareValuesArg(config)
const chartPath = await this.prepareChartPath(config, constants.CHART_FST_REPO_NAME, constants.CHART_FST_DEPLOYMENT_NAME)

await this.chartManager.install(namespace, constants.CHART_FST_DEPLOYMENT_NAME, chartPath, config.version, valuesArg)

this.logger.showUser(chalk.cyan('> waiting for network-node pods to be active (first deployment takes ~10m) ...'))
await this.kubectl.wait('pod',
'--for=jsonpath=\'{.status.phase}\'=Running',
'-l fullstack.hedera.com/type=network-node',
'--timeout=900s'
)
this.logger.showUser(chalk.green('OK'), 'network-node pods are running')
} catch (e) {
throw new FullstackTestingError(`failed install '${constants.CHART_FST_DEPLOYMENT_NAME}' chart`, e)
async prepareConfig (task, argv) {
const cachedConfig = await this.configManager.setupConfig(argv)
const namespace = this.configManager.flagValue(cachedConfig, flags.namespace)
const chartDir = this.configManager.flagValue(cachedConfig, flags.chartDirectory)
const valuesFile = this.configManager.flagValue(cachedConfig, flags.valuesFile)
const deployMirrorNode = this.configManager.flagValue(cachedConfig, flags.deployMirrorNode)
const deployExplorer = this.configManager.flagValue(cachedConfig, flags.deployHederaExplorer)

// prompt if values are missing and create a config object
const config = {
namespace: await prompts.promptNamespaceArg(task, namespace),
chartDir: await prompts.promptChartDir(task, chartDir),
valuesFile: await prompts.promptChartDir(task, valuesFile),
deployMirrorNode: await prompts.promptDeployMirrorNode(task, deployMirrorNode),
deployHederaExplorer: await prompts.promptDeployHederaExplorer(task, deployExplorer),
timeout: '900s',
version: cachedConfig.version
}

// compute values
config.chartPath = await this.prepareChartPath(config.chartDir,
constants.CHART_FST_REPO_NAME, constants.CHART_FST_DEPLOYMENT_NAME)

config.valuesArg = this.prepareValuesArg(config.chartDir,
config.valuesFile, config.deployMirrorNode, config.deployHederaExplorer)

return config
}

async install (argv) {
try {
const config = await this.configManager.setupConfig(argv)
const namespace = this.configManager.flagValue(config, flags.namespace)
const self = this

await this.installFSTChart(config)
const tasks = new Listr([
{
title: 'Initialize',
task: async (ctx, task) => {
ctx.config = await self.prepareConfig(task, argv)
}
},
{
title: `Install chart '${constants.CHART_FST_DEPLOYMENT_NAME}'`,
task: async (ctx, _) => {
await this.chartManager.install(
ctx.config.namespace,
constants.CHART_FST_DEPLOYMENT_NAME,
ctx.config.chartPath,
ctx.config.version,
ctx.config.valuesArg)
}
},
{
title: 'Waiting for network pods to be ready',
task: async (ctx, _) => {
const timeout = ctx.config.timeout || '900s'
await this.kubectl.wait('pod',
'--for=jsonpath=\'{.status.phase}\'=Running',
'-l fullstack.hedera.com/type=network-node',
`--timeout=${timeout}`
)
}
}
])

this.logger.showList('Deployed Charts', await this.chartManager.getInstalledCharts(namespace))
return true
try {
await tasks.run()
} catch (e) {
this.logger.showUserError(e)
throw new FullstackTestingError(`Error installing chart ${constants.CHART_FST_DEPLOYMENT_NAME}`, e)
}

return false
return true
}

async uninstall (argv) {
const namespace = argv.namespace
const self = this

return await this.chartManager.uninstall(namespace, constants.CHART_FST_DEPLOYMENT_NAME)
const tasks = new Listr([
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
ctx.config = {
namespace: await prompts.promptNamespaceArg(task, namespace)
}
}
},
{
title: `Uninstall chart ${constants.CHART_FST_DEPLOYMENT_NAME}`,
task: async (ctx, _) => {
await self.chartManager.uninstall(ctx.config.namespace, constants.CHART_FST_DEPLOYMENT_NAME)
}
}
])

try {
await tasks.run()
} catch (e) {
throw new FullstackTestingError('Error starting node', e)
}

return true
}

async upgrade (argv) {
const namespace = argv.namespace
const self = this

const config = await this.configManager.setupConfig(argv)
const valuesArg = this.prepareValuesArg(argv, config)
const chartPath = await this.prepareChartPath(config)
const tasks = new Listr([
{
title: 'Initialize',
task: async (ctx, task) => {
ctx.config = await self.prepareConfig(task, argv)
}
},
{
title: `Upgrade chart '${constants.CHART_FST_DEPLOYMENT_NAME}'`,
task: async (ctx, _) => {
await this.chartManager.upgrade(
ctx.config.namespace,
constants.CHART_FST_DEPLOYMENT_NAME,
ctx.config.chartPath,
ctx.config.valuesArg)
}
},
{
title: 'Waiting for network pods to be ready',
task: async (ctx, _) => {
const timeout = ctx.config.timeout || '900s'
await this.kubectl.wait('pod',
'--for=jsonpath=\'{.status.phase}\'=Running',
'-l fullstack.hedera.com/type=network-node',
`--timeout=${timeout}`
)
}
}
])

try {
await tasks.run()
} catch (e) {
throw new FullstackTestingError(`Error upgrading chart ${constants.CHART_FST_DEPLOYMENT_NAME}`, e)
}

return await this.chartManager.upgrade(namespace, constants.CHART_FST_DEPLOYMENT_NAME, chartPath, valuesArg)
return true
}

static getCommandDefinition (chartCmd) {
Expand Down Expand Up @@ -116,6 +205,9 @@ export class ChartCommand extends BaseCommand {
chartCmd.logger.debug('==== Finished running `chart install`====')

if (!r) process.exit(1)
}).catch(err => {
chartCmd.logger.showUserError(err)
process.exit(1)
})
}
})
Expand All @@ -131,6 +223,9 @@ export class ChartCommand extends BaseCommand {
chartCmd.logger.debug('==== Finished running `chart uninstall`====')

if (!r) process.exit(1)
}).catch(err => {
chartCmd.logger.showUserError(err)
process.exit(1)
})
}
})
Expand All @@ -152,6 +247,9 @@ export class ChartCommand extends BaseCommand {
chartCmd.logger.debug('==== Finished running `chart upgrade`====')

if (!r) process.exit(1)
}).catch(err => {
chartCmd.logger.showUserError(err)
process.exit(1)
})
}
})
Expand Down
Loading

0 comments on commit 2974c8f

Please sign in to comment.