MeshCentral/agents/meshinstall-linux.js

266 lines
9.5 KiB
JavaScript
Raw Normal View History

/*
Copyright 2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-11-07 00:43:56 +03:00
2020-09-14 23:32:16 +03:00
// The folloing line just below with 'msh=' needs to stay exactly like this since MeshCentral will replace it with the correct settings.
var msh = {};
2020-11-07 00:43:56 +03:00
var displayName = msh.displayName ? msh.displayName : 'MeshCentral Agent';
2020-09-14 23:32:16 +03:00
var s = null, buttons = ['Cancel'], skip = false;
2020-11-07 00:43:56 +03:00
var serviceName = msh.meshServiceName ? msh.meshServiceName : 'meshagent';
try { s = require('service-manager').manager.getService(serviceName); } catch (e) { }
var connectArgs = [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1'];
connectArgs.push('--MeshName="' + msh.MeshName + '"');
connectArgs.push('--MeshType="' + msh.MeshType + '"');
connectArgs.push('--MeshID="' + msh.MeshID + '"');
connectArgs.push('--ServerID="' + msh.ServerID + '"');
connectArgs.push('--MeshServer="' + msh.MeshServer + '"');
connectArgs.push('--AgentCapabilities="0x00000020"');
if (msh.displayName) { connectArgs.push('--displayName="' + msh.displayName + '"'); }
if (msh.agentName) { connectArgs.push('--agentName="' + msh.agentName + '"'); }
2020-11-07 00:43:56 +03:00
function _install(parms)
{
2020-08-03 20:51:54 +03:00
var mstr = require('fs').createWriteStream(process.execPath + '.msh', { flags: 'wb' });
mstr.write('MeshName=' + msh.MeshName + '\n');
mstr.write('MeshType=' + msh.MeshType + '\n');
mstr.write('MeshID=' + msh.MeshID + '\n');
mstr.write('ServerID=' + msh.ServerID + '\n');
mstr.write('MeshServer=' + msh.MeshServer + '\n');
2020-11-07 00:43:56 +03:00
if (msh.agentName) { mstr.write('agentName=' + msh.agentName + '\n'); }
if (msh.meshServiceName) { mstr.write('meshServiceName=' + msh.meshServiceName + '\n'); }
2020-08-03 20:51:54 +03:00
mstr.end();
2020-11-07 00:43:56 +03:00
2020-09-11 01:18:33 +03:00
if (parms == null) { parms = []; }
2020-11-07 00:43:56 +03:00
if (msh.companyName) { parms.unshift('--companyName="' + msh.companyName + '"'); }
if (msh.displayName) { parms.unshift('--displayName="' + msh.displayName + '"'); }
if (msh.meshServiceName) { parms.unshift('--meshServiceName="' + msh.meshServiceName + '"'); }
2020-09-11 01:18:33 +03:00
parms.unshift('--copy-msh=1');
parms.unshift('--no-embedded=1');
parms.unshift('-fullinstall');
parms.unshift(process.execPath.split('/').pop());
2020-11-07 00:43:56 +03:00
2020-09-11 01:18:33 +03:00
global._child = require('child_process').execFile(process.execPath, parms);
2020-08-03 20:51:54 +03:00
global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
global._child.waitExit();
}
2020-11-07 00:43:56 +03:00
function _uninstall()
{
2020-09-11 01:18:33 +03:00
global._child = require('child_process').execFile(process.execPath,
2020-11-07 00:43:56 +03:00
[process.execPath.split('/').pop(), '-fulluninstall', '--no-embedded=1', '--meshServiceName="' + serviceName + '"']);
2020-08-03 20:51:54 +03:00
global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
global._child.waitExit();
}
2020-11-07 00:43:56 +03:00
if (msh.InstallFlags == null)
{
2020-08-03 20:51:54 +03:00
msh.InstallFlags = 3;
2020-11-07 00:43:56 +03:00
} else
{
2020-08-03 20:51:54 +03:00
msh.InstallFlags = parseInt(msh.InstallFlags.toString());
}
2020-11-07 00:43:56 +03:00
if (process.argv.includes('-mesh'))
{
console.log(JSON.stringify(msh, null, 2));
process.exit();
}
2020-11-07 00:43:56 +03:00
if ((msh.InstallFlags & 1) == 1)
{
buttons.unshift('Connect');
2020-11-07 00:43:56 +03:00
if (process.argv.includes('-connect'))
{
global._child = require('child_process').execFile(process.execPath, connectArgs);
global._child.stdout.on('data', function (c) { });
global._child.stderr.on('data', function (c) { });
global._child.on('exit', function (code) { process.exit(code); });
console.log("\nConnecting to: " + msh.MeshServer);
console.log("Device Group: " + msh.MeshName);
console.log('\nPress Ctrl-C to exit\n');
skip = true;
}
}
2020-11-07 00:43:56 +03:00
if ((!skip) && ((msh.InstallFlags & 2) == 2))
{
if (!require('user-sessions').isRoot())
{
console.log('\n' + "Elevated permissions is required to install/uninstall the agent.");
console.log("Please try again with sudo.");
process.exit();
}
2020-11-07 00:43:56 +03:00
if (s)
{
if ((process.platform == 'darwin') || require('message-box').kdialog)
{
2020-08-03 20:51:54 +03:00
buttons.unshift("Setup");
2020-11-07 00:43:56 +03:00
} else
{
2020-08-03 20:51:54 +03:00
buttons.unshift("Uninstall");
buttons.unshift("Update");
}
2020-11-07 00:43:56 +03:00
} else
{
2020-08-03 20:51:54 +03:00
buttons.unshift("Install");
}
}
2020-11-07 00:43:56 +03:00
if (!skip)
{
if (process.platform != 'darwin')
{
if (process.argv.includes('-install') || process.argv.includes('-update'))
{
2020-09-11 01:18:33 +03:00
var p = [];
2020-11-07 00:43:56 +03:00
for (var i = 0; i < process.argv.length; ++i)
{
if (process.argv[i].startsWith('--installPath='))
{
2020-09-11 01:18:33 +03:00
p.push('--installPath="' + process.argv[i].split('=').pop() + '"');
}
}
_install(p);
process.exit();
}
2020-11-07 00:43:56 +03:00
else if (process.argv.includes('-uninstall'))
{
2020-09-11 01:18:33 +03:00
_uninstall();
process.exit();
}
2020-11-07 00:43:56 +03:00
else
{
if (!require('message-box').kdialog && ((require('message-box').zenity == null) || (!require('message-box').zenity.extra)))
{
console.log('\n' + "The graphical version of this installer cannot run on this system.");
console.log("Try installing/updating Zenity, and run again." + '\n');
console.log("You can also run the text version from the command line with the following command(s): ");
2020-11-07 00:43:56 +03:00
if ((msh.InstallFlags & 1) == 1)
{
console.log('./' + process.execPath.split('/').pop() + ' -connect');
2020-09-11 01:18:33 +03:00
}
2020-11-07 00:43:56 +03:00
if ((msh.InstallFlags & 2) == 2)
{
if (s)
{
console.log('./' + process.execPath.split('/').pop() + ' -update');
console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
}
2020-11-07 00:43:56 +03:00
else
{
console.log('./' + process.execPath.split('/').pop() + ' -install');
console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
}
2020-09-11 01:18:33 +03:00
}
console.log('');
process.exit();
2020-09-11 01:18:33 +03:00
}
}
2020-08-03 20:51:54 +03:00
}
2020-11-07 00:43:56 +03:00
else
{
if (!require('user-sessions').isRoot()) { console.log('\n' + "This utility requires elevated permissions. Please try again with sudo."); process.exit(); }
}
2020-08-03 20:58:44 +03:00
}
2020-08-03 20:51:54 +03:00
2020-11-07 00:43:56 +03:00
if (!skip)
{
if (!s)
{
2020-09-11 01:18:33 +03:00
msg = "Agent: " + "NOT INSTALLED" + '\n';
2020-11-07 00:43:56 +03:00
} else
{
2020-09-11 01:18:33 +03:00
msg = "Agent: " + (s.isRunning() ? "RUNNING" : "NOT RUNNING") + '\n';
}
2020-11-07 00:43:56 +03:00
2020-09-11 01:18:33 +03:00
msg += ("Device Group: " + msh.MeshName + '\n');
msg += ("Server URL: " + msh.MeshServer + '\n');
2020-11-07 00:43:56 +03:00
var p = require('message-box').create(displayName + " Setup", msg, 99999, buttons);
p.then(function (v)
{
switch (v)
{
2020-09-11 01:18:33 +03:00
case "Cancel":
2020-08-03 20:51:54 +03:00
process.exit();
2020-09-11 01:18:33 +03:00
break;
case 'Setup':
2020-11-07 00:43:56 +03:00
var d = require('message-box').create(displayName, msg, 99999, ['Update', 'Uninstall', 'Cancel']);
d.then(function (v)
{
switch (v)
{
2020-09-11 01:18:33 +03:00
case 'Update':
case 'Install':
_install();
break;
case 'Uninstall':
_uninstall();
break;
default:
break;
}
process.exit();
}).catch(function (v) { process.exit(); });
break;
case "Connect":
2020-11-07 00:43:56 +03:00
global._child = require('child_process').execFile(process.execPath, connectArgs);
2020-09-11 01:18:33 +03:00
global._child.stdout.on('data', function (c) { });
global._child.stderr.on('data', function (c) { });
global._child.on('exit', function (code) { process.exit(code); });
2020-11-07 00:43:56 +03:00
2020-09-11 01:18:33 +03:00
msg = ("Device Group: " + msh.MeshName + '\n');
msg += ("Server URL: " + msh.MeshServer + '\n');
2020-11-07 00:43:56 +03:00
if (process.platform != 'darwin')
{
if (!require('message-box').zenity && require('message-box').kdialog)
{
2020-09-11 01:18:33 +03:00
msg += ('\nPress OK to Disconnect');
}
2020-08-03 20:51:54 +03:00
}
2020-11-07 00:43:56 +03:00
var d = require('message-box').create(displayName, msg, 99999, ['Disconnect']);
2020-09-11 01:18:33 +03:00
d.then(function (v) { process.exit(); }).catch(function (v) { process.exit(); });
break;
case "Uninstall":
_uninstall();
process.exit();
break;
case "Install":
case "Update":
_install();
process.exit();
break;
default:
console.log(v);
process.exit();
break;
}
2020-11-07 00:43:56 +03:00
}).catch(function (e)
{
2020-09-11 01:18:33 +03:00
console.log(e);
process.exit();
});
}