-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·285 lines (247 loc) · 10.6 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const yargs = require('yargs');
const yaml = require('js-yaml');
const chalk = require('chalk');
const Profile = require('./lib/profile');
const Loader = require('./lib/inspect/checks/loader');
const Reporter = require('./lib/inspect/report');
const Connector = require('infra.connectors');
async function verify(env_address, criteria_path, connector) {
let reporter = new Reporter();
let context = { bakerPath: env_address };
let loader = new Loader();
let groups = await loader.loadChecks(criteria_path);
console.log();
console.log(chalk.underline('Checks'));
for (let group of groups) {
console.log(chalk`\n\t{bold ${group.description}\n}`);
for (let check of group.checks) {
let instance = new check.module(connector, reporter, check.args);
console.log(chalk`\t{white ${check.name} check}`);
let results = await instance.check(context, check.args);
if (check.args && check.args.comment) {
console.log(chalk`\t\t{italic.gray ${check.args.comment}}`);
}
instance.report(results);
}
if( group.any.length > 0 ) {
let cachedChecks = [];
for( var check of group.any ) {
let instance = new check.module(connector, reporter, check.args);
let results = await instance.check(context, check.args);
let cache = {};
cache.results = results;
cache.instance = instance;
cachedChecks.push(cache);
};
let passedIndex = cachedChecks.findIndex( (cache) => cache.results.status == true);
let anyPassed;
if( passedIndex > -1 ) {
anyPassed = group.any[passedIndex];
}
if( anyPassed ) {
console.log(chalk`\t{green At least one of the following checks passed:}`);
console.log(chalk`\t{white ${anyPassed.name} check}`);
let results = cachedChecks[passedIndex].results;
if (anyPassed.args && anyPassed.args.comment) {
console.log(chalk`\t\t{italic.gray ${anyPassed.args.comment}}`);
}
cachedChecks[passedIndex].instance.report(results);
} else {
console.log(chalk`\t{red None of the following checks passed:}`);
let i = 0;
for (let check of group.any) {
console.log(chalk`\t{white ${check.name} check}`);
let cache = cachedChecks[i++];
let results = cache.results;
if (check.args && check.args.comment) {
console.log(chalk`\t\t{italic.gray ${check.args.comment}}`);
}
cache.instance.report(results);
}
}
}
}
reporter.summary();
}
async function main(env_address, criteria_path, connector) {
await verify(env_address, criteria_path, connector);
}
async function selectConnectorFromInventory(connectorType, connectorInfo, argv) {
// TODO: is this always needed?
let env_address = connectorInfo.target || connectorInfo.instance || connectorInfo.name || 'local';
let criteria_path = connectorInfo.criteria_path || argv.criteria_path;
if (!criteria_path) {
criteria_path = path.join(process.cwd(), 'test', 'opunit.yml');
if (!fs.existsSync(criteria_path)) {
console.error(chalk.red(`${connectorType}: criteria file (opunit.yml) was not provided, nor was default path found in test/opunit.yml`));
console.error(chalk.red('Skipping...'));
process.exit(1);
}
}
let connector = null;
let opts = {};
let optsCheck = {inCWD: false};
let name = null;
if (connectorType === 'baker' && await fs.exists(path.join(connectorInfo.target, 'baker.yml'))) {
name = connectorInfo.target;
} else if (connectorType === 'vagrant') {
let checkVagrantRunning = await Connector.getConnector('vagrant', connectorInfo.name, {inCWD: false});
let getStatus = await checkVagrantRunning.getStatus();
if(await getStatus.status === 'running'){
name = connectorInfo.name;
opts['inCWD'] = false;
}
} else if (connectorType === 'docker') {
let checkDockerRunning = await Connector.getConnector('docker', connectorInfo.name, opts);
let getStatus = await checkDockerRunning.containerExists();
if(getStatus){
name = connectorInfo.name;
}
} else if (connectorType === 'ssh' && connectorInfo.target.match(/[@:]+/)) {
name = connectorInfo.target;
opts['privateKey'] = connectorInfo.private_key;
} else if (connectorType === 'local' || connectorType === 'localhost') {
name = argv.env_address;
}
if(name !== null) {
connector = Connector.getConnector(connectorType, name, opts);
}
if (!connector) {
console.error(chalk.red(' => No running environment could be found with the given parameters:'), connectorType, connectorInfo);
console.error(chalk.red(' => Skipping...'));
return [null, null, null];
}
try {
// TODO: refactoring
//let context = { bakerPath: connectorInfo.target };
await connector.ready();
} catch (error) {
console.error(chalk.red(` => ${error}`));
console.error(chalk.red(' => Skipping...'));
return [null, null, null];
}
return [connector, env_address, criteria_path];
}
async function selectConnector(argv) {
const cwd = process.cwd();
let connector = null;
let name = null;
let opts = {};
let optsCheck = {inCWD: false};
let connectorTypeLocal;
if (argv.env_address === 'local' || argv.env_address === 'localhost') {
connectorTypeLocal = 'local';
name = argv.env_address;
} else if (!argv.env_address && await fs.exists(path.join(cwd, 'baker.yml'))) {
connectorTypeLocal = 'baker';
name = cwd;
} else if (!argv.env_address && await fs.exists(path.join(cwd, 'Vagrantfile'))) {
connectorTypeLocal = 'vagrant';
name = path.join(cwd, 'Vagrantfile');
opts['inCWD'] = true;
} else if (argv.env_address && await fs.exists(path.resolve(argv.env_address))) {
connectorTypeLocal = 'baker';
name = path.resolve(argv.env_address);
} else if (argv.env_address && argv.env_address.match(/[@:]+/)) {
connectorTypeLocal = 'ssh';
name = argv.env_address;
opts['privateKey'] = argv.ssh_key;
} else if (argv.env_address) {
let checkVagrantRunning = await Connector.getConnector('vagrant', argv.env_address, {inCWD: false});
let getStatus = await checkVagrantRunning.getStatus();
if(await getStatus.status === 'running'){
connectorTypeLocal = 'vagrant';
opts['inCWD'] = false;
name = argv.env_address;
}
else{
let checkDockerRunning = await (Connector.getConnector('docker', argv.env_address, opts));
let containerExists = await checkDockerRunning.containerExists();
if(containerExists){
connectorTypeLocal = 'docker';
name = (argv.env_address || argv.container);
}
}
}
if(name != null){
connector = Connector.getConnector(connectorTypeLocal, name, opts);
}
if (!connector) {
console.error(chalk.red(' => No running environment could be found with the given name'));
process.exit(1);
}
try {
//let context = { bakerPath: argv.env_address || process.cwd() };
await connector.ready();
} catch (error) {
console.error(chalk.red(` => ${error}`));
process.exit(1);
}
return connector;
}
// Register run command
yargs.command('profile <address>', 'Run check against specified profile ', (yargs) => { }, async (argv) => {
let profile = new Profile();
let repo = argv.address.split(':')[0];
let yml = argv.address.split(':')[1];
// let file = await profile.get('CSC-DevOps/profile', '326.yml')
let file = await profile.get(repo, yml);
await verify('local', file, Connector.getConnector('local', '', {}));
});
yargs.command('verify [env_address]', 'Verify an instance', (yargs) => {
yargs.positional('criteria_path', {
describe: 'path to the opunit.yml file, default is cwd/test/opunit.yml',
type: 'string',
alias: 'c',
});
yargs.positional('ssh_key', {
describe: 'required when using ssh connector. give path to private ssh key',
type: 'string',
});
yargs.positional('container', {
describe: 'required when using docker connector. give name or id of your container',
type: 'string',
alias: 't',
});
yargs.positional('inventory', {
describe: 'path to inventory file',
type: 'string',
alias: 'i',
});
}, async (argv) => {
if (argv.inventory) {
let inventory = yaml.load(await fs.readFile(argv.inventory, 'utf8'));
for (let group of inventory) {
let connectorType = Object.keys(group)[0];
for (let connectorInfo of group[connectorType]) {
console.log('\n===================================');
console.log(`Connector: ${connectorType}`);
console.log(`Info: ${JSON.stringify(connectorInfo)}`);
console.log('===================================');
let [connector, env_address, criteria_path] = await selectConnectorFromInventory(connectorType, connectorInfo, argv);
if (connector && env_address && criteria_path) {
await main(env_address, criteria_path, connector);
}
}
}
} else {
let connector = await selectConnector(argv);
let env_address = argv.env_address || process.cwd();
let { criteria_path } = argv;
// Default to baker_path
if (!criteria_path) {
// TODO: this needs cleanup, trying to get it to work...
criteria_path = path.join(process.cwd(), 'test', 'opunit.yml');
if (!fs.existsSync(criteria_path)) {
console.error(chalk.red('Checks file was not provided, nor was default path found in test/opunit.yml'));
process.exit(1);
}
}
await main(env_address, criteria_path, connector, { ssh_key: argv.ssh_key, container: argv.container });
}
});
// Turn on help and access argv
yargs.help().argv;