meshcore_diagnostic.js 6.0 KB

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