-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.ts
260 lines (241 loc) · 7.85 KB
/
start.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
import { flags } from '@oclif/command';
import cli from 'cli-ux';
import { existsSync } from 'fs';
import Command from '../base';
import { isChainUp } from '../common/chain';
import {
anyContainersUp,
anyVolumes,
createEmptyVolumes,
prepareDockerfile,
removeVolumes,
startContainers,
stopContainers,
} from '../common/containers';
import { isGraphQlUp } from '../common/graphql';
import { isRestUp, validateMnemonics } from '../common/rest';
import { getMetadata, loadSnapshot, Metadata, writeMetadata } from '../common/snapshots';
import { isSubqueryUp } from '../common/subquery';
import { isToolingUp } from '../common/tooling';
import { areUIsUp, clearUIs, fetchUIs } from '../common/uis';
import { hostNow, printInfo, retry } from '../common/util';
import { dataDir, defaultConfig, supportedChainVersions } from '../consts';
import { chainRunningError } from '../errors';
export default class Start extends Command {
static description = 'Start all the services';
static usage = 'start [OPTIONS]';
static flags = {
help: flags.help({ char: 'h' }),
version: flags.string({
char: 'v',
// Note: The actual value passed to the default function doesn't match its type. We use any so we can access the user config if its present
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default: (ctx: any) => {
return ctx.userConfig?.chainTag || defaultConfig.chainTag;
},
description: 'version of the containers to run. `latest` may cause changes between starts',
options: supportedChainVersions,
}),
image: flags.string({
char: 'i',
description:
'(Advanced) Specify a local docker image to use for Polymesh containers. Such an image should be debian based and have the polymesh node binary set as its entrypoint',
}),
chain: flags.string({
char: 'C',
description: '(Advanced) Specify a Polymesh runtime.',
options: [
'dev',
'local',
'testnet-dev',
'testnet-local',
'testnet-bootstrap',
'mainnet-dev',
'mainnet-local',
'mainnet-bootstrap',
'mainnet',
'testnet',
],
}),
snapshot: flags.string({
char: 's',
description:
'Loads snapshot before starting. Current state used if not passed. Can be a "name", path to local file or remote URL',
}),
clean: flags.boolean({
char: 'c',
default: false,
description: 'Cleans state before starting',
}),
only: flags.string({
multiple: true,
default: ['chain', 'subquery', 'gql', 'rest', 'uis'],
char: 'o',
description: 'Run only some services',
options: ['chain', 'subquery', 'gql', 'rest', 'uis'],
}),
verbose: flags.boolean({
description: 'enables verbose logging',
default: false,
}),
restSigners: flags.string({
description: 'Comma separated list of signers available in the rest api. Defaults to `alice`',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default: (ctx: any) => {
return ctx.userConfig?.restSigners || 'alice';
},
}),
restMnemonics: flags.string({
description: 'Comma separated list of signer mnemonics. Defaults to `//Alice`',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default: (ctx: any) => {
return ctx.userConfig?.restMnemonics || '//Alice';
},
}),
vaultUrl: flags.string({
description: 'The URL the Vault transit engine to use with the REST API',
default: '',
}),
vaultToken: flags.string({
description: 'The Vault API key to use with the REST API',
default: '',
}),
uiLatest: flags.boolean({
char: 'u',
description: 'Clears saved UIs so the latest can be fetched',
default: false,
}),
};
async run(): Promise<void> {
const { flags: commandFlags } = this.parse(Start);
const {
clean,
snapshot,
verbose,
version,
image,
chain,
only,
restSigners,
restMnemonics,
uiLatest,
vaultUrl,
vaultToken,
} = commandFlags;
const typedOnly = only as ('chain' | 'subquery' | 'gql' | 'rest' | 'uis')[];
if (await anyContainersUp(this, verbose)) {
this.error(chainRunningError);
}
if (clean) {
cli.action.start('Removing old state');
removeVolumes();
cli.action.stop();
}
if (uiLatest) {
cli.action.start('Clearing current UIs');
clearUIs();
cli.action.stop();
}
const mnemonicValidation = validateMnemonics(restMnemonics, restSigners);
if (typeof mnemonicValidation === 'string') {
this.error(mnemonicValidation);
}
if (!anyVolumes()) {
cli.action.start('No volumes detected. Initializing volumes');
createEmptyVolumes();
cli.action.stop();
}
let metadata: Metadata;
if (snapshot) {
cli.action.start('Loading chain snapshot');
await loadSnapshot(this, snapshot);
cli.action.stop();
}
if (!existsSync(dataDir)) {
cli.action.start('No previous data found. Initializing data directory');
metadata = { version, time: hostNow(), startedAt: '', chain: chain || 'dev' };
if (image) {
metadata.version = image;
}
cli.action.stop();
} else {
cli.log('Found existing data');
metadata = getMetadata();
}
if (!image && version !== metadata.version) {
this.error(
`Polymesh version ${version} was specified, but data was for ${metadata.version}. Either use "--clean" to start with a fresh state, or load a snapshot that matches the version`
);
}
if (chain && chain !== metadata.chain) {
this.error(
`Polymesh chain ${chain} was specified, but data was for ${metadata.chain}. Either use "--clean" to start with a fresh state, or load a snapshot that matches the chain`
);
}
metadata.startedAt = new Date().toISOString();
writeMetadata(metadata);
if (only.includes('uis')) {
cli.action.start('Checking UIs');
await fetchUIs(version);
cli.action.stop();
}
cli.action.start(`Preparing dockerfile for Polymesh version: ${image || version}`);
prepareDockerfile(version, image);
cli.action.stop();
const services = typedOnly.flatMap(o => {
switch (o) {
case 'chain':
return ['alice', 'bob', 'charlie', 'schema'];
case 'subquery':
return ['subquery', 'graphql'];
case 'gql':
return ['tooling'];
case 'rest':
return ['rest_api'];
case 'uis':
return ['dashboard', 'bridge', 'governance', 'issuer'];
}
return [];
});
cli.action.start('Starting the containers (this may take a while the first time)');
await startContainers(
this,
version,
metadata.time,
verbose,
metadata.chain,
services,
restSigners,
restMnemonics,
vaultUrl,
vaultToken,
this.userConfig
);
cli.action.stop();
const allChecks = {
chain: isChainUp,
subquery: isSubqueryUp,
gql: isToolingUp,
graphql: isGraphQlUp,
rest: isRestUp,
uis: areUIsUp,
};
const checks = typedOnly.map(o => allChecks[o]);
cli.action.start('Checking service liveness. This can take up to 5 minutes (Ctrl-C to skip)');
const results = await Promise.all(checks.map(c => retry(c)));
if (!results.every(Boolean)) {
const resultMsgs = checks.map((c, i) => `${c.name}: ${results[i]}`);
await stopContainers(this, verbose);
this.error(
`At least one of the required services did not launch correctly. Results: \n${resultMsgs.join(
'\n'
)}.\nInspect the docker logs to diagnose the problem`,
{ exit: 2 }
);
}
cli.action.stop();
this.log(); // implicit newline
printInfo(this);
this.log('\nHappy testing!');
}
}