Windows service installation improvements.

Ylian Saint-Hilaire committed Nov 28, 2020 at 12:51 UTC 593fd5cc8028d49e25c99d95270980e6bc15c60f
1 file changed +28 -7
meshcentral.js
+28 -7
@@ -339,11 +339,23 @@ function CreateMeshCentralServer(config, args) {
339
340 // Windows background service handling
341 if ((obj.platform == 'win32') && (obj.service != null)) {
342 + // Build MeshCentral parent path and Windows Service path
343 + var mcpath = __dirname;
344 + if (mcpath.endsWith('\\node_modules\\meshcentral') || mcpath.endsWith('/node_modules/meshcentral')) { mcpath = require('path').join(mcpath, '..', '..'); }
345 + var servicepath = obj.path.join(mcpath, 'WinService');
346 +
347 // Check if we need to install, start, stop, remove ourself as a background service
348 if (((obj.args.xinstall == true) || (obj.args.xuninstall == true) || (obj.args.start == true) || (obj.args.stop == true) || (obj.args.restart == true))) {
349 var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'rediraliasport', 'debug'];
350 for (i in xenv) { if (obj.args[xenv[i]] != null) { env.push({ name: 'mesh' + xenv[i], value: obj.args[xenv[i]] }); } } // Set some args as service environement variables.
346 - var svc = new obj.service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: obj.path.join(__dirname, 'winservice.js'), env: env, wait: 2, grow: 0.5 });
351 +
352 + var serviceFilePath = null;
353 + if (obj.fs.existsSync(obj.path.join(servicepath, 'winservice.js'))) { serviceFilePath = obj.path.join(servicepath, 'winservice.js'); }
354 + else if (obj.fs.existsSync(obj.path.join(__dirname, '../WinService/winservice.js'))) { serviceFilePath = obj.path.join(__dirname, '../WinService/winservice.js'); }
355 + else if (obj.fs.existsSync(obj.path.join(__dirname, 'winservice.js'))) { serviceFilePath = obj.path.join(__dirname, 'winservice.js'); }
356 + if (serviceFilePath == null) { console.log('Unable to find winservice.js'); return; }
357 +
358 + var svc = new obj.service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: servicepath, env: env, wait: 2, grow: 0.5 });
359 svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); });
360 svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); });
361 svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); });
@@ -353,7 +365,7 @@ function CreateMeshCentralServer(config, args) {
365
366 if (obj.args.xinstall == true) { try { svc.install(); } catch (e) { logException(e); } }
367 if (obj.args.stop == true || obj.args.restart == true) { try { svc.stop(); } catch (e) { logException(e); } }
356 - if (obj.args.start == true || obj.args.restart == true) { try { svc.start(); } catch (e) { logException(e); } }
368 + if (obj.args.start == true) { try { svc.start(); } catch (e) { logException(e); } }
369 if (obj.args.xuninstall == true) { try { svc.uninstall(); } catch (e) { logException(e); } }
370 return;
371 }
@@ -361,16 +373,23 @@ function CreateMeshCentralServer(config, args) {
373 // Windows service install using the external winservice.js
374 if (obj.args.install == true) {
375 console.log('Installing MeshCentral as Windows Service...');
364 - if (obj.fs.existsSync(obj.path.join(__dirname, '../WinService')) == false) { try { obj.fs.mkdirSync(obj.path.join(__dirname, '../WinService')); } catch (ex) { console.log('ERROR: Unable to create WinService folder: ' + ex); process.exit(); return; } }
365 - try { obj.fs.createReadStream(obj.path.join(__dirname, 'winservice.js')).pipe(obj.fs.createWriteStream(obj.path.join(__dirname, '../WinService/winservice.js'))); } catch (ex) { console.log('ERROR: Unable to copy winservice.js: ' + ex); process.exit(); return; }
366 - require('child_process').exec('node winservice.js --install', { maxBuffer: 512000, timeout: 120000, cwd: obj.path.join(__dirname, '../WinService') }, function (error, stdout, stderr) {
376 + if (obj.fs.existsSync(servicepath) == false) { try { obj.fs.mkdirSync(servicepath); } catch (ex) { console.log('ERROR: Unable to create WinService folder: ' + ex); process.exit(); return; } }
377 + try { obj.fs.createReadStream(obj.path.join(__dirname, 'winservice.js')).pipe(obj.fs.createWriteStream(obj.path.join(servicepath, 'winservice.js'))); } catch (ex) { console.log('ERROR: Unable to copy winservice.js: ' + ex); process.exit(); return; }
378 + require('child_process').exec('node winservice.js --install', { maxBuffer: 512000, timeout: 120000, cwd: servicepath }, function (error, stdout, stderr) {
379 if ((error != null) && (error != '')) { console.log('ERROR: Unable to install MeshCentral as a service: ' + error); process.exit(); return; }
380 console.log(stdout);
381 });
382 return;
383 } else if (obj.args.uninstall == true) {
384 console.log('Uninstalling MeshCentral Windows Service...');
373 - if (obj.fs.existsSync(obj.path.join(__dirname, '../WinService')) == true) {
385 + if (obj.fs.existsSync(servicepath) == true) {
386 + require('child_process').exec('node winservice.js --uninstall', { maxBuffer: 512000, timeout: 120000, cwd: servicepath }, function (error, stdout, stderr) {
387 + if ((error != null) && (error != '')) { console.log('ERROR: Unable to uninstall MeshCentral service: ' + error); process.exit(); return; }
388 + console.log(stdout);
389 + try { obj.fs.unlinkSync(obj.path.join(servicepath, 'winservice.js')); } catch (ex) { }
390 + try { obj.fs.rmdirSync(servicepath); } catch (ex) { }
391 + });
392 + } else if (obj.fs.existsSync(obj.path.join(__dirname, '../WinService')) == true) {
393 require('child_process').exec('node winservice.js --uninstall', { maxBuffer: 512000, timeout: 120000, cwd: obj.path.join(__dirname, '../WinService') }, function (error, stdout, stderr) {
394 if ((error != null) && (error != '')) { console.log('ERROR: Unable to uninstall MeshCentral service: ' + error); process.exit(); return; }
395 console.log(stdout);
@@ -2724,7 +2743,9 @@ function InstallModule(modulename, func, tag1, tag2) {
2743
2744 child_process.exec(npmpath + ` install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
2745 if ((error != null) && (error != '')) {
2727 - console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n');
2746 + var mcpath = __dirname;
2747 + if (mcpath.endsWith('\\node_modules\\meshcentral') || mcpath.endsWith('/node_modules/meshcentral')) { mcpath = require('path').join(mcpath, '..', '..'); }
2748 + console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. To manualy install this module try:\r\n\r\n cd "' + mcpath + '"\r\n npm install ' + modulename + '\r\n node node_modules' + ((require('os').platform() == 'win32') ? '\\' : '/') + 'meshcentral');
2749 process.exit();
2750 return;
2751 }