UNPKG

@hashgraph/solo

Version:

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

155 lines 6.92 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); } }; import { inject } from 'tsyringe-neo'; import { InjectTokens } from '../../../core/dependency-injection/inject-tokens.js'; import { patchInject } from '../../../core/dependency-injection/container-helper.js'; import fs from 'node:fs/promises'; import { CacheArtifactEnum } from '../enums/cache-artifact-enum.js'; import { CachedItem } from '../models/impl/cached-item.js'; import { ArtifactHealthResult } from '../models/impl/artifact-health-result.js'; import chalk from 'chalk'; let ImageCacheHandler = class ImageCacheHandler { engine; provider; store; inspector; logger; constructor(engine, provider, store, inspector, logger) { this.engine = engine; this.provider = provider; this.store = store; this.inspector = inspector; this.logger = logger; this.store = patchInject(store, InjectTokens.CacheCatalogStore, this.constructor.name); this.inspector = patchInject(inspector, InjectTokens.CacheHealthInspector, this.constructor.name); this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name); } getType() { return CacheArtifactEnum.IMAGE; } async resolveRequiredArtifacts() { const targets = await this.provider.getRequiredTargets(); return targets.filter((target) => target.type === this.getType()); } async resolveExpectedCachedItems() { const targets = await this.resolveRequiredArtifacts(); const now = new Date().toISOString(); return targets.map((target) => { const localPath = this.store.resolvePath(target, CacheArtifactEnum.IMAGE); return new CachedItem(target, localPath, now); }); } async pull() { const subTasks = []; const targets = await this.resolveRequiredArtifacts(); for (const target of targets) { subTasks.push({ title: `Caching ${target.name}:${target.version}`, task: async ({ config }, task) => { const image = `${target.name}:${target.version}`; const archivePath = this.store.resolvePath(target, CacheArtifactEnum.IMAGE); const archiveExists = await this.inspector.exists(archivePath); if (!archiveExists) { try { await this.engine.saveImage(image, archivePath); } catch (error) { task.title += ' - ' + chalk.red(`failed to SAVE image: ${image}`); this.logger.error('Failed to save image archive:', error); return; } } config.results.push(new CachedItem(target, archivePath, new Date().toISOString())); }, }); } return subTasks; } async load(target) { const subTasks = []; const items = await this.resolveExpectedCachedItems(); for (const item of items) { const name = `${item.target.name}:${item.target.version}`; subTasks.push({ title: `Loading ${name} into ${target}`, task: async () => { const exists = await this.inspector.exists(item.localPath); if (!exists) { return; } try { await this.engine.loadImageArchiveIntoCluster(item.localPath, target); } catch (error) { this.logger.showUser(`Failed to load image archive into cluster: ${name}`); this.logger.error(error); console.error(error); } }, }); } return subTasks; } async clear() { const items = await this.resolveExpectedCachedItems(); for (const item of items) { await fs.rm(item.localPath, { force: true }); } } async healthcheck() { const results = []; const items = await this.resolveExpectedCachedItems(); for (const item of items) { const exists = await this.inspector.exists(item.localPath); const message = exists ? 'image archive exists' : 'image archive missing'; results.push(new ArtifactHealthResult(item.target, exists, message)); } return results; } async list() { const items = await this.resolveExpectedCachedItems(); const existingItems = []; for (const item of items) { if (await this.inspector.exists(item.localPath)) { existingItems.push(item); } } return existingItems; } // Non-generic async pullKindNodeImageIfMissing() { console.log(await this.resolveExpectedCachedItems()); const item = await this.resolveExpectedCachedItems().then((items) => items[0]); const image = `${item.target.name}:${item.target.version}`; const exists = await this.inspector.exists(item.localPath); if (!exists) { await this.engine.saveImage(image, item.localPath); } } async loadKindNodeImageIntoEngine() { const item = await this.resolveExpectedCachedItems().then((items) => items[0]); const exists = await this.inspector.exists(item.localPath); if (exists) { await this.engine.loadImage(item.localPath); } } }; ImageCacheHandler = __decorate([ __param(2, inject(InjectTokens.CacheCatalogStore)), __param(3, inject(InjectTokens.CacheHealthInspector)), __param(4, inject(InjectTokens.SoloLogger)), __metadata("design:paramtypes", [Object, Object, Object, Object, Object]) ], ImageCacheHandler); export { ImageCacheHandler }; //# sourceMappingURL=image-cache-handler.js.map