diff --git a/app.js b/app.js index 0cc6a6d58..8ce089418 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ var portastic = require('portastic'); const os = require('os'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; const options = {}; +const CONSOLE_LOG_TAG_COMP = 'microgateway app'; options.env = process.env.EDGEMICRO_ENV; options.key = process.env.EDGEMICRO_KEY; @@ -24,15 +25,15 @@ options.target = process.env.EDGEMICRO_API_TARGET; options.port = process.env.PORT || 8000; -if (!options.key ) { writeConsoleLog('log','key is required'); process.exit(1);} -if (!options.secret ) { writeConsoleLog('log','secret is required'); process.exit(1);} -if (!options.org ) { writeConsoleLog('log','org is required'); process.exit(1);} -if (!options.env ) { writeConsoleLog('log','env is required'); process.exit(1);} +if (!options.key ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'key is required'); process.exit(1);} +if (!options.secret ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'secret is required'); process.exit(1);} +if (!options.org ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'org is required'); process.exit(1);} +if (!options.env ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'env is required'); process.exit(1);} if (options.port) { portastic.test(options.port) .then(function(isAvailable){ if(!isAvailable) { - writeConsoleLog('error','port is not available.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'port is not available.'); process.exit(1); } }); @@ -46,19 +47,19 @@ if (options.configUrl) { if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") { request.get(options.configUrl, function(error, response, body) { if (error) { - writeConsoleLog('error',"config file did not download: "+error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: "+error); process.exit(1); } try { fs.writeFileSync(filePath, body, 'utf8'); run.start(options); } catch (err) { - writeConsoleLog('error',"config file could not be written: " + err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err); process.exit(1); } }); } else { - writeConsoleLog('error',"url protocol not supported: "+parsedUrl.protocol); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"url protocol not supported: "+parsedUrl.protocol); process.exit(1); } } else { diff --git a/cli/cmd-cert.js b/cli/cmd-cert.js index 69504112d..c6480dd35 100644 --- a/cli/cmd-cert.js +++ b/cli/cmd-cert.js @@ -5,6 +5,8 @@ const cert = require('./lib/cert')(); const prompt = require('cli-prompt'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway cmd cert'; + const setup = function setup() { commander @@ -113,7 +115,7 @@ const setup = function setup() { function optionError(caller) { return(((obj) => { return((message) => { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); obj.help(); }); })(caller)) diff --git a/cli/cmd-private.js b/cli/cmd-private.js index 4917d3782..8d1df6b1d 100644 --- a/cli/cmd-private.js +++ b/cli/cmd-private.js @@ -8,6 +8,8 @@ const upgradeauth = require('./lib/upgrade-edgeauth')(); const rotatekey = require('./lib/rotate-key')(); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway cmd private'; + var prompt = require('cli-prompt'); module.exports = function() { @@ -233,7 +235,7 @@ function promptForPassword(options, cb) { function optionError(caller) { return(((obj) => { return((message) => { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); obj.help(); }); })(caller)) diff --git a/cli/cmd-token.js b/cli/cmd-token.js index b24bcf19d..315672257 100644 --- a/cli/cmd-token.js +++ b/cli/cmd-token.js @@ -4,6 +4,8 @@ const commander = require('commander'); const token = require('./lib/token')(); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway cmd token'; + const setup = function setup() { commander @@ -63,7 +65,7 @@ const setup = function setup() { function optionError(caller) { return(((obj) => { return((message) => { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); obj.help(); }); })(caller)) diff --git a/cli/cmd.js b/cli/cmd.js index 8b7c84bea..e8ec7eb70 100644 --- a/cli/cmd.js +++ b/cli/cmd.js @@ -23,6 +23,8 @@ const pidpath = configLocations.getPIDFilePath(); var portastic = require('portastic'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway cmd'; + const setup = function setup() { commander .version(require('../package.json').version); @@ -97,7 +99,7 @@ const setup = function setup() { .action((options) => { options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR; init(options, (err, location) => { - writeConsoleLog('log',"config initialized to %s", location) + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"config initialized to %s", location) }) }); @@ -224,7 +226,7 @@ const setup = function setup() { debug("downloading file..."); request.get(options.configUrl, function(error, response, body) { if (error) { - writeConsoleLog('error',"config file did not download: " + error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: " + error); process.exit(1); } try { @@ -232,12 +234,12 @@ const setup = function setup() { fs.writeFileSync(filePath, body, 'utf8'); run.start(options); } catch (err) { - writeConsoleLog('error',"config file could not be written: " + err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err); process.exit(1); } }); } else { - writeConsoleLog('error',"url protocol not supported: " + parsedUrl.protocol); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"url protocol not supported: " + parsedUrl.protocol); process.exit(1); } } else { @@ -288,7 +290,7 @@ const setup = function setup() { debug("downloading file..."); request.get(options.configUrl, function(error, response, body) { if (error) { - writeConsoleLog('error',"config file did not download: " + error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: " + error); process.exit(1); } try { @@ -296,7 +298,7 @@ const setup = function setup() { fs.writeFileSync(filePath, body, 'utf8'); run.reload(options); } catch (err) { - writeConsoleLog('error',"config file could not be written: " + err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err); process.exit(1); } }); @@ -350,7 +352,7 @@ const setup = function setup() { fs.appendFileSync(pidpath, process.pid + '|'); child.start(); } catch (piderr) { - writeConsoleLog('error','failed to start microgateway: ' + piderr); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'failed to start microgateway: ' + piderr); process.exit(1); } } else { @@ -362,10 +364,10 @@ const setup = function setup() { }); fs.unlinkSync(pidpath); } else { - writeConsoleLog('log','pid file not found. please run this command from the folder where microgateway was started.') + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'pid file not found. please run this command from the folder where microgateway was started.') } } catch (piderr) { - writeConsoleLog('error','failed to stop microgateway: ' + piderr); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'failed to stop microgateway: ' + piderr); process.exit(1); } } @@ -591,7 +593,7 @@ const setup = function setup() { function optionError(caller) { return(((obj) => { return((message) => { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); obj.help(); }); })(caller)) @@ -616,7 +618,7 @@ function validateUrl(target) { url.parse(target, true); return true; } catch (err) { - writeConsoleLog('error',"Malformed URL: " + err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"Malformed URL: " + err); return false; } } diff --git a/cli/edgemicro b/cli/edgemicro index 379ac3529..fa01b8463 100755 --- a/cli/edgemicro +++ b/cli/edgemicro @@ -28,16 +28,17 @@ const ltsVersion = 8; const experimental = ["v7", "v9","v11"]; const latest = 12; +const CONSOLE_LOG_TAG_COMP = 'microgateway edgemicro'; //don't print versions when obtaining a token. if (!process.argv.includes("token") && !process.argv.includes("get")) { - writeConsoleLog('log','current nodejs version is %s', version); - writeConsoleLog('log','current edgemicro version is %s', edgemicroVersion); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'current nodejs version is %s', version); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'current edgemicro version is %s', edgemicroVersion); } for (var ver in experimental) { if (version.includes(experimental[ver])) { - writeConsoleLog('log',"You are using a version of NodeJS that is not supported"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"You are using a version of NodeJS that is not supported"); return; } } @@ -45,7 +46,7 @@ for (var ver in experimental) { var majorVersion = parseInt(version.split(".")[0].replace("v", "")); if (majorVersion < ltsVersion || majorVersion > latest) { - writeConsoleLog('log',"You are using a version of NodeJS that is not supported"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"You are using a version of NodeJS that is not supported"); return; } else { require('./cmd')() diff --git a/cli/lib/cert-lib.js b/cli/lib/cert-lib.js index 6accb0341..53159a7e0 100644 --- a/cli/lib/cert-lib.js +++ b/cli/lib/cert-lib.js @@ -12,6 +12,8 @@ const debug = require('debug')('cert') //const prompt = require('cli-prompt'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway cert lib'; + const ERR_STORE_EXISTS = 'com.apigee.secure-store.storekey.already.exists'; const ERR_STORE_MISSING = 'com.apigee.secure-store.securestore_does_not_exist'; //const ERR_STORE_ITEM_MISSING = 'com.apigee.secure-store.storeitem_does_not_exist'; @@ -94,7 +96,7 @@ CertLogic.prototype.installPrivateCert = function(options, callback) { if (callback) { return callback(err); } else { - return writeConsoleLog('log',err, err.stack); + return writeConsoleLog('log', {component: CONSOLE_LOG_TAG_COMP},err, err.stack); } } @@ -112,9 +114,9 @@ CertLogic.prototype.installPrivateCert = function(options, callback) { deleteVault(generateCredentialsObject(options), managementUri, options.org, options.env, vaultName, cb); }, function(cb) { - writeConsoleLog('log','creating KVM'); - writeConsoleLog('log','adding private_key'); - writeConsoleLog('log','adding public_key'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'creating KVM'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding private_key'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding public_key'); var entries = [{ 'name': 'private_key', 'value': privateKey @@ -246,15 +248,15 @@ CertLogic.prototype.generateKeysWithPassword = function generateKeysWithPassword if (res.statusCode >= 200 && res.statusCode <= 202) { if (!res.body.region || !res.body.host) { if (cb) { - cb(writeConsoleLog('error','invalid response from region api', regionUrl, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text)); } else { - writeConsoleLog('error','invalid response from region api', regionUrl, res.text); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text); } return; } - writeConsoleLog('log','configuring host', res.body.host, 'for region', res.body.region); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'configuring host', res.body.host, 'for region', res.body.region); var bootstrapUrl = util.format(managementUri, 'bootstrap', options.org, options.env); var parsedUrl = url.parse(bootstrapUrl); parsedUrl.host = res.body.host; // update to regional host @@ -268,11 +270,11 @@ CertLogic.prototype.generateKeysWithPassword = function generateKeysWithPassword } else { - cb(writeConsoleLog('error','error retrieving region for org', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error retrieving region for org', res.statusCode, res.text)); } }); } else { - cb(writeConsoleLog('error','error uploading credentials', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error uploading credentials', res.statusCode, res.text)); } }); }); @@ -312,7 +314,7 @@ function createCert(cb) { } function deleteVault(credentials, managementUri, organization, environment, vaultName, cb) { - writeConsoleLog('log','deleting KVM'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deleting KVM'); var uri = util.format('%s/v1/organizations/%s/environments/%s/keyvaluemaps/%s', managementUri, organization, environment, vaultName); @@ -330,7 +332,6 @@ function deleteVault(credentials, managementUri, organization, environment, vaul }); - } function createVault(credentials, managementUri, organization, environment, vaultName, entries, cb) { @@ -430,9 +431,9 @@ function uploadCert(options, managementUri, vaultName, privateKey, publicKey, ca deleteVault(generateCredentialsObject(options), managementUri, options.org, options.env, vaultName, cb); }, function(cb) { - writeConsoleLog('log','creating KVM'); - writeConsoleLog('log','adding private_key'); - writeConsoleLog('log','adding public_key'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'creating KVM'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding private_key'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding public_key'); var entries = [{ 'name': 'private_key', 'value': privateKey diff --git a/cli/lib/cert.js b/cli/lib/cert.js index 11853246f..bccbe3445 100644 --- a/cli/lib/cert.js +++ b/cli/lib/cert.js @@ -13,6 +13,8 @@ const assert = require('assert') const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); +const CONSOLE_LOG_TAG_COMP = 'microgateway cert'; + const Cert = function() { }; @@ -37,9 +39,9 @@ Cert.prototype.installCert = function(options, cb) { cert(config).installCertWithPassword(options, (err, res) => { if (err) { if ( cb ) cb(err); - return writeConsoleLog('error', err, 'failed to update cert'); + return writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err, 'failed to update cert'); } - writeConsoleLog('log','installed cert'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'installed cert'); if ( cb ) cb(null,res); if ( !cb ) process.exit(0); }); @@ -69,9 +71,9 @@ Cert.prototype.checkCert = function(options, cb) { if ( cb ) { return cb(err); } - return writeConsoleLog('error', err, 'failed to update cert') + return writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err, 'failed to update cert') } - writeConsoleLog('log','checked cert successfully'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'checked cert successfully'); if ( cb ) cb(null,res); if ( !cb ) process.exit(0); }); @@ -87,12 +89,11 @@ Cert.prototype.deleteCert = function(options,cb) { assert(options.password || options.token,"password is required") - const config = edgeconfig.load({ source: configLocations.getSourcePath(options.org, options.env) }); cert(config).deleteCertWithPassword(options, function(err, msg) { - if ( err ) writeConsoleLog('error', err); - if ( msg ) writeConsoleLog('log',msg); + if ( err ) writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err); + if ( msg ) writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},msg); if ( cb ) cb(err,msg); if ( !cb ) process.exit(0); }) @@ -117,10 +118,10 @@ Cert.prototype.retrievePublicKey = function(options,cb) { cert(config).retrievePublicKey(options, (err, certificate) => { if (err) { if ( cb ) cb(err); - return writeConsoleLog('error', err, 'failed to retrieve public key') + return writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err, 'failed to retrieve public key') } - writeConsoleLog('log', 'succeeded'); - writeConsoleLog('log', certificate); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'succeeded'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, certificate); if ( cb ) cb(null,certificate); if ( !cb ) process.exit(0); }) @@ -135,24 +136,24 @@ Cert.prototype.retrievePublicKeyPrivate = function(options) { const config = edgeconfig.load({ source: configLocations.getSourcePath(options.org, options.env) }); cert(config).retrievePublicKeyPrivate((err, certificate) => { if (err) { - return writeConsoleLog('error', err, 'failed to retrieve public key') + return writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err, 'failed to retrieve public key') } - writeConsoleLog('log','succeeded'); - writeConsoleLog('log', certificate); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'succeeded'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, certificate); }) } /* function optionError(message) { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); this.help(); } function printError(err) { if (err.response) { - writeConsoleLog('log',err.response.error); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err.response.error); } else { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); } } */ diff --git a/cli/lib/configure.js b/cli/lib/configure.js index a3b90010b..2ed00f76e 100644 --- a/cli/lib/configure.js +++ b/cli/lib/configure.js @@ -16,7 +16,7 @@ const BUFFERSIZE = 10000; const BATCHSIZE = 500; const FLUSHINTERVAL = 5000; var defaultConfig ; - +const CONSOLE_LOG_TAG_COMP = 'microgateway configure'; @@ -36,7 +36,7 @@ module.exports = function () { Configure.prototype.configure = function configure(options, cb) { if (!fs.existsSync(configLocations.getDefaultPath(options.configDir))) { - writeConsoleLog('error',"Missing %s, Please run 'edgemicro init'",configLocations.getDefaultPath()) + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"Missing %s, Please run 'edgemicro init'",configLocations.getDefaultPath()) return cb("Please call edgemicro init first") } @@ -75,17 +75,17 @@ Configure.prototype.configure = function configure(options, cb) { const cache = configLocations.getCachePath(options.org, options.env); if (fs.existsSync(cache)) { fs.unlinkSync(cache); - //writeConsoleLog('log', 'deleted ' + cache); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'deleted ' + cache); } const targetPath = configLocations.getSourcePath(options.org, options.env); if (fs.existsSync(targetPath)) { fs.unlinkSync(targetPath); - //writeConsoleLog('log', 'deleted ' + targetPath); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'deleted ' + targetPath); } var configFileDirectory = options.configDir || configLocations.homeDir; - //writeConsoleLog('log', 'init config'); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'init config'); edgeconfig.init({ source: configLocations.getDefaultPath(options.configDir), targetDir: configFileDirectory, @@ -95,13 +95,13 @@ Configure.prototype.configure = function configure(options, cb) { options.deployed = false; deployAuth.checkDeployedProxies(options, (err, options) => { if (err) { - writeConsoleLog('error', err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err); if ( cb ) { cb(err) } else process.exit(1); return; } configureEdgemicroWithCreds(options, (err) => { if (err) { - writeConsoleLog('error', err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err); if ( cb ) { cb(err) } else process.exit(1); } if ( cb ) { cb(err) } else process.exit(0); @@ -125,13 +125,13 @@ function configureEdgemicroWithCreds(options, cb) { tasks.push( function (callback) { setTimeout(() => { - writeConsoleLog('log', 'checking org for existing KVM'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'checking org for existing KVM'); cert.checkCertWithPassword(options, function (err/*, certs */) { if (err) { - writeConsoleLog('log', 'error checking for cert. Installing new cert.'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'error checking for cert. Installing new cert.'); cert.installCertWithPassword(options, callback); } else { - writeConsoleLog('log', 'KVM already exists in your org'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'KVM already exists in your org'); cert.retrievePublicKey(options, callback); } }); @@ -151,7 +151,7 @@ function configureEdgemicroWithCreds(options, cb) { } assert(targetFile, 'must have an assigned target file') - // writeConsoleLog('log', 'updating agent configuration'); + // writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'updating agent configuration'); if (err) { return cb(err) @@ -191,30 +191,30 @@ function configureEdgemicroWithCreds(options, cb) { agentConfig['analytics']['flushInterval'] = FLUSHINTERVAL; } - writeConsoleLog('log'); - writeConsoleLog('log', 'saving configuration information to:', agentConfigPath); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'saving configuration information to:', agentConfigPath); edgeconfig.save(agentConfig, agentConfigPath); // if it didn't throw, save succeeded - writeConsoleLog('log'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); if (options.deployed === true) { - writeConsoleLog('log', 'vault info:\n', results[0]); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'vault info:\n', results[0]); } else { - writeConsoleLog('log', 'vault info:\n', results[1]); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'vault info:\n', results[1]); } - writeConsoleLog('log'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); - writeConsoleLog('log',keySecretMessage); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},keySecretMessage); const key = results[2] ? results[2].key : results[1].key; const secret = results[2] ? results[2].secret : results[1].secret; assert(key, 'must have a key'); assert(secret, 'must have a secret'); - writeConsoleLog('log', ' key:', key); - writeConsoleLog('log', ' secret:', secret); - writeConsoleLog('log'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, ' key:', key); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, ' secret:', secret); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); process.env.EDGEMICRO_KEY = key; process.env.EDGEMICRO_SECRET = secret; - writeConsoleLog('log', 'edgemicro configuration complete!'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'edgemicro configuration complete!'); setTimeout(cb, 50) }); } @@ -227,9 +227,9 @@ function addEnvVars(config) { /* function printError(err) { if (err.response) { - writeConsoleLog('log',err.response.error); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err.response.error); } else { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); } } */ diff --git a/cli/lib/deploy-auth.js b/cli/lib/deploy-auth.js index 58253802a..ac796dd47 100644 --- a/cli/lib/deploy-auth.js +++ b/cli/lib/deploy-auth.js @@ -16,8 +16,10 @@ const debug = require('debug')('edgemicro-auth') var exec = require('child_process').exec; const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway deploy auth'; + var run = function(cmd, cb) { - //writeConsoleLog('log','run %s',cmd) + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'run %s',cmd) // var child = exec(cmd, { maxBuffer: 1024 * 500 @@ -82,14 +84,14 @@ Deployment.prototype.deployWithLeanPayload = function deployWithLeanPayload(opti // copy bin folder into tmp tasks.push(function(cb) { - //writeConsoleLog('log','copy auth app into tmp dir'); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'copy auth app into tmp dir'); cpr(path.resolve(__dirname, '..', '..', 'node_modules', 'microgateway-edgeauth'), tmpDir.name, cb); }); // copy bin folder into tmp tasks.push(function(cb) { - //writeConsoleLog('log','copy config into tmp dir'); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'copy config into tmp dir'); cpr(path.resolve(__dirname, '..', '..', 'config'), tmpDir.name + '/config', cb); }); @@ -128,7 +130,7 @@ Deployment.prototype.deployWithLeanPayload = function deployWithLeanPayload(opti // checks for previously deployed edgemicro internal proxies Deployment.prototype.checkDeployedInternalProxies = function checkDeployedInternalProxies(options, cb) { - //writeConsoleLog('log','checking for previously deployed proxies') + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'checking for previously deployed proxies') const opts = { organization: options.org, api: 'edgemicro-internal', @@ -160,7 +162,7 @@ Deployment.prototype.checkDeployedInternalProxies = function checkDeployedIntern // checks for previously deployed edgemicro proxies Deployment.prototype.checkDeployedProxies = function checkDeployedProxies(options, cb) { - //writeConsoleLog('log','checking for previously deployed proxies') + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'checking for previously deployed proxies') const opts = { organization: options.org, api: 'edgemicro-auth', @@ -265,7 +267,7 @@ Deployment.prototype.deployProxyWithPassword = function deployProxyWithPassword( if (options.runtimeUrl) { setEdgeMicroInternalEndpoint(dir + "/apiproxy/policies/Authenticate-Call.xml", options.runtimeUrl); } - writeConsoleLog('log','Give me a minute or two... this can take a while...'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Give me a minute or two... this can take a while...'); apigeetool.deployProxy(opts, function(err) { if (err) { if (err.code === 'ECONNRESET' && err.message === 'socket hang up') { @@ -276,17 +278,17 @@ Deployment.prototype.deployProxyWithPassword = function deployProxyWithPassword( return callback(err); } else { - writeConsoleLog('log','App %s deployed.', options.proxyName); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'App %s deployed.', options.proxyName); callback(null, options.runtimeUrl ? authUri + '/publicKey' : util.format(authUri + '/publicKey', options.org, options.env)); } - //writeConsoleLog('log','App %s added to your org. Now adding resources.', options.proxyName); + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'App %s added to your org. Now adding resources.', options.proxyName); /* opts.password = options.password; // override a apigeetool side-effect bug installJavaCallout(managementUri, opts, function(err) { if (err) { return callback(err); } - writeConsoleLog('log','App %s deployed.', options.proxyName); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'App %s deployed.', options.proxyName); callback(null, options.runtimeUrl ? authUri + '/publicKey' : util.format(authUri + '/publicKey', options.org, options.env)); });*/ diff --git a/cli/lib/gateway.js b/cli/lib/gateway.js index 942fb257e..0a60c9959 100644 --- a/cli/lib/gateway.js +++ b/cli/lib/gateway.js @@ -20,6 +20,8 @@ const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); const Gateway = function() {}; +const CONSOLE_LOG_TAG_COMP = 'microgateway gateway'; + module.exports = function() { return new Gateway(); }; @@ -39,9 +41,9 @@ Gateway.prototype.start = (options,cb) => { //const self = this; try { fs.accessSync(ipcPath, fs.F_OK); - writeConsoleLog('error','Edgemicro seems to be already running.'); - writeConsoleLog('error','If the server is not running, it might because of incorrect shutdown of the prevous start.'); - writeConsoleLog('error','Try removing ' + ipcPath + ' and start again'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Edgemicro seems to be already running.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'If the server is not running, it might because of incorrect shutdown of the prevous start.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Try removing ' + ipcPath + ' and start again'); process.exit(1); } catch (e) { // Socket does not exist @@ -84,13 +86,13 @@ Gateway.prototype.start = (options,cb) => { edgeconfig.get(configOptions, (err, config) => { if (err) { const exists = fs.existsSync(cache); - writeConsoleLog('error',"failed to retieve config from gateway. continuing, will try cached copy.."); - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"failed to retieve config from gateway. continuing, will try cached copy.."); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if (!exists) { - writeConsoleLog('error','cache configuration ' + cache + ' does not exist. exiting.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'cache configuration ' + cache + ' does not exist. exiting.'); return; } else { - writeConsoleLog('log','using cached configuration from %s', cache); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'using cached configuration from %s', cache); config = edgeconfig.load({ source: cache }); @@ -139,20 +141,20 @@ Gateway.prototype.start = (options,cb) => { socket = new JsonSocket(socket); socket.on('message', (message) => { if (message.command === 'reload') { - writeConsoleLog('log','Received reload instruction. Proceeding to reload'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Received reload instruction. Proceeding to reload'); mgCluster.reload((msg) => { if ( typeof msg === 'string') { - writeConsoleLog('log',msg); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},msg); socket.sendMessage({ 'reloaded' : false, 'message' : msg }); } else { socket.sendMessage(true); - writeConsoleLog('log','Reload completed'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Reload completed'); } }); } else if (message.command === 'stop') { - console.log('Recieved stop instruction. Proceeding to stop'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Recieved stop instruction. Proceeding to stop'); mgCluster.terminate(() => { - writeConsoleLog('log','Stop completed'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Stop completed'); socket.sendMessage(true); process.exit(0); }); @@ -164,12 +166,12 @@ Gateway.prototype.start = (options,cb) => { }); mgCluster.run(); - writeConsoleLog('log','PROCESS PID : ' + process.pid); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'PROCESS PID : ' + process.pid); fs.appendFileSync(pidPath, process.pid); process.on('exit', () => { if (!isWin) { - writeConsoleLog('log','Removing the socket file as part of cleanup'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Removing the socket file as part of cleanup'); fs.unlinkSync(ipcPath); } fs.unlinkSync(pidPath); @@ -184,7 +186,7 @@ Gateway.prototype.start = (options,cb) => { }); process.on('uncaughtException',(err) => { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); debug('Caught Unhandled Exception:'); debug(err); process.exit(0); @@ -199,7 +201,7 @@ Gateway.prototype.start = (options,cb) => { //start the polling mechanism to look for config changes var reloadOnConfigChange = (oldConfig, cache, opts) => { - writeConsoleLog('log','Checking for change in configuration'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Checking for change in configuration'); if (configurl) opts.configurl = configurl; //var self = this; edgeconfig.get(opts, (err, newConfig) => { @@ -208,7 +210,7 @@ Gateway.prototype.start = (options,cb) => { } if (err) { // failed to check new config. so try to check again after pollInterval - writeConsoleLog('error','Failed to check for change in Config. Will retry after ' + pollInterval + ' seconds'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Failed to check for change in Config. Will retry after ' + pollInterval + ' seconds'); setTimeout(() => { reloadOnConfigChange(oldConfig, cache, opts); }, pollInterval * 1000); @@ -216,7 +218,7 @@ Gateway.prototype.start = (options,cb) => { pollInterval = config.edgemicro.config_change_poll_interval ? config.edgemicro.config_change_poll_interval : pollInterval; var isConfigChanged = hasConfigChanged(oldConfig, newConfig); if (isConfigChanged) { - writeConsoleLog('log','Configuration change detected. Saving new config and Initiating reload'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Configuration change detected. Saving new config and Initiating reload'); edgeconfig.save(newConfig, cache); clientSocket.sendMessage({ command: 'reload' @@ -236,7 +238,7 @@ Gateway.prototype.start = (options,cb) => { } if ( cb && (typeof cb === "function") ) { - writeConsoleLog('log',"Calling cb") + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Calling cb") cb(); } @@ -260,13 +262,13 @@ Gateway.prototype.reload = (options) => { }, (err, config) => { if (err) { const exists = fs.existsSync(cache); - writeConsoleLog('error',"failed to retieve config from gateway. continuing, will try cached copy.."); - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"failed to retieve config from gateway. continuing, will try cached copy.."); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if (!exists) { - writeConsoleLog('error','cache configuration ' + cache + ' does not exist. exiting.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'cache configuration ' + cache + ' does not exist. exiting.'); return; } else { - writeConsoleLog('log','using cached configuration from %s', cache); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'using cached configuration from %s', cache); config = edgeconfig.load({ source: cache }) @@ -280,9 +282,9 @@ Gateway.prototype.reload = (options) => { }); socket.on('message', (success) => { if (success) { - writeConsoleLog('log','Reload Completed Successfully'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Reload Completed Successfully'); } else { - writeConsoleLog('error','Reloading edgemicro was unsuccessful'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Reloading edgemicro was unsuccessful'); } process.exit(0); }); @@ -291,7 +293,7 @@ Gateway.prototype.reload = (options) => { socket.on('error', (error) => { if (error) { if (error.code === 'ENOENT') { - writeConsoleLog('error','edgemicro is not running.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'edgemicro is not running.'); } } }); @@ -307,9 +309,9 @@ Gateway.prototype.stop = ( /*options */ ) => { }); socket.on('message', (success) => { if (success) { - writeConsoleLog('log','Stop Completed Succesfully'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Stop Completed Succesfully'); } else { - writeConsoleLog('error','Stopping edgemicro was unsuccessful'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Stopping edgemicro was unsuccessful'); } process.exit(0); }); @@ -317,7 +319,7 @@ Gateway.prototype.stop = ( /*options */ ) => { socket.on('error', (error) => { if (error) { if (error.code === 'ENOENT') { - writeConsoleLog('error','edgemicro is not running.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'edgemicro is not running.'); } } }); @@ -331,14 +333,14 @@ Gateway.prototype.status = ( /* options */ ) => { command: 'status' }); socket.on('message', (result) => { - writeConsoleLog('log','edgemicro is running with ' + result + ' workers'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'edgemicro is running with ' + result + ' workers'); process.exit(0); }); }); socket.on('error', (error)=> { if (error) { if (error.code === 'ENOENT') { - writeConsoleLog('error','edgemicro is not running.'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'edgemicro is not running.'); process.exit(1); } } @@ -361,11 +363,11 @@ function hasConfigChanged(oldConfig, newConfig) { var diff = jsdiff.diffWords(JSON.stringify(oldConfig), JSON.stringify(newConfig)); diff.forEach(function(part) { if (part.added) - debug("Added->" + part.value); + {debug("Added->" + part.value);} else if (part.removed) - debug("Removed->" + part.value); + {debug("Removed->" + part.value);} else - debug("Unchanged->" + part.value); + {debug("Unchanged->" + part.value);} }); } return true; diff --git a/cli/lib/init.js b/cli/lib/init.js index 269e875c3..7ed654b32 100644 --- a/cli/lib/init.js +++ b/cli/lib/init.js @@ -4,6 +4,8 @@ const path = require('path'); const configLocations = require('../../config/locations'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway init'; + module.exports = function init(opts, cb) { if(typeof opts === 'function') { cb = opts; @@ -17,7 +19,7 @@ module.exports = function init(opts, cb) { fs.mkdir(destFileDir, () => { copyFile(srcFile, destFile, (err) => { - if ( err ) writeConsoleLog('log',"failed to init configpath file %s", err); + if ( err ) writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"failed to init configpath file %s", err); cb(err, destFile); }); }); diff --git a/cli/lib/key-gen.js b/cli/lib/key-gen.js index 503d2e039..442b82b74 100644 --- a/cli/lib/key-gen.js +++ b/cli/lib/key-gen.js @@ -13,6 +13,8 @@ const configLocations = require('../../config/locations'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); +const CONSOLE_LOG_TAG_COMP = 'microgateway key gen'; + const KeyGen = function() { }; @@ -40,12 +42,12 @@ KeyGen.prototype.revoke = function(options, cb) { }, function(err, res) { err = translateError(err, res); if (err) { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); return cb(err); } if (res.statusCode >= 200 && res.statusCode <= 202) { if (!res.body.region || !res.body.host) { - cb(writeConsoleLog('error','invalid response from region api', regionUrl, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text)); return; } else { const credentialUrl = util.format('https://%s/edgemicro/%s/organization/%s/environment/%s', res.body.host, 'credential', options.org, options.env); @@ -61,13 +63,13 @@ KeyGen.prototype.revoke = function(options, cb) { }, function(er, re) { er = translateError(er, re); if (er) { - writeConsoleLog('log',er); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},er); return cb(er); } if (res.statusCode >= 200 && res.statusCode <= 202) { - writeConsoleLog('log',"key " + options.key + " revoked successfully"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"key " + options.key + " revoked successfully"); } else { - writeConsoleLog('log',"revoking key " + options.key + " failed with reason code " + res.StatusCode) + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"revoking key " + options.key + " failed with reason code " + res.StatusCode) } }); } @@ -80,19 +82,19 @@ KeyGen.prototype.generate = function generate(options, cb) { this.baseUri = config.edge_config.baseUri; this._generate(options, (err, result) => { if(err){ - writeConsoleLog('error',"failed") - writeConsoleLog('error',err) + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"failed") + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err) cb(err); } - writeConsoleLog('info',config.edge_config.bootstrapMessage); - writeConsoleLog('info',' bootstrap:', result.bootstrap); - writeConsoleLog('log'); - writeConsoleLog('info',config.edge_config.keySecretMessage); - writeConsoleLog('info',' key:', result.key); - writeConsoleLog('info',' secret:', result.secret); - writeConsoleLog('log'); - writeConsoleLog('log','finished'); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},config.edge_config.bootstrapMessage); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},' bootstrap:', result.bootstrap); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},config.edge_config.keySecretMessage); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},' key:', result.key); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},' secret:', result.secret); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'finished'); cb(err,result) }); }; @@ -162,11 +164,11 @@ KeyGen.prototype._generate = function _generate(options, cb) { } if (res.statusCode >= 200 && res.statusCode <= 202) { if (!res.body.region || !res.body.host) { - cb(writeConsoleLog('error','invalid response from region api', regionUrl, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text)); return; } - writeConsoleLog('log','configuring host', res.body.host, 'for region', res.body.region); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'configuring host', res.body.host, 'for region', res.body.region); const bootstrapUrl = util.format(baseUri, 'bootstrap', options.org, options.env); const parsedUrl = url.parse(bootstrapUrl); parsedUrl.host = res.body.host; // update to regional host @@ -180,16 +182,15 @@ KeyGen.prototype._generate = function _generate(options, cb) { }); - } else { - cb(writeConsoleLog('error','error retrieving region for org', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error retrieving region for org', res.statusCode, res.text)); } }); } else { - cb(writeConsoleLog('error','error uploading credentials', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error uploading credentials', res.statusCode, res.text)); } }); diff --git a/cli/lib/private.js b/cli/lib/private.js index d30ba8301..ca34ad6de 100755 --- a/cli/lib/private.js +++ b/cli/lib/private.js @@ -20,6 +20,8 @@ const deploymentFx = require('./deploy-auth'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); +const CONSOLE_LOG_TAG_COMP = 'microgateway private'; + const DEFAULT_HOSTS = 'default,secure'; const Private = function() {}; @@ -31,7 +33,7 @@ module.exports = function() { // begins edgemicro configuration process Private.prototype.configureEdgemicro = function(options, cb) { if (!fs.existsSync(configLocations.getDefaultPath(options.configDir))) { - writeConsoleLog('error',"Missing %s, Please run 'edgemicro init'", configLocations.getDefaultPath(options.configDir)) + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"Missing %s, Please run 'edgemicro init'", configLocations.getDefaultPath(options.configDir)) return cb("Please call edgemicro init first") } @@ -46,16 +48,16 @@ Private.prototype.configureEdgemicro = function(options, cb) { assert(options.mgmtUrl, 'mgmtUrl is required'); const cache = configLocations.getCachePath(options.org, options.env); - writeConsoleLog('log','delete cache config'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'delete cache config'); if (fs.existsSync(cache)) { fs.unlinkSync(cache); - writeConsoleLog('log','deleted ' + cache); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deleted ' + cache); } const targetPath = configLocations.getSourcePath(options.org, options.env); if (fs.existsSync(targetPath)) { fs.unlinkSync(targetPath); - writeConsoleLog('log','deleted ' + targetPath); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deleted ' + targetPath); } options.proxyName = this.name = 'edgemicro-auth'; @@ -86,7 +88,7 @@ Private.prototype.configureEdgemicro = function(options, cb) { var configFileDirectory = options.configDir || configLocations.homeDir; const that = this; - writeConsoleLog('log','init config'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'init config'); edgeconfig.init({ source: configLocations.getDefaultPath(options.configDir), targetDir: configFileDirectory, @@ -98,19 +100,19 @@ Private.prototype.configureEdgemicro = function(options, cb) { options.internaldeployed = false; that.deployment.checkDeployedProxies(options, (err, options) => { if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if ( cb ) { cb(err) } else process.exit(1); return; } else { that.deployment.checkDeployedInternalProxies(options, (err, options) => { if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if ( cb ) { cb(err) } else process.exit(1); return; } else { that.configureEdgemicroWithCreds(options, (err) => { if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if ( cb ) { cb(err) } else process.exit(1); return; } @@ -180,7 +182,7 @@ Private.prototype.configureEdgeMicroInternalProxy = function configureEdgeMicroI function(parallelCb) { async.waterfall(calloutFlow, function(err /*, result */) { if (err) { - writeConsoleLog('log','error - editing apiproxy Callout.xml'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'error - editing apiproxy Callout.xml'); return parallelCb(err); } @@ -221,7 +223,7 @@ Private.prototype.configureEdgeMicroInternalProxy = function configureEdgeMicroI tasks.push(function(parallelCb) { async.waterfall(defaultFlow, function(err /*, result */) { if (err) { - writeConsoleLog('log','error - editing apiproxy default.xml'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'error - editing apiproxy default.xml'); return parallelCb(err); } @@ -249,42 +251,42 @@ Private.prototype.configureEdgemicroWithCreds = function configureEdgemicroWithC if (options.internaldeployed === false) { tasks.push(function(callback) { - writeConsoleLog('log','configuring edgemicro internal proxy'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'configuring edgemicro internal proxy'); that.configureEdgeMicroInternalProxy(options, callback); }); tasks.push(function(callback) { - writeConsoleLog('log','deploying edgemicro internal proxy'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deploying edgemicro internal proxy'); that.deployment.deployEdgeMicroInternalProxy(options, callback); }); } else { - writeConsoleLog('log','Proxy edgemicro-internal is already deployed'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Proxy edgemicro-internal is already deployed'); } if (options.deployed === false) { tasks.push(function(callback) { - writeConsoleLog('log','deploying ', that.name, ' app'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deploying ', that.name, ' app'); that.deployment.deployWithLeanPayload(options, callback); }); } else { - writeConsoleLog('log',that.name, ' is already deployed'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},that.name, ' is already deployed'); } tasks.push(function(callback) { - writeConsoleLog('log','checking org for existing KVM'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'checking org for existing KVM'); that.cert.checkPrivateCert(options, function(err /*, certs */ ) { if (err) { - writeConsoleLog('log','error checking for cert. Installing new cert.'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'error checking for cert. Installing new cert.'); that.cert.installCertWithPassword(options, callback); } else { - writeConsoleLog('log','KVM already exists in your org'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'KVM already exists in your org'); that.cert.retrievePublicKeyPrivate(callback); } }); }); tasks.push(function(callback) { - writeConsoleLog('log','generating keys'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'generating keys'); that.generateKeysWithPassword(options, callback); }); @@ -332,17 +334,17 @@ Private.prototype.configureEdgemicroWithCreds = function configureEdgemicroWithC } writeConsoleLog('log') - writeConsoleLog('log','saving configuration information to:', agentConfigPath); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'saving configuration information to:', agentConfigPath); edgeconfig.save(agentConfig, agentConfigPath); writeConsoleLog('log') if (options.internaldeployed === false && options.deployed === false) { - writeConsoleLog('log','vault info:\n', results[3]); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'vault info:\n', results[3]); } else if (options.internaldeployed === true && options.internaldeployed === false) { - writeConsoleLog('log','vault info:\n', results[1]); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'vault info:\n', results[1]); } - writeConsoleLog('log','edgemicro configuration complete!'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'edgemicro configuration complete!'); cb(); }); @@ -417,27 +419,27 @@ Private.prototype.generateKeysWithPassword = function generateKeysWithPassword(o if (res.statusCode >= 200 && res.statusCode <= 202) { if (!res.body.region || !res.body.host) { - cb(writeConsoleLog('error','invalid response from region api', regionUrl, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text)); return; } - writeConsoleLog('log','configuring host', res.body.host, 'for region', res.body.region); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'configuring host', res.body.host, 'for region', res.body.region); const parsedRes = url.parse(res.body.host); parsedUrl.host = parsedRes.host; // update to regional host - writeConsoleLog('log') - writeConsoleLog('info'that.config.edge_config.keySecretMessage); - writeConsoleLog('info',' key:', key); - writeConsoleLog('info',' secret:', secret); - writeConsoleLog('log') + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}) + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP}, that.config.edge_config.keySecretMessage); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},' key:', key); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},' secret:', secret); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}) process.env.EDGEMICRO_KEY = key; process.env.EDGEMICRO_SECRET = secret; return cb(null, updatedUrl); } else { - cb(writeConsoleLog('error','error retrieving region for org', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error retrieving region for org', res.statusCode, res.text)); } }); } else { - cb(writeConsoleLog('error','error uploading credentials', res.statusCode, res.text)); + cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error uploading credentials', res.statusCode, res.text)); } }); }); @@ -455,7 +457,7 @@ function translateError(err, res) { /* function optionError(message) { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); this.help(); } */ diff --git a/cli/lib/reload-cluster.js b/cli/lib/reload-cluster.js index da173f20e..075a28d22 100644 --- a/cli/lib/reload-cluster.js +++ b/cli/lib/reload-cluster.js @@ -4,6 +4,9 @@ var cluster = require('cluster'); var EventEmitter = require('events').EventEmitter; var cpuCount = require('os').cpus().length; const cache = require('microgateway-plugins').memored; +const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; + +const CONSOLE_LOG_TAG_COMP = 'microgateway reload cluster'; const PURGE_INTERVAL = 60000; // @@ -67,7 +70,7 @@ class ExitCounter { gCurrentExitCount = 0; this.add(prevExitCount) if ( this.averageRate() > this.theta ) { - console.log("EXPERIENCING HIGH RATE OF PROCESS EXITS") + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"EXPERIENCING HIGH RATE OF PROCESS EXITS") return(false) } return(true) @@ -194,7 +197,7 @@ class CallbackList { try { cb() } catch (e) { - console.log(e) + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},e) } }) // when done eliminate this.items = [] @@ -203,8 +206,6 @@ class CallbackList { } - - class WorkerInfo { // constructor(worker) { @@ -225,7 +226,7 @@ class WorkerInfo { var onset = this.trackingStartTime var diff = Date.now() - onset // - //if ( diff > MAX_CONNECT_FAIL_TIME ) console.log(`DISCONNECT TIMEOUT ${this.worker_key}`) + //if ( diff > MAX_CONNECT_FAIL_TIME ) writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},`DISCONNECT TIMEOUT ${this.worker_key}`) // return ( diff > MAX_CONNECT_FAIL_TIME ) } @@ -299,7 +300,6 @@ function untrackTrackedProcesses() { // clear out tracked processes and put the } - // --clearOutStoppedProcesses--------------------------------------- // --closePreconditions--------------------------------------- @@ -334,7 +334,7 @@ function cullProcesses() { w_info.request_disconnect = true } catch (e) { // might have never connected - //console.log(e) + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},e) } } else if ( !(w.isDead()) && !(w.isConnected()) ) { // The processes is no longer connected ... so kill it requestShutdownNow(w,w_info) @@ -378,7 +378,6 @@ function clearOutStoppedProcesses(cb,threshold) { } - function workersFullHouse(rlc) { var wantsmore = rlc.consonantProcesses(); var clearoutCount = Object.keys(tClosers).length @@ -396,7 +395,7 @@ function workersFullHouse(rlc) { },intrval) } else { while ( wantsmore ) { - //console.log(wantsmore) + //writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},wantsmore) rlc.requestNewWorker() wantsmore--; } @@ -405,7 +404,6 @@ function workersFullHouse(rlc) { } - // --healthCheck--------------------------------------- // Checks for processes that might be running and shouldn't be. And, will attempt to eliminate them. // Checks for processes that should be running and requests them. @@ -419,7 +417,6 @@ function healthCheck(rlc,special) { } - const readyCommand = 'ready'; class ClusterManager extends EventEmitter { @@ -729,7 +726,7 @@ class ClusterManager extends EventEmitter { this.emit('message', worker, message); }); worker.on('error',(e) => { - console.log(e.message) + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},e.message) }) // // When a worker exits remove the worker reference from workers array, which holds all the workers @@ -752,7 +749,6 @@ class ClusterManager extends EventEmitter { } - // ========================= Application methods ... (public) // // -----run-------------------------------------------------- diff --git a/cli/lib/rotate-key.js b/cli/lib/rotate-key.js index 3e5722aac..87682bbd1 100644 --- a/cli/lib/rotate-key.js +++ b/cli/lib/rotate-key.js @@ -8,6 +8,8 @@ const request = require("request"); const async = require('async'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway rotate key'; + function createCert(cb) { const options = { @@ -93,9 +95,9 @@ function updateNonCPSKVM(options, serviceKey, newCertificate, newPublicKey, oldP json: payload }, function(err, res /*, body */) { if (err || res.statusCode > 299) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); } else { - writeConsoleLog('log',"Key Rotation successfully completed!"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Key Rotation successfully completed!"); } }); } @@ -262,10 +264,10 @@ function updateCPSKVM(options, serviceKey, newCertificate, newPublicKey, oldPubl } ], function (err /*, results */) { if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); process.exit(1); } else { - writeConsoleLog('log',"Key Rotation successfully completed!"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Key Rotation successfully completed!"); } }); } @@ -289,7 +291,7 @@ RotateKey.prototype.rotatekey = function rotatekey(options /*, cb */) { function(cb) { var privateKeyURI = util.format("%s/v1/organizations/%s/environments/%s/keyvaluemaps/%s/entries/private_key", options.baseuri, options.org, options.env, options.kvm); - writeConsoleLog('log',"Checking if private key exists in the KVM..."); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Checking if private key exists in the KVM..."); request({ uri: privateKeyURI, auth: generateCredentialsObject(options), @@ -300,7 +302,7 @@ RotateKey.prototype.rotatekey = function rotatekey(options /*, cb */) { }); }, function(cb) { - writeConsoleLog('log',"Checking for certificate..."); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Checking for certificate..."); var publicKeyURI = util.format("https://%s-%s.apigee.net/edgemicro-auth/publicKey", options.org, options.env); request({ @@ -314,11 +316,11 @@ RotateKey.prototype.rotatekey = function rotatekey(options /*, cb */) { } ], function(err, results){ if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); process.exit(1); } else { var oldCertificate = results[1]; - writeConsoleLog('log',"Found Certificate"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Found Certificate"); //debug("Old Certificate: \n" + oldCertificate); async.series([ function(cb) { @@ -329,7 +331,7 @@ RotateKey.prototype.rotatekey = function rotatekey(options /*, cb */) { }, function(cb) { - writeConsoleLog('log',"Generating New key/cert pair..."); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Generating New key/cert pair..."); createCert(function(e, newkeys) { if (e) cb(e); else cb(null, newkeys); @@ -337,14 +339,14 @@ RotateKey.prototype.rotatekey = function rotatekey(options /*, cb */) { } ], function(e, res){ if (err) { - writeConsoleLog('error',e); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},e); process.exit(1); } else { - writeConsoleLog('log',"Extract new public key"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Extract new public key"); var newCertificate = res[1].certificate; pem.getPublicKey(newCertificate, function(ee, newkey) { if (ee) { - writeConsoleLog('error',ee); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},ee); process.exit(1); } else { debug("Checking for CPS..."); diff --git a/cli/lib/start-agent.js b/cli/lib/start-agent.js index f9065cfda..5a5157ff1 100644 --- a/cli/lib/start-agent.js +++ b/cli/lib/start-agent.js @@ -5,6 +5,8 @@ const agentConfig = require('../../lib/agent-config'); const assert = require('assert'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway start agent'; + var args; process.argv.forEach((val /*, index, array */) => { @@ -16,7 +18,7 @@ assert(argsJson); agentConfig(argsJson, (e) => { if (e) { - writeConsoleLog('error','edge micro failed to start', e); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'edge micro failed to start', e); return; } if (!cluster.isMaster) { @@ -27,6 +29,6 @@ agentConfig(argsJson, (e) => { } }); } else { - writeConsoleLog('log','edge micro started'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'edge micro started'); } }); \ No newline at end of file diff --git a/cli/lib/token.js b/cli/lib/token.js index 08fbdf7a1..b25d4ad4f 100644 --- a/cli/lib/token.js +++ b/cli/lib/token.js @@ -13,6 +13,8 @@ const configLocations = require('../../config/locations'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); +const CONSOLE_LOG_TAG_COMP = 'microgateway token'; + const Token = function() { }; @@ -25,10 +27,10 @@ Token.prototype.decodeToken = function( options ) { const token = fs.readFileSync(path.resolve(options.file), 'utf8').trim(); try{ const decodedJWT = jwt.decode(token, {complete:true}); - writeConsoleLog('log',decodedJWT); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},decodedJWT); return decodedJWT; }catch(err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); } } @@ -69,7 +71,7 @@ Token.prototype.verifyToken = function(options, cb) { cb(err) return printError(err); } - writeConsoleLog('log',result); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},result); cb(null,result) }); }); @@ -108,7 +110,7 @@ Token.prototype.getToken = function(options, cb) { if ( cb ) cb(err) return printError(err); } - writeConsoleLog('log',JSON.stringify(res.body, null, 2)); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},JSON.stringify(res.body, null, 2)); if ( cb ) cb(null, res.body); }); } @@ -128,15 +130,15 @@ function getPublicKey(organization, environment, authUri, isPublicCloud, cb) { function printError(err) { if (err.response) { - writeConsoleLog('log',err.response.error); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err.response.error); } else { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); } } /* function optionError(message) { - writeConsoleLog('error',message); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message); this.help(); } */ \ No newline at end of file diff --git a/cli/lib/upgrade-edgeauth.js b/cli/lib/upgrade-edgeauth.js index 69aebeed0..c118806b3 100644 --- a/cli/lib/upgrade-edgeauth.js +++ b/cli/lib/upgrade-edgeauth.js @@ -9,6 +9,8 @@ var deployAuth; const path = require('path'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway upgrade edgeauth'; + const UpgradeAuth = function() { } @@ -50,7 +52,7 @@ UpgradeAuth.prototype.upgradeauth = function upgradeauth(options /*, cb */) { deployAuth.deployProxyWithPassword(options.mgmtUrl, 'na', opts, opts.directory, function(err /*, result */ ) { if (err) { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); } }); diff --git a/cli/lib/upgrade-kvm.js b/cli/lib/upgrade-kvm.js index cfd63f015..9524a1920 100644 --- a/cli/lib/upgrade-kvm.js +++ b/cli/lib/upgrade-kvm.js @@ -6,6 +6,8 @@ const util = require("util"); const request = require("request"); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway upgrade kvm'; + function generateCredentialsObject(options) { if (options.token) { return { @@ -36,18 +38,18 @@ UpgradeKVM.prototype.upgradekvm = function upgradekvm(options, cb) { var publicKeyURI = util.format('https://%s-%s.apigee.net/edgemicro-auth/publicKey', options.org, options.env); - writeConsoleLog('log',"Checking for certificate..."); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Checking for certificate..."); request({ uri: publicKeyURI, auth: generateCredentialsObject(options), method: "GET" }, function(err, res, body) { if (err) { - writeConsoleLog('error',err); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); } else { - writeConsoleLog('log',"Certificate found!"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"Certificate found!"); pem.getPublicKey(body, function(err, publicKey) { - writeConsoleLog('log',publicKey.publicKey); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},publicKey.publicKey); var updatekvmuri = util.format("%s/v1/organizations/%s/environments/%s/keyvaluemaps/%s", options.baseuri, options.org, options.env, options.kvm); var payload = { @@ -78,9 +80,9 @@ UpgradeKVM.prototype.upgradekvm = function upgradekvm(options, cb) { if ( cb ) { cb(err) } else process.exit(1); return; } if (res.statusCode !== 200) { - writeConsoleLog('log',"error upgrading KVM: "+ res.statusCode); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"error upgrading KVM: "+ res.statusCode); } else { - writeConsoleLog('log',"KVM update complete"); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"KVM update complete"); process.exit(0); } }); diff --git a/cli/lib/verify.js b/cli/lib/verify.js index 0b8434c16..6f0166304 100644 --- a/cli/lib/verify.js +++ b/cli/lib/verify.js @@ -7,6 +7,8 @@ const async = require('async'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; edgeconfig.setConsoleLogger(writeConsoleLog); +const CONSOLE_LOG_TAG_COMP = 'microgateway verify'; + const configLocations = require('../../config/locations'); @@ -47,18 +49,18 @@ Verify.prototype.verify = function verify(options) { }, function (err, res) { if (err) { - writeConsoleLog('log','verifying analytics negative case: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics negative case: FAIL'); return cb(err); } if (res.statusCode === 401) { - writeConsoleLog('log','verifying analytics negative case: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics negative case: FAIL'); return cb(new Error('analytics up - got 401 Unauthorized. Invalid key/secret credentials.')); } else if (res.statusCode !== 500) { - writeConsoleLog('log','verifying analytics negative case: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics negative case: FAIL'); return cb(new Error('analytics up - got code: ' + res.statusCode)); } else { - writeConsoleLog('log','verifying analytics negative case: OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics negative case: OK'); cb(); } }); @@ -75,18 +77,18 @@ Verify.prototype.verify = function verify(options) { }, function (err, res/*, body */) { if (err) { - writeConsoleLog('log','verifying bootstrap url availability:FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying bootstrap url availability:FAIL'); return cb(err); } if (res.statusCode === 401) { - writeConsoleLog('log','verifying bootstrap url availability:FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying bootstrap url availability:FAIL'); return cb(new Error('bootstrap - got 401 Unauthorized. Invalid key/secret credentials.')); } else if (res.statusCode !== 200) { - writeConsoleLog('log','verifying bootstrap url availability:FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying bootstrap url availability:FAIL'); return cb(new Error('bootstrap - got code: ' + res.statusCode)) } else { - writeConsoleLog('log','verifying bootstrap url availability:OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying bootstrap url availability:OK'); cb(); } }) @@ -99,15 +101,15 @@ Verify.prototype.verify = function verify(options) { }, function (err, res/* , body */) { if (err) { - writeConsoleLog('log','verifying jwt_public_key availability: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying jwt_public_key availability: FAIL'); return cb(err); } if (res.statusCode !== 200) { - writeConsoleLog('log','verifying jwt_public_key availability: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying jwt_public_key availability: FAIL'); return cb(new Error('jwt - got code: ' + res.statusCode)); } else { - writeConsoleLog('log','verifying jwt_public_key availability: OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying jwt_public_key availability: OK'); cb(); } }); @@ -126,15 +128,15 @@ Verify.prototype.verify = function verify(options) { }, function (err, res/* , body */) { if (err) { - writeConsoleLog('log','verifying products availability: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying products availability: FAIL'); return cb(err); } if (res.statusCode !== 200) { - writeConsoleLog('log','verifying products availability: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying products availability: FAIL'); return cb(new Error('products - got code: ' + res.statusCode)); } else { - writeConsoleLog('log','verifying products availability: OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying products availability: OK'); cb(); } }); @@ -165,11 +167,11 @@ Verify.prototype.verify = function verify(options) { }); }, function (err) { if (err) { - writeConsoleLog('log','verifying quota with configured products: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying quota with configured products: FAIL'); cb(err); } - writeConsoleLog('log','verifying quota with configured products: OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying quota with configured products: OK'); cb(); }); }, @@ -204,18 +206,18 @@ Verify.prototype.verify = function verify(options) { }, function (err, res/* , body */) { if (err) { - writeConsoleLog('log','verifying analytics with payload: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: FAIL'); return cb(err); } if (res.statusCode === 401) { - writeConsoleLog('log','verifying analytics with payload: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: FAIL'); return cb(new Error('analytics synthetic - got 401 Unauthorized. Invalid key/secret credentials.')); } else if (res.statusCode !== 200) { - writeConsoleLog('log','verifying analytics with payload: FAIL'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: FAIL'); return cb(new Error('analytics synthetic - got code: ' + res.statusCode)); } else { - writeConsoleLog('log','verifying analytics with payload: OK'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: OK'); return cb(); } }) @@ -233,7 +235,7 @@ Verify.prototype.verify = function verify(options) { } downloadedConfig = config; async.series(tasks, function ( /*asyncErr, res */ ) { - writeConsoleLog('log','verification complete'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verification complete'); agent.close(process.exit); // close and stop agent }); }) @@ -243,8 +245,8 @@ Verify.prototype.verify = function verify(options) { } function printError(err) { if (err.response) { - writeConsoleLog('log',err.response.error); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err.response.error); } else { - writeConsoleLog('log',err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},err); } } diff --git a/cli/package.js b/cli/package.js index 211b1fb96..c67cdcab0 100755 --- a/cli/package.js +++ b/cli/package.js @@ -14,9 +14,11 @@ const parseArgs = require('minimist'); const yaml = require('js-yaml'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway package'; + const argv = parseArgs(process.argv.slice(2)); if (!argv.version) { - writeConsoleLog('error','Usage:', process.argv[1], '--version '); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Usage:', process.argv[1], '--version '); process.exit(1); } @@ -32,13 +34,13 @@ async.parallel([ function(cb) { available('tsc', cb); } ], function(err /*, results*/ ) { if (err) { - writeConsoleLog('error','gulp/tsc not found in path (try "npm i -g gulp typescript")'); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'gulp/tsc not found in path (try "npm i -g gulp typescript")'); process.exit(1); } }); process.chdir(buildDir); -writeConsoleLog('info','building package in', buildDir); +writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'building package in', buildDir); const topLevelFiles = [ 'build.yaml', @@ -106,17 +108,17 @@ async.series(tasks, function(err /*, results */) { }); function git_clone(repo, branch, callback) { - writeConsoleLog('log','git clone', repo); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'git clone', repo); exec( 'git clone ' + (branch ? '-b ' + branch + ' ': '') + '-q ' + repo, function(error, stdout, stderr) { - // writeConsoleLog('info',stdout); - writeConsoleLog('error',stderr); + // writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},stdout); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},stderr); if (error) { - writeConsoleLog('error','error cloning repo', repo, error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error cloning repo', repo, error); callback(error); } else { - writeConsoleLog('info','cloned', repo); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'cloned', repo); callback(); } } @@ -124,18 +126,18 @@ function git_clone(repo, branch, callback) { } function npm_install(dir, production, callback) { - writeConsoleLog('log','npm install', dir, production ? '(production)' : '(development)'); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'npm install', dir, production ? '(production)' : '(development)'); exec( 'npm install ' + (production ? '--production' : ''), {cwd: dir}, function(error, stdout, stderr) { - // writeConsoleLog('info',stdout); - writeConsoleLog('error',stderr); + // writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},stdout); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},stderr); if (error) { - writeConsoleLog('error','error running npm install in dir', dir, error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error running npm install in dir', dir, error); callback(error); } else { - writeConsoleLog('info','installed modules in', dir); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'installed modules in', dir); callback(); } } @@ -143,7 +145,7 @@ function npm_install(dir, production, callback) { } function build_properties(repo, callback) { - writeConsoleLog('log','creating build properties in', repo); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'creating build properties in', repo); const properties = {}; properties.version = argv.version; properties.date = (new Date()).toISOString(); @@ -151,9 +153,9 @@ function build_properties(repo, callback) { 'git show HEAD|grep commit|cut -f2 -d" "', {cwd: repo}, function(error, stdout, stderr) { - writeConsoleLog('error',stderr); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},stderr); if (error) { - writeConsoleLog('error','error running "git show HEAD" in repo', repo, error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error running "git show HEAD" in repo', repo, error); callback(error); } else { properties.head = stdout.trim(); @@ -167,7 +169,7 @@ function build_properties(repo, callback) { function update_version(repo, callback) { const pkgJSON = path.join(repo, 'package.json'); - writeConsoleLog('log','updating version in', pkgJSON); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'updating version in', pkgJSON); const pkg = JSON.parse(fs.readFileSync(pkgJSON)); pkg.version = argv.version; fs.writeFileSync(pkgJSON, JSON.stringify(pkg, null, 2)); @@ -176,14 +178,14 @@ function update_version(repo, callback) { function git_tag(repo, callback) { const tag = 'v' + argv.version; - writeConsoleLog('log','tagging repo', repo, 'with version', tag); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'tagging repo', repo, 'with version', tag); exec( 'git tag ' + tag, {cwd: repo}, function(error, stdout, stderr) { - writeConsoleLog('error',stderr); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},stderr); if (error) { - writeConsoleLog('error','error running "git tag" in repo', repo, error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error running "git tag" in repo', repo, error); callback(error); } else { callback(); @@ -193,18 +195,18 @@ function git_tag(repo, callback) { } function gulp(dir, target, callback) { - writeConsoleLog('log','gulp', target); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'gulp', target); exec( 'gulp ' + target, {cwd: dir}, function(error, stdout, stderr) { - // writeConsoleLog('info',stdout); - writeConsoleLog('error',stderr); + // writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},stdout); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},stderr); if (error) { - writeConsoleLog('error','error running gulp in dir', dir, error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error running gulp in dir', dir, error); callback(error); } else { - writeConsoleLog('info','compiled sources in', dir); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'compiled sources in', dir); callback(); } } @@ -214,7 +216,7 @@ function gulp(dir, target, callback) { function zip(zipfile, dir, dirs, excludes, callback) { const command = '/usr/bin/zip'; const args = ['-q', '--symlinks', '-r', zipfile].concat(dirs, '-x', excludes); - writeConsoleLog('log',dir, command, args.join(' ')); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},dir, command, args.join(' ')); const zip = spawn(command, args, { cwd: dir, stdio: 'inherit' @@ -222,10 +224,10 @@ function zip(zipfile, dir, dirs, excludes, callback) { zip.on('exit', function(error) { if (error) { - writeConsoleLog('error','error creating zip', error); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error creating zip', error); callback(error); } else { - writeConsoleLog('info','built', path.join(dir, zipfile)); + writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'built', path.join(dir, zipfile)); callback(); } }); @@ -234,7 +236,7 @@ function zip(zipfile, dir, dirs, excludes, callback) { function available(cmd, callback) { exec('/usr/bin/which ' + cmd, function(error, stdout /*, stderr */) { - // writeConsoleLog('info',error, stdout); + // writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},error, stdout); callback(error, stdout); } ); diff --git a/forever.js b/forever.js index d39cb539a..9bb86a8d6 100644 --- a/forever.js +++ b/forever.js @@ -2,11 +2,14 @@ var forever = require('forever-monitor'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; + +const CONSOLE_LOG_TAG_COMP = 'microgateway forever'; + try { var foreverOptions = require('./forever.json'); } catch (err) { - writeConsoleLog('error',err); - writeConsoleLog('log',"using default forever options"); + writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"using default forever options"); var foreverOptions = { max: 3, silent: false, killTree: true, minUptime: 2000 }; } diff --git a/lib/plugins.js b/lib/plugins.js index 54024e942..a67db90d1 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -9,6 +9,8 @@ const builtInPlugins = require('microgateway-plugins'); //const yaml = require('js-yaml'); const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; +const CONSOLE_LOG_TAG_COMP = 'microgateway plugins'; + /** *load plugins into the gateway */ @@ -21,12 +23,12 @@ const Plugins = function (gateway, pluginDir, config) { function normalizePluginDir(pluginDir) { var pluginCandidate = null; if (pluginDir) { - writeConsoleLog('log','using pluginDir') + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'using pluginDir') if (!fs.existsSync(pluginDir)) { - writeConsoleLog('log','pluginDir %s does not exist', pluginDir); + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'pluginDir %s does not exist', pluginDir); } else { pluginCandidate = path.normalize(pluginDir); - writeConsoleLog('log','using plugin dir %s', pluginCandidate) + writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'using plugin dir %s', pluginCandidate) } } return pluginCandidate; @@ -84,7 +86,7 @@ function _reorderPlugins(dirs, config) { dirs.splice(index, 1); return true; } else { - writeConsoleLog('warn','sequenced plugin not found:', plugin); + writeConsoleLog('warn',{component: CONSOLE_LOG_TAG_COMP},'sequenced plugin not found:', plugin); return false; } }); diff --git a/lib/server.js b/lib/server.js index 016b96843..03aee5ee1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -13,6 +13,8 @@ const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog; const Agent = function () { }; +const CONSOLE_LOG_TAG_COMP = 'microgateway server'; + module.exports = function () { return new Agent(); }; @@ -65,7 +67,7 @@ Agent.prototype.start = function start(token,pluginDir, config, cb) { // The gateway module loads plugins and then returns the microgateway-core instance. // this.gatewayServer.start((err) => { - if ( err ) writeConsoleLog('error',err); + if ( err ) writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},err); if ( cb ) cb(null, config); });