Skip to content

Instantly share code, notes, and snippets.

@wamoyo
Created January 24, 2015 07:03
Show Gist options
  • Save wamoyo/bb11081f79bc20aeae34 to your computer and use it in GitHub Desktop.
Save wamoyo/bb11081f79bc20aeae34 to your computer and use it in GitHub Desktop.
Doing a Blue-Green (zero downtime) deployment for node.js apps on Bluemix (not finished yet)
/*
* Use this to do a blue-green (zero downtime) deployment on Bluemix for Node.js apps.
*/
var bash = require('child_process');
var util = require('util');
var routesBash;
var events = require('events');
var emitter = new events.EventEmitter();
emitter.steps = [];
// Grab User-Defined Environment Variables.
var envBash = bash.spawn('cf', ['env', process.argv[2]]);
var varsString = '';
var envVars = [];
envBash.stdout.on('data', grabEnvVars);
function grabEnvVars (data) {
varsString += data;
}
envBash.stdout.on('end', cleanEnvVars);
function cleanEnvVars () {
var cleaner = varsString.split('User-Provided:\n')[1].split('\n\n')[0].split('\n');
cleaner.forEach(function (element, index, array) {
var obj = {};
obj[element.split(/:\s/)[0]] = element.split(/:\s/)[1];
envVars.push(obj);
});
}
envBash.on('exit', function (code, signal) {
return code === 0 ? (emitter.emit('got vars'), emitter.steps.push('got vars')) : console.log("Failure : (", code, signal);
});
// Grab Routes
var routesBash = bash.spawn('cf', ['apps']);
var routesString = '';
var routes = [];
var instances;
var memory;
routesBash.stdout.on('data', grabRoutes);
function grabRoutes (data) {
routesString += data;
}
routesBash.stdout.on('end', cleanRoutes);
function cleanRoutes () {
var cleaner = routesString.slice(routesString.indexOf(process.argv[2])).split('\n')[0];
routes = cleaner.split(/,?\s+/);
routes.pop(0);
instances = routes[2].split('/')[0];
memory = routes[3];
routes.splice(0,5);
}
routesBash.on('exit', function (code, signal) {
return code === 0 ? (emitter.emit('got routes'), emitter.steps.push('got routes')) : console.log("Failure : (", code, signal);
});
// Once we have vars and routes, push a green app up.
emitter.on('got vars', setupGreen);
emitter.on('got routes', setupGreen);
function setupGreen () {
emitter.steps.length === 1 && goGreen();
}
//var greenBash = bash.spawn('cf', ['push', '-m', memory, '-i', instances]);
function goGreen () {
console.log("Routes\n", routes);
console.log("Environment Variables\n", envVars)
console.log("Memory:", memory, "Instances:", instances);
process.stdout.write('\033[34m Everything look good? (Yes/No) >');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (text) {
if (text.match(/^yes\n$/ig) || text.match(/^Y\n$/i)) {
console.log('Continuing!');
process.exit();
done();
} else if (text.match(/^no\n$/ig) || text.match(/^N\n$/i)) {
console.log('Canceling!');
process.exit();
done();
} else {
console.log('Whaaaa?!');
}
});
}
// On Errors...
envBash.on('error', errorOut);
routesBash.on('error', errorOut);
function errorOut (code, signal) {
return console.log("Some kind of Bash Error: ", code, signal) && bash.disconnect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment