-
Notifications
You must be signed in to change notification settings - Fork 41
/
postinstall.js
executable file
·400 lines (355 loc) · 12.7 KB
/
postinstall.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
396
397
398
399
400
"use strict";
// External packages we require
const fs = require("fs");
const path = require("path");
const http = require("http");
const https = require("https");
const { ProxyAgent } = require("proxy-agent");
const execSync = require("child_process").execSync;
// How to get to the MQ Redistributable Client package
const protocol = "https://";
const host="public.dhe.ibm.com";
const baseDir="ibmdl/export/pub/software/websphere/messaging/mqdev";
const redistDir=baseDir+"/redist";
// const macDir=baseDir+"/mactoolkit";
// This is the version (VRM) of MQ associated with this level of package
let vrm="9.4.1";
// This is the default fixpack or CSU level that we might want to apply
const defaultFp="0";
// Try a few ways to pick up environment variables
function pEnv(s) {
let e = process.env[s];
let v;
if (!e) {
v = "npm_config_" + s;
e = process.env[v];
}
if (!e) {
v = "npm_config_" + s.toLowerCase();
e = process.env[v];
}
if (!e) {
v = "NPM_CONFIG_" + s;
e = process.env[v];
}
if (!e) {
v = v.toUpperCase();
e = process.env[v];
}
return e;
}
// Allow overriding the VRM - but you need to be careful as this package
// may depend on MQI features in the listed version. Must be given in the
// same format eg "1.2.3". Note that IBM keeps a limited set of versions for
// download - once a version of MQ is no longer supported, that level of
// Redistributable Client package may be removed from public sites.
const vrmenv=pEnv("MQIJS_VRM");
if (vrmenv != null) {
vrm=vrmenv;
}
// Set the fixpack which is also part of the downloadable name. Default
// to 0, which is the base level for CD versions of MQ. Only
// the LTS level gets fixpacks but CD versions do now receive
// CSU ("cumulative security update") releases which are equivalent
// to fixpacks in the installation/version sense.
let vrmf=vrm + "." + defaultFp;
// Allow overriding the fixpack for both LTS and CD versions to permit
// picking up a CSU.
const fixpack=pEnv("MQIJS_FIXPACK");
if (fixpack != null ) {
vrmf=vrm+"."+fixpack;
}
let file=vrmf + "-IBM-MQC-Redist-"; // will be completed by platform filetype
const redistTitle="IBM MQ Redistributable C Client";
const dir=redistDir; // Default
const title=redistTitle;
// Other local variables
let unpackCommand;
let unwantedDirs;
const newBaseDir="redist";
let currentPlatform=process.platform;
// currentPlatform='darwin'; // for forcing a platform test
if (pEnv("MQIJS_PLATFORM") != null) {
currentPlatform=pEnv("MQIJS_PLATFORM"); // Another way to override
}
// Some functions used at the end of the operation
function cleanup(rc) {
// Always run to try to delete the downloaded zip/tar file
try {
// Allow it to be overridden for debug purposes
const doNotRemove = pEnv("MQIJS_NOREMOVE_DOWNLOAD");
if (doNotRemove == null) {
if (rc == 0) {
// Only print the removal message if we've got here OK
// to avoid confusion if there's been some error,
// but try to remove the file anyway just in case.
console.log("Removing " + file);
}
fs.unlinkSync(file);
} else {
// If there was an error, we will leave the file alone if it exists,
// but it's probably not there. So don't print this progress message.
if (rc == 0) {
console.log("Preserving " + file);
}
}
} catch(err) {
// Ignore error
}
// This script originally deliberately ignored errors in downloading
// the redist client package. But when npm changed policy to not
// print a lot of status output during script execution it was harder
// to see that something was broken. So we now exit with an error if
// requested.
process.exit(rc);
}
function printError(err) {
console.error("Error occurred downloading " + title + ": " + err.message);
console.error("You will need to manually install it.");
cleanup(1);
}
// Equivalent of "rm -rf"
function removeDirRecursive(d) {
if (fs.existsSync(d)) {
fs.readdirSync(d).forEach(function (e) {
const ePath = path.join(d, e);
if (fs.lstatSync(ePath).isDirectory()) {
removeDirRecursive(ePath);
} else {
// Keep runmqsc as it might be useful for creating CCDTs locally,
// particularly in a containerised runtime. And runmqakm might be
// needed if you want to manage certs locally rather than outside
// a container.
if (!e.match(/runmqsc/) && !e.match(/runmqakm/) && !e.match(/run.*cred/)) {
fs.unlinkSync(ePath);
}
}
});
fs.rmdirSync(d); // This fails on the directory containing runmqsc but that's fine
}
}
function removePattern(d,type) {
if (fs.lstatSync(d).isDirectory()) {
fs.readdirSync(d).forEach(function (e) {
if (e.match(type)) {
const ePath = path.join(d, e);
try {
fs.unlinkSync(ePath);
} catch (err) {
// ignore the error
}
}
});
}
}
function removeUnthreaded(d) {
if (fs.lstatSync(d).isDirectory()) {
fs.readdirSync(d).forEach(function (e) {
if (e.match(/.*.so/) && !(e.match(/.*_r.so/))) {
const t = e.replace(".so","_r.so");
const tpath = path.join(d,t);
if (fs.existsSync(tpath)) {
const ePath = path.join(d, e);
try {
fs.unlinkSync(ePath);
} catch (err) {
// ignore the error
}
}
}
});
}
}
// The genmqpkg script is better maintained than having the unwanted directories/files
// explicitly named.
function removeUnneededWithGenMQPkg(fullNewBaseDir) {
const doNotRemove = pEnv("MQIJS_NOREMOVE");
if (doNotRemove != null) {
console.log("Environment variable set to keep all files in client package");
} else {
// Set the filters for which files genmqpkg is going to preserve
process.env.genmqpkg_incnls=1;
process.env.genmqpkg_incsdk=1;
process.env.genmqpkg_inctls=1;
process.env.genmqpkg_incras=1;
process.env.genmqpkg_incadm=1; // For runmqsc, even though it leaves more files than we really need
let debugGenObj = {};
let debugGenOpt="";
if (pEnv("MQIJS_TRACE_GENMQPKG") != null) {
debugGenObj = { stdio:"inherit" };
debugGenOpt="-v";
}
const psCmd="cd " + fullNewBaseDir + ";./bin/genmqpkg.sh -b " + debugGenOpt + " " + fullNewBaseDir;
console.log("Running genmqpkg...");
execSync(psCmd, debugGenObj );
cleanup(0);
}
}
// Remove directories from the client that are not needed for Node execution. This
// should help shrink any runtime container a bit
function removeUnneeded() {
let d;
const doNotRemove = pEnv("MQIJS_NOREMOVE");
if (doNotRemove != null) {
console.log("Environment variable set to keep all files in client package");
} else {
for (let i=0;i<unwantedDirs.length;i++) {
try {
removeDirRecursive(path.join(newBaseDir,unwantedDirs[i]));
} catch (err) {
// don't really care
}
}
if (currentPlatform === "win32") {
d = path.join(newBaseDir,"bin64");
removePattern(d,/.exe$/);
removePattern(d,/^imq.*.dll$/);
} else {
d= path.join(newBaseDir,"lib");
removePattern(d,/lib.*so/);
removePattern(d,/amqtrc.fmt/);
removePattern(d,/amqlcelp.*/);
d= path.join(newBaseDir,"lib64");
removePattern(d,/libimq.*so/);
removePattern(d,/libedit.so/);
removePattern(d,/amq.*.dll/);
removePattern(d,/amqlcelp$/);
// Get rid of all the non-threaded libraries
removeUnthreaded(d);
d= path.join(newBaseDir,"lap");
removePattern(d,/.*.jar/);
}
}
cleanup(0);
}
// Check that we have an executable command on Linux. A simple way for the commands we care about
// is to run it with "--help". We know they support that option.
function isRunnable(cmd) {
try {
// Don't want to see any output from the command
execSync(cmd + " --help", { stdio:"ignore" });
} catch (err) {
console.error("ERROR: Cannot run command: %s. Make sure it is installed and in the PATH.", cmd);
return false;
}
return true;
}
// Start main processing here.
// If a particular environment variable is set, do not try to install
// the Redist client package. I did consider doing this automatically by
// trying to locate the libraries in the "usual" places, but decided it was
// better to be explicit about the choice.
const doit = pEnv("MQIJS_NOREDIST");
if (doit != null) {
console.log("Environment variable set to not install " + title);
process.exit(0);
}
// Check if the install is for an environment where there is a Redistributable Client.
if (currentPlatform === "win32") {
// The Windows "unpack" command here simply creates the output directory. Other processing
// will do the real unpack.
file=file+"Win64.zip";
unpackCommand="mkdir " + newBaseDir;
unwantedDirs=[ "exits","exits64", "bin",
"tools/cobol", "tools/cplus", "tools/dotnet",
"tools/c/Samples", "tools/c/include",
"tools/Lib", "tools/Lib64",
"zips",
"java", "bin64/VS2015" ];
} else if (currentPlatform === "linux" && process.arch === "x64"){
file=file+"LinuxX64.tar.gz";
// The unpack process requires that you've got tar and gzip installed
const exe1 = isRunnable("tar");
const exe2 = isRunnable("gzip");
if (!exe1 || !exe2) {
cleanup(1);
}
unpackCommand="mkdir -p " + newBaseDir + " && tar -xvzf " + file + " -C " + newBaseDir;
// These "unwanted" directories are not explicitly used with the genmqpkg.sh command that
// is now being used.
unwantedDirs=[ "samp",
"bin",
"inc",
"java",
"gskit8/lib",
"lib64/netstandard2.0",
".github" ];
// } else if (currentPlatform === 'darwin'){
// Note that the MacOS toolkit is now available via homebrew
// It cannot be simply installed via this script
} else {
console.log("No redistributable client package available for this platform.");
console.log("If an MQ Client library exists for the platform, install it manually.");
if (currentPlatform === 'darwin'){
console.log("You might be able to use homebrew to install the MQ client.");
}
process.exit(0);
}
// Don't download if it looks to already be there - though Node will usually be running this in a clean directory
const idTagFile = path.join(newBaseDir,"swidtag","ibm.com_IBM_MQ_Client-" + vrm + ".swidtag");
// console.log("Checking for existence of " + idTagFile);
if (fs.existsSync(idTagFile)) {
console.log("The " + title + " appears to already be installed");
process.exit(0);
}
console.log("Downloading " + title + " runtime libraries - version " + vrmf);
// Define the file to be downloaded (it will be deleted later, after unpacking)
let url = protocol + host + "/" + dir + "/" + file;
const useLocalUrl = pEnv("MQIJS_LOCAL_URL");
const useLocalServer = pEnv("MQIJS_LOCAL_SERVER");
if (useLocalUrl != null) {
url = useLocalUrl + "/" + file;
} else if (useLocalServer != null) {
url = "http://localhost:8000/"+file; // My local version for testing this script
}
console.log("Getting " + url);
let client = http;
if (url.startsWith("https"))
client=https;
// Now download and unpack the file in the platform-specific fashion.
// The 'request' package would simplify this code, but it introduces too many
// prereqs for just an installation step.
try {
// The downloaded file goes into the current directory (node_modules/ibmmq)
const fd = fs.openSync(file, "w");
const agent = new ProxyAgent();
client.get(url, { agent }, (res) => {
let error;
if (res.statusCode < 200 || res.statusCode > 299) {
error = new Error("Request Failed. HTTP Status Code: " + res.statusCode);
}
if (error) {
res.resume();
printError(error);
}
res.on("data", (chunk) => {
fs.appendFileSync(fd,chunk);
});
res.on("end", () => {
try {
fs.closeSync(fd);
const fullNewBaseDir=path.resolve(newBaseDir);
console.log("Unpacking libraries into "+ fullNewBaseDir + "...");
execSync(unpackCommand);
// On Windows we use PowerShell to do the unpacking of the zip
// file. We can be "reasonably" certain that this will work. The Node.js
// unzip packages seemed to sometimes create corrupt files silently.
if (currentPlatform === "win32") {
const psCmd = "Expand-Archive -Force -Path " + file + " -DestinationPath " + newBaseDir ;
const psCmd2 = "powershell -command \"" + psCmd + "\"";
execSync(psCmd2, { windowsHide:true });
removeUnneeded();
} else {
removeUnneededWithGenMQPkg(fullNewBaseDir);
// removeUnneeded();
}
} catch (err) {
printError(err);
}
});
}).on("error", (err) => {
printError(err);
});
} catch (err) {
printError(err);
}