-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·111 lines (97 loc) · 3.64 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const AirVantage = require('airvantage');
const conzole = require('conzole');
const getToken = require('./lib/gettoken');
const jsonfile = require('jsonfile');
const program = require('commander');
const splash = require('./lib/splash');
const packageJson = jsonfile.readFileSync(`${__dirname}/package.json`);
program
.version(packageJson.version)
.usage('[options] <your-setup.json> (default: setup.json)')
.option('-c, --clean', 'Clean the simulated resources (Systems, Gateways, Applications with the "simulationLabel")')
.parse(process.argv);
if (program.args.length > 1) {
conzole.failed('1 file max expected');
return 1;
}
const clean = program.clean;
const customSetupFile = program.args && program.args[0];
const setupFile = customSetupFile || 'setup.json';
let setup;
try {
setup = jsonfile.readFileSync(`./${setupFile}`);
} catch (error) {
let errorMessage = `Something went wrong: ${error.stack}`;
if (!customSetupFile) {
errorMessage = 'You did not provide any specific setup file and no setup.json file has been found.';
}
conzole.failed(errorMessage);
return 1;
}
let serverConfig;
// First check for a local datacenter config
try {
serverConfig = jsonfile.readFileSync(`./config/${setup.dataCenter}.json`);
} catch (error) {
// Otherwise take the default one
serverConfig = jsonfile.readFileSync(`${__dirname}/config/${setup.dataCenter}.json`);
}
const credentials = Object.assign({}, setup.credentials, serverConfig.credentials);
const simulation = jsonfile.readFileSync(`./simulations/${setup.simulation}.json`);
splash({ clean, setupFile, setup, credentials, simulation });
const Simulator = require('./lib/simulator');
const isLocal = !!serverConfig.authFrontServer;
const airvantage = new AirVantage({
serverUrl: isLocal ? `http://${serverConfig.server}` : `https://${serverConfig.server}`,
credentials: credentials,
companyUid: setup.companyUid,
debug: true
});
const factory = require('./lib/factory')(airvantage, simulation);
const simulator = new Simulator({
airvantageClient: airvantage,
simulation: simulation,
configuration: { server: serverConfig.server }
});
function getSimulationLabel() {
if (simulation.simulationLabel) {
return [simulation.simulationLabel];
}
return [simulation.label];
}
return new Promise((resolve, reject) => {
if (isLocal) {
conzole.quote(`Auth Front "${serverConfig.authFrontServer}"`);
return getToken({
authFrontServer: serverConfig.authFrontServer,
credentials
}).then((token) => {
if (!!token) {
return airvantage.authenticate({ token }).then(resolve);
}
reject(new Error(`Unable to get token from ${serverConfig.authFrontServer}`));
});
}
return airvantage.authenticate().then(resolve);
})
.then(() => {
conzole.done('Authenticated');
if (clean || simulation.clean) {
return factory.clean();
} else {
return factory
.initialize()
.then(() => {
conzole.start('Start simulation - ', getSimulationLabel()[0]);
return airvantage.querySystems({ labels: getSimulationLabel() });
})
.then((systems) => {
conzole.quote('For ', systems.length, 'systems');
return simulator.start(systems);
})
.then(() => conzole.ln().title('Simulation succeeded'));
}
})
.catch((error) => conzole.failed('ERROR:', error.response ? error.response.body : error.stack));