Updated Diagnostic Agent Core

Bryan Roe committed Apr 12, 2019 at 17:43 UTC 738add54ad764e4cc1c1e3141259e68d10ac496a
2 files changed +208 -2
agents/meshcore_diagnostic.js new
+206
@@ -0,0 +1,206 @@
1 +/*
2 +Copyright 2019 Intel Corporation
3 +
4 +Licensed under the Apache License, Version 2.0 (the "License");
5 +you may not use this file except in compliance with the License.
6 +You may obtain a copy of the License at
7 +
8 + http://www.apache.org/licenses/LICENSE-2.0
9 +
10 +Unless required by applicable law or agreed to in writing, software
11 +distributed under the License is distributed on an "AS IS" BASIS,
12 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 +See the License for the specific language governing permissions and
14 +limitations under the License.
15 +*/
16 +
17 +require('MeshAgent').on('Connected', function (status)
18 +{
19 + if (status == 0)
20 + {
21 + return;
22 + }
23 + this.timeout = setTimeout(start, 10000);
24 +});
25 +
26 +
27 +
28 +function sendServerLog(msg)
29 +{
30 + require('MeshAgent').SendCommand({ action: 'diagnostic', value: { command: 'log', value: msg } });
31 +}
32 +function getMeshAgentService()
33 +{
34 + try
35 + {
36 + var ret = require('service-manager').manager.getService(process.platform == 'win32' ? 'mesh agent' : 'meshagent');
37 + return(ret);
38 + }
39 + catch(e)
40 + {
41 + return (null);
42 + }
43 +}
44 +
45 +function getARCHID() {
46 + var ret = 0;
47 + switch (process.platform) {
48 + case 'linux':
49 + // Need to detect Architecture ID
50 + var child = require('child_process').execFile('/bin/sh', ['sh']);
51 + child.stdout.str = '';
52 + child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
53 + child.stdin.write("uname -m\nexit\n");
54 + child.waitExit();
55 + switch (child.stdout.str.trim()) {
56 + case 'x86_64':
57 + case 'amd64':
58 + ret = 6;
59 + break;
60 + case 'x86':
61 + case 'i686':
62 + case 'i586':
63 + case 'i386':
64 + ret = 5;
65 + break;
66 + case 'armv6l':
67 + case 'armv7l':
68 + ret = 25;
69 + break;
70 + default:
71 + break;
72 + }
73 + break;
74 + case 'darwin':
75 + ret = 16;
76 + break;
77 + case 'win32':
78 + ret = process.arch == 'x64' ? 4 : 3;
79 + break;
80 + }
81 + return (ret);
82 +}
83 +
84 +function DownloadAgentBinary(path, ID)
85 +{
86 + var options = require('http').parseUri(require('MeshAgent').ServerInfo.ServerUri);
87 + var downloadUri = 'https://' + options.host + ':' + options.port + '/meshagents?id=' + (ID != null ? ID : getARCHID());
88 + sendServerLog('Diagnostic: Attempting to downlod agent from: ' + downloadUri);
89 +
90 + return (wget(downloadUri, path, { rejectUnauthorized: false }));
91 +}
92 +
93 +function giveup()
94 +{
95 + sendServerLog('Diagnostic: Unable to diagnose Mesh Agent');
96 + finished();
97 +}
98 +function finished()
99 +{
100 + sendServerLog('Diagnostic: End');
101 + require('service-manager').manager.getService('meshagentDiagnostic').stop();
102 +}
103 +
104 +function ConfigureAgent(agent)
105 +{
106 + sendServerLog('...Configuring Agent...');
107 + var info = require('MeshAgent').ServerInfo;
108 +
109 + var msh = 'MeshID=0x' + info.MeshID + '\n' + 'ServerID=' + info.ServerID + '\n' + 'MeshServer=' + info.ServerUri + '\n';
110 + var cfg = require('global-tunnel').proxyConfig;
111 + if(cfg == null)
112 + {
113 + msh += 'ignoreProxyFile=1\n';
114 + }
115 + else
116 + {
117 + msh += ('WebProxy=' + cfg.host + ':' + cfg.port + '\n');
118 + }
119 + if(process.platform == 'win32')
120 + {
121 + require('fs').writeFileSync(agent.appLocation().replace('.exe', '.msh'), msh);
122 + }
123 + else
124 + {
125 + require('fs').writeFileSync(agent.appLocation() + '.msh', msh);
126 + }
127 +}
128 +
129 +function start()
130 +{
131 + sendServerLog('Diagnostic: Start');
132 +
133 + var id = getARCHID();
134 + var s = getMeshAgentService();
135 + if (s == null)
136 + {
137 + DownloadAgentBinary('agent_temporary.bin').then(function ()
138 + {
139 + // SUCCESS
140 + try
141 + {
142 + var agent = require('service-manager').manager.installService(
143 + {
144 + name: process.platform == 'win32' ? 'Mesh Agent' : 'meshagent',
145 + target: 'meshagent',
146 + description: 'Mesh Central Agent v2 Background Service',
147 + displayName: 'Mesh Agent v2 Background Service',
148 + servicePath: 'agent_temporary.bin',
149 + startType: 'DEMAND_START'
150 + });
151 + require('fs').unlinkSync('agent_temporary.bin');
152 + ConfigureAgent(agent);
153 + }
154 + catch(e)
155 + {
156 + giveup();
157 + }
158 + },
159 + function ()
160 + {
161 + // FAILURE
162 + giveup();
163 + });
164 + }
165 + if(s!=null)
166 + {
167 + // Mesh Agent Installation Found
168 + sendServerLog('Diagnostic: Mesh Agent Service => ' + (s.isRunning() ? 'RUNNING' : 'NOT-RUNNING'));
169 + if(s.isRunning())
170 + {
171 + finished();
172 + }
173 + else
174 + {
175 + sendServerLog('Diagnostic: Attempting to start Mesh Agent');
176 + s.start();
177 + sendServerLog('Diagnostic: ' + (s.isRunning() ? '(SUCCESS)' : '(FAILED)'));
178 + if (s.isRunning())
179 + {
180 + finished();
181 + return;
182 + }
183 + else
184 + {
185 + DownloadAgentBinary(s.appLocation()).then(
186 + function () {
187 + sendServerLog('Diagnostic: Downloaded Successfully');
188 + sendServerLog('Diagnostic: Attempting to start Mesh Agent');
189 + s.start();
190 + sendServerLog('Diagnostic: ' + (s.isRunning() ? '(SUCCESS)' : '(FAILED)'));
191 + if (s.isRunning()) {
192 + finished();
193 + return;
194 + }
195 + else {
196 + giveup();
197 + }
198 + },
199 + function () {
200 + sendServerLog('Diagnostic: Download Failed');
201 + giveup();
202 + });
203 + }
204 + }
205 + }
206 +};
meshcentral.js
+2 -2
@@ -1204,8 +1204,8 @@ function CreateMeshCentralServer(config, args) {
1204
1205 // Read the agent recovery core if present
1206 var meshAgentRecoveryCore = null;
1207 - if (obj.fs.existsSync(obj.path.join(__dirname, 'agents', 'agentrecoverycore.js')) == true) {
1208 - try { meshAgentRecoveryCore = obj.fs.readFileSync(obj.path.join(__dirname, 'agents', 'agentrecoverycore.js')).toString(); } catch (ex) { }
1207 + if (obj.fs.existsSync(obj.path.join(__dirname, 'agents', 'meshcore_diagnostic.js')) == true) {
1208 + try { meshAgentRecoveryCore = obj.fs.readFileSync(obj.path.join(__dirname, 'agents', 'meshcore_diagnostic.js')).toString(); } catch (ex) { }
1209 if (meshAgentRecoveryCore != null) {
1210 modulesAdd['windows-agentrecovery'] = ['var addedModules = [];\r\n'];
1211 modulesAdd['linux-agentrecovery'] = ['var addedModules = [];\r\n'];