Skip to content

Commit

Permalink
added new task about commands that work with a single nodeAlias
Browse files Browse the repository at this point in the history
Signed-off-by: instamenta <[email protected]>
  • Loading branch information
instamenta committed Dec 3, 2024
1 parent b9ed15f commit 245fe26
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/commands/node/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class NodeCommandHandlers {
return [
this.tasks.initialize(argv, deleteConfigBuilder.bind(this), lease),
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
RemoteConfigTasks.validateAllNodeStates.bind(this)(
RemoteConfigTasks.validateSingleNodeState.bind(this)(
{ excludedStates: [] }),
this.tasks.identifyExistingNodes(),
this.tasks.loadAdminKey(),
Expand Down Expand Up @@ -152,7 +152,7 @@ export class NodeCommandHandlers {
return [
this.tasks.initialize(argv, addConfigBuilder.bind(this), lease),
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
RemoteConfigTasks.validateAllNodeStates.bind(this)(
RemoteConfigTasks.validateSingleNodeState.bind(this)(
{ excludedStates: [] }),
this.tasks.checkPVCsEnabled(),
this.tasks.identifyExistingNodes(),
Expand Down Expand Up @@ -206,7 +206,7 @@ export class NodeCommandHandlers {
return [
this.tasks.initialize(argv, updateConfigBuilder.bind(this), lease),
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
RemoteConfigTasks.validateAllNodeStates.bind(this)(
RemoteConfigTasks.validateSingleNodeState.bind(this)(
{ excludedStates: [] }),
this.tasks.identifyExistingNodes(),
this.tasks.loadAdminKey(),
Expand Down
107 changes: 77 additions & 30 deletions src/core/config/remote/remote_config_tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import type { NetworkCommand } from '../../../commands/network.js'
import type { DeploymentCommand } from '../../../commands/deployment.js'
import type { MirrorNodeCommand } from '../../../commands/mirror_node.js'
import type { NodeCommandHandlers } from '../../../commands/node/handlers.js'
import type { Optional } from '../../../types/index.js'
import { ComponentsDataWrapper } from './components_data_wrapper.js'

/**
* Static class that handles all tasks related to remote config used by other commands.
Expand Down Expand Up @@ -211,41 +213,17 @@ export class RemoteConfigTasks {
return {
title: 'Validate nodes states',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
task: async (ctx: Context, task): Promise<Listr<any, any, any>> => {
const { config: { namespace, nodeAliases } } = ctx
task: (ctx: Context, task): Listr<any, any, any> => {
const nodeAliases = ctx.config.nodeAliases

const components = this.remoteConfigManager.components

const subTasks: ListrTask<Context, any, any>[] = nodeAliases.map(nodeAlias => ({
title: `Validating state for node ${nodeAlias}`,
task: async (_, task): Promise<void> => {
let nodeComponent: ConsensusNodeComponent
try {
nodeComponent = components.getComponent<ConsensusNodeComponent>(
ComponentType.ConsensusNode,
nodeAlias
)
} catch (e) {
throw new SoloError(`${nodeAlias} not found in remote config for namespace ${namespace}`)
}

if (acceptedStates && !acceptedStates.includes(nodeComponent.state)) {
const errorMessageData =
`accepted states: ${acceptedStates.join(', ')}, ` +
`current state: ${nodeComponent.state}`

throw new SoloError(`${nodeAlias} has invalid state - ` + errorMessageData)
}

if (excludedStates && excludedStates.includes(nodeComponent.state)) {
const errorMessageData =
`excluded states: ${excludedStates.join(', ')}, ` +
`current state: ${nodeComponent.state}`

throw new SoloError(`${nodeAlias} has invalid state - ` + errorMessageData)
}

task.title += ` - ${chalk.green('valid state')}: ${chalk.cyan(nodeComponent.state)}`
task: (_, task): void => {
const state = RemoteConfigTasks.validateNodeState(nodeAlias, components, acceptedStates, excludedStates)

task.title += ` - ${chalk.green('valid state')}: ${chalk.cyan(state)}`
}
}))

Expand All @@ -256,4 +234,73 @@ export class RemoteConfigTasks {
}
}
}

/**
* Creates tasks to validate that specific node state is either one of the accepted states or not one of the excluded.
*
* @param acceptedStates - the state at which the node can be, not matching any of the states throws an error
* @param excludedStates - the state at which the node can't be, matching any of the states throws an error
*/
public static validateSingleNodeState (this: NodeCommandHandlers, { acceptedStates, excludedStates }
: { acceptedStates?: ConsensusNodeStates[], excludedStates?: ConsensusNodeStates[] }
): ListrTask<any, any, any> {
interface Context { config: { namespace: string, nodeAlias: NodeAlias } }

return {
title: 'Validate nodes state',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
task: (ctx: Context, task): void => {
const nodeAlias = ctx.config.nodeAlias

task.title += ` ${nodeAlias}`

const components = this.remoteConfigManager.components

const state = RemoteConfigTasks.validateNodeState(nodeAlias, components, acceptedStates, excludedStates)

task.title += ` - ${chalk.green('valid state')}: ${chalk.cyan(state)}`
}
}
}

/**
* @param nodeAlias - the alias of the node whose state to validate
* @param components - the component data wrapper
* @param acceptedStates - the state at which the node can be, not matching any of the states throws an error
* @param excludedStates - the state at which the node can't be, matching any of the states throws an error
*/
private static validateNodeState (
nodeAlias: NodeAlias,
components: ComponentsDataWrapper,
acceptedStates: Optional<ConsensusNodeStates[]>,
excludedStates: Optional<ConsensusNodeStates[]>,
): ConsensusNodeStates {
let nodeComponent: ConsensusNodeComponent
try {
nodeComponent = components.getComponent<ConsensusNodeComponent>(
ComponentType.ConsensusNode,
nodeAlias
)
} catch (e) {
throw new SoloError(`${nodeAlias} not found in remote config`)
}

if (acceptedStates && !acceptedStates.includes(nodeComponent.state)) {
const errorMessageData =
`accepted states: ${acceptedStates.join(', ')}, ` +
`current state: ${nodeComponent.state}`

throw new SoloError(`${nodeAlias} has invalid state - ` + errorMessageData)
}

if (excludedStates && excludedStates.includes(nodeComponent.state)) {
const errorMessageData =
`excluded states: ${excludedStates.join(', ')}, ` +
`current state: ${nodeComponent.state}`

throw new SoloError(`${nodeAlias} has invalid state - ` + errorMessageData)
}

return nodeComponent.state
}
}

0 comments on commit 245fe26

Please sign in to comment.