From ddd5319bdf9e87f6b1e97288074e51def22ca20d Mon Sep 17 00:00:00 2001 From: Unitech Date: Thu, 7 Jul 2016 22:47:06 +0200 Subject: [PATCH] enhance pm2-docker command - allow to pass keymetrics options --- CHANGELOG.md | 1 + bin/pm2-docker | 77 ++++++++++++++++++++++++++++---------------------- lib/CLI/Log.js | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e210e800..06596904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ pm2.interact(opts, cb) - Startup performance improvement (better performance on ARM) - Startup performance improvement via require path caching - New pm2-docker global command (auto-exit function, log features) +- $ pm2-docker run examples/child.js --timestamp --secret 123213 --public 123213 --machine-name "my-machine-1" - pm2-dev now runs in a different PM2 process - Keymetrics agents can now be attached to each different PM2 instance diff --git a/bin/pm2-docker b/bin/pm2-docker index 62fb3283..584c19e3 100755 --- a/bin/pm2-docker +++ b/bin/pm2-docker @@ -15,45 +15,77 @@ var moment = require('moment'); var Common = require('../lib/Common'); var chalk = require('chalk'); var path = require('path'); +var pm2; process.env.PM2_SILENT = 'true'; commander.version(pkg.version) .option('--raw', 'raw log output') + .option('--secret [key]', 'keymetrics secret key') + .option('--public [key]', 'keymetrics public key') + .option('--machine-name [name]', 'keymetrics machine name') .option('--auto-exit', 'exit if all processes are errored/stopped or 0 apps launched') .option('--timestamp [format]', 'add timestamps to logs') + .option('--color', 'colored output') .usage('[cmd] app'); -var pm2 = new PM2.custom({ - pm2_home : path.join(process.env.HOME, '.pm2') -}); +commander.command('*') + .description('run in development mode') + .action(function(cmd, opts){ + console.error('Nothing to do'); + }); -pm2.connect(function() { - commander.parse(process.argv); -}); +// @todo need to allow passing same option than pm2 start +commander.command('run ') + .alias('start') + .description('run in development mode') + .action(function(cmd, opts) { + + pm2 = new PM2.custom({ + pm2_home : path.join(process.env.HOME, '.pm2'), + secret_key : commander.secret, + public_key : commander.public, + machine_name : commander.machineName, + daemon_mode : true + }); + + if (commander.autoExit) + autoExit(); + run(cmd, opts); + }); + +if (process.argv.length == 2) { + commander.outputHelp(); + process.exit(1); +} + +commander.parse(process.argv); process.on('SIGINT', function() { exitPM2(); }); function run(cmd, opts) { - var needRaw = opts.parent.rawArgs.indexOf('--raw') > -1 || false; + var needRaw = commander.raw; var timestamp = opts.parent.timestamp; if (timestamp === true) timestamp = 'YYYY-MM-DD-HH:mm:ss'; pm2.start(cmd, commander, function(err, obj) { - Log.stream(pm2.Client, 'all', needRaw, timestamp, false); + if (err) + throw new Error(err.message); + if (commander.color) + Log.stream(pm2.Client, 'all', needRaw, timestamp, false); + else + Log.rawStream(pm2.Client, 'all', needRaw, timestamp, false); }); } function exitPM2() { console.log('Exiting PM2'); - pm2.delete('all', function() { - pm2.disconnect(function() { - process.exit(0); - }); + pm2.kill(function() { + process.exit(0); }); } @@ -82,24 +114,3 @@ function autoExit() { }); }, 3000); } - -commander.command('*') - .description('run in development mode') - .action(function(cmd, opts){ - run(cmd, opts); - }); - -// @todo need to allow passing same option than pm2 start -commander.command('run ') - .alias('start') - .description('run in development mode') - .action(function(cmd, opts) { - if (commander.autoExit) - autoExit(); - run(cmd, opts); - }); - -if (process.argv.length == 2) { - commander.outputHelp(); - process.exit(1); -} diff --git a/lib/CLI/Log.js b/lib/CLI/Log.js index 72d004e6..d3aa6b1f 100644 --- a/lib/CLI/Log.js +++ b/lib/CLI/Log.js @@ -126,3 +126,55 @@ Log.stream = function(Client, id, raw, timestamp, exclusive) { }); }); }; + +Log.rawStream = function(Client, id, raw, timestamp, exclusive) { + var that = this; + + Client.launchBus(function(err, bus) { + + bus.on('log:*', function(type, packet) { + if (id !== 'all' + && packet.process.name != id + && packet.process.pm_id != id) + return; + + if ((type === 'out' && exclusive === 'err') + || (type === 'err' && exclusive === 'out') + || (type === 'PM2' && exclusive !== false)) + return; + + if (type === 'PM2' && raw) + return; + + var name = packet.process.name + '-' + packet.process.pm_id; + + var lines; + + if (typeof(packet.data) === 'string') + lines = (packet.data || '').split('\n'); + else + return; + + lines.forEach(function(line) { + if (!line) { + return; + } + + if (!raw) { + if (timestamp) + process.stdout.write('time=' + moment().format(timestamp) + ' '); + if (packet.process.name === 'PM2') + process.stdout.write('app=pm2 '); + if (packet.process.name !== 'PM2') + process.stdout.write('app=' + packet.process.name + ' id=' + packet.process.pm_id + ' '); + if (type === 'out') + process.stdout.write('type=out '); + else if (type === 'err') + process.stdout.write('type=error '); + } + + process.stdout.write(util.format(line) + '\n'); + }); + }); + }); +};