Added Linux --install/--uninstall/--start/--stop for quick Systemd install.
Ylian Saint-Hilaire committed
Jan 9, 2020 at 16:35 UTC
3b9173b9d352435fe9a8528548e6260531c1c619
3 files changed
+91
-9
meshcentral.js
+85
-6
@@ -129,12 +129,14 @@ function CreateMeshCentralServer(config, args) {
129
for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence.
130
131
if ((obj.args.help == true) || (obj.args['?'] == true)) {
132
- console.log('MeshCentral v' + getCurrentVerion() + ', a open source remote computer management web portal.');
132
+ console.log('MeshCentral v' + getCurrentVerion() + ', remote computer management web portal.');
133
+ console.log('This software is open source under Apache 2.0 licence.');
134
console.log('Details at: https://www.meshcommander.com/meshcentral2\r\n');
134
- if (obj.platform == 'win32') {
135
- console.log('Run as a Windows Service');
136
- console.log(' --install/uninstall Install Meshcentral as a background service.');
137
- console.log(' --start/stop/restart Control Meshcentral background service.');
135
+ if ((obj.platform == 'win32') || (obj.platform == 'linux')) {
136
+ console.log('Run as a background service');
137
+ console.log(' --install/uninstall Install MeshCentral as a background service.');
138
+ console.log(' --start/stop/restart Control MeshCentral background service.');
139
+ console.log('');
140
console.log('Run standalone, console application');
141
}
142
console.log(' --notls Use HTTP instead of HTTPS for the main web server.');
@@ -150,7 +152,84 @@ function CreateMeshCentralServer(config, args) {
152
return;
153
}
154
153
- if (obj.service != null) {
155
+ // Linux background service systemd handling
156
+ if (obj.platform == 'linux') {
157
+ if (obj.args.install == true) {
158
+ // Install MeshCentral in Systemd
159
+ console.log('Installing MeshCentral as background Service...');
160
+ var userinfo = require('os').userInfo(), systemdConf = null;
161
+ if (require('fs').existsSync('/etc/systemd/system')) { systemdConf = '/etc/systemd/system/meshcentral.service'; }
162
+ else if (require('fs').existsSync('/lib/systemd/system')) { systemdConf = '/lib/systemd/system/meshcentral.service'; }
163
+ else if (require('fs').existsSync('/usr/lib/systemd/system')) { systemdConf = '/usr/lib/systemd/system/meshcentral.service'; }
164
+ else { console.log('Unable to find systemd configuration folder.'); process.exit(); return; }
165
+ console.log('Writing config file...');
166
+ require('child_process').exec('whereis node', {}, function (error, stdout, stderr) {
167
+ if ((error != null) || (stdout.startsWith('node: ') == false) || (stdout.indexOf('\n') == -1)) { console.log('ERROR: Unable to get node location: ' + error); process.exit(); return; }
168
+ var nodePath = stdout.substring(6, stdout.indexOf('\n'));
169
+ var config = '[Unit]\nDescription=MeshCentral Server\n\n[Service]\nType=simple\nLimitNOFILE=1000000\nExecStart=' + nodePath + ' ' + __dirname + '\nWorkingDirectory=' + userinfo.homedir + '\nEnvironment=NODE_ENV=production\nUser=' + userinfo.username + '\nGroup=' + userinfo.username + '\nRestart=always\n# Restart service after 10 seconds if node service crashes\nRestartSec=10\n# Set port permissions capability\nAmbientCapabilities=cap_net_bind_service\n\n[Install]\nWantedBy=multi-user.target\n';
170
+ require('child_process').exec('echo -e \"' + config.split('\n').join('\\n') + '\" | sudo tee ' + systemdConf, {}, function (error, stdout, stderr) {
171
+ if ((error != null) && (error != '')) { console.log('ERROR: Unable to write config file: ' + error); process.exit(); return; }
172
+ console.log('Enabling service...');
173
+ require('child_process').exec('sudo systemctl enable meshcentral.service', {}, function (error, stdout, stderr) {
174
+ if ((error != null) && (error != '')) { console.log('ERROR: Unable to enable MeshCentral as a service: ' + error); process.exit(); return; }
175
+ if (stdout.length > 0) { console.log(stdout); }
176
+ console.log('Starting service...');
177
+ require('child_process').exec('sudo systemctl start meshcentral.service', {}, function (error, stdout, stderr) {
178
+ if ((error != null) && (error != '')) { console.log('ERROR: Unable to start MeshCentral as a service: ' + error); process.exit(); return; }
179
+ if (stdout.length > 0) { console.log(stdout); }
180
+ console.log('Done.');
181
+ });
182
+ });
183
+ });
184
+ });
185
+ return;
186
+ } else if (obj.args.uninstall == true) {
187
+ // Uninstall MeshCentral in Systemd
188
+ console.log('Uninstalling MeshCentral background service...');
189
+ var systemdConf = null;
190
+ if (require('fs').existsSync('/etc/systemd/system')) { systemdConf = '/etc/systemd/system/meshcentral.service'; }
191
+ else if (require('fs').existsSync('/lib/systemd/system')) { systemdConf = '/lib/systemd/system/meshcentral.service'; }
192
+ else if (require('fs').existsSync('/usr/lib/systemd/system')) { systemdConf = '/usr/lib/systemd/system/meshcentral.service'; }
193
+ else { console.log('Unable to find systemd configuration folder.'); process.exit(); return; }
194
+ console.log('Stopping service...');
195
+ require('child_process').exec('sudo systemctl stop meshcentral.service', {}, function (err, stdout, stderr) {
196
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to stop MeshCentral as a service: ' + err); }
197
+ if (stdout.length > 0) { console.log(stdout); }
198
+ console.log('Disabling service...');
199
+ require('child_process').exec('sudo systemctl disable meshcentral.service', {}, function (err, stdout, stderr) {
200
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to disable MeshCentral as a service: ' + err); }
201
+ if (stdout.length > 0) { console.log(stdout); }
202
+ console.log('Removing config file...');
203
+ require('child_process').exec('sudo rm ' + systemdConf, {}, function (err, stdout, stderr) {
204
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to delete MeshCentral config file: ' + err); }
205
+ console.log('Done.');
206
+ });
207
+ });
208
+ });
209
+ return;
210
+ } else if (obj.args.start == true) {
211
+ // Start MeshCentral in Systemd
212
+ require('child_process').exec('sudo systemctl start meshcentral.service', {}, function (err, stdout, stderr) {
213
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to start MeshCentral: ' + err); process.exit(); return; }
214
+ console.log('Done.');
215
+ });
216
+ } else if (obj.args.stop == true) {
217
+ // Stop MeshCentral in Systemd
218
+ require('child_process').exec('sudo systemctl stop meshcentral.service', {}, function (err, stdout, stderr) {
219
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to stop MeshCentral: ' + err); process.exit(); return; }
220
+ console.log('Done.');
221
+ });
222
+ } else if (obj.args.restart == true) {
223
+ // Restart MeshCentral in Systemd
224
+ require('child_process').exec('sudo systemctl restart meshcentral.service', {}, function (err, stdout, stderr) {
225
+ if ((err != null) && (err != '')) { console.log('ERROR: Unable to restart MeshCentral: ' + err); process.exit(); return; }
226
+ console.log('Done.');
227
+ });
228
+ }
229
+ }
230
+
231
+ // Windows background service handling
232
+ if ((obj.platform == 'win32') && (obj.service != null)) {
233
// Check if we need to install, start, stop, remove ourself as a background service
234
if (((obj.args.xinstall == true) || (obj.args.xuninstall == true) || (obj.args.start == true) || (obj.args.stop == true) || (obj.args.restart == true))) {
235
var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'rediraliasport', 'debug'];
package.json
+1
-1
@@ -1,6 +1,6 @@
1
{
2
"name": "meshcentral",
3
- "version": "0.4.7-d",
3
+ "version": "0.4.7-f",
4
"keywords": [
5
"Remote Management",
6
"Intel AMT",
views/login.handlebars
+5
-2
@@ -321,8 +321,11 @@
321
QV('resetpasswordpanelHint', passRequirements.hint === true);
322
323
// Display the welcome text
324
- if (welcomeText) { QH('welcomeText', welcomeText); }
325
- QH('welcomeText', addTextLink('MeshCentral', Q('welcomeText').innerHTML, 'http://www.meshcommander.com/meshcentral2'));
324
+ if (welcomeText) {
325
+ QH('welcomeText', welcomeText);
326
+ } else {
327
+ QH('welcomeText', addTextLink('MeshCentral', Q('welcomeText').innerHTML, 'http://www.meshcommander.com/meshcentral2'));
328
+ }
329
QV('welcomeText', true);
330
331
window.onresize = center;