UNPKG

7.99 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-server: Start-up file for Dockerised version of QEWD |
5 | |
6 | Copyright (c) 2017-18 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 12 December 2018
28
29*/
30
31var fs = require('fs');
32var module_exists = require('module-exists');
33var child_process = require('child_process');
34var qewd = require('qewd');
35
36const gtm_version = 'V6.3-004';
37const ydb_versionp = 'r1.22';
38const ydb_version = 'r122';
39const ydb_arch = 'x86_64';
40
41function setEnv(params) {
42 for (var name in params) {
43 process.env[name] = params[name];
44 }
45}
46
47function installModule(moduleName, modulePath) {
48 if (!module_exists(moduleName) && !fs.existsSync('/opt/qewd/mapped/node_modules/' + moduleName)) {
49 var prefix = '';
50 if (typeof modulePath !== 'undefined') {
51 prefix = ' --prefix ' + modulePath;
52 }
53 child_process.execSync('npm install --unsafe-perm ' + moduleName + prefix, {stdio:[0,1,2]});
54 console.log('\n' + moduleName + ' installed');
55 }
56 else {
57 console.log(moduleName + ' already installed');
58 }
59}
60
61process.env.USER = 'root';
62process.env.HOME = '/opt/qewd'
63
64var startup;
65var qewd_up = false;
66var qewd_up_config_path = '/opt/qewd/mapped/configuration/config.json';
67var qewd_up_path = '/opt/qewd/node_modules/qewd/up';
68
69if (fs.existsSync(qewd_up_config_path)) {
70 var config = require(qewd_up_config_path);
71 qewd_up = (config.qewd_up === true);
72}
73
74if (qewd_up) {
75 if (process.env.microservice) {
76 console.log('starting up microservice ' + process.env.microservice);
77 //startup = require(qewd_up_path + '/docker_ms_startup')();
78 startup = require(qewd_up_path + '/run')(true);
79 }
80 else {
81 console.log('starting up Docker Orchestrator service');
82 //startup = qewd.up_run_orchestrator();
83 startup = require(qewd_up_path + '/run')(true);
84 }
85}
86else {
87 startup = require('/opt/qewd/mapped/startup');
88}
89
90var userDefined = startup.userDefined;
91if (!userDefined && fs.existsSync('/opt/qewd/mapped/userDefined.json')) {
92 userDefined = require('/opt/qewd/mapped/userDefined.json');
93}
94
95if (userDefined && userDefined.startup_commands) {
96 console.log('Running custom startup commands:');
97 userDefined.startup_commands.forEach(function(cmnd) {
98 console.log(cmnd);
99 child_process.execSync(cmnd, {stdio:[0,1,2]});
100 });
101}
102
103var npmModules;
104var modulePath;
105if (fs.existsSync('/opt/qewd/mapped/install_modules.json')) {
106 if (!fs.existsSync('/opt/qewd/mapped/node_modules')) {
107 fs.mkdirSync('/opt/qewd/mapped/node_modules');
108 }
109
110 modulePath = '/opt/qewd/mapped';
111 process.env.NODE_PATH = '/opt/qewd/mapped/node_modules:' + process.env.NODE_PATH;
112 require('module').Module._initPaths();
113
114 npmModules = require('/opt/qewd/mapped/install_modules.json');
115 npmModules.forEach(function(moduleName) {
116 console.log('\nInstalling module ' + moduleName + ' to ' + modulePath);
117 installModule(moduleName, modulePath);
118 });
119 console.log('** NODE_PATH = ' + process.env.NODE_PATH);
120}
121
122var config = startup.config;
123
124if (config.database && config.database.type === 'gtm') {
125
126 // Define YottaDB Environment Variables for Worker Processes
127
128 console.log('Setting up YottaDB Environment');
129
130 var ydb_path = ydb_versionp + '_' + ydb_arch;
131 var gtm_path = gtm_version + '_' + ydb_arch;
132
133 config.database = {
134 type: 'gtm',
135 params:{
136 ydb_env: {
137 ydb_retention: 42,
138 gtm_retention: 42,
139 LD_LIBRARY_PATH: '/usr/local/lib/yottadb/' + ydb_version,
140 ydb_log: '/tmp/yottadb/' + ydb_path,
141 gtm_log: '/tmp/yottadb/' + ydb_path,
142 gtm_repl_instance: '/root/.yottadb/' + ydb_path + '/g/yottadb.repl',
143 ydb_repl_instance: '/root/.yottadb/' + ydb_path + '/g/yottadb.repl',
144 ydb_gbldir: '/root/.yottadb/' + ydb_path + '/g/yottadb.gld',
145 ydb_etrap: 'Write:(0=$STACK) "Error occurred: ",$ZStatus,!',
146 ydb_dir: '/root/.yottadb',
147 gtmver: gtm_path,
148 ydb_rel: ydb_path,
149 gtmgbldir: '/root/.yottadb/' + ydb_path + '/g/yottadb.gld',
150 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',
151 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',
152 GTMCI: '/opt/qewd/node_modules/nodem/resources/nodem.ci',
153 ydb_ci: '/opt/qewd/node_modules/nodem/resources/nodem.ci',
154 gtmdir: '/root/.fis-gtm',
155 gtm_etrap: 'Write:(0=$STACK) "Error occurred: ",$ZStatus,!',
156 ydb_tmp: '/tmp/yottadb/' + ydb_path,
157 gtm_tmp: '/tmp/yottadb/' + ydb_path,
158 gtm_dist: '/usr/local/lib/yottadb/' + ydb_version,
159 ydb_dist: '/usr/local/lib/yottadb/' + ydb_version
160 }
161 }
162 };
163
164 setEnv(config.database.params.ydb_env);
165
166 // workaround NPM5 bug
167
168 try {
169 var nm = require('nodem');
170 }
171 catch(err) {
172 installModule('nodem');
173 }
174
175 // rundown the default region database (all globals except CacheTempEWDSession)
176 // it may return an error, so wrap in a try/catch
177
178 try {
179 console.log('Running down YottaDB...');
180 child_process.execSync(config.database.params.ydb_env.ydb_dist + '/mupip rundown -region DEFAULT', {stdio:[0,1,2]});
181 child_process.execSync(config.database.params.ydb_env.ydb_dist + '/mupip rundown -region qewdreg', {stdio:[0,1,2]});
182 console.log('Rundown completed');
183 }
184 catch(err) {
185 console.log('Error running down YottaDB: ' + err);
186 console.log('Recovering journal...');
187 child_process.execSync(config.database.params.ydb_env.ydb_dist + '/mupip journal -recover -backward /root/.yottadb/' + ydb_path + '/g/yottadb.mjl', {stdio:[0,1,2]});
188 console.log('Journal recovered');
189 }
190}
191
192// ready to start QEWD now
193
194var qewd_master = qewd.master;
195console.log('Starting QEWD');
196
197var q = qewd_master.start(config, startup.routes);
198if (userDefined) {
199 for (var name in userDefined) {
200 q.userDefined[name] = userDefined[name];
201 }
202}
203
204// invoke user-specific startup code
205
206if (startup.onStarted) {
207 startup.onStarted.call(q);
208}
209