1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd-up: Start-up file for microservices running QEWD natively |
|
5 | | |
|
6 | | Copyright (c) 2017-19 M/Gateway Developments Ltd, |
|
7 | | Redhill, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 5 July 2019
|
28 |
|
29 | */
|
30 |
|
31 | var fs = require('fs');
|
32 | var module_exists = require('module-exists');
|
33 | var child_process = require('child_process');
|
34 | var qewd = require('qewd');
|
35 |
|
36 |
|
37 | const gtm_version = 'V6.3-005';
|
38 | const ydb_versionp = 'r1.24';
|
39 | const ydb_version = 'r124';
|
40 | const ydb_arch = 'x86_64';
|
41 | const updateScriptName = 'update_r122_to_r124';
|
42 |
|
43 | function setEnv(params) {
|
44 | for (var name in params) {
|
45 | process.env[name] = params[name];
|
46 | }
|
47 | }
|
48 |
|
49 | function installModule(moduleName) {
|
50 | var pieces = moduleName.split('@');
|
51 | var rootName;
|
52 | if (moduleName.startsWith('@')) {
|
53 | rootName = '@' + pieces[1];
|
54 | }
|
55 | else {
|
56 | rootName = pieces[0];
|
57 | }
|
58 | if (!module_exists(rootName) && !fs.existsSync(rootName)) {
|
59 | var prefix = '';
|
60 | child_process.execSync('npm install --unsafe-perm ' + moduleName + prefix, {stdio:[0,1,2]});
|
61 | console.log('\n' + moduleName + ' installed');
|
62 | }
|
63 | else {
|
64 | console.log(moduleName + ' already installed');
|
65 | }
|
66 | }
|
67 |
|
68 | var startup;
|
69 | var qewd_up = false;
|
70 |
|
71 | console.log('*** loading ' + __dirname + '/' + 'configuration/config.json');
|
72 |
|
73 | var qewd_up_config_path = __dirname + '/' + 'configuration/config.json';
|
74 |
|
75 | //process.chdir(__dirname);
|
76 |
|
77 | var qewd_up_path = 'qewd/up';
|
78 |
|
79 | if (fs.existsSync(qewd_up_config_path)) {
|
80 | var config = require(qewd_up_config_path);
|
81 | qewd_up = (config.qewd_up === true);
|
82 | console.log('** config = ' + JSON.stringify(config, null, 2));
|
83 | if (config.ms_name) {
|
84 | process.env.microservice = config.ms_name;
|
85 | process.env.mode = 'microservice';
|
86 | }
|
87 | }
|
88 |
|
89 |
|
90 | installModule('qewd-transform-json');
|
91 |
|
92 | if (qewd_up) {
|
93 | if (process.argv[3] && process.argv[3] !== '') {
|
94 | process.env.microservice = process.argv[3];
|
95 | }
|
96 | if (process.env.microservice) {
|
97 | console.log('starting up microservice ' + process.env.microservice);
|
98 | //startup = require(qewd_up_path + '/docker_ms_startup')();
|
99 | startup = require(qewd_up_path + '/run')(false, process.argv[2]);
|
100 | }
|
101 | else {
|
102 | console.log('starting up Orchestrator service');
|
103 | process.env.qewd_service_name = process.argv[2];
|
104 | startup = require(qewd_up_path + '/run')(false, process.argv[2]);
|
105 | }
|
106 | if (!startup) {
|
107 | console.log('QEWD has started');
|
108 | return;
|
109 | }
|
110 | }
|
111 | else {
|
112 | startup = require('./startup');
|
113 | }
|
114 |
|
115 | var userDefined = startup.userDefined;
|
116 | if (!userDefined && fs.existsSync('./userDefined.json')) {
|
117 | userDefined = require('./userDefined.json');
|
118 | }
|
119 |
|
120 | if (userDefined && userDefined.startup_commands) {
|
121 | console.log('Running custom startup commands:');
|
122 | userDefined.startup_commands.forEach(function(cmnd) {
|
123 | console.log(cmnd);
|
124 | child_process.execSync(cmnd, {stdio:[0,1,2]});
|
125 | });
|
126 | }
|
127 |
|
128 | var npmModules;
|
129 | var modulePath;
|
130 | if (fs.existsSync('./install_modules.json')) {
|
131 |
|
132 | npmModules = require('./install_modules.json');
|
133 | npmModules.forEach(function(moduleName) {
|
134 | console.log('\nInstalling module ' + moduleName);
|
135 | installModule(moduleName);
|
136 | });
|
137 | }
|
138 |
|
139 | var config = startup.config;
|
140 |
|
141 | if (!config.database) {
|
142 | config.database = {
|
143 | type: 'gtm'
|
144 | };
|
145 | }
|
146 | else if (!config.database.type) {
|
147 | config.database.type = 'gtm';
|
148 | }
|
149 |
|
150 | /*
|
151 |
|
152 | if (config.database && config.database.type === 'gtm') {
|
153 |
|
154 | // rundown the default region database (all globals except CacheTempEWDSession)
|
155 | // it may return an error, so wrap in a try/catch
|
156 |
|
157 | var ydb_dir = processs.env.ydb_dir;
|
158 | var ydb_path = processs.env.ydb_rel;
|
159 |
|
160 | try {
|
161 | console.log('Running down YottaDB...');
|
162 | child_process.execSync('mupip rundown -region DEFAULT', {stdio:[0,1,2]});
|
163 | child_process.execSync('mupip set -key_size=1019 -region DEFAULT', {stdio:[0,1,2]});
|
164 | child_process.execSync('mupip set -record_size=1048576 -region DEFAULT', {stdio:[0,1,2]});
|
165 | child_process.execSync('mupip rundown -region qewdreg', {stdio:[0,1,2]});
|
166 | child_process.execSync('mupip set -key_size=1019 -region qewdreg', {stdio:[0,1,2]});
|
167 | child_process.execSync('mupip set -record_size=1048576 -region qewdreg', {stdio:[0,1,2]});
|
168 | console.log('Rundown completed');
|
169 | }
|
170 | catch(err) {
|
171 | console.log('Error running down YottaDB: ' + err);
|
172 | console.log('Recovering journal...');
|
173 | child_process.execSync('/mupip journal -recover -backward ' + ydb_dir + '/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
174 | console.log('Journal recovered');
|
175 | }
|
176 |
|
177 | // update database journal file if it's for an old version
|
178 |
|
179 | try {
|
180 | child_process.execSync('/mupip journal -show=header -backward ' + ydb_dir + '/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
181 | }
|
182 | catch(err) {
|
183 | // journal file is for previous YDB version - replace it with new version
|
184 | var updateScript = './' + updateScriptName;
|
185 | child_process.execSync(updateScript, {stdio:[0,1,2]});
|
186 | }
|
187 |
|
188 | }
|
189 |
|
190 | */
|
191 |
|
192 | // ready to start QEWD now
|
193 |
|
194 | var qewd_master = qewd.master;
|
195 | console.log('Starting QEWD');
|
196 |
|
197 | var q = qewd_master.start(config, startup.routes);
|
198 | if (userDefined) {
|
199 | for (var name in userDefined) {
|
200 | q.userDefined[name] = userDefined[name];
|
201 | }
|
202 | }
|
203 |
|
204 | if (q.userDefined && q.userDefined.config) {
|
205 | q.userDefined.config.qewd_up = qewd_up;
|
206 | }
|
207 |
|
208 | var xp = qewd_master.intercept();
|
209 |
|
210 | // invoke user-specific startup code
|
211 |
|
212 | if (startup.onStarted) {
|
213 | startup.onStarted.call(q, config, xp.app, xp.qx.router, xp.qx);
|
214 | }
|
215 |
|