-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
172 lines (152 loc) · 3.68 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
"use strict";
/**
* @license
* Copyright SOAJS All Rights Reserved.
*
* Use of this source code is governed by an Apache license that can be
* found in the LICENSE file at the root of this repository
*/
const fs = require('fs');
const path = require('path');
const errorFile = require('./lib/utils/errors.js');
const utils = require('./lib/utils/utils.js');
let cache = {};
function checkIfSupported(options, cb) {
let isSupported = ((options.strategy[options.function] && typeof (options.strategy[options.function]) === 'function') ? true : false);
if (!isSupported) {
return cb({
"source": "driver",
"error": "error",
"code": 519,
"msg": errorFile[519]
});
}
return cb(null, true);
}
function getStrategy(options, cb) {
let strategyName = options.type.toLowerCase();
if (!strategyName) {
return cb(new Error(`No driver type specified!`));
}
let onePath = [];
onePath.push(__dirname);
onePath.push(options.type);
//added fallback support
if (!options.driver && options.name) {
options.driver = options.name;
}
if (options.driver) {
strategyName += "_" + options.driver.toLowerCase();
onePath.push(options.driver);
}
onePath.push("index.js");
checkCache((strategy) => {
if (strategy) {
return cb(null, strategy);
}
let pathToUse = '';
try {
pathToUse = path.join.apply(null, onePath);
} catch (e) {
return cb(new Error("Invalid Driver Path detected!"));
}
checkStrategy(pathToUse, (error) => {
if (error) {
return cb(error);
}
try {
fs.exists(pathToUse, (exists) => {
if (!exists) {
throw new Error(`Driver not found: ${pathToUse} !!!`);
} else {
cache[strategyName] = require(pathToUse);
return cb(null, cache[strategyName]);
}
});
} catch (e) {
console.log("Error:", e);
return cb(e);
}
});
});
function checkCache(cb) {
if (cache[strategyName]) {
return cb(cache[strategyName]);
}
return cb(null);
}
function checkStrategy(path, cb) {
fs.access(path, fs.constants.F_OK || fs.constants.R_OK, cb);
}
}
/*
let driverOptionsSamples = [
{
type: "infra",
name: "aws",
technology: "cluster"
},
{
type: "infra",
name: "aws",
technology: "vm"
},
{
type: "infra",
name: "aws",
technology: "container",
driver: "docker"
},
{
type: "ci",
name: "travis"
},
{
type: "git",
name: "github"
}
];
*/
module.exports = {
/**
* Generic method that loads the requested driver and execute the method requested in it
* @param driverOptions
* @param method
* @param methodOptions
* @param cb
*
* @Ex:
*
* {type: 'infra', driver: 'aws', technology: 'vm'}, 'listServices', { Object containing listServices Arguments }, () => {}
*/
"execute": function (driverOptions, method, methodOptions, cb) {
getStrategy(driverOptions, (error, strategy) => {
utils.checkError(error, 518, cb, () => {
if (!methodOptions.technology && driverOptions.technology) {
methodOptions.technology = driverOptions.technology;
}
checkIfSupported({strategy: strategy, function: method}, (error) => {
if (error && error.code === 519) {
strategy.executeDriver(method, methodOptions, cb);
} else if (strategy && strategy[method]) {
strategy[method](methodOptions, cb);
} else {
return cb({
"source": "driver",
"error": "error",
"code": 500,
"msg": errorFile[500]
});
}
});
});
});
},
"validateInputs": function (options, section, method, cb) {
utils.validateInputs(options, section, method, (error) => {
utils.checkError(error, (error && error.code) ? error.code : 761, cb, () => {
return cb(null, true);
});
});
}
};