-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathathena.js
350 lines (301 loc) · 9.29 KB
/
athena.js
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
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
// Node
const path = require('path');
const fs = require('fs');
// External
const pm2 = require('pm2');
const yargs = require('yargs');
const dotenv = require('dotenv');
// Project
const {getParsedSettings, log} = require('./src/utils');
const Athena = require('./src/bootstrap');
const commands = require('./src/cluster/commands');
dotenv.config();
process.on('uncaughtException', function (error) {
log.error(error);
});
// Properties
let athena = null;
let cluster = null;
let settings = null;
const should = {};
// Constants
const APP_NAME = 'athena';
const MAIN_SCRIPT_NAME = `${APP_NAME}.js`;
// CLI commands and flags.
const options = yargs
.options({
'debug': {
alias: 'D',
describe: 'enable debug mode',
type: 'boolean'
}
})
.command('run', 'manage Athena tests', {
'tests': {
alias: 't',
describe: 'the tests directory [functional, performance]',
type: 'string'
},
'grep': {
alias: 'g',
describe: 'run only specific tests [functional]',
type: 'string'
},
'bail': {
alias: 'b',
describe: 'fail fast after the first test failure [functional]',
type: 'boolean'
},
'functional': {
alias: 'f',
describe: 'run functional tests',
type: 'boolean',
default: true
},
'performance': {
alias: 'p',
describe: 'run only performance tests',
type: 'boolean',
default: false
},
'cluster': {
describe: 'run the tests inside the cluster [performance]',
type: 'boolean',
default: false
},
'reporter': {
describe: 'provide the reporter type',
type: 'string',
default: null // athena-json-stream
}
})
.command('cluster', 'manage an Athena cluster', {
'addr': {
alias: 'a',
describe: 'the cluster manager\'s address',
type: 'string'
},
'token': {
alias: 'T',
describe: 'the cluster\'s access token',
type: 'string'
},
'init': {
alias: 'i',
describe: 'initiate a new Athena cluster',
type: 'boolean'
},
'join': {
alias: 'j',
describe: 'join an Athena cluster',
type: 'boolean'
},
'foreground': {
describe: 'whether to run the cluster in foreground (internal)',
type: 'boolean',
default: false
},
'run': {
describe: '', // todo:
type: 'boolean'
}
})
.command('preview', 'pretty print the structure of a tests suite', {
'performance': {
describe: 'the performance tests tree view',
type: Boolean,
default: false
},
'functional': {
describe: 'the functional tests tree view',
type: Boolean,
default: false
}
})
.help()
.version()
.argv;
settings = getParsedSettings(options);
/**
* Checks whether the provided commands were used.
* @param commands string The list of commands.
* @return {boolean} True if the provided commands were used, false otherwise.
*/
const requiredCommands = (...commands) => {
return settings
._
.some((cmd) => commands.indexOf(cmd) !== -1);
};
// Define CLI conditions.
should.initClusterInForeground = requiredCommands('cluster') && settings.init && settings.foreground;
should.initClusterInBackground = requiredCommands('cluster') && settings.init && !settings.foreground;
should.joinCluster = requiredCommands('cluster') && settings.join;
should.joinClusterInForeground = should.joinCluster && settings.foreground;
should.initCluster = should.initClusterInBackground || should.initClusterInForeground || should.joinCluster;
should.delegateClusterCommand = requiredCommands('cluster') && settings.run;
should.runFunctionalTests = requiredCommands('run') && settings.functional;
should.runPerformanceTests = requiredCommands('run') && settings.performance;
should.k8sNonInteractive = requiredCommands('cluster') && settings.foreground && settings.k8s;
should.runTests = should.runFunctionalTests || should.runPerformanceTests;
should.initAthena = should.initCluster || should.runTests;
// Iife to avoid process exits.
(function () {
if (should.initAthena) {
athena = new Athena(settings);
}
if (should.initCluster) {
const Cluster = require('./src/cluster');
cluster = new Cluster(athena);
}
// Join a k8s cluster in non-interactive mode.
// command: node athena.js cluster --join --foreground --k8s
if (should.k8sNonInteractive) {
cluster.joinNonInteractive();
return;
}
// command: node athena.js cluster --init --addr <IP>
// Initiates a new Athena
if (should.initClusterInBackground) {
if (!settings.addr) {
log.error(`The --addr is required when initializing a new Athena cluster.`);
}
const args = ['cluster', '--init', `--addr ${settings.addr}`, '--foreground'];
let maybeWatch = false;
if (settings.debug) {
args.push('--debug');
maybeWatch = true;
}
log.info(`Preparing to setup a new cluster on "${settings.addr}" ...`);
pm2.connect(function (err) {
if (err) {
console.error(err);
process.exit(2);
}
(async function () {
const processName = `${APP_NAME}-manager`;
await pm2.start({
name: processName,
script: path.resolve(__dirname, MAIN_SCRIPT_NAME),
args: args.join(' '),
exec_mode: 'cluster',
instances: 1,
watch: maybeWatch,
maxRestarts: 0,
output: path.resolve(__dirname, 'logs', `${processName}-out.log`),
error: path.resolve(__dirname, 'logs', `${processName}-error.log`)
}, function (error, res) {
if (error) {
throw error;
}
pm2.flush(APP_NAME, (err) => {
if (err) {
throw err;
}
});
pm2.describe(0, (err, proc) => {
const logFile = proc[0].pm2_env.pm_out_log_path;
if (fs.existsSync(logFile)) {
fs.unlinkSync(logFile);
}
setTimeout(() => {
if (fs.existsSync(logFile)) {
log.info(fs.readFileSync(logFile, 'UTF-8'));
}
pm2.disconnect();
process.exit(0);
}, 1000);
});
});
})();
});
return;
}
// command: node athena.js cluster --init --addr <IP> --foreground
if (should.initClusterInForeground) {
cluster.init();
return;
}
// command: node athena.js cluster --join --foreground --token <TOKEN> \
//
// --addr <IP>:<PORT>
if (should.joinClusterInForeground) {
cluster.joinCluster();
return;
}
// if (should.fetchTestsFromGitRepo) {
// todo: fetch the tests from the given Git repo use utils.isGitRepo
// try {
// await GitClient.cloneRepoInTempDir('some.git.url.here');
// } catch (e) {
// log.error(e);
// }
// }
// command: node athena.js cluster --join --token <TOKEN> --addr <IP>:<PORT>
if (should.joinCluster) {
if (!settings.token) {
log.error(`The --token is required when joining a new Athena cluster.`);
}
if (!settings.addr) {
log.error(`The --addr is required when joining a new Athena cluster.`);
}
const args = ['cluster', '--join', `--token ${settings.token}`, `--addr ${settings.addr}`, '--foreground'];
let maybeWatch = false;
if (settings.debug) {
args.push('--debug');
maybeWatch = true;
}
log.info(`Attempting to join a new cluster on "${settings.addr}"...`);
pm2.connect(function (err) {
if (err) {
console.error(err);
process.exit(2);
}
(async function () {
const processName = `${APP_NAME}-agent`;
await pm2.start({
name: processName,
script: path.resolve(__dirname, MAIN_SCRIPT_NAME),
args: args.join(' '),
exec_mode: 'cluster',
instances: 1, // todo: instances: settings.cpusLength,
watch: maybeWatch,
output: path.resolve(__dirname, 'logs', `${processName}-out.log`),
error: path.resolve(__dirname, 'logs', `${processName}-error.log`)
}, function (error, res) {
if (error) {
throw error;
}
log.success(`Successfully joined the cluster!`);
pm2.disconnect();
process.exit(0);
});
})();
});
}
// command: node athena.js cluster --run --[performance/functional]
if (should.delegateClusterCommand) {
log.info(`Preparing to run a new cluster job...`);
commands.callClusterCommand('REQ_RUN_PERF');
return;
}
// command: node athena.js --run --performance
if (should.runPerformanceTests) {
athena.runPerformanceTests();
return;
}
// command: node athena.js --run --functional
if (should.runFunctionalTests) {
athena.runFunctionalTests();
}
})();
module.exports = Athena;