diff --git a/packages/dashmate/configs/defaults/getLocalConfigFactory.js b/packages/dashmate/configs/defaults/getLocalConfigFactory.js index 352e74a89f3..fad395536b6 100644 --- a/packages/dashmate/configs/defaults/getLocalConfigFactory.js +++ b/packages/dashmate/configs/defaults/getLocalConfigFactory.js @@ -5,6 +5,7 @@ const { } = require('../../src/constants'); const Config = require('../../src/config/Config'); +const { SSL_PROVIDERS } = require('../../src/constants'); /** * @param {getBaseConfig} getBaseConfig @@ -34,6 +35,9 @@ function getLocalConfigFactory(getBaseConfig) { platform: { dapi: { envoy: { + ssl: { + provider: SSL_PROVIDERS.SELF_SIGNED, + }, http: { port: 2443, }, diff --git a/packages/dashmate/src/commands/config/create.js b/packages/dashmate/src/commands/config/create.js index d938e3a33f1..5dfa6f20af1 100644 --- a/packages/dashmate/src/commands/config/create.js +++ b/packages/dashmate/src/commands/config/create.js @@ -4,6 +4,9 @@ class ConfigCreateCommand extends BaseCommand { /** * @param {Object} args * @param {Object} flags + * @param {renderServiceTemplates} renderServiceTemplates + * @param {writeServiceConfigs} writeServiceConfigs + * @param {ConfigFileJsonRepository} configFileRepository * @param {ConfigFile} configFile * @return {Promise} */ @@ -13,10 +16,18 @@ class ConfigCreateCommand extends BaseCommand { from: fromConfigName, }, flags, + renderServiceTemplates, + writeServiceConfigs, + configFileRepository, configFile, ) { configFile.createConfig(configName, fromConfigName); + configFileRepository.write(configFile); + + const serviceConfigs = renderServiceTemplates(configFile.getConfig(configName)); + writeServiceConfigs(configName, serviceConfigs); + // eslint-disable-next-line no-console console.log(`${configName} created`); } diff --git a/packages/dashmate/src/commands/config/index.js b/packages/dashmate/src/commands/config/index.js index 7b148d39755..2912130b81f 100644 --- a/packages/dashmate/src/commands/config/index.js +++ b/packages/dashmate/src/commands/config/index.js @@ -7,18 +7,25 @@ class ConfigCommand extends ConfigBaseCommand { * @param {Object} args * @param {Object} flags * @param {Config} config + * @param {renderServiceTemplates} renderServiceTemplates + * @param {writeServiceConfigs} writeServiceConfigs * @return {Promise} */ async runWithDependencies( args, flags, config, + renderServiceTemplates, + writeServiceConfigs, ) { const output = `${config.getName()} config:\n\n${inspect( config.getOptions(), { colors: true, depth: null, maxArrayLength: 2 }, )}`; + const serviceConfigs = renderServiceTemplates(config); + writeServiceConfigs(config.getName(), serviceConfigs); + // eslint-disable-next-line no-console console.log(output); } diff --git a/packages/dashmate/src/commands/config/remove.js b/packages/dashmate/src/commands/config/remove.js index 15469a6f7fb..73c12ad9dcc 100644 --- a/packages/dashmate/src/commands/config/remove.js +++ b/packages/dashmate/src/commands/config/remove.js @@ -6,6 +6,9 @@ class ConfigRemoveCommand extends BaseCommand { * @param {Object} flags * @param {ConfigFile} configFile * @param {DefaultConfigs} defaultConfigs + * @param {renderServiceTemplates} renderServiceTemplates + * @param {writeServiceConfigs} writeServiceConfigs + * @param {ConfigFileJsonRepository} configFileRepository * @return {Promise} */ async runWithDependencies( @@ -15,6 +18,9 @@ class ConfigRemoveCommand extends BaseCommand { flags, configFile, defaultConfigs, + renderServiceTemplates, + writeServiceConfigs, + configFileRepository, ) { if (defaultConfigs.has(configName)) { throw new Error(`system config ${configName} can't be removed.\nPlease use 'dashmate reset --hard --config=${configName}' command to reset the configuration`); @@ -22,6 +28,11 @@ class ConfigRemoveCommand extends BaseCommand { configFile.removeConfig(configName); + configFileRepository.write(configFile); + + const serviceConfigs = renderServiceTemplates(configFile.getConfig(configName)); + writeServiceConfigs(configName, serviceConfigs); + // eslint-disable-next-line no-console console.log(`${configName} removed`); } diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 7b12c92d744..55cbc7f6fbf 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -5,6 +5,10 @@ class ConfigSetCommand extends ConfigBaseCommand { * @param args * @param flags * @param {Config} config + * @param {renderServiceTemplates} renderServiceTemplates + * @param {writeServiceConfigs} writeServiceConfigs + * @param {ConfigFileJsonRepository} configFileRepository + * @param {ConfigFile} configFile * @return {Promise} */ async runWithDependencies( @@ -14,6 +18,10 @@ class ConfigSetCommand extends ConfigBaseCommand { }, flags, config, + renderServiceTemplates, + writeServiceConfigs, + configFileRepository, + configFile, ) { // check for existence config.get(optionPath); @@ -28,6 +36,11 @@ class ConfigSetCommand extends ConfigBaseCommand { config.set(optionPath, value); + configFileRepository.write(configFile); + + const serviceConfigs = renderServiceTemplates(config); + writeServiceConfigs(config.getName(), serviceConfigs); + // eslint-disable-next-line no-console console.log(`${optionPath} set to ${optionValue}`); } diff --git a/packages/dashmate/src/listr/tasks/resetNodeTaskFactory.js b/packages/dashmate/src/listr/tasks/resetNodeTaskFactory.js index 7f04c257b1a..99d19c48841 100644 --- a/packages/dashmate/src/listr/tasks/resetNodeTaskFactory.js +++ b/packages/dashmate/src/listr/tasks/resetNodeTaskFactory.js @@ -12,6 +12,7 @@ const wait = require('../../util/wait'); * @param {ConfigFile} configFile * @param {HomeDir} homeDir * @param {generateEnvs} generateEnvs + * @param {ConfigFileJsonRepository} configFileRepository * @return {resetNodeTask} */ function resetNodeTaskFactory( @@ -23,6 +24,7 @@ function resetNodeTaskFactory( configFile, homeDir, generateEnvs, + configFileRepository, ) { /** * @typedef {resetNodeTask} @@ -131,6 +133,8 @@ function resetNodeTaskFactory( configFile.removeConfig(config.getName()); } + configFileRepository.write(configFile); + // Remove service configs let serviceConfigsPath = homeDir.joinPath(baseConfigName); diff --git a/packages/dashmate/src/listr/tasks/setup/setupLocalPresetTaskFactory.js b/packages/dashmate/src/listr/tasks/setup/setupLocalPresetTaskFactory.js index 8586e8349a0..71bf7df587d 100644 --- a/packages/dashmate/src/listr/tasks/setup/setupLocalPresetTaskFactory.js +++ b/packages/dashmate/src/listr/tasks/setup/setupLocalPresetTaskFactory.js @@ -2,7 +2,6 @@ const { Listr } = require('listr2'); const { PRESET_LOCAL, - SSL_PROVIDERS, } = require('../../../constants'); const generateTenderdashNodeKey = require('../../../tenderdash/generateTenderdashNodeKey'); @@ -18,6 +17,8 @@ const generateRandomString = require('../../../util/generateRandomString'); * @param {configFileRepository} configFileRepository * @param {generateHDPrivateKeys} generateHDPrivateKeys * @param {HomeDir} homeDir + * @param {writeServiceConfigs} writeServiceConfigs + * @param {renderServiceTemplates} renderServiceTemplates */ function setupLocalPresetTaskFactory( configFile, @@ -28,6 +29,8 @@ function setupLocalPresetTaskFactory( configFileRepository, generateHDPrivateKeys, homeDir, + writeServiceConfigs, + renderServiceTemplates, ) { /** * @typedef {setupLocalPresetTask} @@ -268,16 +271,6 @@ function setupLocalPresetTaskFactory( } )); - subTasks.push({ - title: 'Save configs', - task: async () => { - configFile.setDefaultGroupName(PRESET_LOCAL); - - // Persist configs - configFileRepository.write(configFile); - }, - }); - return new Listr(subTasks); }, options: { @@ -292,19 +285,29 @@ function setupLocalPresetTaskFactory( title: 'Configure Tenderdash nodes', task: (ctx) => configureTenderdashTask(ctx.configGroup), }, + { + title: 'Persist configs', + task: (ctx) => { + configFile.setDefaultGroupName(PRESET_LOCAL); + + for (const config of ctx.configGroup) { + const serviceConfigFiles = renderServiceTemplates(config); + writeServiceConfigs(config.getName(), serviceConfigFiles); + } + + configFileRepository.write(configFile); + }, + }, { title: 'Configure SSL certificates', task: (ctx) => { const platformConfigs = ctx.configGroup.filter((config) => config.get('platform.enable')); - const subTasks = platformConfigs.map((config) => { - config.set('platform.dapi.envoy.ssl.provider', SSL_PROVIDERS.SELF_SIGNED); - - return { - title: `Generate certificate for ${config.getName()}`, - task: async () => obtainSelfSignedCertificateTask(config), - }; - }); + const subTasks = platformConfigs.map((config) => ({ + title: `Generate certificate for ${config.getName()}`, + task: async () => obtainSelfSignedCertificateTask(config), + } + )); return new Listr(subTasks); }, diff --git a/packages/dashmate/src/listr/tasks/setup/setupRegularPresetTaskFactory.js b/packages/dashmate/src/listr/tasks/setup/setupRegularPresetTaskFactory.js index 85d6582cacc..014eddcd6ab 100644 --- a/packages/dashmate/src/listr/tasks/setup/setupRegularPresetTaskFactory.js +++ b/packages/dashmate/src/listr/tasks/setup/setupRegularPresetTaskFactory.js @@ -22,6 +22,7 @@ const generateRandomString = require('../../../util/generateRandomString'); * @param {ConfigFile} configFile * @param {generateBlsKeys} generateBlsKeys * @param {registerMasternodeTask} registerMasternodeTask + * @param {ConfigFileJsonRepository} configFileRepository * @param {renderServiceTemplates} renderServiceTemplates * @param {writeServiceConfigs} writeServiceConfigs * @param {obtainZeroSSLCertificateTask} obtainZeroSSLCertificateTask @@ -34,6 +35,7 @@ function setupRegularPresetTaskFactory( configFile, generateBlsKeys, registerMasternodeTask, + configFileRepository, renderServiceTemplates, writeServiceConfigs, obtainZeroSSLCertificateTask, @@ -150,6 +152,11 @@ function setupRegularPresetTaskFactory( configFile.setConfig(ctx.config); configFile.setDefaultConfigName(ctx.preset); + configFileRepository.write(configFile); + + const serviceConfigFiles = renderServiceTemplates(ctx.config); + writeServiceConfigs(ctx.config.getName(), serviceConfigFiles); + // eslint-disable-next-line no-param-reassign task.output = chalk`Node configuration completed successfully! diff --git a/packages/dashmate/src/oclif/command/BaseCommand.js b/packages/dashmate/src/oclif/command/BaseCommand.js index 18654a214f2..15d33263715 100644 --- a/packages/dashmate/src/oclif/command/BaseCommand.js +++ b/packages/dashmate/src/oclif/command/BaseCommand.js @@ -91,14 +91,6 @@ class BaseCommand extends Command { async finally(err) { // Save configs collection if (this.container) { - const configFileRepository = this.container.resolve('configFileRepository'); - - if (this.container.has('configFile')) { - const configFile = this.container.resolve('configFile'); - - configFileRepository.write(configFile); - } - // Stop all running containers const stopAllContainers = this.container.resolve('stopAllContainers'); const startedContainers = this.container.resolve('startedContainers'); diff --git a/packages/dashmate/src/oclif/command/ConfigBaseCommand.js b/packages/dashmate/src/oclif/command/ConfigBaseCommand.js index ee6352170ef..7dc302e521e 100644 --- a/packages/dashmate/src/oclif/command/ConfigBaseCommand.js +++ b/packages/dashmate/src/oclif/command/ConfigBaseCommand.js @@ -24,7 +24,7 @@ class ConfigBaseCommand extends BaseCommand { if (defaultConfigName === null) { throw new Error(`Default config is not set. - + You probably need to set up a node with the 'dashmate setup' command first. You can also use the '--config' option, or set the default config with 'dashmate config default'`); @@ -43,12 +43,6 @@ You can also use the '--config' option, or set the default config with 'dashmate config: asValue(config), }); - const renderServiceTemplates = this.container.resolve('renderServiceTemplates'); - const writeServiceConfigs = this.container.resolve('writeServiceConfigs'); - - const serviceConfigFiles = renderServiceTemplates(config); - writeServiceConfigs(config.getName(), serviceConfigFiles); - return super.run(); } } diff --git a/packages/dashmate/src/oclif/command/GroupBaseCommand.js b/packages/dashmate/src/oclif/command/GroupBaseCommand.js index 53476be1f4d..5693f6333b6 100644 --- a/packages/dashmate/src/oclif/command/GroupBaseCommand.js +++ b/packages/dashmate/src/oclif/command/GroupBaseCommand.js @@ -39,14 +39,6 @@ class GroupBaseCommand extends BaseCommand { configGroup: asValue(group), }); - const renderServiceTemplates = this.container.resolve('renderServiceTemplates'); - const writeServiceConfigs = this.container.resolve('writeServiceConfigs'); - - for (const config of group) { - const serviceConfigFiles = renderServiceTemplates(config); - writeServiceConfigs(config.getName(), serviceConfigFiles); - } - return super.run(); } } diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index a62f8bc309a..dbe15620ed1 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -4,12 +4,20 @@ const HomeDir = require('../../../../src/config/HomeDir'); describe('Config set command', () => { const flags = {}; + let config; + let mockRenderServiceTemplates; + let mockWriteServiceConfigs; + let mockConfigFileRepository; beforeEach(async () => { const getBaseConfig = getBaseConfigFactory(HomeDir.createTemp()); config = getBaseConfig(); + + mockRenderServiceTemplates = () => {}; + mockWriteServiceConfigs = () => {}; + mockConfigFileRepository = { write: () => {} }; }); describe('#platform', () => { @@ -18,7 +26,9 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'core.docker.image', value: 'fake_image', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); }); it('should allow setting null', async () => { @@ -26,13 +36,17 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'description', value: null, - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('description')).to.equal(null); await command.runWithDependencies({ option: 'description', value: 'null', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('description')).to.equal(null); }); @@ -43,14 +57,18 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: 107, - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: '107', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); }); @@ -60,13 +78,17 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: 'true', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('dashmate.helper.api.enable')).to.equal(true); await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: true, - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('dashmate.helper.api.enable')).to.equal(true); }); @@ -76,7 +98,9 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect(config.get('core.rpc.allowIps')).to.deep.equal(['1337', '36484']); }); @@ -87,7 +111,9 @@ describe('Config set command', () => { await command.runWithDependencies({ option: 'docker.network', value: '{"subnet":"127.0.0.1/24", "bindIp": "0.0.0.0"}', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); }); it('should throw on unknown path', async () => { @@ -97,7 +123,9 @@ describe('Config set command', () => { try { await command.runWithDependencies({ option: 'fakePath', value: 'fake', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect.fail('should throw error'); } catch (e) { @@ -112,7 +140,9 @@ describe('Config set command', () => { try { await command.runWithDependencies({ option: 'core.rpc.allowIps', value: 'fake_image', - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect.fail('should throw error'); } catch (e) { @@ -127,7 +157,9 @@ describe('Config set command', () => { try { await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: 120, - }, flags, config); + }, flags, config, + mockRenderServiceTemplates, mockWriteServiceConfigs, + mockConfigFileRepository); expect.fail('should throw error'); } catch (e) {