Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #97 from Financial-Times/nbt-scale
Browse files Browse the repository at this point in the history
nbt scale
  • Loading branch information
adgad committed May 5, 2015
2 parents 3ae428e + 46d4ca5 commit dba0122
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

deploy [app] runs haikro deployment scripts with sensible defaults for Next projects
configure [options] [source] [target] downloads environment variables from next-config-vars and uploads them to the current app
scale [source] [target] downloads process information from next-service-registry and scales/sizes the application servers
provision [app] provisions a new instance of an application server
verify [options] internally calls origami-build-tools verify with some Next specific configuration (use only for APPLICATIONS. Front End components should continue to use origami-build-tools verify)
nightwatch [options] [test] runs nightwatch with some sensible defaults
Expand Down
8 changes: 8 additions & 0 deletions bin/next-build-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require('isomorphic-fetch');
var program = require('commander');
var deploy = require('../tasks/deploy');
var configure = require('../tasks/configure');
var scale = require('../tasks/scale');
var provision = require('../tasks/provision');
var verify = require('../tasks/verify');
var build = require('../tasks/build');
Expand Down Expand Up @@ -44,6 +45,13 @@ program
configure({ source: source, target: target, overrides: options.overrides }).catch(exit);
});

program
.command('scale [source] [target]')
.description('downloads process information from next-service-registry and scales/sizes the application servers')
.action(function(source, target) {
scale({ source: source, target: target }).catch(exit);
});

program
.command('provision [app]')
.description('provisions a new instance of an application server')
Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ module.exports = {
nightwatch: require('./tasks/nightwatch'),
provision: require('./tasks/provision'),
purge: require('./tasks/purge'),
scale: require('./tasks/scale'),
verify: require('./tasks/verify')
};
78 changes: 78 additions & 0 deletions tasks/scale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var packageJson = require(process.cwd() + '/package.json');
var herokuAuthToken = require('../lib/heroku-auth-token');
var normalizeName = require('../lib/normalize-name');
var fetchres = require('fetchres');
var scale = require('haikro/lib/scale');

module.exports = function(opts) {

var source = opts.source || normalizeName(packageJson.name, { version: false });
var target = opts.target || packageJson.name;
var overrides = {};
var token;

if (opts.overrides) {
opts.overrides.map(function (o) {
var t = o.split('=');
overrides[t[0]] = t[1];
});
}

function getProcessInfo(serviceData) {
return serviceData &&
serviceData.versions &&
serviceData.versions[Object.keys(serviceData.versions).length.toString()] &&
serviceData.versions[Object.keys(serviceData.versions).length.toString()].processes;
}

console.log('Scaling ' + target + ' using service registry information for ' + source);
return herokuAuthToken()
.then(function(authToken) {
token = authToken;
})
.then(function() {
return fetch('https://ft-next-service-registry.herokuapp.com/services');
})
.then(fetchres.json)
.then(function(data) {
var serviceData = data.filter(function(service) {
return service.name === source;
});
serviceData = serviceData.length ? serviceData[0] : null;

var processInfo = getProcessInfo(serviceData);

if(!processInfo) {
throw new Error("Could not get process info for " + serviceData.name + ". Please check the service registry.");
}
var processProfiles = {
updates: []
};

for( var process in processInfo ) {
if(processInfo.hasOwnProperty(process)) {
processProfiles.updates.push({
process: process,
size: processInfo[process].size,
quantity: processInfo[process].scale
});
}
}

return scale({
token: token,
app: target,
processProfiles: processProfiles
});


})
.then(function(processProfiles) {
console.log(target + " config vars are set to", processProfiles);
})
.catch(function(err) {
console.log ('Error scaling processes - ' + err);
});
};

0 comments on commit dba0122

Please sign in to comment.