import { NodeTypeValue } from '../models/choice' import { fillNodeConfig } from '../questions/config/config.questions' import { useDefaultConfig } from '../questions/config/use_default.question' import { getPort } from '../questions/config/port.question' import { getStoragePath } from '../questions/config/storage.question' import { generateConfig, getConfigAndStoragePath, getDefaultConfig } from '../templates/config/generate_config' import { showError, showInfo } from '../utils/logger.util' import { downloadAndStartDockerImage } from '../templates/docker/image' import { notifyAboutNewNode } from '../templates/cluster_manager/notify_about_new_node' import { notifyClusterManager } from '../questions/cluster_manager/ask_notify.question' import { getAddress } from '../templates/cluster_manager/get_address' import { checkNodeAddress } from '../questions/cluster_manager/check_address.question' import { AppConfig } from '../models/app_config' import { filePathConfig } from '../models/file_path' interface IDDCOptions { appConf: AppConfig filePathConf: filePathConfig nodeType: NodeTypeValue seedPhrase: string walletAddress: string email: string nodeCountryISOCode: string clusterId: number } export async function ddcNodeActions({ appConf, filePathConf, nodeType, seedPhrase, walletAddress, email, nodeCountryISOCode, clusterId, }: IDDCOptions): Promise { // node config generation let nodeConfig = getDefaultConfig() const defaultConfig = await useDefaultConfig() if (defaultConfig.useDefaultConfig) { const port = await getPort() // if user wants to use default config, ask about port. nodeConfig.httpPort = port.httpPort } else { nodeConfig = await fillNodeConfig() } const storagePath = await getStoragePath() appConf.blockchain.secretPhrase = seedPhrase.trim() showInfo('Config generated. Generating config file...') // generate config file try { const filePath = generateConfig(nodeType, { nodeId: 1, // TODO remove when numeric node IDs are not used anymore clusterId, dataPath: storagePath.storagePath, blockChainConfig: appConf.blockchain, nodeConfig, filePathConf, redis: appConf.redis, }) showInfo(`Config file generated: ${filePath}`) } catch (err) { showError('File write failed with error: ' + err) process.exit(1) // we can't rewrite file, exit script } // node start showInfo('Starting node...') const [nodeConfigPath, nodeStoragePath] = getConfigAndStoragePath(storagePath.storagePath, filePathConf) await downloadAndStartDockerImage( nodeConfig.httpPort, nodeConfigPath, nodeStoragePath, appConf.dockerImage, nodeType, ) // notify cluster manager about new node start const notify = await notifyClusterManager() if (notify.notifyClusterManager) { const address = await getAddress(appConf.clusterManagerURL) const result = await checkNodeAddress(`${address}:${nodeConfig.httpPort}`) await notifyAboutNewNode({ clusterManagerURL: appConf.clusterManagerURL, publicKey: walletAddress, nodeAddress: result.nodeURL, email: email, nodeType: nodeType, countryISOCode: nodeCountryISOCode, clusterId, }) } }