ibmapm-restclient
Version:
Node.js rest client for ICAM.
187 lines (163 loc) • 5.97 kB
JavaScript
;
var fs = require('fs');
var os = require('os');
var crypto = require('crypto');
var logger = require('./logutil').getLogger('util.js');
var path = require('path');
var k8sutil = require('../tools/k8sutil');
const MAX_NAME_LENGTH = 19;
const MAX_MSN_LENGTH = 25;
var appGuid;
var applicationName = process.env.APPLICATION_NAME;
var appHostname = os.hostname();
var inited = false;
var BIEnabled = false;
module.exports.init = function() {
let rootPath = process.cwd();
if (inited) {
return;
}
if (process.env.VCAP_APPLICATION) {
let vcapapp = JSON.parse(process.env.VCAP_APPLICATION);
appGuid = vcapapp.application_id;
applicationName = process.env.APPLICATION_NAME || vcapapp.application_name;
} else {
initAppGuid(rootPath);
}
appGuid = genAppGuid(appGuid, applicationName);
inited = true;
};
function genAppGuid(guid, appName) {
logger.debug('util.js', 'appGuidDec', guid, appName);
let retAppGuid = guid;
let pref = appName.replace(/\//g, '_');
pref = pref.replace('-', '');
if (k8sutil.isICP()) {
pref = pref + '_' + k8sutil.getNamespace() + '_' + k8sutil.getPodName();
retAppGuid = crypto.createHash('md5').update(pref).digest('hex');
}
if (pref.length > MAX_NAME_LENGTH) {
pref = pref.substr(0, MAX_NAME_LENGTH);
}
retAppGuid = pref + '_' + retAppGuid;
if (retAppGuid.length > MAX_MSN_LENGTH) {
retAppGuid = retAppGuid.substr(0, MAX_MSN_LENGTH);
}
return retAppGuid;
};
module.exports.getAppGuid = function() {
return appGuid;
};
module.exports.getApplicationName = function() {
return applicationName;
};
module.exports.validateBIPayload = function(payload) {
let BIRequiredKeys = ['SUBNODE_TYPE', 'APP_GUID', 'APP_NAME', 'INSTANCE_ID', 'INSTANCE_INDEX',
'URI', 'START_TIME', 'APP_PORT', 'APP_ENTRY', 'PORT', 'PID', 'CPU_P', 'MEM_RSS',
'TYPE', 'UPTIME', 'REQRATE', 'RESP_TIME', 'MAX_RSPTIME', 'app_memAll', 'app_uptime',
'gc_heapSize', 'gc_heapUsed', 'gc_duration', 'gc_mCount', 'gc_sCount', 'eventloop_time',
'eventloop_latencyMin', 'eventloop_latencyMax', 'eventloop_latencyAvg', 'loop_count',
'loop_minimum', 'loop_maximum', 'loop_average', 'HTTP_REQ'
];
BIRequiredKeys.forEach(function(curr) {
if (payload[curr] === undefined || payload[curr] === null) {
return false;
}
});
return true;
};
function generateAppNameByPackage() {
let name;
for (let i in process.mainModule.paths) {
let packageFile = process.mainModule.paths[i].split('node_modules')[0] +
'/' + 'package.json';
try {
let packageString = fs.readFileSync(packageFile);
let packageJson = JSON.parse(packageString);
if (packageJson.name) {
name = packageJson.name;
break;
}
} catch (e) {
logger.debug(e.message);
}
}
return name;
};
function generateApplicationNameAndGuidbyPath(rootPath) {
let thePath = rootPath;
let theFolder = thePath.replace(/\//g, '_');
let argStr = process.argv[1] ? process.argv[1].replace(/\//g, '_') : '';
// generating appGuid
let appGuidMd5OriginStr = os.hostname() + '_' + theFolder + '_' + argStr;
let appGuidMd5Str = crypto.createHash('md5').update(appGuidMd5OriginStr).digest('hex');
appGuid = appGuidMd5Str.substring(0, Math.min(25, appGuidMd5Str.length));
// generating appGuid end
if (process.argv[1] && process.argv[1].indexOf(thePath) !== -1)
applicationName = applicationName || process.argv[1];
else if (process.argv[1])
applicationName = applicationName || path.join(thePath, process.argv[1]);
else {
applicationName = applicationName || thePath;
}
};
function initAppGuid(rootPath) {
if (!process.env.APPLICATION_NAME) {
// find name in package.json
if (process.mainModule && process.mainModule.paths && process.mainModule.paths.length > 0) {
let name = generateAppNameByPackage();
if (!name) {
logger.debug('Failed to get name in package.json,' +
' will generate applicationName and' +
' APP_GUID by file position.');
generateApplicationNameAndGuidbyPath(rootPath);
} else {
appGuid = appHostname + '_' + name.replace('-', '');
applicationName = name;
}
} else {
generateApplicationNameAndGuidbyPath(rootPath);
}
} else {
applicationName = process.env.APPLICATION_NAME;
appGuid = appHostname + '_' +
process.env.APPLICATION_NAME;
}
};
module.exports.tlsFix8 = function(options) {
var nodever = process.version;
if (options.protocol === 'https:' && nodever.startsWith('v8.')) {
logger.debug('Current node version is ', nodever,
', the options.ecdhCurve should be set to auto.');
options.ecdhCurve = 'auto';
}
};
module.exports.isBase64 = function(str) {
if (process.env.IS_BASE64_FORMAT) {
return module.exports.isTrue(process.env.IS_BASE64_FORMAT);
}
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
return base64regex.test(str);
};
module.exports.decBase64 = function(str) {
return Buffer.from(str, 'base64').toString();
};
module.exports.decBase64Ex = function(str) {
if (module.exports.isBase64(str))
return Buffer.from(str, 'base64').toString();
else
return str;
};
module.exports.isTrue = function(v) {
if (v && ['false', 'False', 'FALSE', ''].indexOf(v) < 0) {
return true;
} else {
return false;
}
};
module.exports.setBIEnabled = function(value) {
BIEnabled = value;
};
module.exports.getBIEnabled = function() {
return BIEnabled;
};