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 | 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 | const gtm_version = 'V6.3-007';
|
37 | const ydb_versionp = 'r1.28';
|
38 | const ydb_version = 'r128';
|
39 | const ydb_arch = 'x86_64';
|
40 | const updateScriptName = 'update_to_r128';
|
41 |
|
42 | function setEnv(params) {
|
43 | for (var name in params) {
|
44 | process.env[name] = params[name];
|
45 | }
|
46 | }
|
47 |
|
48 | function installModule(moduleName, modulePath) {
|
49 | var pieces = moduleName.split('@');
|
50 | var rootName;
|
51 | if (moduleName.startsWith('@')) {
|
52 | rootName = '@' + pieces[1];
|
53 | }
|
54 | else {
|
55 | rootName = pieces[0];
|
56 | }
|
57 | if (!module_exists(rootName) && !fs.existsSync('/opt/qewd/mapped/node_modules/' + rootName)) {
|
58 | var prefix = '';
|
59 | if (typeof modulePath !== 'undefined') {
|
60 | prefix = ' --prefix ' + modulePath;
|
61 | }
|
62 | child_process.execSync('npm install --unsafe-perm ' + moduleName + prefix, {stdio:[0,1,2]});
|
63 | console.log('\n' + moduleName + ' installed');
|
64 | }
|
65 | else {
|
66 | console.log(moduleName + ' already installed');
|
67 | }
|
68 | }
|
69 |
|
70 | process.env.USER = 'root';
|
71 | process.env.HOME = '/opt/qewd';
|
72 | if (!process.env.NODE_PATH) {
|
73 | process.env.NODE_PATH = '/opt/qewd/mapped/modules';
|
74 | }
|
75 |
|
76 | var startup;
|
77 | var qewd_up = false;
|
78 | var qewd_up_config_path = '/opt/qewd/mapped/configuration/config.json';
|
79 | var qewd_up_path = '/opt/qewd/node_modules/qewd/up';
|
80 |
|
81 | if (fs.existsSync(qewd_up_config_path)) {
|
82 | var config = require(qewd_up_config_path);
|
83 | qewd_up = (config.qewd_up === true);
|
84 | }
|
85 |
|
86 | if (qewd_up) {
|
87 | if (process.env.microservice) {
|
88 | console.log('starting up microservice ' + process.env.microservice);
|
89 |
|
90 | startup = require(qewd_up_path + '/run')(true);
|
91 | }
|
92 | else {
|
93 | console.log('starting up Docker Orchestrator service');
|
94 |
|
95 | startup = require(qewd_up_path + '/run')(true);
|
96 | }
|
97 | if (!startup) {
|
98 | console.log('Unable to start QEWD');
|
99 | return;
|
100 | }
|
101 | }
|
102 | else {
|
103 | startup = require('/opt/qewd/mapped/startup');
|
104 | }
|
105 |
|
106 | var userDefined = startup.userDefined;
|
107 | if (!userDefined && fs.existsSync('/opt/qewd/mapped/userDefined.json')) {
|
108 | userDefined = require('/opt/qewd/mapped/userDefined.json');
|
109 | }
|
110 |
|
111 | if (userDefined && userDefined.startup_commands) {
|
112 | console.log('Running custom startup commands:');
|
113 | userDefined.startup_commands.forEach(function(cmnd) {
|
114 | console.log(cmnd);
|
115 | child_process.execSync(cmnd, {stdio:[0,1,2]});
|
116 | });
|
117 | }
|
118 |
|
119 | var npmModules;
|
120 | var modulePath;
|
121 | if (fs.existsSync('/opt/qewd/mapped/install_modules.json')) {
|
122 | if (!fs.existsSync('/opt/qewd/mapped/node_modules')) {
|
123 | fs.mkdirSync('/opt/qewd/mapped/node_modules');
|
124 | }
|
125 |
|
126 | modulePath = '/opt/qewd/mapped';
|
127 | if (!process.env.NODE_PATH.includes('/opt/qewd/mapped/node_modules:')) {
|
128 | process.env.NODE_PATH = '/opt/qewd/mapped/node_modules:' + process.env.NODE_PATH;
|
129 | }
|
130 | require('module').Module._initPaths();
|
131 |
|
132 | npmModules = require('/opt/qewd/mapped/install_modules.json');
|
133 | npmModules.forEach(function(moduleName) {
|
134 | console.log('\nInstalling module ' + moduleName + ' to ' + modulePath);
|
135 | installModule(moduleName, modulePath);
|
136 | });
|
137 | console.log('** NODE_PATH = ' + process.env.NODE_PATH);
|
138 | }
|
139 |
|
140 | var config = startup.config;
|
141 |
|
142 | var ydb_default_params = {
|
143 | database: 'YottaDB',
|
144 | release: ydb_versionp,
|
145 | architecture: ydb_arch.split('_')[0],
|
146 | multithreaded: false
|
147 | };
|
148 |
|
149 | if (!config.database) {
|
150 | config.database = {
|
151 | type: 'dbx',
|
152 | params: ydb_default_params
|
153 | };
|
154 | }
|
155 | if (!config.database.type) {
|
156 | config.database.type = 'dbx';
|
157 | }
|
158 | if (config.database.type === 'dbx' && !config.database.params) {
|
159 | config.database.params = ydb_default_params;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 | config.sessionDocumentName = 'qs';
|
165 |
|
166 | if (config.database && (config.database.type === 'gtm' || (config.database.type === 'dbx' && config.database.params.database === 'YottaDB'))) {
|
167 |
|
168 |
|
169 |
|
170 | console.log('Setting up YottaDB Environment');
|
171 |
|
172 | var ydb_path = ydb_versionp + '_' + ydb_arch;
|
173 | var gtm_path = gtm_version + '_' + ydb_arch;
|
174 | var ydb_env = {
|
175 | ydb_retention: 42,
|
176 | gtm_retention: 42,
|
177 | LD_LIBRARY_PATH: '/usr/local/lib/yottadb/' + ydb_version,
|
178 | ydb_log: '/tmp/yottadb/' + ydb_path,
|
179 | gtm_log: '/tmp/yottadb/' + ydb_path,
|
180 | gtm_repl_instance: '/root/.yottadb/' + ydb_path + '/g/yottadb.repl',
|
181 | ydb_repl_instance: '/root/.yottadb/' + ydb_path + '/g/yottadb.repl',
|
182 | ydb_gbldir: '/root/.yottadb/' + ydb_path + '/g/yottadb.gld',
|
183 | ydb_etrap: 'Write:(0=$STACK) "Error occurred: ",$ZStatus,!',
|
184 | ydb_dir: '/root/.yottadb',
|
185 | gtmver: gtm_path,
|
186 | ydb_rel: ydb_path,
|
187 | gtmgbldir: '/root/.yottadb/' + ydb_path + '/g/yottadb.gld',
|
188 | ydb_routines: '/opt/qewd/node_modules/nodem/src /root/.yottadb/' + ydb_path + '/o*(/root/.yottadb/' + ydb_path + '/r /root/.yottadb/r) /usr/local/lib/yottadb/' + ydb_version + '/libyottadbutil.so',
|
189 | gtmroutines: '/opt/qewd/node_modules/nodem/src /root/.yottadb/' + ydb_path + '/o*(/root/.yottadb/' + ydb_path + '/r /root/.yottadb/r) /usr/local/lib/yottadb/' + ydb_version + '/libyottadbutil.so',
|
190 | GTMCI: '/opt/qewd/node_modules/nodem/resources/nodem.ci',
|
191 | ydb_ci: '/opt/qewd/node_modules/nodem/resources/nodem.ci',
|
192 | gtmdir: '/root/.fis-gtm',
|
193 | gtm_etrap: 'Write:(0=$STACK) "Error occurred: ",$ZStatus,!',
|
194 | ydb_tmp: '/tmp/yottadb/' + ydb_path,
|
195 | gtm_tmp: '/tmp/yottadb/' + ydb_path,
|
196 | gtm_dist: '/usr/local/lib/yottadb/' + ydb_version,
|
197 | ydb_dist: '/usr/local/lib/yottadb/' + ydb_version
|
198 | };
|
199 |
|
200 | setEnv(ydb_env);
|
201 |
|
202 | if (config.database.type === 'gtm') {
|
203 | config.database = {
|
204 | type: 'gtm',
|
205 | params:{
|
206 | ydb_env: ydb_env
|
207 | }
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 | try {
|
213 | var nm = require('nodem');
|
214 | }
|
215 | catch(err) {
|
216 | installModule('nodem');
|
217 | }
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | try {
|
226 | console.log('Running down YottaDB...');
|
227 | child_process.execSync(ydb_env.ydb_dist + '/mupip rundown -region DEFAULT', {stdio:[0,1,2]});
|
228 | child_process.execSync(ydb_env.ydb_dist + '/mupip rundown -region qewdreg', {stdio:[0,1,2]});
|
229 | console.log('Rundown completed');
|
230 | }
|
231 | catch(err) {
|
232 | console.log('Error running down YottaDB: ' + err);
|
233 | console.log('Recovering journal...');
|
234 | child_process.execSync(ydb_env.ydb_dist + '/mupip journal -recover -backward /root/.yottadb/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
235 | console.log('Journal recovered');
|
236 | }
|
237 |
|
238 | try {
|
239 | console.log('Max out the global subscript and data lengths');
|
240 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -key_size=1019 -region DEFAULT', {stdio:[0,1,2]});
|
241 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -record_size=1048576 -region DEFAULT', {stdio:[0,1,2]});
|
242 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -key_size=1019 -region qewdreg', {stdio:[0,1,2]});
|
243 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -record_size=1048576 -region qewdreg', {stdio:[0,1,2]});
|
244 | }
|
245 | catch(err) {
|
246 | console.log('Unable to increase subscript and global lengths');
|
247 | }
|
248 |
|
249 |
|
250 |
|
251 | try {
|
252 | child_process.execSync(ydb_env.ydb_dist + '/mupip journal -show=header -backward /root/.yottadb/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
253 | }
|
254 | catch(err) {
|
255 |
|
256 | var updateScript = '/opt/qewd/' + updateScriptName;
|
257 | child_process.execSync(updateScript, {stdio:[0,1,2]});
|
258 | }
|
259 |
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 | var qewd_master = qewd.master;
|
265 | console.log('Starting QEWD');
|
266 |
|
267 | var q = qewd_master.start(config, startup.routes);
|
268 | if (userDefined) {
|
269 | for (var name in userDefined) {
|
270 | q.userDefined[name] = userDefined[name];
|
271 | }
|
272 | }
|
273 |
|
274 | if (q.userDefined && q.userDefined.config) {
|
275 | q.userDefined.config.qewd_up = qewd_up;
|
276 | }
|
277 |
|
278 | var xp = qewd_master.intercept();
|
279 |
|
280 |
|
281 |
|
282 | if (startup.onStarted) {
|
283 | startup.onStarted.call(q, config, xp.app, xp.qx.router, xp.qx);
|
284 | }
|
285 |
|