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-004';
|
37 | const ydb_versionp = 'r1.22';
|
38 | const ydb_version = 'r122';
|
39 | const ydb_arch = 'x86_64';
|
40 |
|
41 | function setEnv(params) {
|
42 | for (var name in params) {
|
43 | process.env[name] = params[name];
|
44 | }
|
45 | }
|
46 |
|
47 | function 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 |
|
61 | process.env.USER = 'root';
|
62 | process.env.HOME = '/opt/qewd'
|
63 |
|
64 | var startup;
|
65 | var qewd_up = false;
|
66 | var qewd_up_config_path = '/opt/qewd/mapped/configuration/config.json';
|
67 | var qewd_up_path = '/opt/qewd/node_modules/qewd/up';
|
68 |
|
69 | if (fs.existsSync(qewd_up_config_path)) {
|
70 | var config = require(qewd_up_config_path);
|
71 | qewd_up = (config.qewd_up === true);
|
72 | }
|
73 |
|
74 | if (qewd_up) {
|
75 | if (process.env.microservice) {
|
76 | console.log('starting up microservice ' + process.env.microservice);
|
77 |
|
78 | startup = require(qewd_up_path + '/run')(true);
|
79 | }
|
80 | else {
|
81 | console.log('starting up Docker Orchestrator service');
|
82 |
|
83 | startup = require(qewd_up_path + '/run')(true);
|
84 | }
|
85 | }
|
86 | else {
|
87 | startup = require('/opt/qewd/mapped/startup');
|
88 | }
|
89 |
|
90 | var userDefined = startup.userDefined;
|
91 | if (!userDefined && fs.existsSync('/opt/qewd/mapped/userDefined.json')) {
|
92 | userDefined = require('/opt/qewd/mapped/userDefined.json');
|
93 | }
|
94 |
|
95 | if (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 |
|
103 | var npmModules;
|
104 | var modulePath;
|
105 | if (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 |
|
122 | var config = startup.config;
|
123 |
|
124 | if (config.database && config.database.type === 'gtm') {
|
125 |
|
126 |
|
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 |
|
167 |
|
168 | try {
|
169 | var nm = require('nodem');
|
170 | }
|
171 | catch(err) {
|
172 | installModule('nodem');
|
173 | }
|
174 |
|
175 |
|
176 |
|
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 |
|
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 |
|
205 |
|
206 | if (startup.onStarted) {
|
207 | startup.onStarted.call(q);
|
208 | }
|
209 |
|