API Docs for: 0.8.0
Show:

File: include/system/call_home_service.js

  1. /*
  2. Copyright (C) 2016 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. 'use strict';
  18.  
  19. //dependencies
  20. var os = require('os');
  21. var https = require('https');
  22. var domain = require('domain');
  23. var util = require('../util.js');
  24.  
  25. module.exports = function CallHomeServiceModule(pb) {
  26.  
  27. /**
  28. *
  29. * @class CallHomeService
  30. * @constructor
  31. */
  32. function CallHomeService(){}
  33.  
  34. /**
  35. *
  36. * @private
  37. * @static
  38. * @readonly
  39. * @property HOST
  40. * @type {String}
  41. */
  42. var HOST = 'pencilblue.org';
  43.  
  44. /**
  45. *
  46. * @private
  47. * @static
  48. * @readonly
  49. * @property PORT
  50. * @type {Integer}
  51. */
  52. var PORT = 443;
  53.  
  54. /**
  55. *
  56. * @private
  57. * @static
  58. * @readonly
  59. * @property PATH
  60. * @type {String}
  61. */
  62. var PATH = '/api/v1/callhome/event';
  63.  
  64. /**
  65. *
  66. * @private
  67. * @static
  68. * @readonly
  69. * @property METHOD
  70. * @type {String}
  71. */
  72. var METHOD = 'POST';
  73.  
  74. //statics
  75. /**
  76. *
  77. * @static
  78. * @readonly
  79. * @property SYSTEM_SETUP_EVENT
  80. * @type {String}
  81. */
  82. CallHomeService.SYSTEM_SETUP_EVENT = 'system_setup';
  83.  
  84. /**
  85. *
  86. * @static
  87. * @method callHome
  88. * @param {String} type
  89. * @param {Object} data
  90. */
  91. CallHomeService.callHome = function(type, data) {
  92. if (!util.isObject(data)) {
  93. data = {};
  94. }
  95.  
  96. data.type = type;
  97. data.site_ip = pb.config.siteIP;
  98. data.site_name = pb.config.siteName;
  99. data.os = os.type();
  100. data.platform = os.platform();
  101. data.release = os.release();
  102. data.cpus = os.cpus();
  103. data.version = process.versions;
  104. var post_data = JSON.stringify(data);
  105.  
  106. // An object of options to indicate where to post to
  107. var post_options = {
  108. host: HOST,
  109. port: PORT,
  110. path: PATH,
  111. method: METHOD,
  112. headers: {
  113. 'Content-Type': 'application/json',
  114. 'Content-Length': post_data.length
  115. }
  116. };
  117.  
  118. // Set up the request
  119. pb.log.debug('CallHomeService: Sending event [%s] to [%s:%s%s', type, METHOD, HOST, PATH);
  120. _callHome(post_options, post_data);
  121. };
  122.  
  123. /**
  124. *
  125. * @private
  126. * @static
  127. * @method _callHome
  128. * @param {Object} options
  129. * @param {Object} postData
  130. */
  131. function _callHome(options, postData) {
  132.  
  133. var d = domain.create();
  134. d.on('error', function(err) {
  135. pb.log.silly('CallHomeService: An error occurred attempting to send event. %s', err.stack);
  136. });
  137. d.run(getDomainRunner(options, postData));
  138. }
  139.  
  140. /**
  141. *
  142. * @private
  143. * @static
  144. * @method getDomainRunner
  145. * @param {Object} options
  146. * @param {Object} postData
  147. */
  148. function getDomainRunner(options, postData) {
  149. return function() {
  150. var post_req = https.request(options, function(res) {
  151.  
  152. var json = '';
  153. res.setEncoding('utf8');
  154. res.on('data', function (chunk) {
  155. json += chunk;
  156. });
  157. res.on('end', function() {
  158. onResponseRecieved(res, json);
  159. });
  160. });
  161.  
  162. // post the data
  163. post_req.write(postData);
  164. post_req.end();
  165. };
  166. }
  167.  
  168. /**
  169. *
  170. * @private
  171. * @static
  172. * @method onResponseRecieved
  173. * @param {Object} options
  174. * @param {Object} postData
  175. */
  176. function onResponseRecieved(res, json) {
  177. pb.log.silly('CallHomeService: Event Response: %s', json);
  178. }
  179.  
  180. //exports
  181. return CallHomeService;
  182. };
  183.