-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.js
More file actions
executable file
·330 lines (271 loc) · 10.9 KB
/
Copy pathsetup.js
File metadata and controls
executable file
·330 lines (271 loc) · 10.9 KB
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
/*
* Copyright 2016 e-UCM (http://www.e-ucm.es/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* This project has received funding from the European Union’s Horizon
* 2020 research and innovation programme under grant agreement No 644187.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 (link is external)
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/**
* This file is used to generate 'config.js' (used during execution 'npm start') and
* 'config-test.js' (used while testing 'npm test') files.
*
* In order to create the files, needs some predefined values stored in 'config-values.js'.
* For more information about these values, checkout 'config-values.js'.
*
* config.js is generated using the 'defaultValues' from 'config-values.js'.
* config-test.js is generated using the 'testValues' from 'config-values.js'.
*
* Also creates a 'root' user in the database with the 'admin' role.
* Its credentials are:
* Username: 'root'.
* Password: @see 'defaultValues.rootPassword' @ 'config-values.js'.
* Email: @see 'defaultValues.rootEmail' @ 'config-values.js'.
*
* Note:
* This script cleans the database before creating the 'root' user.
*
* Usage:
*
* 'npm run-script setup' - Generates the files asking the user for the values and suggesting the default values from 'config-values.js'.
* 'npm run-script fast-setup' - Generates the files directly using the values from 'config-values.js'.
*
*/
var Fs = require('fs');
var Path = require('path');
var Async = require('async');
var Promptly = require('promptly');
var Mongodb = require('mongodb');
var Handlebars = require('handlebars');
var configTemplatePath = Path.resolve(__dirname, 'config-example.js');
var configPath = Path.resolve(__dirname, 'config.js');
var configTestPath = Path.resolve(__dirname, 'config-test.js');
var configValue = require('./config-values');
var defaultValues = configValue.defaultValues;
var testValues = configValue.testValues;
var registerRoot = function (options, callback) {
var username = 'root';
var adminRole = 'admin';
var mongodbUrl = options.mongodbUrl;
var email = options.rootEmail;
var password = options.rootPassword;
var failedLoginAttempts = options.failedLoginAttempts;
var apiPath = options.apiPath;
var mongoose = require('mongoose');
var app = {
get: function (key) {
return key;
},
config: {
apiPath: apiPath,
loginAttempts: {
failedLoginAttempts: failedLoginAttempts
}
},
db: mongoose.createConnection(mongodbUrl)
};
app.db.on('error', callback);
app.db.on('connected', function () {
require('./schema/user.js')(app, mongoose);
var UserModel = app.db.model('user');
UserModel.register(new UserModel({
username: username,
email: email
}), password, function (err, user) {
if (err) {
console.error('Failed to register \'root\' user.');
return callback(err);
}
if (!user) {
err = new Error('Failed to register \'root\' user.');
return callback(err);
}
require('./roles.js')(app, function (err) {
if (err) {
console.error('Failed to load the roles.');
return callback(err);
}
app.acl.addUserRoles(user.username, adminRole, function (err) {
if (err) {
console.error('Failed to setup root user.');
return callback(err);
}
callback(null, true);
});
});
});
});
};
registerRoot({
mongodbUrl: 'localhost:27017/a2',
rootEmail: 'a@b.com',
rootPassword: 'root',
failedLoginAttempts: '',
apiPath: '/api'
}, function(a,b) {
console.log(a);
console.log(b);
console.log('created');
});
if (process.env.SETUP_MODE === 'fast') {
var options = {
encoding: 'utf-8'
};
var source = Fs.readFileSync(configTemplatePath, options);
var configTemplate = Handlebars.compile(source);
Fs.writeFileSync(configPath, configTemplate(defaultValues));
Fs.writeFileSync(configTestPath, configTemplate(testValues));
console.log('Setup complete.');
process.exit(0);
} else if (process.env.SETUP_MODE === 'register-root') {
registerRoot(defaultValues, function (err, res) {
if (err || !res) {
console.error(err);
if (err.message && err.message.indexOf('ECONNREFUSED') > -1) {
console.error('Could not connect to MongoDB!');
return process.exit(-1);
}
console.log('Did not create root user, continuing anyway!');
return process.exit(0);
}
console.log('Root user registered successfully.');
process.exit(0);
});
} else {
Async.auto({
projectName: function (done) {
var promptOptions = {
default: defaultValues.projectName || 'A2 Module'
};
Promptly.prompt('Project name: (' + promptOptions.default + ')', promptOptions, done);
},
companyName: ['projectName', function (done) {
var promptOptions = {
default: defaultValues.companyName || 'e-UCM Research Group'
};
Promptly.prompt('Company name: (' + promptOptions.default + ')', promptOptions, done);
}],
mongodbUrl: ['companyName', function (done) {
var promptOptions = {
default: defaultValues.mongodbUrl || 'mongodb://localhost:27017/a2'
};
Promptly.prompt('MongoDB URL: (' + promptOptions.default + ')', promptOptions, done);
}],
testMongo: ['mongodbUrl', function (done, results) {
Mongodb.MongoClient.connect(results.mongodbUrl, {}, function (err, db) {
if (err) {
console.error('Failed to connect to Mongodb.');
return done(err);
}
db.close();
});
done(null, true);
}],
redisdbHost: ['testMongo', function (done) {
var promptOptions = {
default: defaultValues.redisdbHost || '127.0.0.1'
};
Promptly.prompt('Redis DB host: (' + promptOptions.default + ')', promptOptions, done);
}],
redisPort: ['redisdbHost', function (done) {
var promptOptions = {
default: defaultValues.redisPort || '6379'
};
Promptly.prompt('Redis DB port: (' + promptOptions.default + ')', promptOptions, done);
}],
redisdbNumber: ['redisPort', function (done) {
var promptOptions = {
default: defaultValues.redisdbNumber || '0'
};
Promptly.prompt('Redis DB number: (' + promptOptions.default + ')', promptOptions, done);
}],
cryptoKey: ['redisdbNumber', function (done) {
var promptOptions = {
default: defaultValues.cryptoKey || 'th15_15_s3cr3t_5hhhh'
};
Promptly.password('Crypto secret key: (' + promptOptions.default + ')', promptOptions, done);
}],
rootEmail: ['cryptoKey', function (done) {
var promptOptions = {
default: defaultValues.rootEmail || 'root@email.com'
};
Promptly.prompt('Root user email: (' + promptOptions.default + ')', promptOptions, done);
}],
rootPassword: ['rootEmail', function (done) {
var promptOptions = {
default: defaultValues.rootPassword || 'root'
};
Promptly.password('Root user password: (' + promptOptions.default + ')', promptOptions, done);
}],
systemEmail: ['rootPassword', function (done, results) {
var promptOptions = {
default: defaultValues.systemEmail || (results.rootEmail || 'system@email.com')
};
Promptly.prompt('System email: (' + promptOptions.default + ')', promptOptions, done);
}],
smtpHost: ['systemEmail', function (done) {
var promptOptions = {
default: defaultValues.smtpHost || 'smtp.gmail.com'
};
Promptly.prompt('SMTP host: (' + promptOptions.default + ')', promptOptions, done);
}],
smtpUsername: ['smtpHost', function (done, results) {
var promptOptions = {
default: defaultValues.smtpUsername || (results.systemEmail || '')
};
Promptly.prompt('SMTP username/email: (' + promptOptions.default + ')', promptOptions, done);
}],
smtpPassword: ['smtpUsername', function (done) {
var promptOptions = {
default: defaultValues.smtpPassword || ''
};
Promptly.password('SMTP password: (' + promptOptions.default + ')', promptOptions, done);
}],
createConfig: ['smtpPassword', function (done, results) {
var fsOptions = {
encoding: 'utf-8'
};
Fs.readFile(configTemplatePath, fsOptions, function (err, src) {
if (err) {
console.error('Failed to read config-example template.');
return done(err);
}
for (var result in results) {
if (results.hasOwnProperty(result)) {
defaultValues[result] = results[result];
}
}
var configTemplate = Handlebars.compile(src);
Fs.writeFile(configPath, configTemplate(defaultValues), function (err) {
if (err) {
console.error('Failed to write config.js file.');
return done(err);
}
Fs.writeFile(configTestPath, configTemplate(testValues), done);
});
});
}],
setupRootUser: ['createConfig', function (done, results) {
results.apiPath = defaultValues.apiPath;
registerRoot(results, done);
}]
}, function (err) {
if (err) {
console.error('Setup failed.');
console.error(err);
return process.exit(1);
}
console.log('Setup complete.');
process.exit(0);
});
}