Commit 5fae2dc7762d851505cee66c96a3d796fbfc382f

Authored by Adhidarma Hadiwinoto
1 parent 7b21ba3b2e
Exists in master

Checking core version

Showing 8 changed files with 144 additions and 48 deletions Side-by-side Diff

... ... @@ -4,8 +4,12 @@ process.chdir(__dirname);
4 4 const fs = require('fs');
5 5 fs.writeFileSync('pid.txt', process.pid);
6 6  
  7 +const matrix = require('komodo-sdk/matrix');
  8 +matrix.NODE_ENV = process.env.NODE_ENV;
  9 +
7 10 const logger = require('komodo-sdk/logger');
8 11 const coreUrl = require('komodo-sdk/core-url');
9 12 logger.verbose('CORE URL: ' + coreUrl);
10 13  
11 14 require('./lib/http-listener');
  15 +
lib/command-handler/error.js
... ... @@ -6,4 +6,5 @@ exports.ERR_INVALID_FORMAT = 'Format perintah salah';
6 6 exports.ERR_INVALID_CREDENTIAL = 'Kesalahan kredensial. User atau PIN tidak sesuai';
7 7 exports.ERR_INVALID_CORE_RESPONSE = 'Respon CORE tidak valid';
8 8 exports.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE = 'CORE tidak merespon dengan HTTP status 200';
9   -exports.ERR_NOT_IMPLEMENTED = 'Perintah belum dapat diproses';
10 9 \ No newline at end of file
  10 +exports.ERR_NOT_IMPLEMENTED = 'Perintah belum dapat diproses';
  11 +exports.ERR_CORE_VERSION_NOT_SUFFICIENT = 'Versi CORE tidak mencukupi';
11 12 \ No newline at end of file
lib/coreapi/core-version.js
... ... @@ -0,0 +1,48 @@
  1 +"use strict";
  2 +
  3 +const util = require('util');
  4 +const naturalCompare = require('natural-compare-lite');
  5 +
  6 +const logger = require('komodo-sdk/logger');
  7 +const matrix = require('komodo-sdk/matrix');
  8 +
  9 +const coreMatrixRequest = util.promisify(require('./matrix'));
  10 +const coreApiRequest = require('./request');
  11 +
  12 +const MINIMUM_VERSION = 'v1.33.1-3-g2ce5525';
  13 +
  14 +let _isSufficient = false;
  15 +matrix.core_version_is_sufficient = false;
  16 +matrix.core_version_requirement = MINIMUM_VERSION;
  17 +
  18 +function logIfDev(...args) {
  19 + matrix.NODE_ENV !== 'production' && logger.verbose(...args);
  20 +}
  21 +
  22 +async function getCoreVersion() {
  23 +
  24 + logIfDev('CORE-VERSION: checking core version');
  25 +
  26 + const coreMatrix = await coreMatrixRequest();
  27 + if (!coreMatrix || !coreMatrix.version_active) return;
  28 +
  29 + _isSufficient = naturalCompare(MINIMUM_VERSION, coreMatrix.version_active) <= 0;
  30 + matrix.core_version_is_sufficient = _isSufficient;
  31 + logger.info('CORE-VERSION', {version_active: coreMatrix.version_active, minimum_version: MINIMUM_VERSION, sufficient: _isSufficient});
  32 +
  33 + if (!_isSufficient) {
  34 + setTimeout(
  35 + function() {
  36 + getCoreVersion();
  37 + },
  38 + 5000
  39 + )
  40 + }
  41 +}
  42 +getCoreVersion();
  43 +
  44 +function isSufficient() {
  45 + return _isSufficient;
  46 +}
  47 +
  48 +module.exports = isSufficient();
0 49 \ No newline at end of file
lib/coreapi/index.js
1 1 "use strict";
2 2  
3   -const request = require('request');
4   -
5   -const coreUrl = require('komodo-sdk/core-url');
  3 +const matrix = require('komodo-sdk/matrix');
6 4 const logger = require('komodo-sdk/logger');
  5 +
7 6 const commandError = require('../command-handler/error');
  7 +const coreApiRequest = require('./request');
  8 +require('./core-version');
8 9  
9   -function execute(coreEndpoint, params, httpMethod, cb) {
10   - const requestOptions = {
11   - url: coreUrl + coreEndpoint,
12   - method: httpMethod || 'GET'
13   - }
14 10  
15   - if (requestOptions.method === 'GET') {
16   - requestOptions.qs = params;
  11 +function _coreApiRequest(...args) {
  12 + if (!matrix.core_version_is_sufficient) {
  13 + logger.info('Blocking request to CORE because of insufficient CORE version');
  14 + return;
17 15 }
18   - else if (requestOptions.method === 'POST') {
19   - requestOptions.data = params;
20   - }
21   -
22   - logger.verbose('Requesting to core', {url: requestOptions.url, http_method: httpMethod, params: params});
23   -
24   - request(requestOptions, function(err, res, body) {
25   - const responseParams = {
26   - directResponse: true,
27   - httpStatusCode: res ? res.statusCode : null,
28   - body: body
29   - }
30   -
31   - if (err) {
32   - cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams);
33   - return;
34   - }
35   -
36   - if (res.statusCode !== 200) {
37   - cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams);
38   - return;
39   - }
40   -
41   - try {
42   - var coreResponseObject = JSON.parse(body);
43   - }
44   - catch(e) {
45   - logger.verbose(commandError.ERR_INVALID_CORE_RESPONSE, {body: body})
46   - cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams);
47   - return;
48   - }
49   -
50   -
51   - cb(err, coreResponseObject, responseParams);
52   - })
53   -
  16 + coreApiRequest(...args);
54 17 }
55 18  
56   -module.exports = execute;
57 19 \ No newline at end of file
  20 +module.exports = _coreApiRequest;
58 21 \ No newline at end of file
lib/coreapi/matrix.js
... ... @@ -0,0 +1,17 @@
  1 +"use strict";
  2 +
  3 +const coreapiRequest = require('./request');
  4 +
  5 +function get(cb) {
  6 + coreapiRequest('/services/matrix', null, 'GET', function(err, responseObject, responseParams) {
  7 + if (err) {
  8 + cb(err);
  9 + return;
  10 + }
  11 +
  12 + cb(null, responseObject);
  13 + });
  14 +
  15 +}
  16 +
  17 +module.exports = get;
0 18 \ No newline at end of file
lib/coreapi/request.js
... ... @@ -0,0 +1,57 @@
  1 +"use strict";
  2 +
  3 +const request = require('request');
  4 +
  5 +const coreUrl = require('komodo-sdk/core-url');
  6 +const logger = require('komodo-sdk/logger');
  7 +const commandError = require('../command-handler/error');
  8 +
  9 +function execute(coreEndpoint, params, httpMethod, cb) {
  10 +
  11 + const requestOptions = {
  12 + url: coreUrl + coreEndpoint,
  13 + method: httpMethod || 'GET'
  14 + }
  15 +
  16 + if (requestOptions.method === 'GET' && params) {
  17 + requestOptions.qs = params;
  18 + }
  19 + else if (requestOptions.method === 'POST' && params) {
  20 + requestOptions.data = params;
  21 + }
  22 +
  23 + logger.verbose('Requesting to core', {url: requestOptions.url, http_method: httpMethod, params: params});
  24 +
  25 + request(requestOptions, function(err, res, body) {
  26 + const responseParams = {
  27 + directResponse: true,
  28 + httpStatusCode: res ? res.statusCode : null,
  29 + body: body
  30 + }
  31 +
  32 + if (err) {
  33 + cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams);
  34 + return;
  35 + }
  36 +
  37 + if (res.statusCode !== 200) {
  38 + cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams);
  39 + return;
  40 + }
  41 +
  42 + try {
  43 + var coreResponseObject = JSON.parse(body);
  44 + }
  45 + catch(e) {
  46 + logger.verbose(commandError.ERR_INVALID_CORE_RESPONSE, {body: body})
  47 + cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams);
  48 + return;
  49 + }
  50 +
  51 +
  52 + cb(err, coreResponseObject, responseParams);
  53 + })
  54 +
  55 +}
  56 +
  57 +module.exports = execute;
0 58 \ No newline at end of file
... ... @@ -2624,6 +2624,11 @@
2624 2624 "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
2625 2625 "dev": true
2626 2626 },
  2627 + "natural-compare-lite": {
  2628 + "version": "1.4.0",
  2629 + "resolved": "https://registry.npmjs.org/natural-compare-lite/-/natural-compare-lite-1.4.0.tgz",
  2630 + "integrity": "sha1-F7CVgZiJef3a/gIB6TG6kzyWy7Q="
  2631 + },
2627 2632 "negotiator": {
2628 2633 "version": "0.6.2",
2629 2634 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
... ... @@ -23,6 +23,7 @@
23 23 "express": "^4.17.1",
24 24 "express-ipfilter": "^1.0.1",
25 25 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
  26 + "natural-compare-lite": "^1.4.0",
26 27 "request": "^2.88.0",
27 28 "yargs": "^13.2.4"
28 29 },