UNPKG

@hashgraph/solo

Version:

An opinionated CLI tool to deploy and manage private Hedera Networks.

187 lines 10.5 kB
// SPDX-License-Identifier: Apache-2.0 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var DefaultKindClient_1; import { GetClustersRequest } from '../request/get/get-clusters-request.js'; import { KindCluster } from '../model/kind-cluster.js'; import { KindExecutionBuilder } from '../execution/kind-execution-builder.js'; import { VersionRequest } from '../request/version-request.js'; import { KindVersion } from '../model/kind-version.js'; import { ClusterCreateRequest } from '../request/cluster/cluster-create-request.js'; import { ClusterCreateOptionsBuilder } from '../model/create-cluster/create-cluster-options-builder.js'; import { ClusterCreateResponse } from '../model/create-cluster/cluster-create-response.js'; import { ClusterDeleteResponse } from '../model/delete-cluster/cluster-delete-response.js'; import { ClusterDeleteOptionsBuilder } from '../model/delete-cluster/cluster-delete-options-builder.js'; import { ClusterDeleteRequest } from '../request/cluster/cluster-delete-request.js'; import { BuildNodeImagesResponse } from '../model/build-node-images/build-node-images-response.js'; import { BuildNodeImagesRequest } from '../request/build/build-node-images-request.js'; import { ExportLogsRequest } from '../request/export/export-logs-request.js'; import { ExportLogsResponse } from '../model/export-logs/export-logs-response.js'; import { ExportLogsOptionsBuilder } from '../model/export-logs/export-logs-options-builder.js'; import { ExportKubeConfigOptionsBuilder } from '../model/export-kubeconfig/export-kubeconfig-options-builder.js'; import { ExportKubeConfigRequest } from '../request/export/export-kubeconfig-request.js'; import { ExportKubeConfigResponse } from '../model/export-kubeconfig/export-kubeconfig-response.js'; import { GetNodesResponse } from '../model/get-nodes/get-nodes-response.js'; import { GetNodesOptionsBuilder } from '../model/get-nodes/get-nodes-options-builder.js'; import { GetNodesRequest } from '../request/get/get-nodes-request.js'; import { GetKubeConfigOptionsBuilder } from '../model/get-kubeconfig/get-kubeconfig-options-builder.js'; import { GetKubeConfigRequest } from '../request/get/get-kubeconfig-request.js'; import { GetKubeConfigResponse } from '../model/get-kubeconfig/get-kubeconfig-response.js'; import { LoadDockerImageRequest } from '../request/load/docker-image-request.js'; import { LoadDockerImageResponse } from '../model/load-docker-image/load-docker-image-response.js'; import { LoadDockerImageOptionsBuilder } from '../model/load-docker-image/load-docker-image-options-builder.js'; import { LoadImageArchiveOptionsBuilder } from '../model/load-image-archive/load-image-archive-options-builder.js'; import { LoadImageArchiveResponse } from '../model/load-image-archive/load-image-archive-response.js'; import { LoadImageArchiveRequest } from '../request/load/image-archive-request.js'; import { KIND_VERSION } from '../../../../version.js'; import { KindVersionRequirementException } from '../errors/kind-version-requirement-exception.js'; import { inject } from 'tsyringe-neo'; import { InjectTokens } from '../../../core/dependency-injection/inject-tokens.js'; import { patchInject } from '../../../core/dependency-injection/container-helper.js'; import path from 'node:path'; import { SemanticVersion } from '../../../business/utils/semantic-version.js'; let DefaultKindClient = class DefaultKindClient { static { DefaultKindClient_1 = this; } executable; logger; installationDirectory; static minimumVersion = new SemanticVersion(KIND_VERSION); constructor(executable, logger, installationDirectory) { this.executable = executable; this.logger = logger; this.installationDirectory = installationDirectory; if (!executable || !executable.trim()) { throw new Error('executable must not be blank'); } this.executable = executable; this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name); this.installationDirectory = patchInject(installationDirectory, InjectTokens.KindInstallationDirectory, this.constructor.name); } async checkVersion() { const version = await this.version(); if (version.lessThan(DefaultKindClient_1.minimumVersion)) { throw new KindVersionRequirementException(`The Kind CLI version ${version} is lower than the minimum required version ${DefaultKindClient_1.minimumVersion}.`); } } async version() { const request = new VersionRequest(); const builder = new KindExecutionBuilder(); builder.executable(this.executable); request.apply(builder); const execution = builder.build(); if (execution instanceof Promise) { throw new TypeError('Unexpected async execution'); } const versionClass = KindVersion; const result = await execution.responseAs(versionClass); if (!(result instanceof KindVersion)) { throw new TypeError('Unexpected response type'); } const semver = result.getVersion(); this.logger?.info?.(`kind version: ${semver.toString()}`); return semver; } async createCluster(clusterName, options) { const builder = ClusterCreateOptionsBuilder.from(options); builder.name(clusterName); return this.executeAsync(new ClusterCreateRequest(builder.build()), ClusterCreateResponse); } async deleteCluster(clusterName, options) { const builder = ClusterDeleteOptionsBuilder.from(options); builder.name(clusterName); return this.executeAsync(new ClusterDeleteRequest(builder.build()), ClusterDeleteResponse); } async buildNodeImage(options) { return this.executeAsync(new BuildNodeImagesRequest(options), BuildNodeImagesResponse); } async exportLogs(clusterName) { const options = ExportLogsOptionsBuilder.builder().name(clusterName).build(); return this.executeAsync(new ExportLogsRequest(options), ExportLogsResponse); } async exportKubeConfig(clusterName) { const options = ExportKubeConfigOptionsBuilder.builder().name(clusterName).build(); return this.executeAsync(new ExportKubeConfigRequest(options), ExportKubeConfigResponse); } async getClusters() { return this.executeAsList(new GetClustersRequest(), KindCluster); } async getNodes(contextName, options) { const builder = GetNodesOptionsBuilder.from(options).name(contextName); return this.executeAsync(new GetNodesRequest(builder.build()), GetNodesResponse); } async getKubeConfig(contextName, options) { const builder = GetKubeConfigOptionsBuilder.from(options).name(contextName); return this.executeAsync(new GetKubeConfigRequest(builder.build()), GetKubeConfigResponse); } async loadDockerImage(imageName, options) { const builder = LoadDockerImageOptionsBuilder.from(options).imageName(imageName); return this.executeAsync(new LoadDockerImageRequest(builder.build()), LoadDockerImageResponse); } async loadImageArchive(archivePath, options) { const builder = LoadImageArchiveOptionsBuilder.from(options).archivePath(archivePath); await this.executeCall(new LoadImageArchiveRequest(builder.build())); return new LoadImageArchiveResponse(); } async executeCall(request) { const builder = new KindExecutionBuilder(); builder.executable(this.executable); builder.environmentVariable('PATH', `${this.installationDirectory}${path.delimiter}${process.env.PATH}`); request.apply(builder); const execution = builder.build(); await execution.call(); } /** * Executes the given request and returns the response as the given class. * The request is executed using the default namespace. * * @param request - The request to execute * @param responseClass - The class of the response * @returns The response */ async executeAsync(request, responseClass) { return this.executeInternal(undefined, request, responseClass, async (b) => { return await b.responseAs(responseClass); }); } /** * Executes the given request and returns the response as a list of the given class. * The request is executed using the default namespace. * * @param request - The request to execute * @param responseClass - The class of the response * @returns A list of response objects */ async executeAsList(request, responseClass) { return this.executeInternal(undefined, request, responseClass, async (b) => { return await b.responseAsList(responseClass); }); } async executeInternal(namespace, request, responseClass, responseFunction) { if (namespace && !namespace.trim()) { throw new Error('namespace must not be blank'); } const builder = new KindExecutionBuilder(); builder.executable(this.executable); builder.environmentVariable('PATH', `${this.installationDirectory}${path.delimiter}${process.env.PATH}`); request.apply(builder); const execution = builder.build(); return responseFunction(execution, responseClass); } }; DefaultKindClient = DefaultKindClient_1 = __decorate([ __param(1, inject(InjectTokens.SoloLogger)), __param(2, inject(InjectTokens.KindInstallationDirectory)), __metadata("design:paramtypes", [String, Object, String]) ], DefaultKindClient); export { DefaultKindClient }; //# sourceMappingURL=default-kind-client.js.map