diff --git a/lib/CLI.js b/lib/CLI.js index de98e9bb..bab6bb58 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -286,11 +286,32 @@ CLI.actionFromJson = function(action, file, jsonVia, cb) { }); }; +/** + * Extend the app.env object of with the properties taken from the app.env_[envName]. + * @param {object} app The app object. + * @param {string} envName The given environment name. + * @returns {object} The app.env variables object. + */ +function mergeEnvironmentVariables(app, envName) { + app.env = app.env || {}; + if (envName && 'env_' + envName in app) { + /** + * Merge specific environment variables + * `--env production` will merge `env_production` with the env + */ + util._extend(app.env, app['env_' + envName]); + } + + return app.env; +} + /** * Process and start a JSON file * @method startJson * @param {string} cmd + * @param {object} opts * @param {string} jsonVia + * @param {function} cb */ CLI.startJson = function(cmd, opts, jsonVia, cb) { var appConf; @@ -310,19 +331,10 @@ CLI.startJson = function(cmd, opts, jsonVia, cb) { return exitCli(cst.ERROR_EXIT); async.eachLimit(appConf, cst.CONCURRENT_ACTIONS, function(app, next) { + mergeEnvironmentVariables(app, opts.env); try { - if (opts.env) { - /** - * Merge specific environment variables - * `--env production` will merge `production_env` with the env - * -> for pm2-deploy - */ - app.env = app.env || {}; - util._extend(app.env, app['env_' + opts.env]); - } - - var app_paths = resolvePaths(app); - } catch(e) { + resolvePaths(app); + } catch (e) { debug(e.stack || e); return next(); } @@ -796,20 +808,12 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { }); async.eachLimit(apps_to_start, cst.CONCURRENT_ACTIONS, function(app, next) { + mergeEnvironmentVariables(app, opts.env); try { - if (opts.env) { - /** - * Merge specific environment variables - * `--env production` will merge `production_env` with the env - */ - app.env = app.env || {}; - util._extend(app.env, app['env_' + opts.env]); - } - - var resolved_paths = resolvePaths(app); - } catch(e) { + resolvePaths(app); + } catch (e) { printError(e); - return cb ? cb({msg : 'Error'}) : exitCli(cst.ERROR_EXIT); + return cb ? cb({msg : e.message || e}) : exitCli(cst.ERROR_EXIT); } Satan.executeRemote('prepare', resolved_paths, function(err, data) { @@ -858,7 +862,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { async.filter(appConf, function(app, callback){ callback(app.name == proc.name); }, function(apps){ var envs = apps.map(function(app){ // Binds env_diff to env and returns it. - return util._extend(app.env || {}, opts.env && ('env_' + opts.env in app) ? app['env_' + opts.env] : {}); + return mergeEnvironmentVariables(app, opts.env); }); // Assigns own enumerable properties of all // Notice: if people use the same name in different apps, @@ -1570,7 +1574,7 @@ function verifyConfs(appConfs){ * @param {Object} conf */ function checkExecMode(conf) { - + if (conf.exec_mode === 'cluster' || conf.exec_mode === 'cluster_mode' || conf.instances && conf.exec_mode === undefined) @@ -1591,7 +1595,7 @@ function checkExecMode(conf) { if (conf.instances && conf.exec_mode === undefined) conf.exec_mode = 'cluster_mode'; - + //process.version.match(/0.10/) && // Tell user about unstability of cluster module + Roadmap if (/^cluster(_mode)?$/i.test(conf.exec_mode) &&