forked from ibmdb/node-ibm_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriverInstall.js
395 lines (354 loc) · 15.6 KB
/
driverInstall.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* Node-ibm_db Installer file.
*/
var fs = require('fs');
var url = require('url');
var os = require('os');
var path = require('path');
var exec = require('child_process').exec;
var request = require('request');
//IBM provided URL for downloading clidriver.
var installerURL = 'https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli';
var CURRENT_DIR = process.cwd();
var DOWNLOAD_DIR = path.resolve(CURRENT_DIR, 'installer');
var INSTALLER_FILE;
var deleteInstallerFile = false;
/*
* "process.env.IBM_DB_INSTALLER_URL"
* USE: to by-pass the IBM provided URL for downloading clidriver.
* HOW: set environment variable with alternate downloading URL link.
* or locally downloaded "tar/zipped clidriver's" parent directory path.
*/
installerURL = process.env.IBM_DB_INSTALLER_URL || installerURL;
installerURL = installerURL + "/";
//Function to download clidriver and install node-ibm_db
var install_node_ibm_db = function(file_url) {
var readStream;
var writeStream;
var platform = os.platform();
var arch = os.arch();
var endian = os.endianness();
var installerfileURL;
var fstream = require('fstream');
var unzipper = require('unzipper');
var IBM_DB_HOME, IBM_DB_INCLUDE, IBM_DB_LIB, IBM_DB_DIR;
if(platform == 'win32') {
if(arch == 'x64') {
var BUILD_FILE = path.resolve(CURRENT_DIR, 'build.zip');
//Windows node binary names should update here.
var ODBC_BINDINGS = 'build\/Release\/odbc_bindings.node';
var ODBC_BINDINGS_V12 = 'build\/Release\/odbc_bindings.node.0.12.7';
var ODBC_BINDINGS_V4 = 'build\/Release\/odbc_bindings.node.4.6.1';
var ODBC_BINDINGS_V6 = 'build\/Release\/odbc_bindings.node.6.9.1';
// Windows add-on binary for node.js v0.10.x has been discontinued.
if(Number(process.version.match(/^v(\d+\.\d+)/)[1]) == 0.10){
console.log('\nERROR: Found unsupported node.js version ' + process.version +
'\nnode-ibm_db do not have precompiled add-on file odbc_bindings.node for\n' +
'node.js v0.10.x on Widnows. Please use the latest version of node.js.\n');
process.exit(1);
}
// Windows add-on binary for node.js v0.12.x has been deprecated.
if(Number(process.version.match(/^v(\d+\.\d+)/)[1]) == 0.12){
console.log('\nWARNING: Found node.js version ' + process.version +
'\nSupport for node-ibm_db on Windows for node.js version 0.12.x is deprecated\n' +
'and will be discontinued soon. Please use the latest version of node.js.\n');
}
/*
* odbcBindingsNode will consist of the node binary-
* file name according to the node version in the system.
*/
var odbcBindingsNode = (Number(process.version.match(/^v(\d+\.\d+)/)[1]) < 4.0) && ODBC_BINDINGS_V12 ||
(Number(process.version.match(/^v(\d+\.\d+)/)[1]) < 5.0) && ODBC_BINDINGS_V4 ||
(Number(process.version.match(/^v(\d+\.\d+)/)[1]) < 7.0) && ODBC_BINDINGS_V6 || ODBC_BINDINGS ;
readStream = fs.createReadStream(BUILD_FILE);
/*
* unzipper will parse the build.zip file content and
* then it will check for the odbcBindingsNode
* (node Binary), when it gets that binary file,
* fstream.Writer will write the same node binary
* but the name will be odbc_bindings.node, and the other
* binary files and build.zip will be discarded.
*/
readStream.pipe(unzipper.Parse())
.on('entry', function (entry) {
if(entry.path === odbcBindingsNode){
entry.pipe(fstream.Writer(ODBC_BINDINGS));
} else {
entry.autodrain();
}
})
.on('error', function(e) {
console.log('error',e);
})
.on('finish', function() {
fs.unlinkSync(BUILD_FILE);
});
removeUsedPackages();
} else {
console.log('Windows 32 bit not supported. Please use an ' +
'x64 architecture.');
return;
}
}
/*
* IF: IBM_DB_HOME path is set,
* clidriver will not be download from remote location
* node-ibm_db will use local clidriver package stored in-
* IBM_DB_HOME path location.
* ELSE: platform specific compressed clidriver package will be download
* and then extract for further use.
*/
if(process.env.IBM_DB_HOME)
{
IBM_DB_HOME = process.env.IBM_DB_HOME;
IBM_DB_INCLUDE = path.resolve(IBM_DB_HOME, 'include');
if (fs.existsSync(IBM_DB_HOME + "/lib64")) {
IBM_DB_LIB = path.resolve(IBM_DB_HOME, 'lib64');
} else if (fs.existsSync(IBM_DB_HOME + "/lib32")) {
IBM_DB_LIB = path.resolve(IBM_DB_HOME, 'lib32');
} else {
IBM_DB_LIB = path.resolve(IBM_DB_HOME, 'lib');
}
console.log('IBM_DB_HOME environment variable have already been set to '+IBM_DB_HOME);
if (!fs.existsSync(IBM_DB_HOME)) {
console.log(IBM_DB_HOME + ' directory does not exist. Please check if you have ' +
'set the IBM_DB_HOME environment variable\'s value correctly.');
}
if (!fs.existsSync(IBM_DB_INCLUDE)) {
console.log(IBM_DB_INCLUDE + ' directory does not exist. Please check if you have ' +
'set the IBM_DB_HOME environment variable\'s value correctly.');
}
if (!fs.existsSync(IBM_DB_LIB)) {
console.log(IBM_DB_LIB + ' directory does not exist. Please check if you have ' +
'set the IBM_DB_HOME environment variable\'s value correctly.');
}
if( platform != 'win32') {
if(!fs.existsSync(IBM_DB_HOME + "/lib"))
fs.symlinkSync(IBM_DB_LIB, path.resolve(IBM_DB_HOME, 'lib'));
if((platform == 'linux') || (platform =='aix') ||
(platform == 'darwin' && arch == 'x64')) {
removeWinBuildArchive();
buildBinary(false);
} else {
console.log('Building binaries for node-ibm_db. This platform is not completely supported, you might encounter errors. In such cases please open an issue on our repository, https://github.com/ibmdb/node-ibm_db.');
buildBinary(false);
}
}
} else {
if(platform == 'win32')
{
if(arch == 'x64') {
installerfileURL = installerURL + 'ntx64_odbc_cli.zip';
}/* else {
installerfileURL = installerURL + 'nt32_odbc_cli.zip';
}*/
}
else if(platform == 'linux')
{
if(arch == 'x64') {
installerfileURL = installerURL + 'linuxx64_odbc_cli.tar.gz';
} else if(arch == 's390x') {
installerfileURL = installerURL + 's390x64_odbc_cli.tar.gz';
} else if(arch == 's390') {
installerfileURL = installerURL + 's390_odbc_cli.tar.gz';
} else if(arch == 'ppc64') {
if(endian == 'LE')
installerfileURL = installerURL + 'ppc64le_odbc_cli.tar.gz';
else
installerfileURL = installerURL + 'ppc64_odbc_cli.tar.gz';
} else if(arch == 'ppc32') {
installerfileURL = installerURL + 'ppc32_odbc_cli.tar.gz';
} else {
installerfileURL = installerURL + 'linuxia32_odbc_cli.tar.gz';
}
}
else if(platform == 'darwin')
{
if(arch == 'x64') {
installerfileURL = installerURL + 'macos64_odbc_cli.tar.gz';
} else {
console.log('Mac OS 32 bit not supported. Please use an ' +
'x64 architecture.');
return;
}
}
else if(platform == 'aix')
{
if(arch == 'ppc')
{
installerfileURL = installerURL + 'aix32_odbc_cli.tar.gz';
}
else
{
installerfileURL = installerURL + 'aix64_odbc_cli.tar.gz';
}
}
else
{
installerfileURL = installerURL + platform + arch +
'_odbc_cli.tar.gz';
}
if(!installerfileURL) {
console.log('Unable to fetch driver download file. Exiting the ' +
'install process.');
process.exit(1);
}
var license_agreement = '\n\n****************************************\nYou are downloading a package which includes the Node.js module for IBM DB2/Informix. The module is licensed under the Apache License 2.0. The package also includes IBM ODBC and CLI Driver from IBM, which is automatically downloaded as the node module is installed on your system/device. The license agreement to the IBM ODBC and CLI Driver is available in '+DOWNLOAD_DIR+' Check for additional dependencies, which may come with their own license agreement(s). Your use of the components of the package and dependencies constitutes your acceptance of their respective license agreements. If you do not accept the terms of any license agreement(s), then delete the relevant component(s) from your device.\n****************************************\n';
var file_name = url.parse(installerfileURL).pathname.split('/').pop();
INSTALLER_FILE = path.resolve(DOWNLOAD_DIR, file_name);
console.log('Downloading DB2 ODBC CLI Driver from ' +
installerfileURL+'...\n');
fs.stat(installerfileURL, function (err, stats) {
if (!err && stats.isFile()) {
INSTALLER_FILE = installerfileURL;
return copyAndExtractDriver();
}
return getInstallerFile(installerfileURL);
});
} // * END OF EXECUTION */
function copyAndExtractDriver() {
if(platform == 'win32') {
readStream = fs.createReadStream(INSTALLER_FILE);
/* unzipper.Extract will extract the clidriver zipped-
* file content to DOWNLOAD_DIR.
*/
var extractCLIDriver = readStream.pipe(unzipper.Extract({path: DOWNLOAD_DIR}));
/* After successful closing of the event,
* license_agreement and Download and extraction
* of DB2 ODBC CLI Driver acknowledgement will display.
*/
extractCLIDriver.on('close', function() {
console.log(license_agreement);
console.log('Downloading and extraction of DB2 ODBC ' +
'CLI Driver completed successfully... \n');
if(deleteInstallerFile) removeInstallerFile();
});
extractCLIDriver.on('err', function() {
console.log(err);
});
}
else
{
var targz = require('targz');
var compress = targz.decompress({src: INSTALLER_FILE, dest: DOWNLOAD_DIR}, function(err){
if(err) {
console.log(err);
process.exit(1);
}
else {
console.log(license_agreement);
console.log('Downloading and extraction of DB2 ODBC ' +
'CLI Driver completed successfully ...');
IBM_DB_HOME = path.resolve(DOWNLOAD_DIR, 'clidriver');
process.env.IBM_DB_HOME = IBM_DB_HOME.replace(/\s/g,'\\ ');
buildBinary(true);
removeWinBuildArchive();
if(deleteInstallerFile) removeInstallerFile();
}
});
}
}
function buildBinary(isDownloaded)
{
var buildString = "node-gyp configure build --IBM_DB_HOME=\"$IBM_DB_HOME\"";
if(isDownloaded) {
buildString = buildString + " --IS_DOWNLOADED=true";
} else {
buildString = buildString + " --IS_DOWNLOADED=false";
}
if( platform == 'win32')
{
buildString = buildString + " --IBM_DB_HOME_WIN=%IBM_DB_HOME%";
}
var childProcess = exec(buildString, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log(error);
process.exit(1);
}
if(platform == 'darwin' && arch == 'x64')
{
// Run the install_name_tool
var nameToolCommand = "install_name_tool -change libdb2.dylib \"$IBM_DB_HOME/lib/libdb2.dylib\" ./build/Release/odbc_bindings.node" ;
var nameToolCmdProcess = exec(nameToolCommand ,
function (error1, stdout1, stderr1) {
if (error1 !== null) {
console.log('Error setting up the lib path to ' +
'odbc_bindings.node file.Error trace:\n'+error1);
process.exit(1);
}
});
}
removeUsedPackages();
});
} //buildBinary
function removeUsedPackages()
{
var packages = ["nan", "fstream", "unzipper", "targz", "request"];
for( var index = 0; index < packages.length; index++ )
{
var command = "npm uninstall " + packages[index];
var childProcess = exec(command, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log(error);
// Ignore error and continue to remove other packages.
// Installation of ibm_db should not fail due to such errors.
}
});
}
}
function removeWinBuildArchive()
{
var WIN_BUILD_FILE = path.resolve(CURRENT_DIR, 'build.zip');
fs.exists(WIN_BUILD_FILE, function(exists)
{
if (exists)
{
fs.unlinkSync(WIN_BUILD_FILE);
}
});
}
function removeInstallerFile()
{
// Delete downloaded odbc_cli.tar.gz file.
fs.exists(INSTALLER_FILE, function(exists)
{
if (exists)
{
fs.unlinkSync(INSTALLER_FILE);
}
});
}
// Function to download clidriver file using request module.
function getInstallerFile(installerfileURL) {
// Variable to save downloading progress
var received_bytes = 0;
var total_bytes = 0;
var outStream = fs.createWriteStream(INSTALLER_FILE);
request
.get(installerfileURL)
.on('error', function(err) {
console.log(err);
})
.on('response', function(data) {
total_bytes = parseInt(data.headers['content-length']);
})
.on('data', function(chunk) {
received_bytes += chunk.length;
showDownloadingProgress(received_bytes, total_bytes);
})
.pipe(outStream);
deleteInstallerFile = true;
outStream.once('close', copyAndExtractDriver)
.once('error', function (err) {
cosole.log(err);
});
};
function showDownloadingProgress(received, total) {
var percentage = ((received * 100) / total).toFixed(2);
process.stdout.write((platform == 'win32') ? "\033[0G": "\r");
process.stdout.write(percentage + "% | " + received + " bytes downloaded out of " + total + " bytes.");
}
}; //install_node_ibm_db
install_node_ibm_db();