1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var url = require("url");
|
12 | var path = require("path");
|
13 | var unzip = require('unzip');
|
14 | var request = require('request');
|
15 | var path = require("path");
|
16 | var upath = require("upath");
|
17 | var queryString = require('querystring');
|
18 | var http = require('http');
|
19 | var https = require('https');
|
20 | var fs = require('fs');
|
21 | var fsExtra = require('fs-extra');
|
22 | var async = require('async');
|
23 | var exec = require('child_process').exec;
|
24 | var spawn = require('child_process').spawn;
|
25 |
|
26 | const Entities = require('html-entities').AllHtmlEntities;
|
27 |
|
28 | const entities = new Entities();
|
29 |
|
30 |
|
31 |
|
32 | var httpsFlag = false;
|
33 | var serverOptions = {};
|
34 | var verbose = false;
|
35 |
|
36 |
|
37 | var configFile = __dirname + '/../../config.json';
|
38 | var targetAddonsFolder = __dirname + "/../";
|
39 | var tempDir = 'temp-installation/';
|
40 | var descriptorFile = "medimage-installer.json";
|
41 |
|
42 |
|
43 |
|
44 | function normalizeInclWinNetworks(path)
|
45 | {
|
46 |
|
47 |
|
48 |
|
49 | if((path[0] == "\\")&&(path[1] == "\\")) {
|
50 |
|
51 | return "\/" + upath.normalize(path);
|
52 | } else {
|
53 | if((path[0] == "\/")&&(path[1] == "\/")) {
|
54 |
|
55 | return "\/" + upath.normalize(path);
|
56 | } else {
|
57 | return upath.normalize(path);
|
58 | }
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 |
|
64 | function getMasterConfig(defaultConfig, callback) {
|
65 | exec("npm get medimage:configFile", {
|
66 | maxBuffer: 2000 * 1024
|
67 | }, (err, stdout, stderr) => {
|
68 | if (err) {
|
69 |
|
70 | console.log("There was a problem running the addon. Error:" + err);
|
71 | callback(err, "");
|
72 |
|
73 | } else {
|
74 | if((stdout != "")&&(!stdout.startsWith("undefined"))) {
|
75 | callback(null, stdout.trim());
|
76 |
|
77 | } else {
|
78 | callback("Global not set", null);
|
79 |
|
80 | }
|
81 | }
|
82 | });
|
83 | }
|
84 |
|
85 | function havePermission(configFile, cb) {
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | getMasterConfig(configFile, function(err, masterConfigFile) {
|
91 | if(err) {
|
92 |
|
93 | } else {
|
94 | configFile = masterConfigFile;
|
95 | }
|
96 | console.log("Using config file:" + configFile);
|
97 |
|
98 |
|
99 | fs.readFile(configFile, function read(err, data) {
|
100 | if (err) {
|
101 | cb("Sorry, cannot read config file! " + err);
|
102 | } else {
|
103 | var content = JSON.parse(data);
|
104 |
|
105 |
|
106 | if(content.lockDown == false) {
|
107 |
|
108 | cb(null, true);
|
109 |
|
110 | } else {
|
111 |
|
112 | cb(null, false);
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 | };
|
118 | });
|
119 | });
|
120 |
|
121 | }
|
122 |
|
123 |
|
124 | function noTrailSlash(str)
|
125 | {
|
126 | if(str.slice(-1) == "/") {
|
127 | return str.slice(0,-1);
|
128 | } else {
|
129 | return str;
|
130 | }
|
131 |
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function getPlatform() {
|
139 |
|
140 | var platform = process.platform;
|
141 | if(verbose == true) console.log(process.platform);
|
142 | var isWin = /^win/.test(platform);
|
143 | if(verbose == true) console.log("IsWin=" + isWin);
|
144 | if(isWin) {
|
145 | if(process.arch == 'x64') {
|
146 | return "win64";
|
147 | } else {
|
148 | return "win32";
|
149 | }
|
150 | } else {
|
151 | if(platform == "darwin") {
|
152 | return "mac";
|
153 | } else {
|
154 | return "unix";
|
155 | }
|
156 |
|
157 | }
|
158 |
|
159 |
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 | function unzipAndRemoveNew(filename, tmpFilePath, cb) {
|
165 |
|
166 |
|
167 |
|
168 | var possFileName = filename.replace(/.zip/i, "") + "/";
|
169 |
|
170 | try {
|
171 |
|
172 | fs.createReadStream(tmpFilePath).pipe(unzip.Extract({ path: targetAddonsFolder + tempDir }))
|
173 | .on('close', function() {
|
174 | console.log("Finished");
|
175 |
|
176 |
|
177 | var dir = "";
|
178 | var fileCnt = 0;
|
179 | var dirCnt = 0;
|
180 |
|
181 | fs.readdirSync(targetAddonsFolder + tempDir).forEach(file => {
|
182 | if(verbose == true) console.log(file);
|
183 | if(fs.lstatSync(targetAddonsFolder + tempDir + '/' + file).isDirectory()) {
|
184 | if(verbose == true) console.log("Is a directory");
|
185 | dir = file;
|
186 | dirCnt ++;
|
187 | } else {
|
188 | if((file[0] == '.')||(file == filename)) {
|
189 | if(verbose == true) console.log("Is a hidden file - not counted");
|
190 | } else {
|
191 | if(verbose == true) console.log("Is a file");
|
192 | fileCnt ++;
|
193 | }
|
194 | }
|
195 | })
|
196 |
|
197 | if((fileCnt == 0)&&(dirCnt == 1)) {
|
198 |
|
199 | var dirName = dir;
|
200 |
|
201 |
|
202 | var fileCnt = 0;
|
203 | var dirCnt = 0;
|
204 | var dir = "";
|
205 |
|
206 | fs.readdirSync(targetAddonsFolder + tempDir + '/' + dirName).forEach(file => {
|
207 | if(verbose == true) console.log(file);
|
208 | if(fs.lstatSync(targetAddonsFolder + tempDir + '/' + dirName + '/' + file).isDirectory()) {
|
209 | if(verbose == true) console.log("Is a directory");
|
210 | dir = file;
|
211 | dirCnt ++;
|
212 | } else {
|
213 | if(file[0] == '.') {
|
214 | if(verbose == true) console.log("Is a hidden file - not counted");
|
215 | } else {
|
216 | if(verbose == true) console.log("Is a file");
|
217 | fileCnt ++;
|
218 | }
|
219 | }
|
220 | })
|
221 |
|
222 | if((fileCnt == 0)&&(dirCnt == 1)) {
|
223 |
|
224 | dirName = dirName + "/" + dir;
|
225 |
|
226 | }
|
227 |
|
228 |
|
229 |
|
230 | } else {
|
231 |
|
232 | var dirName = "";
|
233 | }
|
234 |
|
235 | console.log("Output dirname = '" + dirName +"'");
|
236 |
|
237 |
|
238 |
|
239 | fs.unlink(tmpFilePath, function(err) {
|
240 | if(err) {
|
241 | cb(err, null);
|
242 | } else {
|
243 | cb(null, dirName);
|
244 | }
|
245 | });
|
246 |
|
247 |
|
248 | });
|
249 | } catch(err) {
|
250 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=There was a problem unzipping the file.&EXTENDED=" + err + " Expected folder:" + tempDir);
|
251 | process.exit(0);
|
252 | }
|
253 |
|
254 |
|
255 |
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function downloadAndUnzip(filename, url, opts, cb) {
|
264 | var tmpFilePath = targetAddonsFolder + tempDir + filename;
|
265 | fsExtra.ensureDir(targetAddonsFolder + tempDir, function(err) {
|
266 | if(err) {
|
267 | cb(err, null)
|
268 | } else {
|
269 |
|
270 |
|
271 |
|
272 | var stream = request({url: url, forever: true, followAllRedirects: true });
|
273 | var alreadyClosed = false;
|
274 | stream.pipe(fs.createWriteStream(tmpFilePath)
|
275 | .on('error', function(err) {
|
276 | console.log("Error writing to file");
|
277 | cb(err, null);
|
278 | })
|
279 | )
|
280 | .on('close', function() {
|
281 |
|
282 | console.log("Downloaded successfully!" + tmpFilePath);
|
283 | if(alreadyClosed == false) {
|
284 | alreadyClosed = true;
|
285 |
|
286 |
|
287 | unzipAndRemoveNew(filename, tmpFilePath, cb);
|
288 |
|
289 | } else {
|
290 | console.log("2nd close event");
|
291 | }
|
292 | });
|
293 |
|
294 |
|
295 | }
|
296 | })
|
297 |
|
298 |
|
299 | }
|
300 |
|
301 | function removeUnreadableChars(str)
|
302 | {
|
303 | str = str.replace(/\"\;/g, '');
|
304 | str = str.replace(/\&\#10\;/g, '');
|
305 | return str;
|
306 | }
|
307 |
|
308 |
|
309 | function execCommands(commandArray, prepend, cb)
|
310 | {
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | var commandStatus = "success";
|
316 | var commandMessage = "";
|
317 |
|
318 | async.eachOfSeries(commandArray,
|
319 |
|
320 | function(runBlock, cnt, callback){
|
321 |
|
322 | var timeOut = 400000;
|
323 | var timedOut = false;
|
324 |
|
325 |
|
326 | if(commandArray[cnt].attempt) {
|
327 |
|
328 | var cmd = prepend + commandArray[cnt].attempt;
|
329 |
|
330 | if(commandArray[cnt].timeoutAfterSeconds) {
|
331 | timeOut = commandArray[cnt].timeoutAfterSeconds * 1000;
|
332 |
|
333 | }
|
334 |
|
335 | if(commandArray[cnt].warnOnTimeout) {
|
336 | var warningMessage = commandArray[cnt].warnOnTimeout;
|
337 | }
|
338 | } else {
|
339 |
|
340 | var cmd = prepend + commandArray[cnt];
|
341 | }
|
342 | console.log("Running command: " + cmd);
|
343 |
|
344 | var runningOutput = "";
|
345 |
|
346 | var outputStdOut = "";
|
347 | var outputStdError = "";
|
348 |
|
349 | try {
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | cmds = cmd.split(" ");
|
356 | var args = [];
|
357 | var command = "";
|
358 | if(cmds[0]) {
|
359 | args = cmds;
|
360 | }
|
361 |
|
362 |
|
363 | var platform = getPlatform();
|
364 | if((platform == "win32")||(platform == "win64")) {
|
365 | command = "cmd.exe"
|
366 | args.unshift('/low','/s','/c');
|
367 |
|
368 | } else {
|
369 |
|
370 | command = "nice";
|
371 | args.unshift('-10');
|
372 | }
|
373 |
|
374 | var running = spawn(command, args);
|
375 |
|
376 | running.stdout.on('data', (data) => {
|
377 | if(verbose == true) console.log(data.toString());
|
378 | outputStdOut += data.toString();
|
379 |
|
380 | });
|
381 |
|
382 | running.stderr.on('data', (data) => {
|
383 | if(verbose == true) console.log(data.toString());
|
384 | outputStdError += data.toString();
|
385 | });
|
386 |
|
387 |
|
388 |
|
389 | running.on('close', (code, signal) => {
|
390 | if(code != 0) {
|
391 |
|
392 |
|
393 |
|
394 | var msg = "Command: " + cmd + ". Error:" + outputStdError;
|
395 | console.log(msg);
|
396 |
|
397 |
|
398 | console.log("Stdout:" + outputStdOut);
|
399 | console.log("Stderr:" + outputStdError);
|
400 |
|
401 |
|
402 |
|
403 | msg = entities.encodeNonUTF(msg);
|
404 |
|
405 |
|
406 | msg = removeUnreadableChars(msg).substr(0,500);
|
407 |
|
408 |
|
409 |
|
410 | if(timedOut == false) {
|
411 | commandStatus = "error";
|
412 | commandMessage = "The installation was not complete. There was a problem running one of the installation commands. " + msg;
|
413 | } else {
|
414 |
|
415 | }
|
416 | callback(commandMessage, commandStatus);
|
417 | } else {
|
418 |
|
419 | console.log("Stdout from command:" + outputStdOut);
|
420 | callback(null, commandStatus);
|
421 |
|
422 | }
|
423 | });
|
424 |
|
425 |
|
426 | if(timeOut) {
|
427 |
|
428 | setTimeout(function(){
|
429 | if(commandArray[cnt].warnOnTimeout) {
|
430 | commandMessage = commandMessage + commandArray[cnt].warnOnTimeout;
|
431 | commandStatus = "warn";
|
432 | } else {
|
433 | commandMessage = commandMessage + " The installation command timed out and was not complete."
|
434 | commandStatus = "error";
|
435 |
|
436 | }
|
437 | timedOut = true;
|
438 |
|
439 | if((platform == 'win32')||(platform == 'win64')) {
|
440 | console.log(running.pid + " timed out");
|
441 |
|
442 |
|
443 | exec('taskkill /pid ' + running.pid + ' /T /F');
|
444 | } else {
|
445 |
|
446 | console.log(running.pid + " timed out");
|
447 | process.kill(running.pid, 'SIGHUP');
|
448 | }
|
449 |
|
450 |
|
451 | }, timeOut);
|
452 | }
|
453 |
|
454 |
|
455 |
|
456 | } catch(err) {
|
457 | var msg = "There was a problem running the command " + cmd;
|
458 | var ext = err;
|
459 |
|
460 | console.log("Error:" + err);
|
461 | console.log("Stdout:" + outputStdOut);
|
462 | console.log("Stderr:" + outputStdError);
|
463 |
|
464 |
|
465 | ext = entities.encodeNonUTF(ext);
|
466 |
|
467 |
|
468 |
|
469 | ext = removeUnreadableChars(msg).substr(0,500);
|
470 | commandMessage = "The installation was not complete. There was a problem running the one of the installation commands.&EXTENDED=" + cmd + " Error:" + ext;
|
471 |
|
472 | commandStatus = "error";
|
473 | callback(commandMessage, commandStatus);
|
474 |
|
475 | }
|
476 |
|
477 | },
|
478 | function(err){
|
479 |
|
480 | console.log("All tasks finished");
|
481 | if(err) {
|
482 |
|
483 | cb(err, commandStatus);
|
484 | } else {
|
485 | console.log('Completed all commands successfully!');
|
486 | if(commandStatus == "warn") {
|
487 |
|
488 | cb(commandMessage, commandStatus);
|
489 | } else {
|
490 | cb(null, commandStatus);
|
491 | }
|
492 | }
|
493 | }
|
494 | );
|
495 |
|
496 |
|
497 | }
|
498 |
|
499 |
|
500 |
|
501 | function openAndRunDescriptor(directory, opts)
|
502 | {
|
503 | var commandStatus = "success";
|
504 | var commandMessage = "";
|
505 |
|
506 |
|
507 |
|
508 | try {
|
509 | process.chdir(directory);
|
510 | } catch(err) {
|
511 | return console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=This is not a directory.&EXTENDED=" + err);
|
512 | process.exit(0);
|
513 | }
|
514 | var desc = directory + "/" + descriptorFile;
|
515 | console.log("Checking installer file " + desc);
|
516 |
|
517 | if(fs.existsSync(desc) == true) {
|
518 | try {
|
519 | var data = fsExtra.readJsonSync(desc);
|
520 | } catch(err) {
|
521 | console.log("Error: there was a problem in the medimage-installer .json file. " + err);
|
522 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=There was a problem in the medimage-installer .json file.&EXTENDED=" + err);
|
523 | process.exit(0);
|
524 |
|
525 | }
|
526 |
|
527 |
|
528 | if(data) {
|
529 |
|
530 | if(data.installCommands) {
|
531 |
|
532 |
|
533 | var platform = getPlatform();
|
534 | async.waterfall([
|
535 | function(callback) {
|
536 | console.log("Checking all platform commands");
|
537 | if(data.installCommands.all) {
|
538 |
|
539 | var prepend = "";
|
540 | if((platform == "unix")||(platform == "mac")) {
|
541 | if(opts.password != "") {
|
542 | prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
543 | }
|
544 | }
|
545 | execCommands(data.installCommands.all, prepend, function(err, type) {
|
546 | if(type == "warn") {
|
547 | commandMessage = commandMessage + " " + err;
|
548 | callback(null);
|
549 | } else {
|
550 | callback(err);
|
551 | }
|
552 | });
|
553 | } else {
|
554 | callback(null);
|
555 | }
|
556 | },
|
557 | function(callback) {
|
558 | console.log("Checking Win32 commands");
|
559 | if((data.installCommands.win32) && (platform == "win32")) {
|
560 |
|
561 | execCommands(data.installCommands.win32, "", function(err, type) {
|
562 | if(type == "warn") {
|
563 | commandMessage = commandMessage + " " + err;
|
564 | callback(null);
|
565 | } else {
|
566 | callback(err);
|
567 | }
|
568 | });
|
569 | } else {
|
570 | callback(null);
|
571 | }
|
572 | },
|
573 | function(callback) {
|
574 | console.log("Checking Win64 commands");
|
575 | if((data.installCommands.win64) && (platform == "win64")) {
|
576 |
|
577 | execCommands(data.installCommands.win64, "", function(err, type) {
|
578 | if(type == "warn") {
|
579 | commandMessage = commandMessage + " " + err;
|
580 | callback(null);
|
581 | } else {
|
582 | callback(err);
|
583 | }
|
584 | });
|
585 | } else {
|
586 | callback(null);
|
587 | }
|
588 | },
|
589 | function(callback) {
|
590 | console.log("Checking Unix commands");
|
591 | if((data.installCommands.unix) && (platform == "unix")) {
|
592 |
|
593 | if(opts.password != "") {
|
594 | var prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
595 | } else {
|
596 | var prepend = "";
|
597 | }
|
598 | execCommands(data.installCommands.unix, prepend, function(err, type) {
|
599 | if(type == "warn") {
|
600 | commandMessage = commandMessage + " " + err;
|
601 | callback(null);
|
602 | } else {
|
603 | callback(err);
|
604 | }
|
605 | });
|
606 | } else {
|
607 | callback(null);
|
608 | }
|
609 | },
|
610 | function(callback) {
|
611 | console.log("Checking Mac commands");
|
612 | if((data.installCommands.mac) && (platform == "mac")) {
|
613 |
|
614 | if(opts.password != "") {
|
615 | var prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
616 | } else {
|
617 | var prepend = "";
|
618 | }
|
619 | execCommands(data.installCommands.mac, prepend, function(err, type) {
|
620 | if(type == "warn") {
|
621 | commandMessage = commandMessage + " " + err;
|
622 | callback(null, 'done');
|
623 | } else {
|
624 | callback(err, 'done');
|
625 | }
|
626 | });
|
627 | } else {
|
628 | callback(null, 'done');
|
629 | }
|
630 | }
|
631 | ],
|
632 | function (err, result) {
|
633 |
|
634 | if(err) {
|
635 | console.log("The installation was not complete.");
|
636 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=" + err);
|
637 |
|
638 | process.exit(0);
|
639 | } else {
|
640 |
|
641 |
|
642 | var mainMessage = "The installation was completed successfully!";
|
643 | var extendedMessage = "";
|
644 |
|
645 | if(data.successMessages) {
|
646 |
|
647 |
|
648 | if(data.successMessages.all) {
|
649 |
|
650 | if(data.successMessages.all.main) {
|
651 | mainMessage = data.successMessages.all.main;
|
652 | }
|
653 | if(data.successMessages.all.extended) {
|
654 | extendedMessage = data.successMessages.all.extended;
|
655 | }
|
656 |
|
657 | }
|
658 |
|
659 | if(data.successMessages[platform]) {
|
660 |
|
661 | if(data.successMessages[platform].main) {
|
662 | mainMessage = data.successMessages[platform].main;
|
663 | }
|
664 | if(data.successMessages[platform].extended) {
|
665 | extendedMessage = data.successMessages[platform].extended;
|
666 | }
|
667 |
|
668 | }
|
669 | }
|
670 |
|
671 |
|
672 | if(commandMessage != "") {
|
673 | mainMessage = mainMessage + "<div style='padding-top:20px;'><div class='panel panel-warning'><div class='panel-heading'>Warning</div><div class='panel-body'>" + commandMessage + "</div></div></div>";
|
674 | }
|
675 |
|
676 |
|
677 | removeOldTemp(opts, function(err) {
|
678 |
|
679 |
|
680 |
|
681 | if(err) {
|
682 |
|
683 | console.log("The installation was completed successfully, but the cleanup was not. ");
|
684 | console.log("reloadConfig:true");
|
685 | console.log("returnParams:?FINISHED=true&TABSTART=install-addon-tab&MSG=" + mainMessage + " Warning: the cleanup was not finished.&EXTENDED=You should check your add-ons folder and remove the " + tempDir + " folder manually. " + extendedMessage);
|
686 | process.exit(0);
|
687 | } else {
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | console.log(mainMessage);
|
693 | console.log("reloadConfig:true");
|
694 | console.log("returnParams:?FINISHED=true&TABSTART=install-addon-tab&MSG=" + mainMessage + "&EXTENDED=" + extendedMessage);
|
695 | process.exit(0);
|
696 |
|
697 | }
|
698 |
|
699 | })
|
700 |
|
701 | }
|
702 | });
|
703 |
|
704 | } else {
|
705 | console.log("Warning: no valid install commands.");
|
706 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=No valid install commands.");
|
707 | process.exit(0);
|
708 |
|
709 | }
|
710 | } else {
|
711 | console.log("Warning: no valid JSON data found");
|
712 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=No valid JSON data found.");
|
713 | process.exit(0);
|
714 | }
|
715 | } else {
|
716 | console.log("Warning: no installer script was found");
|
717 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=No installer script was found.");
|
718 | process.exit(0);
|
719 | }
|
720 |
|
721 |
|
722 | }
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 | function renameFolder(filename, dirname, opts) {
|
729 |
|
730 |
|
731 |
|
732 |
|
733 | var nozipFilename = filename.replace(/.zip/i, "");
|
734 |
|
735 | var dirIn = normalizeInclWinNetworks(targetAddonsFolder + tempDir);
|
736 | if(dirname) {
|
737 | dirIn = normalizeInclWinNetworks(targetAddonsFolder + tempDir + "/" + noTrailSlash(dirname));
|
738 | }
|
739 |
|
740 |
|
741 | var desc = normalizeInclWinNetworks(dirIn + "/" + descriptorFile);
|
742 | console.log("Checking for file:" + desc);
|
743 | var dirOut = "";
|
744 | if(fs.existsSync(desc) == true) {
|
745 | try {
|
746 | var data = fsExtra.readJsonSync(desc);
|
747 | if(data) {
|
748 | dirOut = normalizeInclWinNetworks(targetAddonsFolder + data.name);
|
749 | }
|
750 | } catch(err) {
|
751 | console.log("Error: there was a problem in the medimage-installer .json file.");
|
752 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=There was a problem in the medimage-installer .json file.&EXTENDED=" + err);
|
753 | process.exit(0);
|
754 |
|
755 | }
|
756 | }
|
757 |
|
758 | if(dirOut == "") {
|
759 | console.log("Using filename to determine directory...");
|
760 |
|
761 |
|
762 | var dirOut = filename.replace(/[0-9\.]+/g, "");
|
763 | dirOut = dirOut.replace(/zip/i, "");
|
764 | dirOut = targetAddonsFolder + dirOut.replace(/-$/, '');
|
765 | }
|
766 |
|
767 | dirIn = normalizeInclWinNetworks(dirIn);
|
768 | dirOut = normalizeInclWinNetworks(dirOut);
|
769 | console.log("Dir in=" + dirIn + "\nDir out=" + dirOut);
|
770 |
|
771 |
|
772 | fsExtra.move(dirIn, dirOut, { overwrite: true }, function(err) {
|
773 | if (err) {
|
774 | return console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=Could not rename the folder.&EXTENDED=" + err);
|
775 | } else {
|
776 | console.log('Success renaming!');
|
777 | openAndRunDescriptor(dirOut, opts);
|
778 | return;
|
779 | }
|
780 | });
|
781 |
|
782 | }
|
783 |
|
784 | function uninstall(addonName, opts)
|
785 | {
|
786 | var dirOut = normalizeInclWinNetworks(targetAddonsFolder + addonName);
|
787 |
|
788 |
|
789 | try {
|
790 | process.chdir(dirOut);
|
791 | } catch(err) {
|
792 | return console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=This is not a directory.&EXTENDED=" + err);
|
793 | process.exit(0);
|
794 | }
|
795 |
|
796 |
|
797 |
|
798 | var desc = dirOut + "/" + descriptorFile;
|
799 | console.log("Checking for file:" + desc);
|
800 |
|
801 |
|
802 |
|
803 | if(fs.existsSync(desc) == true) {
|
804 | try {
|
805 | var data = fsExtra.readJsonSync(desc);
|
806 | } catch(err) {
|
807 | console.log("Error: there was a problem in the medimage-installer .json file. " + err);
|
808 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=There was a problem in the medimage-installer .json file.&EXTENDED=" + err);
|
809 | process.exit(0);
|
810 |
|
811 | }
|
812 |
|
813 |
|
814 | if(data) {
|
815 |
|
816 | if(data.uninstallCommands) {
|
817 |
|
818 |
|
819 |
|
820 | var platform = getPlatform();
|
821 | async.waterfall([
|
822 | function(callback) {
|
823 | console.log("Checking all platform commands");
|
824 | if(data.uninstallCommands.all) {
|
825 | var prepend = "";
|
826 | if((platform == "unix")||(platform == "mac")) {
|
827 | if(opts.password != "") {
|
828 | prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
829 | }
|
830 | }
|
831 |
|
832 | execCommands(data.uninstallCommands.all, prepend, function(err) {
|
833 | callback(err);
|
834 | });
|
835 | } else {
|
836 | callback(null);
|
837 | }
|
838 | },
|
839 | function(callback) {
|
840 | console.log("Checking Win32 commands");
|
841 | if((data.uninstallCommands.win32) && (platform == "win32")) {
|
842 |
|
843 | execCommands(data.uninstallCommands.win32, "", function(err) {
|
844 | callback(err);
|
845 | });
|
846 | } else {
|
847 | callback(null);
|
848 | }
|
849 | },
|
850 | function(callback) {
|
851 | console.log("Checking Win64 commands");
|
852 | if((data.uninstallCommands.win64) && (platform == "win64")) {
|
853 |
|
854 | execCommands(data.uninstallCommands.win64, "", function(err) {
|
855 | callback(err);
|
856 | });
|
857 | } else {
|
858 | callback(null);
|
859 | }
|
860 | },
|
861 | function(callback) {
|
862 | console.log("Checking Unix commands");
|
863 | if((data.uninstallCommands.unix) && (platform == "unix")) {
|
864 |
|
865 | if(opts.password != "") {
|
866 | var prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
867 | }
|
868 | execCommands(data.uninstallCommands.unix, prepend, function(err) {
|
869 | callback(err);
|
870 | });
|
871 | } else {
|
872 | callback(null);
|
873 | }
|
874 | },
|
875 | function(callback) {
|
876 | console.log("Checking Mac commands");
|
877 | if((data.uninstallCommands.mac) && (platform == "mac")) {
|
878 |
|
879 | if(opts.password != "") {
|
880 | var prepend = "echo \"" + opts.password + "\" | sudo -S ";
|
881 | }
|
882 | execCommands(data.uninstallCommands.mac, prepend, function(err) {
|
883 | callback(err, 'done');
|
884 | });
|
885 | } else {
|
886 | callback(null);
|
887 | }
|
888 | }
|
889 | ],
|
890 | function (err, success) {
|
891 |
|
892 | if(err) {
|
893 | console.log("The uninstallation was not complete.");
|
894 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=The uninstallation was not complete.&EXTENDED=" + err);
|
895 | process.exit(0);
|
896 | } else {
|
897 |
|
898 |
|
899 | process.chdir(targetAddonsFolder);
|
900 |
|
901 |
|
902 | console.log("Removing folder:" + dirOut);
|
903 | if(((platform == "unix")||(platform == "mac"))&&(opts.password != "")) {
|
904 | var rmrf = [ "echo \"" + opts.password + "\" | sudo -S rm -rf " + dirOut ];
|
905 | execCommands(rmrf, "", function(err) {
|
906 | if(err) {
|
907 | console.log("The uninstallation was not complete.");
|
908 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=The uninstallation was not complete.&EXTENDED=" + err);
|
909 | } else {
|
910 | console.log("The uninstallation was completed successfully!");
|
911 | console.log("reloadConfig:true");
|
912 | console.log("returnParams:?FINISHED=true&TABSTART=install-addon-tab&MSG=The uninstallation was completed successfully!");
|
913 | }
|
914 | process.exit(0);
|
915 | });
|
916 | } else {
|
917 |
|
918 | fsExtra.remove(dirOut, function(err) {
|
919 | if(err) {
|
920 | console.log("The uninstallation was not complete.");
|
921 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=The uninstallation was not complete.&EXTENDED=" + err);
|
922 |
|
923 | } else {
|
924 | console.log("The uninstallation was completed successfully!");
|
925 | console.log("reloadConfig:true");
|
926 | console.log("returnParams:?FINISHED=true&TABSTART=install-addon-tab&MSG=The uninstallation was completed successfully!");
|
927 |
|
928 |
|
929 | }
|
930 | process.exit(0);
|
931 |
|
932 | });
|
933 |
|
934 |
|
935 |
|
936 | }
|
937 | }
|
938 | });
|
939 |
|
940 | }
|
941 | } else {
|
942 | console.log("Warning: no valid JSON data found");
|
943 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=Warning: no valid JSON datafound");
|
944 | process.exit(0);
|
945 | }
|
946 | } else {
|
947 | console.log("Warning: no valid JSON data found");
|
948 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=Warning: no valid JSON data found");
|
949 | process.exit(0);
|
950 | }
|
951 |
|
952 |
|
953 |
|
954 |
|
955 | }
|
956 |
|
957 |
|
958 | function removeOldTemp(opts, cb)
|
959 | {
|
960 |
|
961 |
|
962 | var platform = getPlatform();
|
963 |
|
964 | process.chdir(targetAddonsFolder);
|
965 | var dirOut = targetAddonsFolder + tempDir;
|
966 |
|
967 | if(((platform == "unix")||(platform == "mac"))&&(opts.password != "")) {
|
968 | var rmrf = [ "echo \"" + opts.password + "\" | sudo -S rm -rf " + dirOut ];
|
969 | execCommands(rmrf, "", function(err) {
|
970 | if(err) {
|
971 | console.log("The installation was not complete.");
|
972 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=The installation was not complete. The old temporary folder could not be removed.&EXTENDED=" + err);
|
973 | process.exit(0);
|
974 | } else {
|
975 | cb(null);
|
976 | }
|
977 | });
|
978 | } else {
|
979 |
|
980 | fsExtra.remove(dirOut, function(err) {
|
981 | if(err) {
|
982 | console.log("The installation was not complete.");
|
983 | console.log("returnParams:?FINISHED=false&TABSTART=install-addon-tab&MSG=The installation was not complete. The old temporary folder could not be removed.&EXTENDED=" + err);
|
984 | process.exit(0);
|
985 |
|
986 | } else {
|
987 | cb(null);
|
988 | }
|
989 | });
|
990 |
|
991 | }
|
992 |
|
993 | }
|
994 |
|
995 |
|
996 |
|
997 |
|
998 | havePermission(configFile, function(err, ret) {
|
999 |
|
1000 | if(err) {
|
1001 | console.log("returnParams:?FINISHED=false&MSG=Sorry you do not have permissions to install add-ons. Please contact your administrator.");
|
1002 |
|
1003 | } else {
|
1004 |
|
1005 | if(ret == true) {
|
1006 |
|
1007 | if(process.argv[2]) {
|
1008 |
|
1009 | var opts = queryString.parse(decodeURIComponent(process.argv[2]));
|
1010 |
|
1011 | if((opts.zipfileURL)&&(opts.zipfileURL != "")) {
|
1012 |
|
1013 | var zipfileURL = opts.zipfileURL;
|
1014 |
|
1015 |
|
1016 |
|
1017 | var parsed = url.parse(zipfileURL);
|
1018 | var filename = path.basename(parsed.pathname);
|
1019 |
|
1020 | console.log("Filename: " + filename + " URL: " + zipfileURL);
|
1021 |
|
1022 |
|
1023 | try {
|
1024 | removeOldTemp(opts, function() {
|
1025 |
|
1026 | downloadAndUnzip(filename, zipfileURL, opts, function(err, dirname) {
|
1027 | console.log("Files unzipped");
|
1028 |
|
1029 | renameFolder(filename, dirname, opts);
|
1030 |
|
1031 | });
|
1032 | });
|
1033 | } catch(err) {
|
1034 | console.log("returnParams:?FINISHED=false&MSG=The installation was not complete.&EXTENDED=" + err);
|
1035 | process.exit(0);
|
1036 |
|
1037 | }
|
1038 | }
|
1039 |
|
1040 | if(opts.uninstall) {
|
1041 | try {
|
1042 | uninstall(opts.uninstall, opts);
|
1043 | } catch(err) {
|
1044 | console.log("returnParams:?FINISHED=false&MSG=The installation was not complete.&EXTENDED=" + err);
|
1045 | process.exit(0);
|
1046 |
|
1047 | }
|
1048 | }
|
1049 |
|
1050 | if((!opts.uninstall)&&(!opts.zipfileURL)) {
|
1051 | console.log("You should enter a 'zipfileURL' or 'uninstall' param.");
|
1052 | console.log("returnParams:?FINISHED=false&MSG=Sorry, we don't seem to have an Add-on requested.");
|
1053 | process.exit(0);
|
1054 | }
|
1055 |
|
1056 |
|
1057 |
|
1058 | } else {
|
1059 | console.log("Usage: node install-addon.js zipfileURL%3Dhttp://url.To.Zip/file/name.zip (with urlencoded URL)");
|
1060 |
|
1061 | }
|
1062 | } else {
|
1063 |
|
1064 | console.log("returnParams:?FINISHED=false&MSG=Sorry, you don't have permission to install or uninstall add-ons. Please check your config.json.");
|
1065 | }
|
1066 | }
|
1067 | });
|