-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrelay.ts
393 lines (332 loc) · 12.4 KB
/
relay.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
/**
* 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 { Listr } from 'listr2'
import { SoloError, MissingArgumentError } from '../core/errors.ts'
import * as helpers from '../core/helpers.ts'
import type { ProfileManager, AccountManager } from '../core/index.ts'
import { constants } from '../core/index.ts'
import { BaseCommand } from './base.ts'
import * as flags from './flags.ts'
import * as prompts from './prompts.ts'
import { getNodeAccountMap } from '../core/helpers.ts'
import { type NodeAliases } from '../types/aliases.ts'
import { type Opts } from '../types/index.ts'
export class RelayCommand extends BaseCommand {
private readonly profileManager: ProfileManager
private readonly accountManager: AccountManager
constructor (opts: Opts) {
super(opts)
if (!opts || !opts.profileManager) throw new MissingArgumentError('An instance of core/ProfileManager is required', opts.downloader)
this.profileManager = opts.profileManager
this.accountManager = opts.accountManager
}
static get DEPLOY_CONFIGS_NAME () {
return 'deployConfigs'
}
static get DEPLOY_FLAGS_LIST () {
return [
flags.chainId,
flags.chartDirectory,
flags.namespace,
flags.nodeAliasesUnparsed,
flags.operatorId,
flags.operatorKey,
flags.profileFile,
flags.profileName,
flags.quiet,
flags.relayReleaseTag,
flags.replicaCount,
flags.valuesFile
]
}
static get DESTROY_FLAGS_LIST () {
return [
flags.chartDirectory,
flags.namespace,
flags.nodeAliasesUnparsed
]
}
async prepareValuesArg (valuesFile: string, nodeAliases: NodeAliases, chainID: string, relayRelease: string,
replicaCount: number, operatorID: string, operatorKey: string, namespace: string) {
let valuesArg = ''
const profileName = this.configManager.getFlag<string>(flags.profileName) as string
const profileValuesFile = await this.profileManager.prepareValuesForRpcRelayChart(profileName)
if (profileValuesFile) {
valuesArg += this.prepareValuesFiles(profileValuesFile)
}
valuesArg += ` --set config.MIRROR_NODE_URL=http://${constants.SOLO_DEPLOYMENT_CHART}-rest`
valuesArg += ` --set config.MIRROR_NODE_URL_WEB3=http://${constants.SOLO_DEPLOYMENT_CHART}-web3`
valuesArg += ' --set config.MIRROR_NODE_AGENT_CACHEABLE_DNS=false'
valuesArg += ' --set config.MIRROR_NODE_RETRY_DELAY=2001'
valuesArg += ' --set config.MIRROR_NODE_GET_CONTRACT_RESULTS_DEFAULT_RETRIES=21'
if (chainID) {
valuesArg += ` --set config.CHAIN_ID=${chainID}`
}
if (relayRelease) {
valuesArg += ` --set image.tag=${relayRelease.replace(/^v/, '')}`
}
if (replicaCount) {
valuesArg += ` --set replicaCount=${replicaCount}`
}
if (operatorID) {
valuesArg += ` --set config.OPERATOR_ID_MAIN=${operatorID}`
}
if (operatorKey) {
valuesArg += ` --set config.OPERATOR_KEY_MAIN=${operatorKey}`
}
if (!nodeAliases) {
throw new MissingArgumentError('Node IDs must be specified')
}
const networkJsonString = await this.prepareNetworkJsonString(nodeAliases, namespace)
valuesArg += ` --set config.HEDERA_NETWORK='${networkJsonString}'`
if (valuesFile) {
valuesArg += this.prepareValuesFiles(valuesFile)
}
return valuesArg
}
/**
* created a json string to represent the map between the node keys and their ids
* output example '{"node-1": "0.0.3", "node-2": "0.004"}'
*/
async prepareNetworkJsonString (nodeAliases: NodeAliases = [], namespace: string) {
if (!nodeAliases) {
throw new MissingArgumentError('Node IDs must be specified')
}
const networkIds = {}
const accountMap = getNodeAccountMap(nodeAliases)
const networkNodeServicesMap = await this.accountManager.getNodeServiceMap(namespace)
nodeAliases.forEach(nodeAlias => {
const haProxyClusterIp = networkNodeServicesMap.get(nodeAlias).haProxyClusterIp
const haProxyGrpcPort = networkNodeServicesMap.get(nodeAlias).haProxyGrpcPort
const networkKey = `${haProxyClusterIp}:${haProxyGrpcPort}`
networkIds[networkKey] = accountMap.get(nodeAlias)
})
return JSON.stringify(networkIds)
}
prepareReleaseName (nodeAliases: NodeAliases = []) {
if (!nodeAliases) {
throw new MissingArgumentError('Node IDs must be specified')
}
let releaseName = 'relay'
nodeAliases.forEach(nodeAlias => {
releaseName += `-${nodeAlias}`
})
return releaseName
}
async deploy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()
interface RelayDeployConfigClass {
chainId: string
chartDirectory: string
namespace: string
nodeAliasesUnparsed: string
operatorId: string
operatorKey: string
profileFile: string
profileName: string
relayReleaseTag: string
replicaCount: number
valuesFile: string
chartPath: string
isChartInstalled: boolean
nodeAliases: NodeAliases
releaseName: string
valuesArg: string
getUnusedConfigs: () => string[]
}
interface Context {
config: RelayDeployConfigClass
}
const tasks = new Listr<Context>([
{
title: 'Initialize',
task: async (ctx, task) => {
// reset nodeAlias
self.configManager.setFlag(flags.nodeAliasesUnparsed, '')
self.configManager.update(argv)
await prompts.execute(task, self.configManager, RelayCommand.DEPLOY_FLAGS_LIST)
// prompt if inputs are empty and set it in the context
ctx.config = this.getConfig(RelayCommand.DEPLOY_CONFIGS_NAME, RelayCommand.DEPLOY_FLAGS_LIST,
['nodeAliases']) as RelayDeployConfigClass
ctx.config.nodeAliases = helpers.parseNodeAliases(ctx.config.nodeAliasesUnparsed)
ctx.config.releaseName = self.prepareReleaseName(ctx.config.nodeAliases)
ctx.config.isChartInstalled = await self.chartManager.isChartInstalled(ctx.config.namespace, ctx.config.releaseName)
self.logger.debug('Initialized config', { config: ctx.config })
return lease.buildAcquireTask(task)
}
},
{
title: 'Prepare chart values',
task: async (ctx) => {
const config = ctx.config
config.chartPath = await self.prepareChartPath(config.chartDirectory, constants.JSON_RPC_RELAY_CHART, constants.JSON_RPC_RELAY_CHART)
config.valuesArg = await self.prepareValuesArg(
config.valuesFile,
config.nodeAliases,
config.chainId,
config.relayReleaseTag,
config.replicaCount,
config.operatorId,
config.operatorKey,
config.namespace
)
}
},
{
title: 'Deploy JSON RPC Relay',
task: async (ctx) => {
const config = ctx.config
await self.chartManager.install(config.namespace, config.releaseName, config.chartPath, '', config.valuesArg)
await self.k8.waitForPods([constants.POD_PHASE_RUNNING], [
'app=hedera-json-rpc-relay',
`app.kubernetes.io/instance=${config.releaseName}`
], 1, 900, 1000)
// reset nodeAlias
self.configManager.setFlag(flags.nodeAliasesUnparsed, '')
}
},
{
title: 'Check relay is ready',
task: async (ctx) => {
const config = ctx.config
try {
await self.k8.waitForPodReady([
'app=hedera-json-rpc-relay',
`app.kubernetes.io/instance=${config.releaseName}`
], 1, 100, 2000)
} catch (e: Error | any) {
throw new SoloError(`Relay ${config.releaseName} is not ready: ${e.message}`, e)
}
}
}
], {
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION
})
try {
await tasks.run()
} catch (e: Error | any) {
throw new SoloError('Error installing relays', e)
} finally {
await lease.release()
}
return true
}
async destroy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()
interface RelayDestroyConfigClass {
chartDirectory: string
namespace: string
nodeAliases: NodeAliases
releaseName: string
isChartInstalled: boolean
}
interface Context {
config: RelayDestroyConfigClass
}
const tasks = new Listr<Context>([
{
title: 'Initialize',
task: async (ctx, task) => {
// reset nodeAlias
self.configManager.setFlag(flags.nodeAliasesUnparsed, '')
self.configManager.update(argv)
await prompts.execute(task, self.configManager, RelayCommand.DESTROY_FLAGS_LIST)
// prompt if inputs are empty and set it in the context
ctx.config = {
chartDirectory: self.configManager.getFlag<string>(flags.chartDirectory) as string,
namespace: self.configManager.getFlag<string>(flags.namespace) as string,
nodeAliases: helpers.parseNodeAliases((self.configManager.getFlag<string>(flags.nodeAliasesUnparsed) as string))
} as RelayDestroyConfigClass
ctx.config.releaseName = this.prepareReleaseName(ctx.config.nodeAliases)
ctx.config.isChartInstalled = await this.chartManager.isChartInstalled(ctx.config.namespace, ctx.config.releaseName)
self.logger.debug('Initialized config', { config: ctx.config })
return lease.buildAcquireTask(task)
}
},
{
title: 'Destroy JSON RPC Relay',
task: async (ctx) => {
const config = ctx.config
await this.chartManager.uninstall(config.namespace, config.releaseName)
this.logger.showList('Destroyed Relays', await self.chartManager.getInstalledCharts(config.namespace))
// reset nodeAliasesUnparsed
self.configManager.setFlag(flags.nodeAliasesUnparsed, '')
},
skip: (ctx) => !ctx.config.isChartInstalled
}
], {
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION
})
try {
await tasks.run()
} catch (e: Error | any) {
throw new SoloError('Error uninstalling relays', e)
} finally {
await lease.release()
}
return true
}
getCommandDefinition (): { command: string; desc: string; builder: Function } {
const relayCmd = this
return {
command: 'relay',
desc: 'Manage JSON RPC relays in solo network',
builder: (yargs: any) => {
return yargs
.command({
command: 'deploy',
desc: 'Deploy a JSON RPC relay',
builder: (y: any) => {
flags.setCommandFlags(y, ...RelayCommand.DEPLOY_FLAGS_LIST)
},
handler: (argv: any) => {
relayCmd.logger.debug("==== Running 'relay install' ===", { argv })
relayCmd.deploy(argv).then(r => {
relayCmd.logger.debug('==== Finished running `relay install`====')
if (!r) process.exit(1)
}).catch(err => {
relayCmd.logger.showUserError(err)
process.exit(1)
})
}
})
.command({
command: 'destroy',
desc: 'Destroy JSON RPC relay',
builder: (y: any) => flags.setCommandFlags(y,
flags.chartDirectory,
flags.namespace,
flags.nodeAliasesUnparsed
),
handler: (argv: any) => {
relayCmd.logger.debug("==== Running 'relay uninstall' ===", { argv })
relayCmd.logger.debug(argv)
relayCmd.destroy(argv).then(r => {
relayCmd.logger.debug('==== Finished running `relay uninstall`====')
if (!r) process.exit(1)
})
}
})
.demandCommand(1, 'Select a relay command')
}
}
}
}