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 = 'armv7l';
|
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 | startup = require(qewd_up_path + '/run')(true);
|
90 | }
|
91 | else {
|
92 | console.log('starting up Docker Orchestrator service');
|
93 | startup = require(qewd_up_path + '/run')(true);
|
94 | }
|
95 | if (!startup) {
|
96 | console.log('Unable to start QEWD');
|
97 | return;
|
98 | }
|
99 | }
|
100 | else {
|
101 | startup = require('/opt/qewd/mapped/startup');
|
102 | }
|
103 |
|
104 | var userDefined = startup.userDefined;
|
105 | if (!userDefined && fs.existsSync('/opt/qewd/mapped/userDefined.json')) {
|
106 | userDefined = require('/opt/qewd/mapped/userDefined.json');
|
107 | }
|
108 |
|
109 | if (userDefined && userDefined.startup_commands) {
|
110 | console.log('Running custom startup commands:');
|
111 | userDefined.startup_commands.forEach(function(cmnd) {
|
112 | console.log(cmnd);
|
113 | child_process.execSync(cmnd, {stdio:[0,1,2]});
|
114 | });
|
115 | }
|
116 |
|
117 | var npmModules;
|
118 | var modulePath;
|
119 | if (fs.existsSync('/opt/qewd/mapped/install_modules.json')) {
|
120 | if (!fs.existsSync('/opt/qewd/mapped/node_modules')) {
|
121 | fs.mkdirSync('/opt/qewd/mapped/node_modules');
|
122 | }
|
123 |
|
124 | modulePath = '/opt/qewd/mapped';
|
125 | if (!process.env.NODE_PATH.includes('/opt/qewd/mapped/node_modules:')) {
|
126 | process.env.NODE_PATH = '/opt/qewd/mapped/node_modules:' + process.env.NODE_PATH;
|
127 | }
|
128 | require('module').Module._initPaths();
|
129 |
|
130 | npmModules = require('/opt/qewd/mapped/install_modules.json');
|
131 | npmModules.forEach(function(moduleName) {
|
132 | console.log('\nInstalling module ' + moduleName + ' to ' + modulePath);
|
133 | installModule(moduleName, modulePath);
|
134 | });
|
135 | console.log('** NODE_PATH = ' + process.env.NODE_PATH);
|
136 | }
|
137 |
|
138 | var config = startup.config;
|
139 |
|
140 | var ydb_default_params = {
|
141 | database: 'YottaDB',
|
142 | release: ydb_versionp,
|
143 | architecture: 'arm',
|
144 | multithreaded: false
|
145 | };
|
146 |
|
147 | if (!config.database) {
|
148 | config.database = {
|
149 | type: 'dbx',
|
150 | params: ydb_default_params
|
151 | };
|
152 | }
|
153 | if (!config.database.type) {
|
154 | config.database.type = 'dbx';
|
155 | }
|
156 | if (config.database.type === 'dbx' && !config.database.params) {
|
157 | config.database.params = ydb_default_params;
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 | config.sessionDocumentName = 'qs';
|
163 |
|
164 | if (config.database && (config.database.type === 'gtm' || (config.database.type === 'dbx' && config.database.params.database === 'YottaDB'))) {
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | console.log('Setting up YottaDB Environment');
|
170 |
|
171 | var ydb_path = ydb_versionp + '_' + ydb_arch;
|
172 | var gtm_path = gtm_version + '_' + ydb_arch;
|
173 |
|
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@0.14.2');
|
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 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | console.log('Rundown completed');
|
237 | }
|
238 | catch(err) {
|
239 | console.log('Error running down YottaDB: ' + err);
|
240 | console.log('Recovering journal...');
|
241 | child_process.execSync(ydb_env.ydb_dist + '/mupip journal -recover -backward /root/.yottadb/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
242 |
|
243 | console.log('Journal recovered');
|
244 | }
|
245 |
|
246 | try {
|
247 | console.log('Max out the global subscript and data lengths');
|
248 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -key_size=1019 -region DEFAULT', {stdio:[0,1,2]});
|
249 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -record_size=1048576 -region DEFAULT', {stdio:[0,1,2]});
|
250 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -key_size=1019 -region qewdreg', {stdio:[0,1,2]});
|
251 | child_process.execSync(ydb_env.ydb_dist + '/mupip set -record_size=1048576 -region qewdreg', {stdio:[0,1,2]});
|
252 | }
|
253 | catch(err) {
|
254 | console.log('Unable to increase subscript and global lengths');
|
255 | }
|
256 |
|
257 |
|
258 |
|
259 | try {
|
260 | child_process.execSync(ydb_env.ydb_dist + '/mupip journal -show=header -backward /root/.yottadb/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
|
261 |
|
262 | }
|
263 | catch(err) {
|
264 |
|
265 | var updateScript = '/opt/qewd/' + updateScriptName;
|
266 | child_process.execSync(updateScript, {stdio:[0,1,2]});
|
267 | }
|
268 |
|
269 | }
|
270 |
|
271 |
|
272 |
|
273 | var qewd_master = qewd.master;
|
274 | console.log('Starting QEWD');
|
275 |
|
276 | var q = qewd_master.start(config, startup.routes);
|
277 | if (userDefined) {
|
278 | for (var name in userDefined) {
|
279 | q.userDefined[name] = userDefined[name];
|
280 | }
|
281 | }
|
282 |
|
283 | if (q.userDefined && q.userDefined.config) {
|
284 | q.userDefined.config.qewd_up = qewd_up;
|
285 | }
|
286 |
|
287 | var xp = qewd_master.intercept();
|
288 |
|
289 |
|
290 |
|
291 | if (startup.onStarted) {
|
292 | startup.onStarted.call(q, config, xp.app, xp.qx.router, xp.qx);
|
293 | }
|
294 |
|