2Copyright 2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17require('MeshAgent').on('Connected', function (status)
23 this.timeout = setTimeout(start, 10000);
28function sendServerLog(msg)
30 require('MeshAgent').SendCommand({ action: 'diagnostic', value: { command: 'log', value: msg } });
32function getMeshAgentService()
36 var ret = require('service-manager').manager.getService(process.platform == 'win32' ? 'mesh agent' : 'meshagent');
47 switch (process.platform) {
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");
55 switch (child.stdout.str.trim()) {
78 ret = process.arch == 'x64' ? 4 : 3;
84function DownloadAgentBinary(path, ID)
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);
90 return (wget(downloadUri, path, { rejectUnauthorized: false }));
95 sendServerLog('Diagnostic: Unable to diagnose Mesh Agent');
100 sendServerLog('Diagnostic: End');
101 require('service-manager').manager.getService('meshagentDiagnostic').stop();
104function ConfigureAgent(agent)
106 sendServerLog('...Configuring Agent...');
107 var info = require('MeshAgent').ServerInfo;
109 var msh = 'MeshID=0x' + info.MeshID + '\n' + 'ServerID=' + info.ServerID + '\n' + 'MeshServer=' + info.ServerUri + '\n';
110 var cfg = require('global-tunnel').proxyConfig;
113 msh += 'ignoreProxyFile=1\n';
117 msh += ('WebProxy=' + cfg.host + ':' + cfg.port + '\n');
119 if(process.platform == 'win32')
121 require('fs').writeFileSync(agent.appLocation().replace('.exe', '.msh'), msh);
125 require('fs').writeFileSync(agent.appLocation() + '.msh', msh);
131 sendServerLog('Diagnostic: Start');
133 var id = getARCHID();
134 var s = getMeshAgentService();
137 DownloadAgentBinary('agent_temporary.bin').then(function ()
142 var agent = require('service-manager').manager.installService(
144 name: process.platform == 'win32' ? 'Mesh Agent' : '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'
151 require('fs').unlinkSync('agent_temporary.bin');
152 ConfigureAgent(agent);
167 // Mesh Agent Installation Found
168 sendServerLog('Diagnostic: Mesh Agent Service => ' + (s.isRunning() ? 'RUNNING' : 'NOT-RUNNING'));
175 sendServerLog('Diagnostic: Attempting to start Mesh Agent');
177 sendServerLog('Diagnostic: ' + (s.isRunning() ? '(SUCCESS)' : '(FAILED)'));
185 DownloadAgentBinary(s.appLocation()).then(
187 sendServerLog('Diagnostic: Downloaded Successfully');
188 sendServerLog('Diagnostic: Attempting to start Mesh Agent');
190 sendServerLog('Diagnostic: ' + (s.isRunning() ? '(SUCCESS)' : '(FAILED)'));
200 sendServerLog('Diagnostic: Download Failed');