forked from hummingbot/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-manager-v2.ts
467 lines (415 loc) · 15.1 KB
/
config-manager-v2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import Ajv from 'ajv';
import { ValidateFunction, DefinedError } from 'ajv';
import fs from 'fs';
import fse from 'fs-extra';
import path from 'path';
import yaml from 'js-yaml';
import * as migrations from './config-migration/migrations';
import { rootPath } from '../paths';
type Configuration = { [key: string]: any };
type ConfigurationDefaults = { [namespaceId: string]: Configuration };
export type Migration = (
configRootFullPath: string,
configRootTemplateFullPath: string
) => void;
type MigrationFunctions = {
[key: string]: Migration;
};
interface _ConfigurationNamespaceDefinition {
configurationPath: string;
schemaPath: string;
}
type ConfigurationNamespaceDefinition = _ConfigurationNamespaceDefinition & {
[key: string]: string;
};
type ConfigurationNamespaceDefinitions = {
[namespaceId: string]: ConfigurationNamespaceDefinition;
};
interface ConfigurationRoot {
version: number;
configurations: ConfigurationNamespaceDefinitions;
}
const NamespaceTag: string = '$namespace ';
export const ConfigRootSchemaPath: string = path.join(
__dirname,
'schema/configuration-root-schema.json'
);
const ConfigTemplatesDir: string = path.join(__dirname, '../templates/');
const ConfigDir: string = path.join(rootPath(), 'conf/');
interface UnpackedConfigNamespace {
namespace: ConfigurationNamespace;
configPath: string;
}
export function deepCopy(srcObject: any, dstObject: any): any {
for (const [key, value] of Object.entries(srcObject)) {
if (srcObject[key] instanceof Array) {
if (!dstObject[key]) dstObject[key] = [];
deepCopy(srcObject[key], dstObject[key]);
} else if (srcObject[key] instanceof Object) {
if (!dstObject[key]) dstObject[key] = {};
deepCopy(srcObject[key], dstObject[key]);
} else if (
typeof srcObject[key] === typeof dstObject[key] ||
!dstObject[key]
) {
dstObject[key] = value;
}
}
}
export function initiateWithTemplate(templateFile: string, configFile: string) {
fs.copyFileSync(templateFile, configFile);
}
const ajv: Ajv = new Ajv();
export const percentRegexp = new RegExp(/^(\d+)\/(\d+)$/);
export class ConfigurationNamespace {
/**
* This class encapsulates a namespace under the configuration tree.
* A namespace represents the top-level component of a configuration path.
* e.g. if the config path is "server.certificatePath", then "server" is the
* namespace.
*
* Each namespace contains a JSON schema and a YAML configuration file.
*
* The JSON schema specifies the properties and data types allowed within the
* namespace. e.g. you may specify that the "server" namespace has a few
* mandatory properties dealing with certificates and private keys. This means
* any missing properties or any properties outsides of the JSON schema would
* cause a failure to initialize the namespace, and also cannot be set into
* the namespace.
*
* The YAML configuration file is where the actual configuration tree goes
* to. It is automatically validated against the JSON schema at namespace
* initiation. It is automatically saved to and validated against JSON schema
* again at every set() call.
*
* Note that configuration paths may have multiple levels. What it implies
* is those configurations are stored in nested dictionaries - aka. a tree.
* e.g. if the config path is "ethereum.networks.goerli.networkID", then,
* what it means you're accessing ["networks"]["goerli"]["networkID"] under
* the "ethereum" namespace.
*/
readonly #namespaceId: string;
readonly #schemaPath: string;
readonly #configurationPath: string;
readonly #templatePath: string;
readonly #validator: ValidateFunction;
#configuration: Configuration;
constructor(
id: string,
schemaPath: string,
configurationPath: string,
templatePath: string
) {
this.#namespaceId = id;
this.#schemaPath = schemaPath;
this.#configurationPath = configurationPath;
this.#templatePath = templatePath;
this.#configuration = {};
if (!fs.existsSync(schemaPath)) {
throw new Error(
`The JSON schema for namespace ${id} (${schemaPath}) does not exist.`
);
}
this.#validator = ajv.compile(
JSON.parse(fs.readFileSync(schemaPath).toString())
);
if (!fs.existsSync(configurationPath)) {
// copy from template
initiateWithTemplate(this.templatePath, this.configurationPath);
}
this.loadConfig();
}
get id(): string {
return this.#namespaceId;
}
get schemaPath(): string {
return this.#schemaPath;
}
get configurationPath(): string {
return this.#configurationPath;
}
get configuration(): Configuration {
return this.#configuration;
}
get templatePath(): string {
return this.#templatePath;
}
loadConfig() {
const configCandidate: Configuration = yaml.load(
fs.readFileSync(this.#configurationPath, 'utf8')
) as Configuration;
if (!this.#validator(configCandidate)) {
// merge with template file and try validating again
const configTemplateCandidate: Configuration = yaml.load(
fs.readFileSync(this.#templatePath, 'utf8')
) as Configuration;
deepCopy(configCandidate, configTemplateCandidate);
if (!this.#validator(configTemplateCandidate)) {
for (const err of this.#validator.errors as DefinedError[]) {
if (err.keyword === 'additionalProperties') {
throw new Error(
`${this.id} config file seems to be outdated/broken due to additional property "${err.params.additionalProperty}". Kindly fix manually.`
);
} else {
throw new Error(
`${this.id} config file seems to be outdated/broken due to "${err.keyword}" in "${err.instancePath}" - ${err.message}. Kindly fix manually.`
);
}
}
}
this.#configuration = configTemplateCandidate;
this.saveConfig();
return;
}
this.#configuration = configCandidate;
}
saveConfig() {
fs.writeFileSync(this.#configurationPath, yaml.dump(this.#configuration));
}
get(configPath: string): any {
const pathComponents: Array<string> = configPath.split('.');
let cursor: Configuration | any = this.#configuration;
for (const component of pathComponents) {
cursor = cursor[component];
if (cursor === undefined) {
return cursor;
}
}
return cursor;
}
set(configPath: string, value: any): void {
const pathComponents: Array<string> = configPath.split('.');
const configClone: Configuration = JSON.parse(
JSON.stringify(this.#configuration)
);
let cursor: Configuration | any = configClone;
let parent: Configuration = configClone;
for (const component of pathComponents.slice(0, -1)) {
parent = cursor;
cursor = cursor[component];
if (cursor === undefined) {
parent[component] = {};
cursor = parent[component];
}
}
const lastComponent: string = pathComponents[pathComponents.length - 1];
cursor[lastComponent] = value;
if (!this.#validator(configClone)) {
throw new Error(
`Cannot set ${this.id}.${configPath} to ${value}: ` +
'JSON schema violation.'
);
}
this.#configuration = configClone;
this.saveConfig();
}
}
export class ConfigManagerV2 {
/**
* This class encapsulates the configuration tree and all the contained
* namespaces and files for Hummingbot Gateway. It also contains a defaults
* mechanism for modules to set default configurations under their namespaces.
*
* The configuration manager starts by loading the root configuration file,
* which defines all the configuration namespaces. The root configuration file
* has a fixed JSON schema, that only allows namespaces to be defined there.
*
* After the namespaces are loaded into the configuration manager during
* initiation, the get() and set() functions will map configuration keys and
* values to the appropriate namespaces.
*
* e.g. get('ethereum.networks.goerli.networkID') will be mapped to
* ethereumNamespace.get('networks.goerli.networkID')
* e.g. set('ethereum.networks.goerli.networkID', 42) will be mapped to
* ethereumNamespace.set('networks.goerli.networkID', 42)
*
* File paths in the root configuration file may be defined as absolute paths
* or relative paths. Any relative paths would be rebased to the root
* configuration file's parent directory.
*
* The static function `setDefaults()` is expected to be called by gateway
* modules, to set default configurations under their own namespaces. Default
* configurations are used in the `get()` function if the corresponding config
* key is not found in its configuration namespace.
*/
readonly #namespaces: { [key: string]: ConfigurationNamespace };
private static _instance: ConfigManagerV2;
public static getInstance(): ConfigManagerV2 {
if (!ConfigManagerV2._instance) {
const rootPath = path.join(ConfigDir, 'root.yml');
if (!fs.existsSync(rootPath)) {
// copy from template
fs.copyFileSync(path.join(ConfigTemplatesDir, 'root.yml'), rootPath);
}
const listsPath = path.join(ConfigDir, 'lists');
if (!fs.existsSync(listsPath)) {
// copy from template
fse.copySync(path.join(ConfigTemplatesDir, 'lists'), listsPath);
}
ConfigManagerV2._instance = new ConfigManagerV2(rootPath);
}
return ConfigManagerV2._instance;
}
static defaults: ConfigurationDefaults = {};
constructor(configRootPath: string) {
this.#namespaces = {};
this.loadConfigRoot(configRootPath);
}
static setDefaults(namespaceId: string, defaultTree: Configuration) {
ConfigManagerV2.defaults[namespaceId] = defaultTree;
}
static getFromDefaults(namespaceId: string, configPath: string): any {
if (!(namespaceId in ConfigManagerV2.defaults)) {
return undefined;
}
const pathComponents: Array<string> = configPath.split('.');
const defaultConfig: Configuration = ConfigManagerV2.defaults[namespaceId];
let cursor: Configuration | any = defaultConfig;
for (const pathComponent of pathComponents) {
cursor = cursor[pathComponent];
if (cursor === undefined) {
return cursor;
}
}
return cursor;
}
get namespaces(): { [key: string]: ConfigurationNamespace } {
return this.#namespaces;
}
get allConfigurations(): { [key: string]: Configuration } {
const result: { [key: string]: Configuration } = {};
for (const [key, value] of Object.entries(this.#namespaces)) {
result[key] = value.configuration;
}
return result;
}
getNamespace(id: string): ConfigurationNamespace | undefined {
return this.#namespaces[id];
}
addNamespace(
id: string,
schemaPath: string,
configurationPath: string,
templatePath: string
): void {
this.#namespaces[id] = new ConfigurationNamespace(
id,
schemaPath,
configurationPath,
templatePath
);
}
unpackFullConfigPath(fullConfigPath: string): UnpackedConfigNamespace {
const pathComponents: Array<string> = fullConfigPath.split('.');
if (pathComponents.length < 2) {
throw new Error('Configuration paths must have at least two components.');
}
const namespaceComponent: string = pathComponents[0];
const namespace: ConfigurationNamespace | undefined =
this.#namespaces[namespaceComponent];
if (namespace === undefined) {
throw new Error(
`The configuration namespace ${namespaceComponent} does not exist.`
);
}
const configPath: string = pathComponents.slice(1).join('.');
return {
namespace,
configPath,
};
}
get(fullConfigPath: string): any {
const { namespace, configPath } = this.unpackFullConfigPath(fullConfigPath);
const configValue: any = namespace.get(configPath);
if (configValue === undefined) {
return ConfigManagerV2.getFromDefaults(namespace.id, configPath);
}
return configValue;
}
set(fullConfigPath: string, value: any) {
const { namespace, configPath } = this.unpackFullConfigPath(fullConfigPath);
namespace.set(configPath, value);
}
loadConfigRoot(configRootPath: string) {
// Load the config root file.
const configRootFullPath: string = fs.realpathSync(configRootPath);
const configRootTemplateFullPath: string = path.join(
ConfigTemplatesDir,
'root.yml'
);
const configRootDir: string = path.dirname(configRootFullPath);
const configRoot: ConfigurationRoot = yaml.load(
fs.readFileSync(configRootFullPath, 'utf8')
) as ConfigurationRoot;
const configRootTemplate: ConfigurationRoot = yaml.load(
fs.readFileSync(configRootTemplateFullPath, 'utf8')
) as ConfigurationRoot;
// version control to only handle upgrades
if (configRootTemplate.version > configRoot.version) {
// run migration in order if available
for (
let num = configRoot.version + 1;
num <= configRootTemplate.version;
num++
) {
if ((migrations as MigrationFunctions)[`updateToVersion${num}`]) {
(migrations as MigrationFunctions)[`updateToVersion${num}`](
configRootFullPath,
configRootTemplateFullPath
);
}
}
}
// Validate the config root file.
const validator: ValidateFunction = ajv.compile(
JSON.parse(fs.readFileSync(ConfigRootSchemaPath).toString())
);
if (!validator(configRoot)) {
throw new Error('Configuration root file is invalid.');
}
// Extract the namespace ids.
const namespaceMap: ConfigurationNamespaceDefinitions = {};
for (const namespaceKey of Object.keys(configRoot.configurations)) {
namespaceMap[namespaceKey.slice(NamespaceTag.length)] =
configRoot.configurations[namespaceKey];
}
// Rebase the file paths in config & template roots if they're relative paths.
for (const namespaceDefinition of Object.values(namespaceMap)) {
for (const [key, filePath] of Object.entries(namespaceDefinition)) {
if (!path.isAbsolute(filePath)) {
if (key === 'configurationPath') {
namespaceDefinition['templatePath'] = path.join(
ConfigTemplatesDir,
filePath
);
namespaceDefinition[key] = path.join(configRootDir, filePath);
} else if (key === 'schemaPath') {
namespaceDefinition[key] = path.join(
path.dirname(ConfigRootSchemaPath),
filePath
);
}
} else {
throw new Error(`Absolute path not allowed for ${key}.`);
}
}
}
// Add the namespaces according to config root.
for (const [namespaceId, namespaceDefinition] of Object.entries(
namespaceMap
)) {
this.addNamespace(
namespaceId,
namespaceDefinition.schemaPath,
namespaceDefinition.configurationPath,
namespaceDefinition.templatePath
);
}
}
}
export function resolveDBPath(oldPath: string): string {
if (oldPath.charAt(0) === '/') return oldPath;
const dbDir: string = path.join(rootPath(), 'db/');
fse.mkdirSync(dbDir, { recursive: true });
return path.join(dbDir, oldPath);
}