Skip to content

Commit

Permalink
chore: remove more circular dependencies (#931)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
jeromy-cannon authored Dec 6, 2024
1 parent 88f3458 commit 9cd610d
Show file tree
Hide file tree
Showing 42 changed files with 734 additions and 565 deletions.
6 changes: 5 additions & 1 deletion src/commands/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as constants from '../core/constants.js';
import {type AccountManager} from '../core/account_manager.js';
import {type AccountId, AccountInfo, HbarUnit, PrivateKey} from '@hashgraph/sdk';
import {FREEZE_ADMIN_ACCOUNT} from '../core/constants.js';
import {type Opts} from '../types/index.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
import {type CommandBuilder} from '../types/aliases.js';

Expand Down Expand Up @@ -626,4 +626,8 @@ export class AccountCommand extends BaseCommand {
},
};
}

close(): Promise<void> {
return this.closeConnections();
}
}
19 changes: 11 additions & 8 deletions src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ import {type K8} from '../core/k8.js';
import {type ChartManager} from '../core/chart_manager.js';
import {type ConfigManager} from '../core/config_manager.js';
import {type DependencyManager} from '../core/dependency_managers/index.js';
import type {CommandFlag, CommandHandlers, Opts} from '../types/index.js';
import type {Lease} from '../core/lease/lease.js';
import {type Opts} from '../types/command_types.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type Lease} from '../core/lease/types.js';
import {Listr} from 'listr2';

export class BaseCommand extends ShellRunner {
export interface CommandHandlers {
parent: BaseCommand;
}

export abstract class BaseCommand extends ShellRunner {
protected readonly helm: Helm;
protected readonly k8: K8;
protected readonly chartManager: ChartManager;
Expand Down Expand Up @@ -179,6 +184,8 @@ export class BaseCommand extends ShellRunner {
return this.localConfig;
}

abstract close(): Promise<void>;

commandActionBuilder(actionTasks: any, options: any, errorString: string, lease: Lease | null) {
return async function (argv: any, commandDef: CommandHandlers) {
const tasks = new Listr([...actionTasks], options);
Expand All @@ -191,11 +198,7 @@ export class BaseCommand extends ShellRunner {
} finally {
const promises = [];

// @ts-ignore
if (commandDef.close) {
// @ts-ignore
promises.push(commandDef.close());
}
promises.push(commandDef.parent.close());

if (lease) promises.push(lease.release());
await Promise.all(promises);
Expand Down
5 changes: 5 additions & 0 deletions src/commands/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,9 @@ export class ClusterCommand extends BaseCommand {

return chartPath;
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
3 changes: 1 addition & 2 deletions src/commands/context/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
*
*/
import {type BaseCommand} from '../base.js';
import {type BaseCommand, type CommandHandlers} from '../base.js';
import {type ContextCommandTasks} from './tasks.js';
import * as helpers from '../../core/helpers.js';
import * as constants from '../../core/constants.js';
import {type CommandHandlers} from '../../types/index.js';
import * as ContextFlags from './flags.js';

export class ContextCommandHandlers implements CommandHandlers {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import * as ContextFlags from './flags.js';
import {YargsCommand} from '../../core/yargs_command.js';
import {BaseCommand} from './../base.js';
import type {Opts} from '../../types/index.js';
import {type Opts} from '../../types/command_types.js';
import {ContextCommandTasks} from './tasks.js';
import {ContextCommandHandlers} from './handlers.js';

Expand Down Expand Up @@ -55,4 +55,9 @@ export class ContextCommand extends BaseCommand {
},
};
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
8 changes: 7 additions & 1 deletion src/commands/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import chalk from 'chalk';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {ListrLease} from '../core/lease/listr_lease.js';
import type {Namespace} from '../core/config/remote/types.js';
import type {CommandFlag, ContextClusterStructure} from '../types/index.js';
import {type ContextClusterStructure} from '../types/config_types.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type CommandBuilder} from '../types/aliases.js';

export class DeploymentCommand extends BaseCommand {
Expand Down Expand Up @@ -155,4 +156,9 @@ export class DeploymentCommand extends BaseCommand {
},
};
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
2 changes: 1 addition & 1 deletion src/commands/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as constants from '../core/constants.js';
import {ConfigManager} from '../core/config_manager.js';
import * as version from '../../version.js';
import path from 'path';
import type {CommandFlag} from '../types/index.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type ListrTaskWrapper} from 'listr2';
import fs from 'fs';
import {IllegalArgumentError, SoloError} from '../core/errors.js';
Expand Down
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {NodeCommand} from './node/index.js';
import {RelayCommand} from './relay.js';
import {AccountCommand} from './account.js';
import {DeploymentCommand} from './deployment.js';
import {type Opts} from '../types/index.js';
import {type Opts} from '../types/command_types.js';

/**
* Return a list of Yargs command builder to be exposed through CLI
Expand Down
5 changes: 5 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,9 @@ export class InitCommand extends BaseCommand {
},
};
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
53 changes: 49 additions & 4 deletions src/commands/mirror_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/
import {ListrEnquirerPromptAdapter} from '@listr2/prompt-adapter-enquirer';
import {Listr} from 'listr2';
import {Listr, type ListrTask} from 'listr2';
import {SoloError, IllegalArgumentError, MissingArgumentError} from '../core/errors.js';
import * as constants from '../core/constants.js';
import {type AccountManager} from '../core/account_manager.js';
Expand All @@ -25,8 +25,10 @@ import {Flags as flags} from './flags.js';
import {getEnvValue} from '../core/helpers.js';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {type CommandBuilder, type PodName} from '../types/aliases.js';
import type {Opts} from '../types/index.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
import {ComponentType} from '../core/config/remote/enumerations.js';
import {MirrorNodeComponent} from '../core/config/remote/components/mirror_node_component.js';

export class MirrorNodeCommand extends BaseCommand {
private readonly accountManager: AccountManager;
Expand Down Expand Up @@ -421,7 +423,7 @@ export class MirrorNodeCommand extends BaseCommand {
);
},
},
RemoteConfigTasks.addMirrorNodeAndMirrorNodeToExplorer.bind(this)(),
this.addMirrorNodeAndMirrorNodeExplorer(),
],
{
concurrent: false,
Expand Down Expand Up @@ -518,7 +520,7 @@ export class MirrorNodeCommand extends BaseCommand {
},
skip: ctx => !ctx.config.isChartInstalled,
},
RemoteConfigTasks.removeMirrorNodeAndMirrorNodeToExplorer.bind(this)(),
this.removeMirrorNodeAndMirrorNodeExplorer(),
],
{
concurrent: false,
Expand Down Expand Up @@ -591,4 +593,47 @@ export class MirrorNodeCommand extends BaseCommand {
},
};
}

/** Removes the mirror node and mirror node explorer components from remote config. */
public removeMirrorNodeAndMirrorNodeExplorer(): ListrTask<any, any, any> {
return {
title: 'Remove mirror node and mirror node explorer from remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
task: async (): Promise<void> => {
await this.remoteConfigManager.modify(async remoteConfig => {
remoteConfig.components.remove('mirrorNode', ComponentType.MirrorNode);

remoteConfig.components.remove('mirrorNodeExplorer', ComponentType.MirrorNode);
});
},
};
}

/** Adds the mirror node and mirror node explorer components to remote config. */
public addMirrorNodeAndMirrorNodeExplorer(): ListrTask<any, any, any> {
return {
title: 'Add mirror node and mirror node explorer to remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
task: async (ctx): Promise<void> => {
await this.remoteConfigManager.modify(async remoteConfig => {
const {
config: {namespace},
} = ctx;
const cluster = this.remoteConfigManager.currentCluster;

remoteConfig.components.add('mirrorNode', new MirrorNodeComponent('mirrorNode', cluster, namespace));

remoteConfig.components.add(
'mirrorNodeExplorer',
new MirrorNodeComponent('mirrorNodeExplorer', cluster, namespace),
);
});
},
};
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
47 changes: 44 additions & 3 deletions src/commands/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import {ListrEnquirerPromptAdapter} from '@listr2/prompt-adapter-enquirer';
import chalk from 'chalk';
import {Listr} from 'listr2';
import {Listr, type ListrTask} from 'listr2';
import {SoloError, IllegalArgumentError, MissingArgumentError} from '../core/errors.js';
import {BaseCommand} from './base.js';
import {Flags as flags} from './flags.js';
Expand All @@ -32,8 +32,12 @@ import {type PlatformInstaller} from '../core/platform_installer.js';
import {type ProfileManager} from '../core/profile_manager.js';
import {type CertificateManager} from '../core/certificate_manager.js';
import {type CommandBuilder, type NodeAlias, type NodeAliases} from '../types/aliases.js';
import type {Opts} from '../types/index.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
import {ConsensusNodeComponent} from '../core/config/remote/components/consensus_node_component.js';
import {ConsensusNodeStates} from '../core/config/remote/enumerations.js';
import {EnvoyProxyComponent} from '../core/config/remote/components/envoy_proxy_component.js';
import {HaProxyComponent} from '../core/config/remote/components/ha_proxy_component.js';

export interface NetworkDeployConfigClass {
applicationEnv: string;
Expand Down Expand Up @@ -475,7 +479,7 @@ export class NetworkCommand extends BaseCommand {
});
},
},
RemoteConfigTasks.addNodesAndProxies.bind(this)(),
this.addNodesAndProxies(),
],
{
concurrent: false,
Expand Down Expand Up @@ -721,4 +725,41 @@ export class NetworkCommand extends BaseCommand {
},
};
}
/** Adds the consensus node, envoy and haproxy components to remote config. */
public addNodesAndProxies(): ListrTask<any, any, any> {
return {
title: 'Add node and proxies to remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
task: async (ctx): Promise<void> => {
const {
config: {namespace, nodeAliases},
} = ctx;
const cluster = this.remoteConfigManager.currentCluster;

await this.remoteConfigManager.modify(async remoteConfig => {
for (const nodeAlias of nodeAliases) {
remoteConfig.components.add(
nodeAlias,
new ConsensusNodeComponent(nodeAlias, cluster, namespace, ConsensusNodeStates.INITIALIZED),
);

remoteConfig.components.add(
`envoy-${nodeAlias}`,
new EnvoyProxyComponent(`envoy-${nodeAlias}`, cluster, namespace),
);

remoteConfig.components.add(
`haproxy-${nodeAlias}`,
new HaProxyComponent(`haproxy-${nodeAlias}`, cluster, namespace),
);
}
});
},
};
}

close(): Promise<void> {
// no-op
return Promise.resolve();
}
}
Loading

0 comments on commit 9cd610d

Please sign in to comment.