Server self-update correctly not allowed when running as Windows Service.

Ylian Saint-Hilaire committed Jul 18, 2019 at 13:11 UTC c2844f65d17cd933f23c40980c9adb88ad99c543
3 files changed +18 -42
meshcentral.js
+16 -40
@@ -136,23 +136,7 @@ function CreateMeshCentralServer(config, args) {
136 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 });
137 svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); });
138 svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); });
139 - svc.on('start', function () {
140 - console.log('MeshCentral service started.');
141 - // Attempt to copy the demon folder to "meshcentral-data\win-deamon".
142 - try {
143 - var sourceDaemonFolder = obj.path.join(__dirname, 'daemon');
144 - var targetDaemonFolder = obj.path.join(obj.datapath, 'win-deamon');
145 - if (!obj.fs.existsSync(targetDaemonFolder)) { obj.fs.mkdirSync(targetDaemonFolder); }
146 - var files = obj.fs.readdirSync(sourceDaemonFolder);
147 - for (var i in files) {
148 - if (files[i].endsWith('.log') == false) {
149 - var bin = obj.fs.readFileSync(obj.path.join(sourceDaemonFolder, files[i]), 'binary');
150 - obj.fs.writeFileSync(obj.path.join(targetDaemonFolder, files[i]), bin, 'binary');
151 - }
152 - }
153 - } catch (e) { console.error(e); }
154 - process.exit();
155 - });
139 + svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); });
140 svc.on('stop', function () { console.log('MeshCentral service stopped.'); if (obj.args.stop) { process.exit(); } if (obj.args.restart) { console.log('Holding 5 seconds...'); setTimeout(function () { svc.start(); }, 5000); } });
141 svc.on('alreadyinstalled', function () { console.log('MeshCentral service already installed.'); process.exit(); });
142 svc.on('invalidinstallation', function () { console.log('Invalid MeshCentral service installation.'); process.exit(); });
@@ -192,33 +176,19 @@ function CreateMeshCentralServer(config, args) {
176 process.exit(); // User CTRL-C exit.
177 } else if (childProcess.xrestart == 3) {
178 // Server self-update exit
195 - var version = '';
196 - if (typeof obj.args.selfupdate == 'string') { version = '@' + obj.args.selfupdate; }
197 - var child_process = require('child_process');
179 + var updateArgs = [];
180 var npmpath = ((typeof obj.args.npmpath == 'string') ? obj.args.npmpath : 'npm');
199 - var npmproxy = ((typeof obj.args.npmproxy == 'string') ? (' --proxy ' + obj.args.npmproxy) : '');
200 - var xxprocess = child_process.exec(npmpath + npmproxy + ' install meshcentral' + version, { maxBuffer: Infinity, cwd: obj.parentpath }, function (error, stdout, stderr) { });
181 + if (typeof obj.args.npmproxy == 'string') { updateArgs.push('--proxy', obj.args.npmproxy); }
182 + updateArgs.push('install', 'meshcentral');
183 + if (typeof obj.args.selfupdate == 'string') { updateArgs.push('@' + obj.args.selfupdate); }
184 +
185 + var child_process = require('child_process');
186 + var xxprocess = child_process.execFile(npmpath, updateArgs, { maxBuffer: Infinity, cwd: obj.parentpath }, function (error, stdout, stderr) { });
187 xxprocess.data = '';
188 xxprocess.stdout.on('data', function (data) { xxprocess.data += data; });
189 xxprocess.stderr.on('data', function (data) { xxprocess.data += data; });
190 xxprocess.on('close', function (code) {
191 console.log('Update completed...');
206 - if (require('os').platform() == 'win32') {
207 - // On Windows, attempt to copy "meshcentral-data\win-deamon" folder back into "deamon".
208 - // This folder will have been removed after update and needs to be added back.
209 - try {
210 - var sourceDaemonFolder = obj.path.join(obj.datapath, 'win-deamon');
211 - var targetDaemonFolder = obj.path.join(__dirname, 'daemon');
212 - if (!obj.fs.existsSync(targetDaemonFolder)) { obj.fs.mkdirSync(targetDaemonFolder); }
213 - var files = obj.fs.readdirSync(sourceDaemonFolder);
214 - for (var i in files) {
215 - if (files[i].endsWith('.log') == false) {
216 - var bin = obj.fs.readFileSync(obj.path.join(sourceDaemonFolder, files[i]), 'binary');
217 - obj.fs.writeFileSync(obj.path.join(targetDaemonFolder, files[i]), bin, 'binary');
218 - }
219 - }
220 - } catch (e) { console.error(e); }
221 - }
192 setTimeout(function () { obj.launchChildServer(startArgs); }, 1000);
193 });
194 } else {
@@ -264,7 +234,10 @@ function CreateMeshCentralServer(config, args) {
234 };
235
236 // Initiate server self-update
267 - obj.performServerUpdate = function () { console.log('Starting self upgrade...'); process.exit(200); };
237 + obj.performServerUpdate = function () {
238 + if (obj.serverSelfWriteAllowed == true) { console.log('Starting self upgrade...'); process.exit(200); return true; }
239 + return false;
240 + };
241
242 // Initiate server self-update
243 obj.performServerCertUpdate = function () { console.log('Updating server certificates...'); process.exit(200); };
@@ -557,8 +530,11 @@ function CreateMeshCentralServer(config, args) {
530 obj.StartEx1b = function () {
531 var i;
532
533 + // Check if self update is allowed. If running as a Windows service, self-update is not possible.
534 + if (obj.fs.existsSync(obj.path.join(__dirname, 'daemon'))) { obj.serverSelfWriteAllowed = false; }
535 +
536 // If we are targetting a specific version, update now.
561 - if (typeof obj.args.selfupdate == 'string') {
537 + if ((obj.serverSelfWriteAllowed == true) && (typeof obj.args.selfupdate == 'string')) {
538 obj.args.selfupdate = obj.args.selfupdate.toLowerCase();
539 if (obj.currentVer !== obj.args.selfupdate) { obj.performServerUpdate(); return; } // We are targetting a specific version, run self update now.
540 }
meshuser.js
+1 -1
@@ -611,7 +611,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
611 }
612 case 'serverupdate': {
613 r = 'Performing server update...';
614 - parent.parent.performServerUpdate();
614 + if (parent.parent.performServerUpdate() == false) { r = 'Server self-update not possible.'; }
615 break;
616 }
617 case 'print': {
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.3.8-a",
3 + "version": "0.3.8-b",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",