@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
183 lines • 8.97 kB
JavaScript
// 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 DefaultHelmClient_1;
import { HelmExecutionBuilder } from '../execution/helm-execution-builder.js';
import { Repository } from '../model/repository.js';
import { Version } from '../model/version.js';
import { Release } from '../model/chart/release.js';
import { ReleaseItem } from '../model/release/release-item.js';
import { ChartDependencyUpdateRequest } from '../request/chart/chart-dependency-update-request.js';
import { ChartInstallRequest } from '../request/chart/chart-install-request.js';
import { ChartTestRequest } from '../request/chart/chart-test-request.js';
import { ChartUninstallRequest } from '../request/chart/chart-uninstall-request.js';
import { ChartUpgradeRequest } from '../request/chart/chart-upgrade-request.js';
import { VersionRequest } from '../request/common/version-request.js';
import { ReleaseListRequest } from '../request/release/release-list-request.js';
import { RepositoryAddRequest } from '../request/repository/repository-add-request.js';
import { RepositoryListRequest } from '../request/repository/repository-list-request.js';
import { RepositoryRemoveRequest } from '../request/repository/repository-remove-request.js';
import { inject, injectable } from 'tsyringe-neo';
import { InjectTokens } from '../../../core/dependency-injection/inject-tokens.js';
import { patchInject } from '../../../core/dependency-injection/container-helper.js';
import { SoloError } from '../../../core/errors/solo-error.js';
import { RepositoryUpdateRequest } from '../request/repository/repository-update-request.js';
import path from 'node:path';
import { ChartPullRequest } from '../request/chart/chart-pull-request.js';
let DefaultHelmClient = class DefaultHelmClient {
static { DefaultHelmClient_1 = this; }
logger;
installationDirectory;
/**
* The name of the namespace argument.
*/
static NAMESPACE_ARG_NAME = 'namespace';
constructor(logger, installationDirectory) {
this.logger = logger;
this.installationDirectory = installationDirectory;
this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
this.installationDirectory = patchInject(installationDirectory, InjectTokens.HelmInstallationDirectory, this.constructor.name);
}
ERROR_401_REGEX = /\b401\b.*\bunauthorized\b/i;
ERROR_403_REGEX = /\b401\b.*\bunauthorized\b/i;
async version() {
const request = new VersionRequest();
const builder = new HelmExecutionBuilder();
this.applyBuilderDefaults(builder);
request.apply(builder);
const execution = builder.build();
if (execution instanceof Promise) {
throw new TypeError('Unexpected async execution');
}
const versionClass = Version;
const result = await execution.responseAs(versionClass);
if (!(result instanceof Version)) {
throw new TypeError('Unexpected response type');
}
const semanticVersion = result.asSemanticVersion();
this.logger.showUser(`helm version: ${semanticVersion.toString()}`);
return semanticVersion;
}
async listRepositories() {
return this.executeAsList(new RepositoryListRequest(), Repository);
}
async addRepository(repository, options) {
await this.executeAsync(new RepositoryAddRequest(repository, options));
}
async removeRepository(repository) {
await this.executeAsync(new RepositoryRemoveRequest(repository));
}
async installChart(releaseName, chart, options) {
const release = Release;
const request = new ChartInstallRequest(releaseName, chart, options);
return this.executeInternal(options.namespace, request, release, async (execution) => {
return await execution.responseAs(release);
});
}
async uninstallChart(releaseName, options) {
await this.executeAsync(new ChartUninstallRequest(releaseName, options));
}
async testChart(releaseName, options) {
await this.executeAsync(new ChartTestRequest(releaseName, options));
}
async listReleases(allNamespaces, namespace, kubeContext) {
return this.executeAsList(new ReleaseListRequest(allNamespaces, namespace, kubeContext), ReleaseItem);
}
async dependencyUpdate(chartName) {
await this.executeAsync(new ChartDependencyUpdateRequest(chartName));
}
async upgradeChart(releaseName, chart, options) {
const request = new ChartUpgradeRequest(releaseName, chart, options);
return this.executeInternal(options.namespace, request, Release, async (execution) => execution.responseAs(Release));
}
/**
* Applies the default namespace and authentication configuration to the given builder.
* @param _builder - The builder to apply to which the defaults should be applied
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
applyBuilderDefaults(_builder) { }
/**
* 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 HelmExecutionBuilder();
this.applyBuilderDefaults(builder);
request.apply(builder);
if (namespace) {
builder.argument(DefaultHelmClient_1.NAMESPACE_ARG_NAME, namespace);
}
builder.environmentVariable('PATH', `${this.installationDirectory}${path.delimiter}${process.env.PATH}`);
const execution = builder.build();
try {
return await responseFunction(execution, responseClass);
}
catch (error) {
const errorMessage = error?.message ?? '';
if (!this.ERROR_401_REGEX.test(errorMessage) && !this.ERROR_403_REGEX.test(errorMessage)) {
// Throw original for all other cases
throw error;
}
this.logger.showUser([
'Detected expired Docker authentication for GHCR (ghcr.io).',
'Fix: run one of the following and retry:',
' - docker logout ghcr.io',
' - docker logout http://ghcr.io/',
].join('\n'));
throw new SoloError('GHCR stale Docker auth detected');
}
}
async updateRepositories() {
await this.executeAsync(new RepositoryUpdateRequest());
}
async pullChartPackage(chart, version, destinationDirectory) {
await this.executeAsync(new ChartPullRequest(chart, version, destinationDirectory));
}
};
DefaultHelmClient = DefaultHelmClient_1 = __decorate([
injectable()
/**
* The default implementation of the HelmClient interface.
*/
,
__param(0, inject(InjectTokens.SoloLogger)),
__param(1, inject(InjectTokens.HelmInstallationDirectory)),
__metadata("design:paramtypes", [Object, String])
], DefaultHelmClient);
export { DefaultHelmClient };
//# sourceMappingURL=default-helm-client.js.map