import { showInfo } from '../../utils/logger.util' import { BlockchainConfig } from '../../models/blockchain' import { Config } from '../../models/config' import { LogLevelValue, NodeTypeValue } from '../../models/choice' import { checkIfDirExistElseMakeDir } from '../../utils/checker.util' import writeYamlFile = require('write-yaml-file') import { filePathConfig } from '../../models/file_path' import { Redis } from '../../models/redis' export function generateConfig( nodeType: NodeTypeValue, { nodeId, clusterId, dataPath, blockChainConfig, nodeConfig, filePathConf, redis }: ConfigVariables, ): string { showInfo('Generating node config...') const [configPath, storagePath] = getConfigAndStoragePath(dataPath, filePathConf) checkIfDirExistElseMakeDir(configPath) // node config will be here checkIfDirExistElseMakeDir(storagePath) // node data will be here const configFilePath = configPath + '/config.yml' let config = { nodeId, clusterId, log: { level: nodeConfig.logLevel, 'report-caller': nodeConfig.logReportCaller, 'json-format': nodeConfig.logJSONFormat, }, http: { port: nodeConfig.httpPort, 'max-concurrent-streams': nodeConfig.httpMaxConcurrentStreams, 'max-stream-size': nodeConfig.httpStreamSize, 'max-frame-size': nodeConfig.httpMaxFrameSize, 'initial-window-size': nodeConfig.httpInitialWindowSize, }, 'debug-enabled': false, 'hide-banner': false, redis, 'prometheus-pushgateway': { url: 'http://pushgateway:9091', interval: '0s', basicAuth: { username: 'admin', password: 'admin', }, }, } as any if (nodeType === NodeTypeValue.Storage) { config = { ...config, datastore: { badger: { path: storagePath, }, }, 'ddc-bucket-contract': { 'api-url': blockChainConfig.apiURL, 'account-id': blockChainConfig.ddcBucketContract, }, consistency: { 'cleanup-interval': '30m', 'incremental-repair': { interval: '3h', portion: 1.1, }, }, storage: 50000000000, } } else if (nodeType === NodeTypeValue.CDN) { config = { ...config, badger: { path: storagePath }, blockchain: { 'api-url': blockChainConfig.apiURL, 'secret-phrase': blockChainConfig.secretPhrase, 'ddc-bucket-contract': blockChainConfig.ddcBucketContract, 'activity-capture-contract': blockChainConfig.activityCaptureContract, }, cache: { 'smart-contract': { 'node-expiration': '2h', 'account-expiration': '30m', 'bucket-expiration': '1h', }, ack: { size: 1024, }, data: { 'collect-metrics': true, l1_max_size_mb: 1024, l2_max_size_mb: 1024, l2_max_evict: 1, }, }, } } writeYamlFile.sync(configFilePath, config) return configFilePath } export function getDefaultConfig(): Config { return { logLevel: LogLevelValue.Info, logReportCaller: false, logJSONFormat: true, httpPort: 8080, httpMaxConcurrentStreams: 250, httpStreamSize: 5242880, httpMaxFrameSize: 5242880, httpInitialWindowSize: 5242880, } } export function getConfigAndStoragePath(basePath: string, filePathConf: filePathConfig): [string, string] { const dataPathPrefix = basePath.replace(/\/*$/, '') return [dataPathPrefix + filePathConf.configPostfixPath, dataPathPrefix + filePathConf.badgerPostfixPath] } type ConfigVariables = { dataPath: string blockChainConfig: BlockchainConfig nodeConfig: Config filePathConf: filePathConfig redis: Redis nodeId: number clusterId: number }