Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

feat: config versions #199

Merged
merged 9 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion src/config/ConfigCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ConfigCollection {
* @param {Config[]} [configs]
* @param {string|null} [currentConfigName=null]
*/
constructor(configs = [], currentConfigName = null) {
constructor(configs = [], currentConfigName = null, currentConfigFormatVersion) {
this.configsMap = configs.reduce((configsMap, config) => {
// eslint-disable-next-line no-param-reassign
configsMap[config.getName()] = config;
Expand All @@ -17,6 +17,7 @@ class ConfigCollection {
}, {});

this.setDefaultConfigName(currentConfigName);
this.setConfigFormatVersion(currentConfigFormatVersion);
}

/**
Expand Down Expand Up @@ -53,6 +54,27 @@ class ConfigCollection {
return this.defaultConfigName;
}

/**
* Set current config format version
*
* @param {string} version
* @returns {ConfigCollection}
*/
setConfigFormatVersion(version) {
this.configFormatVersion = version;

return this;
}

/**
* Get current config format version if set
*
* @returns {string|null}
*/
getConfigFormatVersion() {
return this.configFormatVersion;
}

/**
* Get current config if set
*
Expand Down
19 changes: 16 additions & 3 deletions src/config/configFile/ConfigJsonFileRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ const configFileJsonSchema = require('./configFileJsonSchema');
const ConfigFileNotFoundError = require('../errors/ConfigFileNotFoundError');
const InvalidConfigFileFormatError = require('../errors/InvalidConfigFileFormatError');

const packageJson = require('../../../package.json');

class ConfigJsonFileRepository {
/**
* @param configFilePath
*/
constructor(configFilePath) {
constructor(configFilePath, migrateConfigOptions) {
this.configFilePath = configFilePath;
this.migrateConfigOptions = migrateConfigOptions;
this.ajv = new Ajv();
}

Expand Down Expand Up @@ -49,12 +52,21 @@ class ConfigJsonFileRepository {
let configs;
try {
configs = Object.entries(configFileData.configs)
.map(([name, options]) => new Config(name, options));
.map(([name, options]) => {
const migratedOptions = this.migrateConfigOptions(
name,
options,
configFileData.configFormatVersion,
packageJson.version,
);

return new Config(name, migratedOptions);
});
} catch (e) {
throw new InvalidConfigFileFormatError(this.configFilePath, e);
}

return new ConfigCollection(configs, configFileData.defaultConfigName);
return new ConfigCollection(configs, configFileData.defaultConfigName, packageJson.version);
}

/**
Expand All @@ -66,6 +78,7 @@ class ConfigJsonFileRepository {
async write(configCollection) {
const configFileData = {
defaultConfigName: configCollection.getDefaultConfigName(),
configFormatVersion: configCollection.getConfigFormatVersion(),
};

configFileData.configs = configCollection.getAllConfigs().reduce((configsMap, config) => {
Expand Down
5 changes: 4 additions & 1 deletion src/config/configFile/configFileJsonSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ module.exports = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
configFormatVersion: {
type: 'string',
},
defaultConfigName: {
type: ['string', 'null'],
},
configs: {
type: 'object',
},
},
required: ['defaultConfigName', 'configs'],
required: ['configFormatVersion', 'defaultConfigName', 'configs'],
additionalProperties: false,
};
29 changes: 29 additions & 0 deletions src/config/configOptionMigrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const lodashGet = require('lodash.get');
const lodashSet = require('lodash.set');

const systemConfigs = require('./systemConfigs/systemConfigs');

module.exports = {
'0.17.0-dev.12': (name, options) => {
// Rename tendermint to tenderdash
lodashSet(options, 'platform.drive.tenderdash', options.platform.drive.tendermint);
// eslint-disable-next-line no-param-reassign
delete options.platform.drive.tendermint;

// Copy new configs from system defaults
const sourceConfigName = name in systemConfigs ? name : 'base';
const paths = [
'platform.dapi.nginx.rateLimiter.enable',
'platform.dapi.nginx.rateLimiter.burst',
'platform.dapi.nginx.rateLimiter.rate',
'platform.drive.tenderdash.validatorKey',
'platform.drive.tenderdash.nodeKey',
];

paths.forEach((path) => {
lodashSet(options, path, lodashGet(systemConfigs[sourceConfigName], path));
});

return options;
},
};
19 changes: 19 additions & 0 deletions src/config/migrateConfigOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const semver = require('semver');

const configOptionMigrations = require('./configOptionMigrations');

function migrateConfigOptions(name, options, fromVersion, toVersion) {
if (fromVersion === toVersion) {
return options;
}

return Object.keys(configOptionMigrations)
.filter((version) => (semver.gt(version, fromVersion) && semver.lte(version, toVersion)))
.sort(semver.compare)
.reduce((migratedOptions, version) => {
const migrationFunction = configOptionMigrations[version];
return migrationFunction(name, migratedOptions);
}, options);
}

module.exports = migrateConfigOptions;
4 changes: 3 additions & 1 deletion src/config/systemConfigs/createSystemConfigsFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const Config = require('../Config');

const ConfigCollection = require('../ConfigCollection');

const packageJson = require('../../../package.json');

/**
* @param {Object} systemConfigs
* @return {createSystemConfigs}
Expand All @@ -16,7 +18,7 @@ function createSystemConfigsFactory(systemConfigs) {
new Config(name, options)
));

return new ConfigCollection(configs, 'base');
return new ConfigCollection(configs, 'base', packageJson.version);
}

return createSystemConfigs;
Expand Down
2 changes: 2 additions & 0 deletions src/createDIContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ensureHomeDirFactory = require('./config/configFile/ensureHomeDirFactory')
const ConfigJsonFileRepository = require('./config/configFile/ConfigJsonFileRepository');
const createSystemConfigsFactory = require('./config/systemConfigs/createSystemConfigsFactory');
const resetSystemConfigFactory = require('./config/systemConfigs/resetSystemConfigFactory');
const migrateConfigOptions = require('./config/migrateConfigOptions');
const systemConfigs = require('./config/systemConfigs/systemConfigs');

const renderServiceTemplatesFactory = require('./templates/renderServiceTemplatesFactory');
Expand Down Expand Up @@ -67,6 +68,7 @@ async function createDIContainer(options) {
systemConfigs: asValue(systemConfigs),
createSystemConfigs: asFunction(createSystemConfigsFactory),
resetSystemConfig: asFunction(resetSystemConfigFactory),
migrateConfigOptions: asValue(migrateConfigOptions),
// `configCollection` and `config` are registering on command init
});

Expand Down