2014-01-14 19:30:36 +01:00
var commander = require ( 'commander' ) ;
var fs = require ( 'fs' ) ;
2014-03-07 16:08:19 +00:00
var path = require ( 'path' ) ;
2014-01-14 19:30:36 +01:00
var util = require ( 'util' ) ;
var async = require ( 'async' ) ;
var Monit = require ( './Monit' ) ;
var UX = require ( './CliUx' ) ;
var Log = require ( './Log' ) ;
var Satan = require ( './Satan' ) ;
var Common = require ( './Common' ) ;
var cst = require ( '../constants.js' ) ;
var pkg = require ( '../package.json' ) ;
var extItps = require ( './interpreter.json' ) ;
2014-01-11 18:48:41 +01:00
var InteractorDaemonizer = require ( './InteractorDaemonizer' ) ;
2014-03-07 16:08:19 +00:00
var p = path ;
2014-01-11 18:48:41 +01:00
2013-09-28 17:33:46 +02:00
var CLI = module . exports = { } ;
2013-10-16 14:35:38 +02:00
require ( 'colors' ) ;
2013-09-28 17:33:46 +02:00
2014-01-14 19:30:36 +01:00
/**
* Method to start a script
2014-06-13 16:11:43 +02:00
* @method startFile
2014-01-14 19:30:36 +01:00
* @param {string} script script name (will be resolved according to location)
2014-06-13 16:24:16 +02:00
* @return
2014-01-14 19:30:36 +01:00
*/
2013-09-28 17:33:46 +02:00
CLI . startFile = function ( script ) {
var appConf = {
script : script ,
name : p . basename ( script , '.js' )
} ;
2014-02-19 19:04:10 +00:00
if ( commander . nodeArgs )
appConf [ 'nodeArgs' ] = commander . nodeArgs ;
2013-09-28 17:33:46 +02:00
if ( commander . name )
2013-10-15 13:11:05 +02:00
appConf [ 'name' ] = commander . name ;
2013-09-28 17:33:46 +02:00
if ( commander . instances )
2013-11-13 15:10:59 +01:00
appConf [ 'instances' ] = commander . instances ;
2013-09-28 17:33:46 +02:00
if ( commander . error )
2013-10-15 13:11:05 +02:00
appConf [ 'error_file' ] = commander . error ;
2013-09-28 17:33:46 +02:00
if ( commander . output )
2013-10-15 13:11:05 +02:00
appConf [ 'out_file' ] = commander . output ;
2013-09-28 17:33:46 +02:00
if ( commander . pid )
2013-10-15 13:11:05 +02:00
appConf [ 'pid_file' ] = commander . pid ;
2013-09-28 17:33:46 +02:00
if ( commander . cron )
2013-10-15 13:11:05 +02:00
appConf [ 'cron_restart' ] = commander . cron ;
2014-04-11 18:22:36 +08:00
if ( commander . mergeLogs )
appConf [ 'merge_logs' ] = true ;
2014-04-04 12:26:07 +08:00
if ( commander . watch )
appConf [ 'watch' ] = true ;
2014-03-03 19:22:49 +00:00
if ( commander . runAsUser )
appConf [ 'run_as_user' ] = commander . runAsUser ;
if ( commander . runAsGroup )
appConf [ 'run_as_group' ] = commander . runAsGroup ;
2013-11-13 15:10:59 +01:00
2014-06-10 18:00:38 +02:00
if ( commander . executeCommand ) {
2013-10-15 13:11:05 +02:00
appConf [ 'exec_mode' ] = 'fork_mode' ;
2013-11-13 15:10:59 +01:00
2014-06-10 18:00:38 +02:00
if ( commander . interpreter )
appConf [ 'exec_interpreter' ] = commander . interpreter ;
else if ( extItps [ path . extname ( script ) ] )
appConf [ 'exec_interpreter' ] = extItps [ path . extname ( script ) ] ;
else
appConf [ 'exec_interpreter' ] = 'none' ;
2013-11-22 13:27:40 +01:00
}
2014-06-10 18:00:38 +02:00
else {
appConf [ 'exec_mode' ] = 'cluster_mode' ;
2013-11-22 13:27:40 +01:00
appConf [ 'exec_interpreter' ] = 'node' ;
2014-06-10 18:00:38 +02:00
}
2013-11-22 13:27:40 +01:00
2013-10-15 13:11:05 +02:00
if ( commander . startOneTime )
appConf [ 'one_launch_only' ] = cst . ONE _LAUNCH _STATUS ;
2013-10-30 17:10:58 +01:00
// if (appConf['exec_mode'] == 'cluster_mode' && process.version.match(/0.10/)) {
2014-04-13 15:05:19 +08:00
// printOut(cst.PREFIX_MSG_ERR + ' [Warning], you\'re using the 0.10.x node version, it\'s prefered that you switch to fork mode by adding the -x parameter.');
2013-10-30 17:10:58 +01:00
// }
2013-11-13 15:10:59 +01:00
2013-09-28 17:33:46 +02:00
// Script arguments
2013-09-29 18:39:41 +02:00
var env = commander . rawArgs . indexOf ( '--' ) + 1 ;
if ( env > 1 )
appConf [ 'args' ] = JSON . stringify ( commander . rawArgs . slice ( env , commander . rawArgs . length ) ) ;
2013-10-27 15:43:32 +01:00
2013-09-28 17:33:46 +02:00
if ( commander . write ) {
var dst _path = path . join ( process . env . PWD , path . basename ( script , '.js' ) + '-pm2.json' ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Writing configuration to ' , dst _path ) ;
2013-09-28 17:33:46 +02:00
fs . writeFileSync ( dst _path , JSON . stringify ( appConf ) ) ;
}
2013-11-13 15:10:59 +01:00
2014-02-13 13:55:50 +01:00
/*
* Re start script name that is already launched
*/
2013-10-15 13:11:05 +02:00
Satan . executeRemote ( 'findByFullPath' , path . resolve ( process . cwd ( ) , script ) , function ( err , exec ) {
2014-02-13 13:55:50 +01:00
if ( exec &&
( exec [ 0 ] . pm2 _env . status == cst . STOPPED _STATUS ||
exec [ 0 ] . pm2 _env . status == cst . STOPPING _STATUS ) ) {
2013-10-15 13:11:05 +02:00
var app _name = exec [ 0 ] . pm2 _env . name ;
Satan . executeRemote ( 'restartProcessName' , app _name , function ( err , list ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process successfully started' ) ;
2013-10-15 13:11:05 +02:00
return speedList ( ) ;
} ) ;
return false ;
}
else if ( exec && ! commander . force ) {
2013-09-28 17:33:46 +02:00
console . error ( cst . PREFIX _MSG _ERR + 'Script already launched, add -f option to force re execution' ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-06-10 18:00:38 +02:00
Satan . executeRemote ( 'prepare' , resolvePaths ( appConf ) , function ( err ) {
if ( err ) {
console . error ( cst . PREFIX _MSG _ERR + 'Error while launching application' , err . stack || err ) ;
return speedList ( ) ;
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process launched' ) ;
2014-06-10 18:00:38 +02:00
return speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ) ;
2014-01-10 18:04:03 +01:00
return false ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Get version of the daemonized PM2
2014-06-13 16:11:43 +02:00
* @method getVersion
2014-06-13 16:24:16 +02:00
* @callback cb
2014-06-13 16:11:43 +02:00
*/
2014-05-16 14:43:49 +02:00
CLI . getVersion = function ( cb ) {
Satan . executeRemote ( 'getVersion' , { } , function ( err , version ) {
cb ? cb . apply ( null , arguments ) : exitCli ( cst . SUCCESS _EXIT ) ;
} ) ;
} ;
2014-01-29 13:43:18 +01:00
/**
2014-06-13 16:24:16 +02:00
* Apply a RPC method on the json file
2014-06-13 16:11:43 +02:00
* @method actionFromJson
2014-01-29 13:43:18 +01:00
* @param {string} action RPC Method
2014-06-13 16:24:16 +02:00
* @param {string} JSON file
* @param {string} jsonVia action type
2014-01-29 13:43:18 +01:00
*/
2014-01-29 17:49:08 -06:00
CLI . actionFromJson = function ( action , file , jsonVia ) {
if ( jsonVia == 'pipe' )
var appConf = JSON . parse ( file ) ;
else {
var data = fs . readFileSync ( file ) ;
var appConf = JSON . parse ( data ) ;
}
2014-01-29 13:43:18 +01:00
if ( ! Array . isArray ( appConf ) ) appConf = [ appConf ] ; //convert to array
async . eachLimit ( appConf , cst . CONCURRENT _ACTIONS , function ( proc , next ) {
var name ;
if ( ! proc . name )
name = p . basename ( proc . script ) ;
else
name = proc . name ;
Satan . executeRemote ( action , name , function ( err , list ) {
if ( err )
console . error ( err ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Stopping process by name ' + name ) ;
2014-01-29 13:43:18 +01:00
next ( ) ;
} ) ;
} , function ( err ) {
if ( err ) {
2014-04-13 15:05:19 +08:00
printOut ( err ) ;
2014-01-29 13:43:18 +01:00
return exitCli ( cst . ERROR _EXIT ) ;
}
return setTimeout ( speedList , 800 ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Process and start a JSON file
2014-06-13 16:11:43 +02:00
* @method startFromJson
2014-06-13 16:24:16 +02:00
* @param {string} cmd
* @param {string} jsonVia
2014-06-13 16:11:43 +02:00
*/
2014-01-29 17:49:08 -06:00
CLI . startFromJson = function ( cmd , jsonVia ) {
2014-04-04 12:26:07 +08:00
var appConf ;
2014-01-29 17:49:08 -06:00
if ( jsonVia == 'pipe' )
2014-04-04 12:26:07 +08:00
appConf = JSON . parse ( cmd ) ;
2014-01-29 17:49:08 -06:00
else {
var data = fs . readFileSync ( cmd ) ;
2014-04-04 12:26:07 +08:00
appConf = JSON . parse ( data ) ;
2014-01-29 17:49:08 -06:00
}
2013-09-28 17:33:46 +02:00
2013-10-27 14:16:58 +01:00
if ( ! Array . isArray ( appConf ) ) appConf = [ appConf ] ; //convert to array
2014-01-11 18:48:41 +01:00
( function ex ( apps ) {
if ( apps . length == 0 ) return speedList ( ) ;
var appPaths = resolvePaths ( apps [ 0 ] ) ;
2013-10-27 14:16:58 +01:00
2014-04-12 15:39:58 +02:00
if ( commander . watch )
appPaths . watch = true ;
2014-01-11 18:48:41 +01:00
var rpcCall = 'findByScript' ;
var rpcArg = p . basename ( appPaths . script ) ;
2013-10-27 14:16:58 +01:00
2014-01-11 18:48:41 +01:00
//find script by port
2014-04-04 12:26:07 +08:00
if ( appPaths . port ) {
2014-01-11 18:48:41 +01:00
rpcCall = 'findByPort' ;
rpcArg = appPaths . port ;
}
2013-10-27 14:16:58 +01:00
2014-01-11 18:48:41 +01:00
Satan . executeRemote ( rpcCall , rpcArg , function ( err , exec ) {
if ( exec && ! commander . force ) {
console . error ( cst . PREFIX _MSG + 'Script already launched, add -f option to force re execution' ) ;
nextApp ( ) ;
return false ;
2014-04-04 12:26:07 +08:00
} else {
2014-01-11 18:48:41 +01:00
launchApp ( appPaths ) ;
return false ;
2013-10-27 14:16:58 +01:00
}
2014-01-11 18:48:41 +01:00
} ) ;
2013-10-27 14:16:58 +01:00
2014-06-13 16:11:43 +02:00
/**
* Description
* @method launchApp
* @param {} appPaths
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-01-11 18:48:41 +01:00
function launchApp ( appPaths ) {
2014-06-10 18:00:38 +02:00
Satan . executeRemote ( 'prepare' , appPaths , function ( err ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process launched' ) ;
2014-01-11 18:48:41 +01:00
nextApp ( ) ;
2013-10-27 14:16:58 +01:00
} ) ;
2014-01-11 18:48:41 +01:00
}
2013-10-27 14:16:58 +01:00
2014-06-13 16:11:43 +02:00
/**
* Description
* @method nextApp
* @return CallExpression
*/
2014-01-11 18:48:41 +01:00
function nextApp ( ) {
apps . shift ( ) ;
return ex ( apps ) ;
}
2013-10-27 14:16:58 +01:00
2014-01-11 18:48:41 +01:00
return false ;
} ) ( appConf ) ;
} ;
2013-09-28 17:33:46 +02:00
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Startup script generation
2014-06-13 16:11:43 +02:00
* @method startup
2014-06-13 16:24:16 +02:00
* @param {string} platform type (centos|redhat|amazon|systemd)
2014-06-13 16:11:43 +02:00
*/
2013-10-16 14:05:55 +02:00
CLI . startup = function ( platform ) {
2013-10-16 14:35:38 +02:00
var exec = require ( 'child_process' ) . exec ;
2013-09-28 17:33:46 +02:00
if ( process . getuid ( ) != 0 ) {
2013-10-16 14:35:38 +02:00
exec ( 'whoami' , function ( err , stdout , stderr ) {
console . error ( cst . PREFIX _MSG + 'You have to run this command as root' ) ;
console . error ( cst . PREFIX _MSG + 'Execute the following command :' ) ;
if ( platform === undefined ) platform = '' ;
2013-12-26 14:22:00 +09:00
console . error ( cst . PREFIX _MSG + 'sudo env PATH=$PATH:' + p . dirname ( process . execPath ) + ' pm2 startup ' + platform + ' -u ' + stdout . trim ( ) ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-10-16 14:35:38 +02:00
} ) ;
return ;
}
2013-11-13 15:10:59 +01:00
2013-10-16 14:05:55 +02:00
var INIT _SCRIPT = "/etc/init.d/pm2-init.sh" ;
2014-01-27 17:07:24 +01:00
var script ;
2014-03-06 17:23:31 -03:00
if ( platform == 'systemd' ) {
script = fs . readFileSync ( path . join ( _ _dirname , cst . SYSTEMD _STARTUP _SCRIPT ) ) ;
INIT _SCRIPT = '/etc/systemd/system/pm2.service' ;
}
else if ( platform == 'centos' || platform == 'redhat' )
2014-01-27 17:07:24 +01:00
script = fs . readFileSync ( path . join ( _ _dirname , cst . CENTOS _STARTUP _SCRIPT ) ) ;
2014-04-07 22:31:10 -04:00
else if ( platform == 'amazon' )
script = fs . readFileSync ( path . join ( _ _dirname , cst . AMAZON _STARTUP _SCRIPT ) ) ;
2014-01-27 17:07:24 +01:00
else
script = fs . readFileSync ( path . join ( _ _dirname , cst . UBUNTU _STARTUP _SCRIPT ) ) ;
2013-11-13 15:10:59 +01:00
2014-03-06 17:23:31 -03:00
var user = commander . user || 'root' ;
2013-09-28 17:33:46 +02:00
script = script . toString ( ) . replace ( /%PM2_PATH%/g , process . mainModule . filename ) ;
script = script . toString ( ) . replace ( /%HOME_PATH%/g , process . env . HOME ) ;
2014-01-15 23:37:17 +01:00
script = script . toString ( ) . replace ( /%NODE_PATH%/g , p . dirname ( process . execPath ) ) ;
2014-03-06 17:23:31 -03:00
script = script . toString ( ) . replace ( /%USER%/g , user ) ;
2013-11-13 15:10:59 +01:00
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Generating system init script in ' + INIT _SCRIPT ) ;
2014-01-10 17:09:34 +01:00
2013-09-28 17:33:46 +02:00
fs . writeFileSync ( INIT _SCRIPT , script ) ;
2013-11-13 15:10:59 +01:00
if ( fs . existsSync ( INIT _SCRIPT ) == false ) {
2014-04-13 15:05:19 +08:00
printOut ( script ) ;
printOut ( cst . PREFIX _MSG _ERR + ' There is a problem when trying to write file : ' + INIT _SCRIPT ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-10-16 14:05:55 +02:00
}
2013-11-13 15:10:59 +01:00
2013-09-28 17:33:46 +02:00
var cmd ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Making script booting at startup...' ) ;
2014-01-27 17:07:24 +01:00
2014-03-06 17:23:31 -03:00
if ( platform == 'systemd' ) {
cmd = [
'sudo -Ei -u ' + user + ' pm2 dump' , //We need an empty dump so that the first resurrect works correctly
'pm2 kill' ,
'systemctl daemon-reload' ,
2014-03-06 18:01:44 -03:00
'systemctl enable pm2' ,
2014-03-06 17:23:31 -03:00
'systemctl start pm2'
] . join ( ' && ' ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + '-systemd- Using the command %s' , cmd ) ;
2014-03-06 17:23:31 -03:00
}
2014-04-07 22:31:10 -04:00
else if ( platform == 'centos' || platform == 'redhat' || platform == 'amazon' ) {
2014-01-27 17:07:24 +01:00
cmd = 'chmod +x ' + INIT _SCRIPT + '; chkconfig --add ' + p . basename ( INIT _SCRIPT ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + '-centos- Using the command %s' , cmd ) ;
2014-01-27 17:07:24 +01:00
fs . openSync ( '/var/lock/subsys/pm2-init.sh' , 'w' ) ;
2014-04-13 15:05:19 +08:00
printOut ( '/var/lock/subsys/pm2-init.sh lockfile has been added' ) ;
2014-01-10 17:09:34 +01:00
}
else {
2013-09-28 17:33:46 +02:00
cmd = 'chmod +x ' + INIT _SCRIPT + '; update-rc.d ' + p . basename ( INIT _SCRIPT ) + ' defaults' ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + '-ubuntu- Using the command %s' , cmd ) ;
2014-01-10 17:09:34 +01:00
}
2013-09-28 17:33:46 +02:00
exec ( cmd , function ( err , stdo , stde ) {
if ( err ) {
console . error ( err ) ;
2014-04-13 15:05:19 +08:00
printOut ( '----- Are you sure you use the right platform command line option ? centos / redhat, amazon, ubuntu or systemd?' ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-04-13 15:05:19 +08:00
printOut ( stdo ) ;
printOut ( cst . PREFIX _MSG + 'Done.' ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2013-09-28 17:33:46 +02:00
} ) ;
2014-01-11 18:48:41 +01:00
} ;
2014-05-07 18:27:36 +02:00
/**
2014-06-13 16:24:16 +02:00
* Launch interactor
2014-06-13 16:11:43 +02:00
* @method interact
2014-06-13 16:24:16 +02:00
* @param {string} secret_key
* @param {string} public_key
* @param {string} machine_name
2014-05-07 18:27:36 +02:00
*/
2014-03-24 18:51:02 +08:00
CLI . interact = function ( secret _key , public _key , machine _name ) {
InteractorDaemonizer . launchOrAttach ( secret _key , public _key , machine _name , function ( status ) {
2014-01-11 18:48:41 +01:00
if ( status == false )
2014-04-13 15:05:19 +08:00
printOut ( 'Interactor already launched' ) ;
2014-01-11 18:48:41 +01:00
else
2014-04-13 15:05:19 +08:00
printOut ( 'Successfully launched interactor' ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2014-01-11 18:48:41 +01:00
} ) ;
} ;
2013-10-16 14:35:38 +02:00
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Kill interactor
2014-06-13 16:11:43 +02:00
* @method killInteract
*/
2014-06-13 16:24:16 +02:00
CLI . killInteract = function ( ) {
2014-04-09 15:37:22 +08:00
InteractorDaemonizer . ping ( function ( online ) {
if ( ! online ) {
console . error ( 'Interactor not launched' ) ;
return exitCli ( cst . ERROR _EXIT ) ;
}
InteractorDaemonizer . launchRPC ( function ( ) {
InteractorDaemonizer . rpc . kill ( function ( err ) {
if ( err ) {
console . error ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
}
2014-04-13 15:05:19 +08:00
printOut ( 'Interactor successfully killed' ) ;
2014-04-09 15:37:22 +08:00
return exitCli ( cst . SUCCESS _EXIT ) ;
} ) ;
2014-01-11 18:48:41 +01:00
} ) ;
2014-04-09 15:37:22 +08:00
return false ;
2014-01-11 18:48:41 +01:00
} ) ;
2013-09-28 17:33:46 +02:00
} ;
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Get information about interactor connection
2014-06-13 16:11:43 +02:00
* @method infoInteract
*/
2014-05-07 18:27:36 +02:00
CLI . infoInteract = function ( ) {
getInteractInfo ( function ( err , data ) {
if ( err ) {
console . error ( 'Interactor not launched' ) ;
return exitCli ( cst . ERROR _EXIT ) ;
}
console . log ( data ) ;
return exitCli ( cst . SUCCESS _EXIT ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Ping daemon - if PM2 daemon not launched, it will launch it
2014-06-13 16:11:43 +02:00
* @method ping
*/
2014-01-10 18:04:03 +01:00
CLI . ping = function ( ) {
Satan . executeRemote ( 'ping' , { } , function ( err , res ) {
if ( err ) {
console . error ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-10 18:04:03 +01:00
}
2014-04-13 15:05:19 +08:00
printOut ( res ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2014-01-10 18:04:03 +01:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
2014-06-13 16:24:16 +02:00
* Resurrect processes
2014-06-13 16:11:43 +02:00
* @method resurrect
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-05-16 03:52:34 +02:00
CLI . resurrect = function ( cb ) {
2014-01-14 19:30:36 +01:00
try {
2014-03-20 16:54:56 +08:00
var apps = fs . readFileSync ( cst . DUMP _FILE _PATH ) ;
2014-01-14 19:30:36 +01:00
} catch ( e ) {
2014-03-20 16:54:56 +08:00
console . error ( cst . PREFIX _MSG + 'No processes saved; DUMP file doesn\'t exist' ) ;
2014-05-16 03:52:34 +02:00
if ( cb ) return cb ( e ) ;
else return exitCli ( cst . ERROR _EXIT ) ;
2014-01-14 19:30:36 +01:00
}
( function ex ( apps ) {
2014-05-16 03:52:34 +02:00
if ( ! apps [ 0 ] ) return cb ? cb ( ) : speedList ( ) ;
2014-01-14 19:30:36 +01:00
Satan . executeRemote ( 'prepare' , apps [ 0 ] , function ( err ) {
if ( err )
console . error ( cst . PREFIX _MSG _ERR + ' Process %s not launched - (script missing)' , apps [ 0 ] . pm _exec _path ) ;
else
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process %s launched' , apps [ 0 ] . pm _exec _path ) ;
2014-01-14 19:30:36 +01:00
apps . shift ( ) ;
return ex ( apps ) ;
} ) ;
return false ;
} ) ( JSON . parse ( apps ) ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method updatePM2
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-05-16 03:52:34 +02:00
CLI . updatePM2 = function ( cb ) {
console . log ( 'Be sure to haave the latest version by doing `npm install pm2@latest -g` before doing this procedure.' ) ;
CLI . dump ( function ( err ) {
console . log ( cst . PREFIX _MSG + '--- dumped' ) ;
CLI . killDaemon ( function ( err ) {
console . log ( cst . PREFIX _MSG + '--- killed' ) ;
Satan . launchDaemon ( function ( err , child ) {
console . log ( cst . PREFIX _MSG + '--- resurrected' ) ;
if ( err ) {
console . error ( err ) ;
}
CLI . resurrect ( function ( ) {
console . log ( '>>>>>>>>>> PM2 updated' . blue . bold ) ;
cb ? cb ( ) : speedList ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ;
2014-01-14 19:30:36 +01:00
/**
* Dump current processes managed by pm2 into DUMP_FILE_PATH file
2014-06-13 16:11:43 +02:00
* @method dump
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-01-14 19:30:36 +01:00
*/
2014-05-16 03:52:34 +02:00
CLI . dump = function ( cb ) {
2013-09-29 18:39:41 +02:00
var env _arr = [ ] ;
2013-09-28 17:33:46 +02:00
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
console . error ( 'Error retrieving process list: ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-06-13 16:11:43 +02:00
/**
* Description
* @method fin
* @param {} err
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
function fin ( err ) {
2013-09-29 18:39:41 +02:00
fs . writeFileSync ( cst . DUMP _FILE _PATH , JSON . stringify ( env _arr ) ) ;
2013-09-28 17:33:46 +02:00
UX . processing . stop ( ) ;
2014-05-16 03:52:34 +02:00
if ( cb ) return cb ( null ) ;
else return exitCli ( cst . SUCCESS _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
( function ex ( apps ) {
if ( ! apps [ 0 ] ) return fin ( null ) ;
2013-09-29 18:39:41 +02:00
delete apps [ 0 ] . pm2 _env . instances ;
2013-12-16 15:57:36 +01:00
delete apps [ 0 ] . pm2 _env . pm _id ;
2013-09-29 18:39:41 +02:00
env _arr . push ( apps [ 0 ] . pm2 _env ) ;
2013-09-28 17:33:46 +02:00
apps . shift ( ) ;
return ex ( apps ) ;
} ) ( list ) ;
} ) ;
} ;
2014-01-14 19:30:36 +01:00
/**
* Launch API interface
2014-06-13 16:11:43 +02:00
* @method web
2014-06-13 16:24:16 +02:00
* @return
2014-01-14 19:30:36 +01:00
*/
2013-09-28 17:33:46 +02:00
CLI . web = function ( ) {
2013-11-21 16:49:40 -05:00
Satan . executeRemote ( 'prepare' , resolvePaths ( {
2013-09-28 17:33:46 +02:00
script : p . resolve ( p . dirname ( module . filename ) , './HttpInterface.js' ) ,
2013-12-04 19:52:21 +01:00
name : 'Pm2Http' + cst . WEB _INTERFACE ,
exec _mode : 'fork_mode'
2014-06-10 18:00:38 +02:00
} ) , function ( err ) {
if ( err ) {
console . error ( cst . PREFIX _MSG _ERR + 'Error while launching application' , err . stack || err ) ;
return speedList ( ) ;
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process launched' ) ;
2013-09-28 17:33:46 +02:00
speedList ( ) ;
} ) ;
} ;
2014-01-14 19:30:36 +01:00
/**
* CLI method for reloading
2014-06-13 16:11:43 +02:00
* @method reload
2014-01-14 19:30:36 +01:00
* @param {string} reload_method RPC method to hit (can be reloadProcessId or softReloadProcessId)
2014-06-13 16:24:16 +02:00
* @return
2014-01-14 19:30:36 +01:00
*/
CLI . reload = function ( reload _method ) {
2014-01-10 19:13:37 +01:00
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
2013-09-28 17:33:46 +02:00
if ( err ) {
2014-01-10 19:13:37 +01:00
console . error ( 'Error retrieving process list: ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-01-10 19:13:37 +01:00
2014-01-10 23:04:46 +01:00
async . eachLimit ( list , 1 , function ( proc , next ) {
2014-02-13 13:55:50 +01:00
if ( ( proc . state == cst . STOPPED _STATUS ||
proc . state == cst . STOPPING _STATUS ) &&
2014-01-10 19:13:37 +01:00
proc . pm2 _env . exec _mode != 'cluster_mode' ) {
2014-01-10 23:04:46 +01:00
return next ( ) ;
2014-01-10 19:13:37 +01:00
}
2014-01-14 19:30:36 +01:00
Satan . executeRemote ( reload _method , proc . pm2 _env . pm _id , function ( err , res ) {
2014-01-10 19:13:37 +01:00
if ( err ) {
console . error ( 'Error : ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-10 19:13:37 +01:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process %s succesfully reloaded' , proc . pm2 _env . name ) ;
2014-01-10 23:04:46 +01:00
return next ( ) ;
2014-01-10 19:13:37 +01:00
} ) ;
return false ;
2014-01-10 23:04:46 +01:00
} , function ( err ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'All processes reloaded' ) ;
2014-01-11 15:49:52 +01:00
return setTimeout ( speedList , 500 ) ;
2014-01-10 23:04:46 +01:00
} ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-01-14 19:30:36 +01:00
/**
* CLI method for reloading
2014-06-13 16:11:43 +02:00
* @method reloadProcessName
2014-01-14 19:30:36 +01:00
* @param {string} process_name name of processes to reload
* @param {string} reload_method RPC method to hit (can be reloadProcessId or softReloadProcessId)
2014-06-13 16:24:16 +02:00
* @return
2014-01-14 19:30:36 +01:00
*/
CLI . reloadProcessName = function ( process _name , reload _method ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Reloading process by name %s' , process _name ) ;
2014-01-10 19:13:37 +01:00
getProcessByName ( process _name , function ( err , processes ) {
2014-04-15 22:48:05 +08:00
if ( processes . length === 0 ) {
printError ( 'No processes with this name: %s' , process _name ) ;
return exitCli ( cst . ERROR _EXIT ) ;
}
2014-01-10 23:04:46 +01:00
async . eachLimit ( processes , 1 , function ( proc , next ) {
2014-01-10 19:13:37 +01:00
if ( proc . state == cst . STOPPED _STATUS ||
2014-02-13 13:55:50 +01:00
proc . state == cst . STOPPING _STATUS ||
2014-01-10 19:13:37 +01:00
proc . pm2 _env . exec _mode != 'cluster_mode' ) {
2014-01-10 23:04:46 +01:00
return next ( ) ;
2014-01-10 19:13:37 +01:00
}
2014-01-14 19:30:36 +01:00
Satan . executeRemote ( reload _method , proc . pm2 _env . pm _id , function ( err , res ) {
2014-01-10 19:13:37 +01:00
if ( err ) {
console . error ( 'Error : ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-10 19:13:37 +01:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process %s succesfully reloaded' , proc . pm2 _env . name ) ;
2014-01-10 23:04:46 +01:00
return next ( ) ;
2014-01-10 19:13:37 +01:00
} ) ;
return false ;
2014-01-10 23:04:46 +01:00
} , function ( err ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'All processes reloaded' ) ;
2014-01-11 15:49:52 +01:00
return setTimeout ( speedList , 500 ) ;
2014-01-10 23:04:46 +01:00
} ) ;
2014-01-08 17:55:20 +01:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method restartProcessByName
* @param {} pm2_name
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-29 18:39:41 +02:00
CLI . restartProcessByName = function ( pm2 _name ) {
Satan . executeRemote ( 'restartProcessName' , pm2 _name , function ( err , list ) {
2013-09-28 17:33:46 +02:00
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2013-10-27 14:16:58 +01:00
UX . processing . stop ( ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process ' + pm2 _name + ' restarted' ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-29 18:39:41 +02:00
} ) ;
} ;
2013-09-28 17:33:46 +02:00
2014-06-13 16:11:43 +02:00
/**
* Description
* @method restartProcessById
* @param {} pm2_id
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-29 18:39:41 +02:00
CLI . restartProcessById = function ( pm2 _id ) {
Satan . executeRemote ( 'restartProcessId' , pm2 _id , function ( err , res ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-29 18:39:41 +02:00
}
2013-10-27 14:16:58 +01:00
UX . processing . stop ( ) ;
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process ' + pm2 _id + ' restarted' ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method restartAll
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . restartAll = function ( ) {
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
}
if ( list && list . length === 0 ) {
printError ( 'No process launched' ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2013-11-13 15:10:59 +01:00
2014-04-09 17:31:26 +08:00
2013-12-22 16:41:07 +01:00
( function rec ( processes ) {
var proc = processes [ 0 ] ;
if ( proc == null ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process restarted...' ) ;
2013-12-22 16:50:04 +01:00
return setTimeout ( speedList , 1000 ) ;
2013-12-22 16:41:07 +01:00
}
Satan . executeRemote ( 'restartProcessId' , proc . pm2 _env . pm _id , function ( err , res ) {
2013-10-16 13:49:03 +02:00
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-10-16 13:49:03 +02:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Process ' + proc . pm2 _env . name + ' restarted' ) ;
2013-12-22 16:41:07 +01:00
processes . shift ( ) ;
2014-04-09 17:31:26 +08:00
return rec ( processes ) ;
2013-11-13 15:10:59 +01:00
} ) ;
2014-01-10 18:04:03 +01:00
return false ;
2013-12-22 16:41:07 +01:00
} ) ( list ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method stopAll
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . stopAll = function ( ) {
Satan . executeRemote ( 'stopAll' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
UX . processing . stop ( ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method deleteProcess
* @param {} process_name
* @param {} jsonVia
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-01-30 12:58:03 -06:00
CLI . deleteProcess = function ( process _name , jsonVia ) {
2014-02-13 13:55:50 +01:00
if ( jsonVia == 'pipe' )
2014-01-30 12:58:03 -06:00
return CLI . actionFromJson ( 'deleteProcessName' , process _name , 'pipe' ) ;
if ( process _name . indexOf ( '.json' ) > 0 )
return CLI . actionFromJson ( 'deleteProcessName' , process _name , 'file' ) ;
2014-01-29 13:43:18 +01:00
else if ( process _name == 'all' ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Stopping and deleting all processes' ) ;
2013-09-29 18:39:41 +02:00
Satan . executeRemote ( 'deleteAll' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-29 18:39:41 +02:00
}
UX . processing . stop ( ) ;
2013-11-13 15:10:59 +01:00
speedList ( ) ;
2013-10-15 13:11:05 +02:00
} ) ;
}
else if ( ! isNaN ( parseInt ( process _name ) ) ) {
2014-04-13 15:05:19 +08:00
printOut ( 'Stopping and deleting process by id : %s' , process _name ) ;
2013-10-15 13:11:05 +02:00
Satan . executeRemote ( 'deleteProcessId' , process _name , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-10-15 13:11:05 +02:00
}
UX . processing . stop ( ) ;
speedList ( ) ;
2013-09-29 18:39:41 +02:00
} ) ;
}
else {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Stopping and deleting process by name %s' , process _name ) ;
2013-10-15 13:11:05 +02:00
Satan . executeRemote ( 'deleteProcessName' , process _name , function ( err , list ) {
2013-09-29 18:39:41 +02:00
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-29 18:39:41 +02:00
}
UX . processing . stop ( ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-29 18:39:41 +02:00
} ) ;
}
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method stopProcessName
* @param {} name
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . stopProcessName = function ( name ) {
Satan . executeRemote ( 'stopProcessName' , name , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Stopping process by name ' + name ) ;
2013-09-28 17:33:46 +02:00
UX . processing . stop ( ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method stopId
* @param {} pm2_id
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . stopId = function ( pm2 _id ) {
Satan . executeRemote ( 'stopProcessId' , pm2 _id , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
return exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + ' Process stopped' ) ;
2013-09-28 17:33:46 +02:00
UX . processing . stop ( ) ;
2013-10-15 13:11:05 +02:00
speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method generateSample
* @param {} name
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . generateSample = function ( name ) {
var sample = fs . readFileSync ( path . join ( _ _dirname , cst . SAMPLE _FILE _PATH ) ) ;
var dt = sample . toString ( ) . replace ( /VARIABLE/g , name ) ;
var f _name = name + '-pm2.json' ;
fs . writeFileSync ( path . join ( process . env . PWD , f _name ) , dt ) ;
console . info ( 'Sample generated on current folder\n%s :\n' , f _name ) ;
console . info ( dt ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2013-09-28 17:33:46 +02:00
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method list
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . list = function ( ) {
2014-01-13 16:58:27 +01:00
speedList ( ) ;
2013-09-28 17:33:46 +02:00
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method jlist
* @param {} debug
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . jlist = function ( debug ) {
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
if ( debug )
2014-04-13 15:05:19 +08:00
printOut ( list ) ;
2013-09-28 17:33:46 +02:00
else
2014-04-13 15:05:19 +08:00
printOut ( JSON . stringify ( list ) ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method flush
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . flush = function ( ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Flushing ' + cst . PM2 _LOG _FILE _PATH ) ;
2013-09-28 17:33:46 +02:00
fs . openSync ( cst . PM2 _LOG _FILE _PATH , 'w' ) ;
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
list . forEach ( function ( l ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Flushing' ) ;
printOut ( cst . PREFIX _MSG + l . pm2 _env . pm _out _log _path ) ;
printOut ( cst . PREFIX _MSG + l . pm2 _env . pm _err _log _path ) ;
2013-09-28 17:33:46 +02:00
2013-09-29 18:39:41 +02:00
fs . openSync ( l . pm2 _env . pm _out _log _path , 'w' ) ;
fs . openSync ( l . pm2 _env . pm _err _log _path , 'w' ) ;
2013-09-28 17:33:46 +02:00
} ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . SUCCESS _EXIT ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method describeProcess
* @param {} pm2_id
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-04-13 16:01:40 +08:00
CLI . describeProcess = function ( pm2 _id ) {
var found = false ;
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
console . error ( 'Error retrieving process list: ' + err ) ;
exitCli ( cst . ERROR _EXIT ) ;
}
list . forEach ( function ( proc ) {
if ( proc . pm _id == pm2 _id ) {
found = true ;
UX . describeTable ( proc ) ;
}
} ) ;
if ( found === false ) {
2014-06-14 14:17:51 -04:00
printError ( '%d id doesn\'t exist' , pm2 _id ) ;
2014-04-13 16:01:40 +08:00
return exitCli ( cst . ERROR _EXIT ) ;
}
return exitCli ( cst . SUCCESS _EXIT ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method reloadLogs
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-04-13 15:05:19 +08:00
CLI . reloadLogs = function ( ) {
printOut ( 'Reloading all logs...' ) ;
Satan . executeRemote ( 'reloadLogs' , { } , function ( err , logs ) {
if ( err ) printError ( err ) ;
printOut ( 'All logs reloaded' ) ;
exitCli ( cst . SUCCESS _EXIT ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method sendSignalToProcessName
* @param {} signal
* @param {} process_name
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-01-08 16:12:57 +01:00
CLI . sendSignalToProcessName = function ( signal , process _name ) {
Satan . executeRemote ( 'sendSignalToProcessName' , {
signal : signal ,
process _name : process _name
} , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-08 16:12:57 +01:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Succesfully sent signal %s to process name %s' , signal , process _name ) ;
2014-01-08 16:12:57 +01:00
UX . processing . stop ( ) ;
speedList ( ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method sendSignalToProcessId
* @param {} signal
* @param {} process_id
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-01-08 16:12:57 +01:00
CLI . sendSignalToProcessId = function ( signal , process _id ) {
Satan . executeRemote ( 'sendSignalToProcessId' , {
signal : signal ,
process _id : process _id
} , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-08 16:12:57 +01:00
}
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'Succesfully sent signal %s to process id %s' , signal , process _id ) ;
2014-01-08 16:12:57 +01:00
UX . processing . stop ( ) ;
speedList ( ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method monit
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . monit = function ( ) {
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
if ( Object . keys ( list ) . length == 0 ) {
2014-04-13 15:05:19 +08:00
printOut ( cst . PREFIX _MSG + 'No online process to monitor' ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2013-11-13 15:10:59 +01:00
2013-09-28 17:33:46 +02:00
Monit . init ( list ) ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method refresh
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
function refresh ( cb ) {
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
console . error ( 'Error retrieving process list: ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
setTimeout ( function ( ) {
Monit . refresh ( list ) ;
refresh ( ) ;
} , 400 ) ;
} ) ;
}
refresh ( ) ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method streamLogs
* @param {} id
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
CLI . streamLogs = function ( id ) {
var tdb = { } ;
2013-11-13 15:10:59 +01:00
2013-09-28 17:33:46 +02:00
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
2014-04-09 17:31:26 +08:00
printError ( err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2013-11-13 15:10:59 +01:00
2014-04-13 15:05:19 +08:00
printOut ( '########### Starting streaming logs for [%s] process' , id || 'all' ) ;
2014-01-10 17:40:26 +01:00
list . forEach ( function ( proc ) {
if ( ( ! id || ( id && ! isNaN ( parseInt ( id ) ) && proc . pm _id == id ) ) ||
2014-01-13 16:58:27 +01:00
( ! id || ( id && isNaN ( parseInt ( id ) ) && proc . pm2 _env . name == id ) ) ) {
2014-01-10 17:40:26 +01:00
var app _name = proc . pm2 _env . name || p . basename ( proc . pm2 _env . pm _exec _path ) ;
if ( proc . pm2 _env . pm _out _log _path )
Log . stream ( proc . pm2 _env . pm _out _log _path ,
app _name + '-' + proc . pm _id + ' (out)' ) ;
if ( proc . pm2 _env . pm _err _log _path )
Log . stream ( proc . pm2 _env . pm _err _log _path ,
app _name + '-' + proc . pm _id + ' (err)' ) ;
2013-09-28 17:33:46 +02:00
}
2014-01-10 17:40:26 +01:00
} ) ;
2013-09-28 17:33:46 +02:00
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method killDaemon
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-05-16 03:52:34 +02:00
CLI . killDaemon = function ( cb ) {
printOut ( cst . PREFIX _MSG + 'Killing pm2...' ) ;
2014-04-13 15:05:19 +08:00
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
2013-09-28 17:33:46 +02:00
if ( err ) {
2014-04-13 15:05:19 +08:00
console . error ( 'Error retrieving process list: ' + err ) ;
2014-05-16 03:52:34 +02:00
cb ? cb ( ) : exitCli ( cst . ERROR _EXIT ) ;
2013-09-28 17:33:46 +02:00
}
2014-04-13 15:05:19 +08:00
2014-04-13 16:01:40 +08:00
async . eachLimit ( list , 2 , function ( proc , next ) {
2014-04-13 15:05:19 +08:00
Satan . executeRemote ( 'deleteProcessId' , proc . pm2 _env . pm _id , function ( err , res ) {
if ( err )
printError ( 'Error : ' + err ) ;
2014-05-16 03:52:34 +02:00
printOut ( cst . PREFIX _MSG + 'Process %s stopped' , proc . pm2 _env . name ) ;
2014-04-13 15:05:19 +08:00
return next ( ) ;
} ) ;
return false ;
} , function ( err ) {
2014-05-16 03:52:34 +02:00
printOut ( cst . PREFIX _MSG + 'All processes stopped' ) ;
2014-04-13 15:05:19 +08:00
Satan . killDaemon ( function ( err , res ) {
if ( err ) {
printError ( err ) ;
2014-05-16 03:52:34 +02:00
if ( cb ) return cb ( err ) ;
else exitCli ( cst . ERROR _EXIT ) ;
2014-04-13 15:05:19 +08:00
}
console . info ( 'PM2 stopped' ) ;
2014-05-16 03:52:34 +02:00
cb ? cb ( null , res ) : exitCli ( cst . SUCCESS _EXIT ) ;
2014-04-13 15:05:19 +08:00
} ) ;
} ) ;
2013-09-28 17:33:46 +02:00
} ) ;
2014-04-13 15:05:19 +08:00
2013-09-28 17:33:46 +02:00
} ;
//
// Private methods
//
2014-01-10 16:08:52 +01:00
var gl _retry = 0 ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method speedList
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2013-09-28 17:33:46 +02:00
function speedList ( ) {
2014-01-08 16:12:57 +01:00
var self = this ;
2014-01-10 16:08:52 +01:00
UX . processing . stop ( ) ;
2014-01-27 13:18:11 +01:00
2014-05-07 18:27:36 +02:00
getInteractInfo ( function ( i _err , interact _infos ) {
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
if ( gl _retry == 0 ) {
gl _retry += 1 ;
return setTimeout ( speedList , 1400 ) ;
}
console . error ( 'Error retrieving process list: %s.\nA process seems to be on infinite loop, retry in 5 seconds' , err ) ;
exitCli ( cst . ERROR _EXIT ) ;
}
if ( commander . miniList && ! commander . silent )
UX . miniDisplay ( list ) ;
else if ( ! commander . silent ) {
if ( interact _infos ) {
printOut ( '●' . green . bold + ' Connected to VitalSigns public id : %s - machine name : %s' , interact _infos . public _key , interact _infos . machine _name ) ;
}
UX . dispAsTable ( list , interact _infos ) ;
printOut ( ' Use `pm2 desc[ribe] <id>` to get more details' ) ;
2014-01-08 16:12:57 +01:00
}
2014-04-13 16:01:40 +08:00
2014-05-07 18:27:36 +02:00
if ( commander . daemon ) {
return exitCli ( cst . SUCCESS _EXIT ) ;
}
else {
printOut ( '--no-daemon option enabled = do not exit pm2 CLI' ) ;
printOut ( 'PM2 dameon PID = %s' , fs . readFileSync ( cst . PM2 _PID _FILE _PATH ) ) ;
return Log . stream ( cst . PM2 _LOG _FILE _PATH ) ;
}
} ) ;
2013-09-28 17:33:46 +02:00
} ) ;
}
2014-06-13 16:11:43 +02:00
/**
* Description
* @method printError
* @param {} msg
* @return CallExpression
*/
2014-04-09 17:31:26 +08:00
function printError ( msg ) {
if ( msg instanceof Error )
return console . error ( msg . message ) ;
2014-04-14 13:14:13 +08:00
return console . error . apply ( console , arguments ) ;
2014-04-09 17:31:26 +08:00
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method printOut
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-04-14 13:14:13 +08:00
function printOut ( ) {
console . log . apply ( console , arguments ) ;
}
2014-04-09 17:31:26 +08:00
2014-06-13 16:11:43 +02:00
/**
* Description
* @method getInteractInfo
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-05-07 18:27:36 +02:00
function getInteractInfo ( cb ) {
InteractorDaemonizer . ping ( function ( online ) {
if ( ! online ) {
return cb ( { msg : 'offline' } ) ;
}
InteractorDaemonizer . launchRPC ( function ( ) {
InteractorDaemonizer . rpc . getInfos ( function ( err , infos ) {
if ( err ) {
return cb ( err ) ;
}
return cb ( null , infos ) ;
} ) ;
} ) ;
return false ;
} ) ;
} ;
2014-06-13 16:11:43 +02:00
/**
* Description
* @method exitCli
* @param {} code
* @return CallExpression
*/
2014-01-29 12:45:46 +01:00
function exitCli ( code ) {
Satan . client . sock . close ( ) ;
return process . exit ( code ) ;
}
2014-06-13 16:11:43 +02:00
/**
* Description
* @method resolvePaths
* @param {} appConf
* @return app
*/
2013-11-21 16:46:45 -05:00
function resolvePaths ( appConf ) {
2014-05-23 10:23:52 +02:00
var cwd = null ;
if ( appConf . cwd ) {
cwd = p . resolve ( appConf . cwd ) ;
process . env . PWD = appConf . cwd ;
}
2014-05-15 17:47:58 +02:00
var app = Common . resolveAppPaths ( appConf , cwd , console . log ) ;
2013-11-21 16:43:26 -05:00
if ( app instanceof Error ) {
console . error ( cst . PREFIX _MSG _ERR + app . message ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2013-09-29 21:45:36 +02:00
}
2013-11-21 16:43:26 -05:00
return app ;
2013-09-28 17:33:46 +02:00
}
2014-01-10 19:13:37 +01:00
2014-06-13 16:11:43 +02:00
/**
* Description
* @method getProcessByName
* @param {} name
* @param {} cb
2014-06-13 16:24:16 +02:00
* @return
2014-06-13 16:11:43 +02:00
*/
2014-01-10 19:13:37 +01:00
function getProcessByName ( name , cb ) {
var arr = [ ] ;
Satan . executeRemote ( 'getMonitorData' , { } , function ( err , list ) {
if ( err ) {
console . error ( 'Error retrieving process list: ' + err ) ;
2014-01-29 12:45:46 +01:00
exitCli ( cst . ERROR _EXIT ) ;
2014-01-10 19:13:37 +01:00
}
list . forEach ( function ( proc ) {
if ( p . basename ( proc . pm2 _env . pm _exec _path ) == name ||
p . basename ( proc . pm2 _env . pm _exec _path ) == p . basename ( name ) ||
proc . pm2 _env . name == name ) {
arr . push ( proc ) ;
}
} ) ;
return cb ( null , arr ) ;
} ) ;
}