EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
meshcore_diagnostic.js
Go to the documentation of this file.
1/*
2Copyright 2019 Intel Corporation
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17require('MeshAgent').on('Connected', function (status)
18{
19 if (status == 0)
20 {
21 return;
22 }
23 this.timeout = setTimeout(start, 10000);
24});
25
26
27
28function sendServerLog(msg)
29{
30 require('MeshAgent').SendCommand({ action: 'diagnostic', value: { command: 'log', value: msg } });
31}
32function 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
45function 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
84function 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
93function giveup()
94{
95 sendServerLog('Diagnostic: Unable to diagnose Mesh Agent');
96 finished();
97}
98function finished()
99{
100 sendServerLog('Diagnostic: End');
101 require('service-manager').manager.getService('meshagentDiagnostic').stop();
102}
103
104function 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
129function 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};