Skip to content

Commit

Permalink
Merge pull request #847 from koresar/development
Browse files Browse the repository at this point in the history
Allow env vars per each ecosystem's deploy
  • Loading branch information
Unitech committed Nov 28, 2014
2 parents 826d5e7 + 8dfc2e0 commit 677e9b3
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 45 deletions.
5 changes: 4 additions & 1 deletion ADVANCED_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,10 @@ It contains this:
"ref" : "origin/master",
"repo" : "git@github.com:repo.git",
"path" : "/var/www/development",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev"
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev",
"env" : {
"NODE_ENV": "dev"
}
}
}
}
Expand Down
111 changes: 68 additions & 43 deletions lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,20 @@ CLI.actionFromJson = function(action, file, jsonVia, cb) {
* 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;
var appConf, deployConf = null;

if (jsonVia == 'pipe')
appConf = json5.parse(cmd);
else {
var data = fs.readFileSync(cmd);
appConf = json5.parse(data);
// v2 JSON declaration
if (appConf.deploy) deployConf = appConf.deploy;
if (appConf.apps) appConf = appConf.apps;
}

Expand All @@ -310,19 +313,11 @@ 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, deployConf);
var app_paths = null;
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) {
app_paths = resolvePaths(app);
} catch (e) {
debug(e.stack || e);
return next();
}
Expand Down Expand Up @@ -756,25 +751,27 @@ CLI.remote = function(command, opts, cb) {
}, 300);
});
});
}
};

/**
* Start or restart|reload|gracefulReload a JSON configuration file
* @param {string} action restart|reload
* @param {string} json_conf json file path
* @param {string} opts option like environment type and co
* @callback cb optionnal
* @callback cb optional
* @param cb
*/
CLI._jsonStartOrAction = function(action, json_conf, opts, cb) {
try {
var data = fs.readFileSync(json_conf);
var data = fs.readFileSync(json_conf);
} catch(e) {
printError('Configuration file %s is missing. Action canceled.', json_conf);
return cb ? cb(e) : exitCli(cst.ERROR_EXIT);
}

var appConf = json5.parse(data);
var appConf = json5.parse(data), deployConf = null;
// v2 JSON declaration
if (appConf.deploy) deployConf = appConf.deploy;
if (appConf.apps) appConf = appConf.apps;

if ((appConf = verifyConfs(appConf)) == null)
Expand All @@ -796,20 +793,13 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) {
});

async.eachLimit(apps_to_start, cst.CONCURRENT_ACTIONS, function(app, next) {
mergeEnvironmentVariables(app, opts.env, deployConf);
var resolved_paths = null;
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) {
resolved_paths = 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) {
Expand Down Expand Up @@ -858,7 +848,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, deployConf);
});
// Assigns own enumerable properties of all
// Notice: if people use the same name in different apps,
Expand Down Expand Up @@ -1366,7 +1356,7 @@ var Version = require('./tools/VersionManagement.js');
*/
CLI.pullAndRestart = function (process_name, cb) {
Version._pull({process_name: process_name, action: 'restart'}, cb);
}
};

/**
* CLI method for updating a repository
Expand All @@ -1376,7 +1366,7 @@ CLI.pullAndRestart = function (process_name, cb) {
*/
CLI.pullAndReload = function (process_name, cb) {
Version._pull({process_name: process_name, action: 'reload'}, cb);
}
};

/**
* CLI method for updating a repository
Expand All @@ -1386,7 +1376,7 @@ CLI.pullAndReload = function (process_name, cb) {
*/
CLI.pullAndGracefulReload = function (process_name, cb) {
Version._pull({process_name: process_name, action: 'gracefulReload'}, cb);
}
};

/**
* CLI method for updating a repository to a specific commit id
Expand All @@ -1396,7 +1386,7 @@ CLI.pullAndGracefulReload = function (process_name, cb) {
*/
CLI.pullCommitId = function (opts, cb) {
Version.pullCommitId(opts.pm2_name, opts.commit_id);
}
};

/**
* CLI method for downgrading a repository to the previous commit (older)
Expand All @@ -1414,6 +1404,10 @@ CLI.backward = Version.backward;
*/
CLI.forward = Version.forward;

//
// Private methods
//

/**
* Description
* @method getInteractInfo
Expand Down Expand Up @@ -1441,10 +1435,6 @@ function getInteractInfo(cb) {
});
}


//
// Private methods
//
var gl_retry = 0;
/**
* Description
Expand Down Expand Up @@ -1487,10 +1477,45 @@ function speedList() {
});
}

/**
* Extend the app.env object of with the properties taken from the app.env_[envName] and deploy configuration.
* @param {Object} app The app object.
* @param {string} envName The given environment name.
* @param {Object} deployConf Deployment configuration object (from JSON file or whatever).
* @returns {Object} The app.env variables object.
*/
function mergeEnvironmentVariables(app, envName, deployConf) {
if (!envName) {
// The environment name was not set with either `pm2 deploy envName` or `--env envName`
app.env = app.env || {};
} else {
var finalEnv = {};

// First merge variables from deploy.production.env object as least priority.
if (deployConf && deployConf[envName] && deployConf[envName]['env']) {
util._extend(finalEnv, deployConf[envName]['env']);
}

// Then merge app.env object.
if (app.env) {
util._extend(finalEnv, app.env);
}

// Then, last and highest priority, merge the app.env_production object.
if ('env_' + envName in app) {
util._extend(finalEnv, app['env_' + envName]);
}

app.env = finalEnv;
}

return app.env;
}

/**
* Description
* @method resolvePaths
* @param {} appConf
* @param {object} appConf
* @return app
*/
function resolvePaths(appConf) {
Expand All @@ -1505,7 +1530,6 @@ function resolvePaths(appConf) {
if (app instanceof Error) {
printError(cst.PREFIX_MSG_ERR + app.message);
throw new Error(app.message);
return null;
}
return app;
}
Expand Down Expand Up @@ -1570,7 +1594,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)
Expand All @@ -1591,7 +1615,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) &&
Expand Down Expand Up @@ -1642,13 +1666,14 @@ function prepareInterpreter(conf){
conf.exec_interpreter = betterInterpreter || 'none';
}
}

/**
* Show warnings
* @param {String} warning
*/
function warn(warning){
printOut(cst.PREFIX_MSG_WARNING + warning);
};
}

function deployHelp() {
console.log('');
Expand Down
5 changes: 4 additions & 1 deletion lib/samples/sample.json5
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
ref : "origin/master",
repo : "git@github.com:repo.git",
path : "/var/www/development",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev"
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev",
env : {
NODE_ENV: "dev"
}
}
}
}
9 changes: 9 additions & 0 deletions test/bash/env-refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ sleep 0.5
grep "YES" out-env.log &> /dev/null
spec "should contain env variable"


$pm2 restart env-refreshed.json
>out-env.log

sleep 0.5
grep "HEYYYY" out-env.log &> /dev/null
spec "should contain refreshed env variable via json"


$pm2 start env-ecosystem.json --env production
>out-env.log

sleep 0.5
grep "No worries!" out-env.log &> /dev/null
spec "should use deploy.production.env.TEST_VARIABLE"
23 changes: 23 additions & 0 deletions test/fixtures/env-ecosystem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"apps" : [{
"name" : "env2",
"script" : "./env.js",
"out_file" : "out-env.log",
"merge_logs" : true,
"env": {
"NODE_ENV": "production"
}
}],
"deploy" : {
"production" : {
"user" : "node",
"host" : "212.83.163.1",
"ref" : "origin/master",
"repo" : "git@github.com:repo.git",
"path" : "/var/www/production",
"env" : {
"TEST_VARIABLE": "No worries!"
}
}
}
}

0 comments on commit 677e9b3

Please sign in to comment.