add freebsd install/uinstall/start/stop/restart commands #6040
Signed-off-by: si458 <simonsmith5521@gmail.com>
si458 committed
Sep 19, 2024 at 22:33 UTC
a1899a719f4eb2dfaef856175dd8d985235f7ab6
1 file changed
+86
meshcentral.js
+86
@@ -394,6 +394,92 @@ function CreateMeshCentralServer(config, args) {
394
return;
395
}
396
}
397
+
398
+ // FreeBSD background service handling, MUST USE SPAWN FOR SERVICE COMMANDS!
399
+ if (obj.platform == 'freebsd') {
400
+ if (obj.args.install == true) {
401
+ // Install MeshCentral in rc.d
402
+ console.log('Installing MeshCentral as background Service...');
403
+ var systemdConf = "/usr/local/etc/rc.d/meshcentral";
404
+ const userinfo = require('os').userInfo();
405
+ console.log('Writing config file...');
406
+ require('child_process').exec('which node', {}, function (error, stdout, stderr) {
407
+ if ((error != null) || (stdout.indexOf('\n') == -1)) { console.log('ERROR: Unable to get node location: ' + error); process.exit(); return; }
408
+ const nodePath = stdout.substring(0, stdout.indexOf('\n'));
409
+ const config = '#!/bin/sh\n# MeshCentral FreeBSD Service Script\n# PROVIDE: meshcentral\n# REQUIRE: NETWORKING\n# KEYWORD: shutdown\n. /etc/rc.subr\nname=meshcentral\nuser=' + userinfo.username + '\nrcvar=meshcentral_enable\n: \\${meshcentral_enable:=\\"NO\\"}\n: \\${meshcentral_args:=\\"\\"}\npidfile=/var/run/meshcentral/meshcentral.pid\ncommand=\\"/usr/sbin/daemon\\"\nmeshcentral_chdir=\\"' + obj.parentpath + '\\"\ncommand_args=\\"-r -u \\${user} -P \\${pidfile} -S -T meshcentral -m 3 ' + nodePath + ' ' + __dirname + ' \\${meshcentral_args}\\"\nload_rc_config \\$name\nrun_rc_command \\"\\$1\\"\n';
410
+ require('child_process').exec('echo \"' + config + '\" | tee ' + systemdConf + ' && chmod +x ' + systemdConf, {}, function (error, stdout, stderr) {
411
+ if ((error != null) && (error != '')) { console.log('ERROR: Unable to write config file: ' + error); process.exit(); return; }
412
+ console.log('Enabling service...');
413
+ require('child_process').exec('sysrc meshcentral_enable="YES"', {}, function (error, stdout, stderr) {
414
+ if ((error != null) && (error != '')) { console.log('ERROR: Unable to enable MeshCentral as a service: ' + error); process.exit(); return; }
415
+ if (stdout.length > 0) { console.log(stdout); }
416
+ console.log('Starting service...');
417
+ const service = require('child_process').spawn('service', ['meshcentral', 'start']);
418
+ service.stdout.on('data', function (data) { console.log(data.toString()); });
419
+ service.stderr.on('data', function (data) { console.log(data.toString()); });
420
+ service.on('exit', function (code) {
421
+ console.log((code === 0) ? 'Done.' : 'ERROR: Unable to start MeshCentral as a service');
422
+ process.exit(); // Must exit otherwise we just hang
423
+ });
424
+ });
425
+ });
426
+ });
427
+ return;
428
+ } else if (obj.args.uninstall == true) {
429
+ // Uninstall MeshCentral in rc.d
430
+ console.log('Uninstalling MeshCentral background service...');
431
+ var systemdConf = "/usr/local/etc/rc.d/meshcentral";
432
+ console.log('Stopping service...');
433
+ const service = require('child_process').spawn('service', ['meshcentral', 'stop']);
434
+ service.stdout.on('data', function (data) { console.log(data.toString()); });
435
+ service.stderr.on('data', function (data) { console.log(data.toString()); });
436
+ service.on('exit', function (code) {
437
+ if (code !== 0) { console.log('ERROR: Unable to stop MeshCentral as a service'); }
438
+ console.log('Disabling service...');
439
+ require('child_process').exec('sysrc -x meshcentral_enable', {}, function (err, stdout, stderr) {
440
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to disable MeshCentral as a service: ' + err); }
441
+ if (stdout.length > 0) { console.log(stdout); }
442
+ console.log('Removing config file...');
443
+ require('child_process').exec('rm ' + systemdConf, {}, function (err, stdout, stderr) {
444
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to delete MeshCentral config file: ' + err); }
445
+ console.log('Done.');
446
+ process.exit(); // Must exit otherwise we just hang
447
+ });
448
+ });
449
+ });
450
+ return;
451
+ } else if (obj.args.start == true) {
452
+ // Start MeshCentral in rc.d
453
+ const service = require('child_process').spawn('service', ['meshcentral', 'start']);
454
+ service.stdout.on('data', function (data) { console.log(data.toString()); });
455
+ service.stderr.on('data', function (data) { console.log(data.toString()); });
456
+ service.on('exit', function (code) {
457
+ console.log((code === 0) ? 'Done.' : 'ERROR: Unable to start MeshCentral as a service: ' + error);
458
+ process.exit(); // Must exit otherwise we just hang
459
+ });
460
+ return;
461
+ } else if (obj.args.stop == true) {
462
+ // Stop MeshCentral in rc.d
463
+ const service = require('child_process').spawn('service', ['meshcentral', 'stop']);
464
+ service.stdout.on('data', function (data) { console.log(data.toString()); });
465
+ service.stderr.on('data', function (data) { console.log(data.toString()); });
466
+ service.on('exit', function (code) {
467
+ console.log((code === 0) ? 'Done.' : 'ERROR: Unable to stop MeshCentral as a service: ' + error);
468
+ process.exit(); // Must exit otherwise we just hang
469
+ });
470
+ return;
471
+ } else if (obj.args.restart == true) {
472
+ // Restart MeshCentral in rc.d
473
+ const service = require('child_process').spawn('service', ['meshcentral', 'restart']);
474
+ service.stdout.on('data', function (data) { console.log(data.toString()); });
475
+ service.stderr.on('data', function (data) { console.log(data.toString()); });
476
+ service.on('exit', function (code) {
477
+ console.log((code === 0) ? 'Done.' : 'ERROR: Unable to restart MeshCentral as a service: ' + error);
478
+ process.exit(); // Must exit otherwise we just hang
479
+ });
480
+ return;
481
+ }
482
+ }
483
484
// Index a recorded file
485
if (obj.args.indexmcrec != null) {