Skip to content

Commit

Permalink
wip: refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Ivo Yankov <[email protected]>
  • Loading branch information
Ivo-Yankov committed Sep 25, 2024
1 parent 3fd1c86 commit ffb029c
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 220 deletions.
7 changes: 2 additions & 5 deletions src/commands/account.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,10 @@ export class AccountCommand extends BaseCommand {

/**
* Return Yargs command definition for 'node' command
* @param {AccountCommand} accountCmd an instance of NodeCommand
* @returns {{command: string, desc: string, builder: Function}}
*/
static getCommandDefinition (accountCmd) {
if (!accountCmd || !(accountCmd instanceof AccountCommand)) {
throw new IllegalArgumentError('An instance of AccountCommand is required', accountCmd)
}
getCommandDefinition () {
const accountCmd = this
return {
command: 'account',
desc: 'Manage Hedera accounts in solo network',
Expand Down
7 changes: 2 additions & 5 deletions src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,10 @@ export class ClusterCommand extends BaseCommand {

/**
* Return Yargs command definition for 'cluster' command
* @param {ClusterCommand} clusterCmd - an instance of ClusterCommand
* @returns {{command: string, desc: string, builder: Function}}
*/
static getCommandDefinition (clusterCmd) {
if (!clusterCmd || !(clusterCmd instanceof ClusterCommand)) {
throw new IllegalArgumentError('Invalid ClusterCommand instance')
}
getCommandDefinition () {
const clusterCmd = this
return {
command: 'cluster',
desc: 'Manage fullstack testing cluster',
Expand Down
14 changes: 7 additions & 7 deletions src/commands/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ function Initialize (opts) {
const mirrorNodeCmd = new MirrorNodeCommand(opts)

return [
InitCommand.getCommandDefinition(initCmd),
ClusterCommand.getCommandDefinition(clusterCmd),
NetworkCommand.getCommandDefinition(networkCommand),
NodeCommand.getCommandDefinition(nodeCmd),
RelayCommand.getCommandDefinition(relayCmd),
AccountCommand.getCommandDefinition(accountCmd),
MirrorNodeCommand.getCommandDefinition(mirrorNodeCmd)
initCmd.getCommandDefinition(),
clusterCmd.getCommandDefinition(),
networkCommand.getCommandDefinition(),
nodeCmd.getCommandDefinition(),
relayCmd.getCommandDefinition(),
accountCmd.getCommandDefinition(),
mirrorNodeCmd.getCommandDefinition()
]
}

Expand Down
7 changes: 2 additions & 5 deletions src/commands/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,10 @@ export class InitCommand extends BaseCommand {

/**
* Return Yargs command definition for 'init' command
* @param {InitCommand} initCmd - an instance of InitCommand
* @returns A object representing the Yargs command definition
*/
static getCommandDefinition (initCmd) {
if (!initCmd || !(initCmd instanceof InitCommand)) {
throw new IllegalArgumentError('Invalid InitCommand')
}
getCommandDefinition () {
const initCmd = this
return {
command: 'init',
desc: 'Initialize local environment and default flags',
Expand Down
7 changes: 2 additions & 5 deletions src/commands/mirror_node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,10 @@ export class MirrorNodeCommand extends BaseCommand {

/**
* Return Yargs command definition for 'mirror-mirror-node' command
* @param {MirrorNodeCommand} mirrorNodeCmd an instance of MirrorNodeCommand
* @returns {{command: string, desc: string, builder: Function}}
*/
static getCommandDefinition (mirrorNodeCmd) {
if (!mirrorNodeCmd || !(mirrorNodeCmd instanceof MirrorNodeCommand)) {
throw new IllegalArgumentError('Invalid MirrorNodeCommand instance', mirrorNodeCmd)
}
getCommandDefinition () {
const mirrorNodeCmd = this
return {
command: 'mirror-node',
desc: 'Manage Hedera Mirror Node in solo network',
Expand Down
7 changes: 2 additions & 5 deletions src/commands/network.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,10 @@ export class NetworkCommand extends BaseCommand {
}

/**
* @param {NetworkCommand} networkCmd
* @returns {{command: string, desc: string, builder: Function}}
*/
static getCommandDefinition (networkCmd) {
if (!networkCmd || !(networkCmd instanceof NetworkCommand)) {
throw new IllegalArgumentError('An instance of NetworkCommand is required', networkCmd)
}
getCommandDefinition () {
const networkCmd = this
return {
command: 'network',
desc: 'Manage solo network deployment',
Expand Down
135 changes: 58 additions & 77 deletions src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import path from 'path'
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
import * as helpers from '../core/helpers.mjs'
import {
addDebugOptions, commandActionBuilder, commandBuilder,
addDebugOptions,
getNodeAccountMap,
getNodeLogs,
renameAndCopyFile,
sleep,
validatePath
} from '../core/helpers.mjs'
import { constants, Templates, Zippy } from '../core/index.mjs'
import { constants, Templates, Zippy, YargsCommand } from '../core/index.mjs'
import { BaseCommand } from './base.mjs'
import * as flags from './flags.mjs'
import * as prompts from './prompts.mjs'
Expand Down Expand Up @@ -59,7 +59,7 @@ import {
LOCAL_HOST
} from '../core/constants.mjs'
import { NodeStatusCodes, NodeStatusEnums } from '../core/enumerations.mjs'
import {NodeCommandTasks} from './node/tasks.mjs'
import { NodeCommandTasks } from './node/tasks.mjs'

/**
* Defines the core functionalities of 'node' command
Expand Down Expand Up @@ -90,9 +90,12 @@ export class NodeCommand extends BaseCommand {
this._portForwards = []

this.tasks = new NodeCommandTasks({
accountManager: opts.accountManager,
logger: opts.logger,
accountManager: opts.accountManager
k8: opts.k8
})

this.commandNamespace = 'node'
}

/**
Expand Down Expand Up @@ -768,32 +771,6 @@ export class NodeCommand extends BaseCommand {
return this.taskCheckNetworkNodePods(ctx, task, config.existingNodeIds)
}

/**
* Download generated config files and key files from the network node
* @param config
*/
async downloadNodeGeneratedFiles (config) {
const node1FullyQualifiedPodName = Templates.renderNetworkPodName(config.existingNodeIds[0])

// copy the config.txt file from the node1 upgrade directory
await this.k8.copyFrom(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, `${constants.HEDERA_HAPI_PATH}/data/upgrade/current/config.txt`, config.stagingDir)

// if directory data/upgrade/current/data/keys does not exist then use data/upgrade/current
let keyDir = `${constants.HEDERA_HAPI_PATH}/data/upgrade/current/data/keys`
if (!await this.k8.hasDir(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, keyDir)) {
keyDir = `${constants.HEDERA_HAPI_PATH}/data/upgrade/current`
}
const signedKeyFiles = (await this.k8.listDir(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, keyDir)).filter(file => file.name.startsWith(constants.SIGNING_KEY_PREFIX))
await this.k8.execContainer(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, ['bash', '-c', `mkdir -p ${constants.HEDERA_HAPI_PATH}/data/keys_backup && cp -r ${keyDir} ${constants.HEDERA_HAPI_PATH}/data/keys_backup/`])
for (const signedKeyFile of signedKeyFiles) {
await this.k8.copyFrom(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, `${keyDir}/${signedKeyFile.name}`, `${config.keysDir}`)
}

if (await this.k8.hasFile(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, `${constants.HEDERA_HAPI_PATH}/data/upgrade/current/application.properties`)) {
await this.k8.copyFrom(node1FullyQualifiedPodName, constants.ROOT_CONTAINER, `${constants.HEDERA_HAPI_PATH}/data/upgrade/current/application.properties`, `${config.stagingDir}/templates`)
}
}

async initializeSetup (config, k8) {
// compute other config parameters
config.keysDir = path.join(validatePath(config.cacheDir), 'keys')
Expand Down Expand Up @@ -1993,13 +1970,7 @@ export class NodeCommand extends BaseCommand {
const self = this

return [
{
title: 'Download generated files from an existing node',
task: async (ctx, task) => {
const config = /** @type {NodeAddConfigClass} **/ ctx.config
await this.downloadNodeGeneratedFiles(config)
}
},
this.tasks.downloadNodeGeneratedFiles(),
{
title: 'Prepare staging directory',
task: async (ctx, parentTask) => {
Expand Down Expand Up @@ -2254,16 +2225,37 @@ export class NodeCommand extends BaseCommand {
return true
}

async prepareUpgrade (argv) {
const action = helpers.commandActionBuilder([
this.tasks.sendPrepareUpgradeTransaction()
], {
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION
}, 'preparing node upgrade')

await action(argv, this)
}

async prepareUpgrade(argv) {
async freezeUpgrade (argv) {
const action = helpers.commandActionBuilder([
this.tasks.sendPrepareUpgradeTransaction()
], {
this.tasks.sendFreezeUpgradeTransaction()
], {
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION
}, 'preparing node upgrade').bind(this)
}, 'executing node freeze upgrade')

await action(argv)
await action(argv, this)
}

async downloadGeneratedFiles (argv) {
const action = helpers.commandActionBuilder([
this.tasks.downloadNodeGeneratedFiles()
], {
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION
}, 'downloading generated files')

await action(argv, this)
}

async enableJVMPortForwarding (nodeId) {
Expand Down Expand Up @@ -2292,13 +2284,10 @@ export class NodeCommand extends BaseCommand {
// Command Definition
/**
* Return Yargs command definition for 'node' command
* @param {NodeCommand} nodeCmd - an instance of NodeCommand
* @returns {{command: string, desc: string, builder: Function}}
*/
static getCommandDefinition (nodeCmd) {
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) {
throw new IllegalArgumentError('An instance of NodeCommand is required', nodeCmd)
}
getCommandDefinition () {
const nodeCmd = this
return {
command: 'node',
desc: 'Manage Hedera platform node in solo network',
Expand Down Expand Up @@ -2518,23 +2507,27 @@ export class NodeCommand extends BaseCommand {
})
}
})
.command({
.command(new YargsCommand({
command: 'prepare-upgrade',
desc: 'TODO',
builder: y => flags.setCommandFlags(y, ...NodeCommand.DELETE_FLAGS_LIST),
handler: argv => {
nodeCmd.logger.debug('==== Running \'node prepare-upgrade\' ===')
nodeCmd.logger.debug(argv)

nodeCmd.prepareUpgrade(argv).then(r => {
nodeCmd.logger.debug('==== Finished running `node prepare-upgrade`====')
if (!r) process.exit(1)
}).catch(err => {
nodeCmd.logger.showUserError(err)
process.exit(1)
})
}
})
description: 'Prepare the network for a Freeze Upgrade operation',
flags: [],
commandDef: nodeCmd,
handler: 'prepareUpgrade'
}))
.command(new YargsCommand({
command: 'freeze-upgrade',
description: 'Performs a Freeze Upgrade operation with on the network after it has been prepared with prepare-upgrade',
flags: [],
commandDef: nodeCmd,
handler: 'freezeUpgrade'
}))
.command(new YargsCommand({
command: 'download-generated-files',
description: 'Downloads the generated files from an existing node',
flags: [],
commandDef: nodeCmd,
handler: 'downloadGeneratedFiles'
}))
.demandCommand(1, 'Select a node command')
}
}
Expand Down Expand Up @@ -2787,13 +2780,7 @@ export class NodeCommand extends BaseCommand {
}
},
this.tasks.sendPrepareUpgradeTransaction(),
{
title: 'Download generated files from an existing node',
task: async (ctx, task) => {
const config = /** @type {NodeUpdateConfigClass} **/ ctx.config
await this.downloadNodeGeneratedFiles(config)
}
},
this.tasks.downloadNodeGeneratedFiles(),
this.tasks.sendFreezeUpgradeTransaction(),
{
title: 'Prepare staging directory',
Expand Down Expand Up @@ -3087,13 +3074,7 @@ export class NodeCommand extends BaseCommand {
}
},
this.tasks.sendPrepareUpgradeTransaction(),
{
title: 'Download generated files from an existing node',
task: async (ctx, task) => {
const config = /** @type {NodeDeleteConfigClass} **/ ctx.config
await this.downloadNodeGeneratedFiles(config)
}
},
this.tasks.downloadNodeGeneratedFiles(),
this.tasks.sendFreezeUpgradeTransaction(),
{
title: 'Prepare staging directory',
Expand Down
Loading

0 comments on commit ffb029c

Please sign in to comment.