Skip to content

Commit

Permalink
feature: allow to start raw command via `pm2 start "my command with p…
Browse files Browse the repository at this point in the history
…arams"` + alias script in ecosystem file with cmd & command
  • Loading branch information
Unitech committed May 27, 2018
1 parent ff1a7f3 commit af60e99
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 36 deletions.
15 changes: 3 additions & 12 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,24 +630,17 @@ class API {
else
app_conf.exec_mode = 'fork';

// Options set via environment variables
if (process.env.PM2_DEEP_MONITORING)
app_conf.deep_monitoring = true;

if (typeof app_conf.name == 'function'){
if (typeof app_conf.name == 'function')
delete app_conf.name;
}

delete app_conf.args;

var argsIndex;

if (opts.rawArgs && (argsIndex = opts.rawArgs.indexOf('--')) >= 0) {
if (opts.rawArgs && (argsIndex = opts.rawArgs.indexOf('--')) >= 0)
app_conf.args = opts.rawArgs.slice(argsIndex + 1);
}
else if (opts.scriptArgs) {
else if (opts.scriptArgs)
app_conf.args = opts.scriptArgs;
}

app_conf.script = script;

Expand All @@ -656,8 +649,6 @@ class API {

app_conf = appConf[0];

app_conf.username = Common.getCurrentUsername();

/**
* If -w option, write configuration to configuration.json file
*/
Expand Down
66 changes: 42 additions & 24 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,34 @@ Common.verifyConfs = function(appConfs){
for (var i = 0; i < appConfs.length; i++) {
var app = appConfs[i];

if (app.disable_trace) {
app.trace = false
delete app.disable_trace;
// JSON conf: alias cmd to script
if (app.cmd && !app.script) {
app.script = app.cmd
delete app.cmd
}
// JSON conf: alias command to script
if (app.command && !app.script) {
app.script = app.command
delete app.command
}

app.username = Common.getCurrentUsername();

// If command is like pm2 start "python xx.py --ok"
// Then automatically start the script with bash -c and set a name eq to command
if (app.script && app.script.indexOf(' ') > -1) {
var _script = app.script;
if (require('shelljs').which('bash'))
app.script = 'bash';
else if (require('shelljs').which('sh'))
app.script = 'sh';
else
throw new Error('bash and sh not available in $PATH')

app.args = ['-c', _script];
if (!app.name) {
app.name = _script
}
}

if ((app.uid || app.gid) && app.force !== true) {
Expand All @@ -596,14 +621,20 @@ Common.verifyConfs = function(appConfs){
}
}

// If no uid set and command runned as sudo, use the parent shell USER
// to set it to his uid and not root
// if (!app.uid && process.env.SUDO_USER) {
// app.uid = process.env.SUDO_USER;
// }
// Specific options of PM2.io
if (process.env.PM2_DEEP_MONITORING)
app.deep_monitoring = true;

// Warn deprecates.
checkDeprecates(app);
if (app.disable_trace) {
app.trace = false
delete app.disable_trace;
}

if (app.instances == 'max')
app.instances = 0;
// Sanity check, default to number of cores if value can't be parsed
if (typeof(app.instances) === 'string')
app.instances = parseInt(app.instances) || 0;

// Check Exec mode
checkExecMode(app);
Expand All @@ -612,9 +643,8 @@ Common.verifyConfs = function(appConfs){
prepareAppName(app);

var ret = Config.validateJSON(app);
//debug('After processing', ret);
// Show errors if existing.

// Show errors if existing.
if (ret.errors && ret.errors.length > 0){
ret.errors.forEach(function(err){
warn(err);
Expand Down Expand Up @@ -688,18 +718,6 @@ function checkExecMode(conf) {
}
}

/**
* Check deprecates and show warnings.
* @param {Object} conf
*/
function checkDeprecates(conf){
if (conf.instances == 'max')
conf.instances = 0;
// Sanity check, default to number of cores if value can't be parsed
if (typeof(conf.instances) === 'string')
conf.instances = parseInt(conf.instances) || 0;
}

/**
* Render an app name if not existing.
* @param {Object} conf
Expand Down
2 changes: 2 additions & 0 deletions test/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ spec "module safeguard system (--safe)"
# CLI
bash ./test/e2e/cli/reload.sh
spec "Reload"
bash ./test/e2e/cli/start-app.sh
spec "Command line passing"
bash ./test/e2e/cli/operate-regex.sh
spec "Operate process that match regex"
bash ./test/e2e/cli/interpreter.sh
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/cli/start-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/../include.sh"

echo -e "\033[1mRunning tests:\033[0m"

cd $file_path/start-app

#
# Direct command
#
$pm2 delete all

$pm2 start "node -e 'setTimeout(function() { }, 100000); console.log(process.env.TEST)'" -l test.log --merge-logs
should 'should have started command' 'online' 1
should 'should have not been restarted' 'restart_time: 0' 1

cat test.log | grep "undefined"
spec "should have printed undefined env var"

TEST='ok' $pm2 restart 0 --update-env
cat test.log | grep "ok"

should 'should have started command' 'online' 1
should 'should have not been restarted' 'restart_time: 1' 1
spec "should have printed undefined env var"

#
# Direct command via Conf file
#
$pm2 delete all

$pm2 start ecosystem.config.js
should 'should have started command' 'online' 1
should 'should have not been restarted' 'restart_time: 0' 1
cat test-conf.log | grep "test_val"
spec "should have printed the test_val"
10 changes: 10 additions & 0 deletions test/fixtures/start-app/ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
apps : [{
cmd: "node -e 'setTimeout(function() { }, 100000); console.log(process.env.TEST)'",
log: 'test-conf.log',
merge_logs: true,
env: {
TEST: 'test_val'
}
}]
};
21 changes: 21 additions & 0 deletions test/fixtures/start-app/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

var http = require('http');

var app = http.createServer(function(req, res) {
res.writeHead(200);
res.end('hey');
})

var listener = app.listen(0, function() {
console.log('Listening on port ' + listener.address().port);
});

process.on('message', function(msg) {
if (msg == 'shutdown') {
console.log('Closing all connections...');
setTimeout(function() {
console.log('Finished closing connections');
process.exit(0);
}, 100);
}
});

0 comments on commit af60e99

Please sign in to comment.