-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprofile_manager.ts
466 lines (386 loc) · 17.5 KB
/
profile_manager.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
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import fs from 'fs'
import path from 'path'
import { SoloError, IllegalArgumentError, MissingArgumentError } from './errors.ts'
import * as yaml from 'js-yaml'
import { flags } from '../commands/index.ts'
import { type ConfigManager, constants, helpers, Templates } from './index.ts'
import dot from 'dot-object'
import { getNodeAccountMap } from './helpers.ts'
import * as semver from 'semver'
import { readFile, writeFile } from 'fs/promises'
import { type SoloLogger } from './logging.ts'
import { type SemVer } from 'semver'
import { type NodeAlias, type NodeAliases } from '../types/aliases.ts'
const consensusSidecars = [
'recordStreamUploader', 'eventStreamUploader', 'backupUploader', 'accountBalanceUploader', 'otelCollector']
export class ProfileManager {
private readonly logger: SoloLogger
private readonly configManager: ConfigManager
private readonly cacheDir: string
private profiles: Map<string, object>
private profileFile: string | undefined
constructor (logger: SoloLogger, configManager: ConfigManager, cacheDir: string = constants.SOLO_VALUES_DIR) {
if (!logger) throw new MissingArgumentError('An instance of core/SoloLogger is required')
if (!configManager) throw new MissingArgumentError('An instance of core/ConfigManager is required')
this.logger = logger
this.configManager = configManager
this.profiles = new Map()
cacheDir = path.resolve(cacheDir)
this.cacheDir = cacheDir
}
loadProfiles (forceReload = false): Map<string, object> {
const profileFile = this.configManager.getFlag<string>(flags.profileFile)
if (!profileFile) throw new MissingArgumentError('profileFile is required')
// return the cached value as quickly as possible
if (this.profiles && this.profileFile === profileFile && !forceReload) {
return this.profiles
}
if (!fs.existsSync(profileFile)) throw new IllegalArgumentError(`profileFile does not exist: ${profileFile}`)
// load profile file
this.profiles = new Map()
const yamlData = fs.readFileSync(profileFile, 'utf8')
const profileItems = yaml.load(yamlData) as Record<string, object>
// add profiles
for (const key in profileItems) {
let profile = profileItems[key]
profile = profile || {}
this.profiles.set(key, profile)
}
this.profileFile = profileFile
return this.profiles
}
getProfile (profileName: string): object {
if (!profileName) throw new MissingArgumentError('profileName is required')
if (!this.profiles || this.profiles.size <= 0) {
this.loadProfiles()
}
if (!this.profiles || !this.profiles.has(profileName)) throw new IllegalArgumentError(`Profile does not exists with name: ${profileName}`)
return this.profiles.get(profileName) as object
}
/**
* Set value in the yaml object
* @param itemPath - item path in the yaml
* @param value - value to be set
* @param yamlRoot - root of the yaml object
* @returns
*/
_setValue (itemPath: string, value: any, yamlRoot: object): object {
// find the location where to set the value in the yaml
const itemPathParts: string[] = itemPath.split('.')
let parent = yamlRoot
let current = parent
let prevItemPath = ''
for (let itemPathPart of itemPathParts) {
if (helpers.isNumeric(itemPathPart)) {
// @ts-ignore
itemPathPart = Number.parseInt(itemPathPart) // numeric path part can only be array index i.e. an integer
if (!Array.isArray(parent[prevItemPath])) {
parent[prevItemPath] = []
}
if (!parent[prevItemPath][itemPathPart]) {
parent[prevItemPath][itemPathPart] = {}
}
parent = parent[prevItemPath]
prevItemPath = itemPathPart
current = parent[itemPathPart]
} else {
if (!current[itemPathPart]) {
current[itemPathPart] = {}
}
parent = current
prevItemPath = itemPathPart
current = parent[itemPathPart]
}
}
parent[prevItemPath] = value
return yamlRoot
}
/**
* Set items for the chart
* @param itemPath - item path in the yaml, if empty then root of the yaml object will be used
* @param items - the element object
* @param yamlRoot - root of the yaml object to update
* @private
*/
_setChartItems (itemPath: string, items: any, yamlRoot: object) {
if (!items) return
const dotItems = dot.dot(items)
for (const key in dotItems) {
let itemKey = key
// if it is an array key like extraEnv[0].JAVA_OPTS, convert it into dot separated key as extraEnv.0.JAVA_OPTS
if (key.indexOf('[') !== -1) {
itemKey = key.replace('[', '.').replace(']', '')
}
if (itemPath) {
this._setValue(`${itemPath}.${itemKey}`, dotItems[key], yamlRoot)
} else {
this._setValue(itemKey, dotItems[key], yamlRoot)
}
}
}
resourcesForConsensusPod (profile: any, nodeAliases: NodeAliases, yamlRoot: object): object {
if (!profile) throw new MissingArgumentError('profile is required')
const accountMap = getNodeAccountMap(nodeAliases)
// set consensus pod level resources
for (let nodeIndex = 0; nodeIndex < nodeAliases.length; nodeIndex++) {
this._setValue(`hedera.nodes.${nodeIndex}.name`, nodeAliases[nodeIndex], yamlRoot)
this._setValue(`hedera.nodes.${nodeIndex}.accountId`, accountMap.get(nodeAliases[nodeIndex]), yamlRoot)
}
const stagingDir = Templates.renderStagingDir(
this.configManager.getFlag(flags.cacheDir),
this.configManager.getFlag(flags.releaseTag)
)
if (!fs.existsSync(stagingDir)) {
fs.mkdirSync(stagingDir, { recursive: true })
}
const configTxtPath = this.prepareConfigTxt(
this.configManager.getFlag(flags.namespace),
accountMap,
stagingDir,
this.configManager.getFlag(flags.releaseTag),
this.configManager.getFlag(flags.app),
this.configManager.getFlag(flags.chainId))
for (const flag of flags.nodeConfigFileFlags.values()) {
const filePath = this.configManager.getFlag<string>(flag)
if (!filePath) {
throw new SoloError(`Configuration file path is missing for: ${flag.name}`)
}
const fileName = path.basename(filePath)
const destPath = path.join(stagingDir, 'templates', fileName)
this.logger.debug(`Copying configuration file to staging: ${filePath} -> ${destPath}`)
fs.cpSync(filePath, destPath, { force: true })
}
this._setFileContentsAsValue('hedera.configMaps.configTxt', configTxtPath, yamlRoot)
this._setFileContentsAsValue('hedera.configMaps.log4j2Xml', path.join(stagingDir, 'templates', 'log4j2.xml'), yamlRoot)
this._setFileContentsAsValue('hedera.configMaps.settingsTxt', path.join(stagingDir, 'templates', 'settings.txt'), yamlRoot)
this._setFileContentsAsValue('hedera.configMaps.applicationProperties', path.join(stagingDir, 'templates', 'application.properties'), yamlRoot)
this._setFileContentsAsValue('hedera.configMaps.apiPermissionsProperties', path.join(stagingDir, 'templates', 'api-permission.properties'), yamlRoot)
this._setFileContentsAsValue('hedera.configMaps.bootstrapProperties', path.join(stagingDir, 'templates', 'bootstrap.properties'), yamlRoot)
if (this.configManager.getFlag(flags.applicationEnv)) {
this._setFileContentsAsValue('hedera.configMaps.applicationEnv', this.configManager.getFlag(flags.applicationEnv), yamlRoot)
}
if (profile.consensus) {
// set default for consensus pod
this._setChartItems('defaults.root', profile.consensus.root, yamlRoot)
// set sidecar resources
for (const sidecar of consensusSidecars) {
this._setChartItems(`defaults.sidecars.${sidecar}`, profile.consensus[sidecar], yamlRoot)
}
}
return yamlRoot
}
resourcesForHaProxyPod (profile: any, yamlRoot: object) {
if (!profile) throw new MissingArgumentError('profile is required')
if (!profile.haproxy) return // use chart defaults
return this._setChartItems('defaults.haproxy', profile.haproxy, yamlRoot)
}
resourcesForEnvoyProxyPod (profile: any, yamlRoot: object) {
if (!profile) throw new MissingArgumentError('profile is required')
if (!profile.envoyProxy) return // use chart defaults
return this._setChartItems('defaults.envoyProxy', profile.envoyProxy, yamlRoot)
}
resourcesForHederaExplorerPod (profile: any, yamlRoot: object) {
if (!profile) throw new MissingArgumentError('profile is required')
if (!profile.explorer) return
return this._setChartItems('hedera-explorer', profile.explorer, yamlRoot)
}
resourcesForMinioTenantPod (profile: any, yamlRoot: object) {
if (!profile) throw new MissingArgumentError('profile is required')
// @ts-ignore
if (!profile.minio || !profile.minio.tenant) return // use chart defaults
for (const poolIndex in profile.minio.tenant.pools) {
const pool = profile.minio.tenant.pools[poolIndex]
for (const prop in pool) {
if (prop !== 'resources') {
this._setValue(`minio-server.tenant.pools.${poolIndex}.${prop}`, pool[prop], yamlRoot)
}
}
this._setChartItems(`minio-server.tenant.pools.${poolIndex}`, pool, yamlRoot)
}
return yamlRoot
}
/**
* Prepare a values file for Solo Helm chart
* @param profileName resource profile name
* @returns return the full path to the values file
*/
prepareValuesForSoloChart (profileName: string) {
if (!profileName) throw new MissingArgumentError('profileName is required')
const profile = this.getProfile(profileName)
const nodeAliases = helpers.parseNodeAliases(this.configManager.getFlag(flags.nodeAliasesUnparsed))
if (!nodeAliases) throw new SoloError('Node IDs are not set in the config')
// generate the yaml
const yamlRoot = {}
this.resourcesForConsensusPod(profile, nodeAliases, yamlRoot)
this.resourcesForHaProxyPod(profile, yamlRoot)
this.resourcesForEnvoyProxyPod(profile, yamlRoot)
this.resourcesForMinioTenantPod(profile, yamlRoot)
// write the yaml
const cachedValuesFile = path.join(this.cacheDir, `solo-${profileName}.yaml`)
return new Promise<string>((resolve, reject) => {
fs.writeFile(cachedValuesFile, yaml.dump(yamlRoot), (err) => {
if (err) {
reject(err)
}
resolve(cachedValuesFile)
})
})
}
async bumpHederaConfigVersion (applicationPropertiesPath: string) {
const lines = (await readFile(applicationPropertiesPath, 'utf-8')).split('\n')
for (const line of lines) {
if (line.startsWith('hedera.config.version=')) {
const version = parseInt(line.split('=')[1]) + 1
lines[lines.indexOf(line)] = `hedera.config.version=${version}`
break
}
}
await writeFile(applicationPropertiesPath, lines.join('\n'))
}
async prepareValuesForNodeAdd (configTxtPath: string, applicationPropertiesPath: string) {
const yamlRoot = {}
this._setFileContentsAsValue('hedera.configMaps.configTxt', configTxtPath, yamlRoot)
await this.bumpHederaConfigVersion(applicationPropertiesPath)
this._setFileContentsAsValue('hedera.configMaps.applicationProperties', applicationPropertiesPath, yamlRoot)
// write the yaml
const cachedValuesFile = path.join(this.cacheDir, 'solo-node-add.yaml')
return new Promise<string>((resolve, reject) => {
fs.writeFile(cachedValuesFile, yaml.dump(yamlRoot), (err) => {
if (err) {
reject(err)
}
resolve(cachedValuesFile)
})
})
}
/**
* Prepare a values file for rpc-relay Helm chart
* @param profileName - resource profile name
* @returns return the full path to the values file
*/
prepareValuesForRpcRelayChart (profileName: string) {
if (!profileName) throw new MissingArgumentError('profileName is required')
const profile = this.getProfile(profileName) as any
if (!profile.rpcRelay) return Promise.resolve()// use chart defaults
// generate the yaml
const yamlRoot = {}
this._setChartItems('', profile.rpcRelay, yamlRoot)
// write the yaml
const cachedValuesFile = path.join(this.cacheDir, `rpcRelay-${profileName}.yaml`)
return new Promise<string>((resolve, reject) => {
fs.writeFile(cachedValuesFile, yaml.dump(yamlRoot), (err) => {
if (err) {
reject(err)
}
resolve(cachedValuesFile)
})
})
}
/**
* Prepare a values file for mirror-node Helm chart
* @param profileName - resource profile name
* @returns return the full path to the values file
*/
prepareValuesForMirrorNodeChart (profileName: string){
if (!profileName) throw new MissingArgumentError('profileName is required')
const profile = this.getProfile(profileName) as any
if (!profile.mirror) return Promise.resolve() // use chart defaults
// generate the yaml
const yamlRoot = {}
if (profile.mirror.postgresql) {
if (profile.mirror.postgresql.persistence) {
this._setValue('hedera-mirror-node.postgresql.persistence.size', profile.mirror.postgresql.persistence.size, yamlRoot)
}
this._setChartItems('hedera-mirror-node.postgresql.postgresql', profile.mirror.postgresql.postgresql, yamlRoot)
}
this._setChartItems('hedera-mirror-node.importer', profile.mirror.importer, yamlRoot)
this._setChartItems('hedera-mirror-node.rest', profile.mirror.rest, yamlRoot)
this._setChartItems('hedera-mirror-node.web3', profile.mirror.web3, yamlRoot)
this._setChartItems('hedera-mirror-node.grpc', profile.mirror.grpc, yamlRoot)
this._setChartItems('hedera-mirror-node.monitor', profile.mirror.monitor, yamlRoot)
this.resourcesForHederaExplorerPod(profile, yamlRoot)
// write the yaml
const cachedValuesFile = path.join(this.cacheDir, `mirror-${profileName}.yaml`)
return new Promise<string>((resolve, reject) => {
fs.writeFile(cachedValuesFile, yaml.dump(yamlRoot), (err) => {
if (err) {
reject(err)
}
resolve(cachedValuesFile)
})
})
}
/**
* Writes the contents of a file as a value for the given nested item path in the yaml object
* @param itemPath - nested item path in the yaml object to store the file contents
* @param valueFilePath - path to the file whose contents will be stored in the yaml object
* @param yamlRoot - root of the yaml object
*/
private _setFileContentsAsValue (itemPath: string, valueFilePath: string, yamlRoot: object) {
const fileContents = fs.readFileSync(valueFilePath, 'utf8')
this._setValue(itemPath, fileContents, yamlRoot)
}
/**
* Prepares config.txt file for the node
* @param namespace - namespace where the network is deployed
* @param nodeAccountMap - the map of node aliases to account IDs
* @param destPath - path to the destination directory to write the config.txt file
* @param releaseTag - release tag e.g. v0.42.0
* @param [appName] - the app name (default: HederaNode.jar)
* @param [chainId] - chain ID (298 for local network)
* @returns the config.txt file path
*/
prepareConfigTxt (namespace: string, nodeAccountMap: Map<NodeAlias, string>, destPath: string, releaseTag: string,
appName = constants.HEDERA_APP_NAME, chainId = constants.HEDERA_CHAIN_ID) {
if (!nodeAccountMap || nodeAccountMap.size === 0) throw new MissingArgumentError('nodeAccountMap the map of node IDs to account IDs is required')
if (!releaseTag) throw new MissingArgumentError('release tag is required')
if (!fs.existsSync(destPath)) throw new IllegalArgumentError(`config destPath does not exist: ${destPath}`, destPath)
// init variables
const internalPort = constants.HEDERA_NODE_INTERNAL_GOSSIP_PORT
const externalPort = constants.HEDERA_NODE_EXTERNAL_GOSSIP_PORT
const nodeStakeAmount = constants.HEDERA_NODE_DEFAULT_STAKE_AMOUNT
// @ts-ignore
const releaseVersion = semver.parse(releaseTag, { includePrerelease: true }) as SemVer
try {
const configLines: string[] = []
configLines.push(`swirld, ${chainId}`)
configLines.push(`app, ${appName}`)
let nodeSeq = 0
for (const nodeAlias of nodeAccountMap.keys()) {
const internalIP = Templates.renderFullyQualifiedNetworkPodName(namespace, nodeAlias)
const externalIP = Templates.renderFullyQualifiedNetworkSvcName(namespace, nodeAlias)
const account = nodeAccountMap.get(nodeAlias)
if (releaseVersion.minor >= 40) {
configLines.push(`address, ${nodeSeq}, ${nodeSeq}, ${nodeAlias}, ${nodeStakeAmount}, ${internalIP}, ${internalPort}, ${externalIP}, ${externalPort}, ${account}`)
} else {
configLines.push(`address, ${nodeSeq}, ${nodeAlias}, ${nodeStakeAmount}, ${internalIP}, ${internalPort}, ${externalIP}, ${externalPort}, ${account}`)
}
nodeSeq += 1
}
if (releaseVersion.minor >= 41) {
configLines.push(`nextNodeId, ${nodeSeq}`)
}
const configFilePath = path.join(destPath, 'config.txt')
fs.writeFileSync(configFilePath, configLines.join('\n'))
return configFilePath
} catch (e: Error | any) {
throw new SoloError('failed to generate config.txt', e)
}
}
}