forked from tony-shannon/qewd-baseline
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.js
More file actions
442 lines (369 loc) · 13.3 KB
/
install.js
File metadata and controls
442 lines (369 loc) · 13.3 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
----------------------------------------------------------------------------
| QEWD-JSdb: Quick Installer |
| |
| Copyright (c) 2019 M/Gateway Developments Ltd, |
| Redhill, Surrey UK. |
| All rights reserved. |
| |
| http://www.mgateway.com |
| Email: rtweed@mgateway.com |
| |
| |
| Licensed 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 CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
----------------------------------------------------------------------------
5 December 2019
*/
module.exports = function() {
var ask = this.ask;
var fs = this.fs;
var isNumeric = this.isNumeric;
var npm_install = this.install_module;
var path = require('path');
var transform = this.transform;
var createJSONFile = fs.createJSONFile;
var createFile = fs.createFile;
var customScript;
try {
customScript = require('/node/custom-install');
console.log('Custom script loaded');
}
catch(err) {
console.log('No custom script or unable to load it');
}
// -----------------------------------------------
var helpers = {
createUuid: function() {
return uuid();
},
createHost: function(protocol, host) {
if (!host || host === '') {
return '<!delete>';
}
protocol = protocol || 'http';
return protocol + '://' + host;
},
createUri: function(protocol, host, port, path) {
var uri = protocol + '://' + host;
if (port === 80 || port === 443) {
port = '';
}
if (port && port !== '') {
uri = uri + ':' + port;
}
if (path) {
uri = uri + path;
}
return uri;
},
getExpiry: function(expiry) {
return expiry/1000;
}
};
function createConfig(serviceName, settings) {
var config_template_path = '/node/' + serviceName + '/configuration/config_template.json';
var config_path = '/node/' + serviceName + '/configuration/config.json';
try {
var config_template = require(config_template_path);
}
catch(err) {
console.log('Error! Unable to load ' + config_template_path);
console.log('**** Unable to continue, so Installation has been cancelled ****');
return false;
}
var config = transform(config_template, settings, helpers);
createJSONFile(config, config_path);
return true;
}
var start_container = [
"#!/bin/bash",
"# This script will correctly start the QEWD Container",
"#",
"#",
"# Run this startup script from the '~/qewd-baseline' folder",
"# (or whatever you've renamed it) using:",
"#",
"# - with persistence: ./start_p",
"# - without persistence: ./start",
"#",
"echo 'Starting qewd container'",
"docker run -d --name qewd --rm --network qewd-net -p {{port}}:8080 -v {{volume_path}}:/opt/qewd/mapped {{ydb_persistence}} {{container}}",
"echo 'qewd Container has been started'"
];
var debug_container = [
"#!/bin/bash",
"# This script will correctly start the QEWD Container in debug/shell mode",
"#",
"#",
"# Run this startup script from the '~/qewd-baseline' folder",
"# (or whatever you've renamed it) using:",
"#",
"# ./start_debug",
"#",
"echo 'Starting qewd container in debug/shell mode'",
"echo ' '",
"echo 'From the QEWD Container shell, start QEWD using:'",
"echo ' '",
"echo ' node qewd'",
"docker run -it --name qewd --rm --network qewd-net -p {{port}}:8080 -p 9229:9229 -v {{volume_path}}:/opt/qewd/mapped --entrypoint=/bin/bash {{container}}",
];
var stop_container = [
"#!/bin/bash",
"# This script will cleanly stop the qewd Container by issuing a command to QEWD to stop",
"#",
"#",
"# Run this shutdown script from the '~/qewd-baseline' folder",
"# (or whatever you've renamed it) using:",
"#",
"# ./stop",
"#",
"echo 'Stopping qewd Container'",
"#",
"# Send authenticated requests to cleanly shut down QEWD Containers",
"#",
"docker run -it --name qewd_shutdown --rm --network qewd-net -v $PWD:/node -e \"node_script=shutdown\" rtweed/node-runner"
];
// -------------------------------------------------
console.log(' ');
console.log('*************** QEWD-baseline Installer ***************');
console.log(' ');
console.log(' ');
console.log(' ');
console.log('*************** QEWD-baseline Installer ***************');
console.log(' ');
console.log(' Running this script will install all the components');
console.log(' needed by QEWD, and correctly configure it for');
console.log(' your platform');
var ok = ask.keyInYNStrict('Is this what you want to do?');
if (!ok) {
console.log('**** Installation has been cancelled ****');
return;
}
console.log('*** Installation will now commence');
// JSdb Container
var config_path = '/node/configuration/config.json';
var config;
try {
config = require(config_path);
// ensure minimum working configuration
if (!config.qewd_up) {
config.qewd_up = true;
}
if (!config.qewd) {
config.qewd = {
bodyParser: 'body-parser'
};
}
if (!config.qewd.bodyParser) {
config.qewd.bodyParser = 'body-parser';
}
}
catch(err) {
console.log('Error! Unable to load ' + config_path);
console.log('Using default settings instead...');
config = {
qewd_up: true,
qewd: {
bodyParser: 'body-parser'
}
};
}
var settings = {
port: config.port || 8080
};
// QEWD Listener Port
ok = false;
port = settings.port;
do {
console.log(' ');
console.log('Specify the port on which the QEWD Web Server will listen.');
console.log(' ');
port = ask.question('QEWD listener port (' + port + '): ', {defaultInput: port});
if (port !== '' && isNumeric(port)) {
ok = true;
}
else {
console.log('*** Error: you must enter a valid port number');
port = settings.port;
}
} while (!ok);
console.log(' ');
console.log('QEWD Port will be ' + port);
settings.port = port;
config.qewd.port = port;
// QEWD Worker Process Pool
ok = false;
var poolSize = config.qewd.poolSize || 2;
do {
console.log(' ');
console.log('Set the maximum number of QEWD Worker Processes that will be started.');
console.log(' ');
poolSize = ask.question('QEWD Worker Pool Size (' + poolSize + '): ', {defaultInput: poolSize});
if (poolSize !== '' && isNumeric(poolSize)) {
ok = true;
}
else {
console.log('*** Error: you must enter a valid pool size number');
poolSize = config.qewd.poolSize || 2;
}
} while (!ok);
console.log(' ');
console.log('QEWD Worker Pool Size set to ' + poolSize);
config.qewd.poolSize = poolSize;
// Management Password
ok = false;
var password = config.qewd.managementPassword || 'keepThisSecret!';
do {
console.log(' ');
console.log('If you use the QEWD Monitor application, it will ask for a password.');
console.log('You can change that password now');
console.log(' ');
password = ask.question('QEWD Monitor Password (' + password + '): ', {defaultInput: password});
if (password === '') {
password = config.qewd.managementPassword || 'keepThisSecret!';
console.log('*** Error: you must specify a password');
}
else {
config.qewd.managementPassword = password;
ok = true;
}
} while (!ok);
console.log(' ');
console.log('QEWD Monitor password will be ' + password);
createJSONFile(config, config_path);
// Loading Modules from NPM
var install_modules_path = '/node/install_modules.json';
if (fs.existsSync(install_modules_path)) {
console.log(' ');
console.log('Now installing module dependencies from NPM');
console.log(' ');
console.log('This is usually a one-time load of the Node.js Modules');
console.log('that are required by your QEWD system.');
console.log('However, you may want to force a clean reload of them now');
console.log(' ');
console.log('Note: if this is the first time you are running this installer,');
console.log('it does not matter what you answer - all modules will be loaded');
console.log(' ');
var cleardown = ask.keyInYNStrict('Do you want to clear down all pre-loaded modules?');
var module_path;
if (cleardown) {
module_path = '/node/node_modules';
fs.removeSync(module_path);
}
var modules;
try {
modules = require(install_modules_path);
modules.forEach(function(module) {
npm_install(module);
});
}
catch(err) {
console.log('Error! Unable to load ' + modules_path);
console.log(err);
return;
}
}
// QEWD Folder for volume mapping
console.log(' ');
console.log('The QEWD Container must map the QEWD-baseline folder,');
console.log('so you need to confirm what the correct folder is.');
console.log('This installer can then automatically create the correct')
console.log('"docker run" commands for you in the startup file');
console.log(' ');
var volume_path = process.env.HOST_VOLUME || settings.volume_path;
ok = false;
do {
console.log(' ');
volume_path = ask.question('QEWD-baseline folder (' + volume_path + '): ', {defaultInput: volume_path});
if (volume_path !== '') {
ok = true;
}
else {
console.log('*** Error: you must enter a valid volume path');
volume_path = settings.volume_path;
}
} while (!ok);
console.log(' ');
console.log('The path that will be volume mapped is ' + volume_path);
settings.volume_path = volume_path;
// Persistent data storage
// Add the pre-initialised files to the mapped volume
// but only if they aren't there already.
// If they already exist, they may have data in them!
var yottadb_path = '/node/yottadb';
var os = 'linux';
if (process.env.PLATFORM === 'arm') {
os = 'rpi';
}
if (!fs.existsSync(yottadb_path)) {
this.shell('svn export https://github.com/robtweed/yotta-gbldir-files/trunk/r1.28/' + os + ' ' + yottadb_path);
}
// Create startup files for persistence and non-persistence
console.log(' ');
console.log('Now creating the startup files for you to use to start the');
console.log('QDWD Container on your server, using your configuration settings.');
var suffix = '';
if (process.env.PLATFORM === 'arm') {
suffix = '-rpi';
}
settings.container = 'rtweed/qewd-server' + suffix;
var volume_map = '-v ' + volume_path + '/yottadb:/root/.yottadb/r1.28_';
if (os === 'linux') {
volume_map = volume_map + 'x86_64/g';
}
else {
volume_map = volume_map + 'armv7l/g';
}
settings.ydb_persistence = volume_map;
var content = transform(start_container, settings, helpers);
var filePath = '/node/start_p';
createFile(content, filePath);
this.shell('chmod +x ' + filePath);
settings.ydb_persistence = '';
content = transform(start_container, settings, helpers);
filePath = '/node/start';
createFile(content, filePath);
this.shell('chmod +x ' + filePath);
filePath = '/node/stop';
stop_container.forEach(function(line, index) {
if (line.includes('node-runner')) {
stop_container[index] = line + suffix;
}
});
createFile(stop_container, filePath);
this.shell('chmod +x ' + filePath);
filePath = '/node/start_debug';
content = transform(debug_container, settings, helpers);
createFile(content, filePath);
this.shell('chmod +x ' + filePath);
// If a custom installation file has been added, run it now
var ctx = this;
ctx.settings = settings;
if (customScript) {
console.log('Now running your custom install script...');
customScript.call(ctx);
console.log(' ');
console.log('Custom script completed');
console.log(' ');
}
console.log(' ');
console.log('*** Installation and Configuration was successful');
console.log(' ');
console.log('You can now start up the QEWD Container by running the commands:');
console.log('- with data persistence to host files; ./start_p');
console.log('- without persistence; ./start');
console.log(' ');
console.log('You can shut it down using the command:');
console.log('./stop');
return;
};