Added customization support
Bryan Roe committed
Nov 6, 2020 at 13:43 UTC
e4a0a31f864d77efe8e289b65a421d5d0a854303
1 file changed
+107
-65
agents/meshinstall-linux.js
+107
-65
@@ -14,63 +14,83 @@ See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
+
18
+
19
// The folloing line just below with 'msh=' needs to stay exactly like this since MeshCentral will replace it with the correct settings.
20
var msh = {};
21
+var displayName = msh.displayName ? msh.displayName : 'MeshCentral Agent';
22
var s = null, buttons = ['Cancel'], skip = false;
20
-try { s = require('service-manager').manager.getService('meshagent'); } catch (e) { }
23
+var serviceName = msh.meshServiceName ? msh.meshServiceName : 'meshagent';
24
+
25
+try { s = require('service-manager').manager.getService(serviceName); } catch (e) { }
26
+
27
+var connectArgs = [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1'];
28
+connectArgs.push('--MeshName="' + msh.MeshName + '"');
29
+connectArgs.push('--MeshType="' + msh.MeshType + '"');
30
+connectArgs.push('--MeshID="' + msh.MeshID + '"');
31
+connectArgs.push('--ServerID="' + msh.ServerID + '"');
32
+connectArgs.push('--MeshServer="' + msh.MeshServer + '"');
33
+connectArgs.push('--AgentCapabilities="0x00000020"');
34
+if (msh.displayName) { connectArgs.push('--displayName="' + msh.displayName + '"'); }
35
+if (msh.agentName) { connectArgs.push('--agentName="' + msh.agentName + '"'); }
36
22
-function _install(parms) {
37
+function _install(parms)
38
+{
39
var mstr = require('fs').createWriteStream(process.execPath + '.msh', { flags: 'wb' });
40
mstr.write('MeshName=' + msh.MeshName + '\n');
41
mstr.write('MeshType=' + msh.MeshType + '\n');
42
mstr.write('MeshID=' + msh.MeshID + '\n');
43
mstr.write('ServerID=' + msh.ServerID + '\n');
44
mstr.write('MeshServer=' + msh.MeshServer + '\n');
45
+ if (msh.agentName) { mstr.write('agentName=' + msh.agentName + '\n'); }
46
+ if (msh.meshServiceName) { mstr.write('meshServiceName=' + msh.meshServiceName + '\n'); }
47
mstr.end();
30
-
48
+
49
if (parms == null) { parms = []; }
50
+ if (msh.companyName) { parms.unshift('--companyName="' + msh.companyName + '"'); }
51
+ if (msh.displayName) { parms.unshift('--displayName="' + msh.displayName + '"'); }
52
+ if (msh.meshServiceName) { parms.unshift('--meshServiceName="' + msh.meshServiceName + '"'); }
53
parms.unshift('--copy-msh=1');
54
parms.unshift('--no-embedded=1');
55
parms.unshift('-fullinstall');
56
parms.unshift(process.execPath.split('/').pop());
36
-
57
+
58
global._child = require('child_process').execFile(process.execPath, parms);
59
global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
60
global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
61
global._child.waitExit();
62
}
63
43
-function _uninstall() {
64
+function _uninstall()
65
+{
66
global._child = require('child_process').execFile(process.execPath,
45
- [process.execPath.split('/').pop(), '-fulluninstall', '--no-embedded=1']);
46
-
67
+ [process.execPath.split('/').pop(), '-fulluninstall', '--no-embedded=1', '--meshServiceName="' + serviceName + '"']);
68
+
69
global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
70
global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
71
global._child.waitExit();
72
}
73
52
-if (msh.InstallFlags == null) {
74
+if (msh.InstallFlags == null)
75
+{
76
msh.InstallFlags = 3;
54
-} else {
77
+} else
78
+{
79
msh.InstallFlags = parseInt(msh.InstallFlags.toString());
80
}
81
58
-if (process.argv.includes('-mesh')) {
82
+if (process.argv.includes('-mesh'))
83
+{
84
console.log(JSON.stringify(msh, null, 2));
85
process.exit();
86
}
87
63
-if ((msh.InstallFlags & 1) == 1) {
88
+if ((msh.InstallFlags & 1) == 1)
89
+{
90
buttons.unshift('Connect');
65
- if (process.argv.includes('-connect')) {
66
- global._child = require('child_process').execFile(process.execPath,
67
- [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1',
68
- '--MeshName="' + msh.MeshName + '"', '--MeshType="' + msh.MeshType + '"',
69
- '--MeshID="' + msh.MeshID + '"',
70
- '--ServerID="' + msh.ServerID + '"',
71
- '--MeshServer="' + msh.MeshServer + '"',
72
- '--AgentCapabilities="0x00000020"']);
73
-
91
+ if (process.argv.includes('-connect'))
92
+ {
93
+ global._child = require('child_process').execFile(process.execPath, connectArgs);
94
global._child.stdout.on('data', function (c) { });
95
global._child.stderr.on('data', function (c) { });
96
global._child.on('exit', function (code) { process.exit(code); });
@@ -82,54 +102,72 @@ if ((msh.InstallFlags & 1) == 1) {
102
}
103
}
104
85
-if ((!skip) && ((msh.InstallFlags & 2) == 2)) {
86
- if (!require('user-sessions').isRoot()) {
105
+if ((!skip) && ((msh.InstallFlags & 2) == 2))
106
+{
107
+ if (!require('user-sessions').isRoot())
108
+ {
109
console.log('\n' + "Elevated permissions is required to install/uninstall the agent.");
110
console.log("Please try again with sudo.");
111
process.exit();
112
}
91
- if (s) {
92
- if ((process.platform == 'darwin') || require('message-box').kdialog) {
113
+ if (s)
114
+ {
115
+ if ((process.platform == 'darwin') || require('message-box').kdialog)
116
+ {
117
buttons.unshift("Setup");
94
- } else {
118
+ } else
119
+ {
120
buttons.unshift("Uninstall");
121
buttons.unshift("Update");
122
}
98
- } else {
123
+ } else
124
+ {
125
buttons.unshift("Install");
126
}
127
}
128
103
-if (!skip) {
104
- if (process.platform != 'darwin') {
105
- if (process.argv.includes('-install') || process.argv.includes('-update')) {
129
+if (!skip)
130
+{
131
+ if (process.platform != 'darwin')
132
+ {
133
+ if (process.argv.includes('-install') || process.argv.includes('-update'))
134
+ {
135
var p = [];
107
- for (var i = 0; i < process.argv.length; ++i) {
108
- if (process.argv[i].startsWith('--installPath=')) {
136
+ for (var i = 0; i < process.argv.length; ++i)
137
+ {
138
+ if (process.argv[i].startsWith('--installPath='))
139
+ {
140
p.push('--installPath="' + process.argv[i].split('=').pop() + '"');
141
}
142
}
143
_install(p);
144
process.exit();
145
}
115
- else if (process.argv.includes('-uninstall')) {
146
+ else if (process.argv.includes('-uninstall'))
147
+ {
148
_uninstall();
149
process.exit();
150
}
119
- else {
120
- if (!require('message-box').kdialog && ((require('message-box').zenity == null) || (!require('message-box').zenity.extra))) {
151
+ else
152
+ {
153
+ if (!require('message-box').kdialog && ((require('message-box').zenity == null) || (!require('message-box').zenity.extra)))
154
+ {
155
console.log('\n' + "The graphical version of this installer cannot run on this system.");
156
console.log("Try installing/updating Zenity, and run again." + '\n');
157
console.log("You can also run the text version from the command line with the following command(s): ");
124
- if ((msh.InstallFlags & 1) == 1) {
158
+ if ((msh.InstallFlags & 1) == 1)
159
+ {
160
console.log('./' + process.execPath.split('/').pop() + ' -connect');
161
}
127
- if ((msh.InstallFlags & 2) == 2) {
128
- if (s) {
162
+ if ((msh.InstallFlags & 2) == 2)
163
+ {
164
+ if (s)
165
+ {
166
console.log('./' + process.execPath.split('/').pop() + ' -update');
167
console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
168
}
132
- else {
169
+ else
170
+ {
171
console.log('./' + process.execPath.split('/').pop() + ' -install');
172
console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
173
}
@@ -139,32 +177,40 @@ if (!skip) {
177
}
178
}
179
}
142
- else {
180
+ else
181
+ {
182
if (!require('user-sessions').isRoot()) { console.log('\n' + "This utility requires elevated permissions. Please try again with sudo."); process.exit(); }
183
}
184
}
185
186
148
-if (!skip) {
149
- if (!s) {
187
+if (!skip)
188
+{
189
+ if (!s)
190
+ {
191
msg = "Agent: " + "NOT INSTALLED" + '\n';
151
- } else {
192
+ } else
193
+ {
194
msg = "Agent: " + (s.isRunning() ? "RUNNING" : "NOT RUNNING") + '\n';
195
}
154
-
196
+
197
msg += ("Device Group: " + msh.MeshName + '\n');
198
msg += ("Server URL: " + msh.MeshServer + '\n');
157
-
158
- var p = require('message-box').create("MeshCentral Agent Setup", msg, 99999, buttons);
159
- p.then(function (v) {
160
- switch (v) {
199
+
200
+ var p = require('message-box').create(displayName + " Setup", msg, 99999, buttons);
201
+ p.then(function (v)
202
+ {
203
+ switch (v)
204
+ {
205
case "Cancel":
206
process.exit();
207
break;
208
case 'Setup':
165
- var d = require('message-box').create("MeshCentral Agent", msg, 99999, ['Update', 'Uninstall', 'Cancel']);
166
- d.then(function (v) {
167
- switch (v) {
209
+ var d = require('message-box').create(displayName, msg, 99999, ['Update', 'Uninstall', 'Cancel']);
210
+ d.then(function (v)
211
+ {
212
+ switch (v)
213
+ {
214
case 'Update':
215
case 'Install':
216
_install();
@@ -179,28 +225,23 @@ if (!skip) {
225
}).catch(function (v) { process.exit(); });
226
break;
227
case "Connect":
182
- global._child = require('child_process').execFile(process.execPath,
183
- [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1',
184
- '--MeshName="' + msh.MeshName + '"', '--MeshType="' + msh.MeshType + '"',
185
- '--MeshID="' + msh.MeshID + '"',
186
- '--ServerID="' + msh.ServerID + '"',
187
- '--MeshServer="' + msh.MeshServer + '"',
188
- '--AgentCapabilities="0x00000020"']);
189
-
228
+ global._child = require('child_process').execFile(process.execPath, connectArgs);
229
global._child.stdout.on('data', function (c) { });
230
global._child.stderr.on('data', function (c) { });
231
global._child.on('exit', function (code) { process.exit(code); });
193
-
232
+
233
msg = ("Device Group: " + msh.MeshName + '\n');
234
msg += ("Server URL: " + msh.MeshServer + '\n');
196
-
197
- if (process.platform != 'darwin') {
198
- if (!require('message-box').zenity && require('message-box').kdialog) {
235
+
236
+ if (process.platform != 'darwin')
237
+ {
238
+ if (!require('message-box').zenity && require('message-box').kdialog)
239
+ {
240
msg += ('\nPress OK to Disconnect');
241
}
242
}
202
-
203
- var d = require('message-box').create("MeshCentral Agent", msg, 99999, ['Disconnect']);
243
+
244
+ var d = require('message-box').create(displayName, msg, 99999, ['Disconnect']);
245
d.then(function (v) { process.exit(); }).catch(function (v) { process.exit(); });
246
break;
247
case "Uninstall":
@@ -217,7 +258,8 @@ if (!skip) {
258
process.exit();
259
break;
260
}
220
- }).catch(function (e) {
261
+ }).catch(function (e)
262
+ {
263
console.log(e);
264
process.exit();
265
});