@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
66 lines • 2.75 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import fs from 'node:fs';
import path from 'node:path';
import * as constants from '../../core/constants.js';
import { PathEx } from '../../business/utils/path-ex.js';
export class RemoteConfigCollector {
k8Factory;
logger;
constructor(k8Factory, logger) {
this.k8Factory = k8Factory;
this.logger = logger;
}
/**
* Sanitize a string for safe use as a filename on all platforms.
* Replaces characters invalid on Windows with underscores.
*/
sanitizeFilename(input) {
return input.replaceAll(/[^A-Za-z0-9._-]/g, '_');
}
async collect(customOutputDirectory = '') {
const outputDirectory = customOutputDirectory
? path.resolve(customOutputDirectory, 'remote-config')
: PathEx.join(constants.SOLO_LOGS_DIR, 'remote-config');
fs.mkdirSync(outputDirectory, { recursive: true });
const contexts = this.k8Factory.default().contexts();
for (const context of contexts.list()) {
const k8 = this.k8Factory.getK8(context);
try {
const configMaps = await k8
.configMaps()
.listForAllNamespaces([constants.SOLO_REMOTE_CONFIGMAP_LABEL_SELECTOR]);
for (const configMap of configMaps) {
const namespace = configMap.namespace.name;
const outputFileName = `${this.sanitizeFilename(context)}-${this.sanitizeFilename(namespace)}-${this.sanitizeFilename(configMap.name)}.json`;
const outputFilePath = PathEx.join(outputDirectory, outputFileName);
fs.writeFileSync(outputFilePath, JSON.stringify(this.toSerializableConfigMap(configMap), undefined, 2), 'utf8');
this.logger.info(`Saved solo-remote-config for ${context}/${namespace} to ${outputFilePath}`);
}
}
catch (error) {
this.logger.warn(`Failed to get solo-remote-config in context ${context}: ${error.message}`);
}
}
return outputDirectory;
}
toSerializableConfigMap(configMap) {
const output = {
name: configMap.name,
namespace: configMap.namespace.name,
labels: configMap.labels ?? {},
data: {},
};
if (configMap.data) {
for (const [key, value] of Object.entries(configMap.data)) {
try {
output.data[key] = JSON.parse(value);
}
catch {
output.data[key] = value;
}
}
}
return output;
}
}
//# sourceMappingURL=remote-config-collector.js.map