From ee8228f28303a6bb93ca3d6bfee3e95a51a5fb9a Mon Sep 17 00:00:00 2001 From: Arjun Gadhia Date: Fri, 1 May 2015 16:40:39 +0100 Subject: [PATCH 1/4] WIP: sketch of what nbt scale should do --- README.md | 3 +- bin/next-build-tools.js | 8 +++ main.js | 1 + tasks/scale.js | 113 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tasks/scale.js diff --git a/README.md b/README.md index 1f851370..7bd486b4 100644 --- a/README.md +++ b/README.md @@ -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 [app] [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 @@ -17,7 +18,7 @@ deploy-vcl [options] [folder] Deploys VCL in [folder] to the specified fastly service. Requires FASTLY_KEY env var run Runs the local app through the router deploy-static [options] [otherSources...] Deploys static to [destination] on S3 (where [destination] is a full S3 URL). Requires AWS_ACCESS and AWS_SECRET env vars - * + * Options: diff --git a/bin/next-build-tools.js b/bin/next-build-tools.js index bad6608b..caaf6b5b 100755 --- a/bin/next-build-tools.js +++ b/bin/next-build-tools.js @@ -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'); @@ -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') diff --git a/main.js b/main.js index f7f295ae..d3452e3e 100644 --- a/main.js +++ b/main.js @@ -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') }; diff --git a/tasks/scale.js b/tasks/scale.js new file mode 100644 index 00000000..00ba5322 --- /dev/null +++ b/tasks/scale.js @@ -0,0 +1,113 @@ +'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 || source; + var overrides = {}; + var token; + + if (opts.overrides) { + opts.overrides.map(function (o) { + var t = o.split('='); + overrides[t[0]] = t[1]; + }); + } + + var authorizedPostHeaders = { + 'Accept': 'application/vnd.heroku+json; version=3', + 'Content-Type': 'application/json' + }; + + function getProcessInfo(serviceData) { + return serviceData && + serviceData.versions && + serviceData.versions[Object.keys(serviceData.versions).length.toString()] && + serviceData.versions[Object.keys(serviceData.versions).length.toString()].processes; + } + + 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 + }); + }; + } + + // scale({ + // token: token, + // app: normalizeName(packageJson.name), + // processProfiles: { + // updates: [ + + // ] + // } + // }) + + + // desired["___WARNING___"] = "Don't edit config vars manually. Make PR to git.svc.ft.com/projects/NEXTPRIVATE/repos/config-vars/browse"; + // var patch = {}; + + // Object.keys(current).forEach(function(key) { + // patch[key] = null; + // }); + + // Object.keys(desired).forEach(function(key) { + // patch[key] = desired[key]; + // }); + + // Object.keys(overrides).forEach(function(key) { + // patch[key] = overrides[key]; + // }); + + // Object.keys(patch).forEach(function(key) { + // if (patch[key] === null) { + // console.log("Deleting config var: " + key); + // } else if (patch[key] !== current[key]) { + // console.log("Setting config var: " + key); + // } + // }); + + // console.log("Setting environment keys", Object.keys(patch)); + + // return fetch('https://api.heroku.com/apps/' + target + '/config-vars', { + // headers: authorizedPostHeaders, + // method: 'patch', + // body: JSON.stringify(patch) + // }); + }) + .then(function() { + console.log(target + " config vars are set"); + }); +}; From e8e711048c1588ae361b6079cdc4694bee1454dd Mon Sep 17 00:00:00 2001 From: Arjun Gadhia Date: Fri, 1 May 2015 16:43:40 +0100 Subject: [PATCH 2/4] Ooh generated readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7bd486b4..544fa6ba 100644 --- a/README.md +++ b/README.md @@ -7,7 +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 [app] [target] downloads process information from next-service-registry and scales/sizes the application servers + 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 @@ -18,7 +18,7 @@ deploy-vcl [options] [folder] Deploys VCL in [folder] to the specified fastly service. Requires FASTLY_KEY env var run Runs the local app through the router deploy-static [options] [otherSources...] Deploys static to [destination] on S3 (where [destination] is a full S3 URL). Requires AWS_ACCESS and AWS_SECRET env vars - * + * Options: From 1d8157db1172ba46738b6d9a76e582f9673d89cb Mon Sep 17 00:00:00 2001 From: Arjun Gadhia Date: Fri, 1 May 2015 16:48:27 +0100 Subject: [PATCH 3/4] Tidy up --- tasks/scale.js | 49 ++++--------------------------------------------- 1 file changed, 4 insertions(+), 45 deletions(-) diff --git a/tasks/scale.js b/tasks/scale.js index 00ba5322..8f47a970 100644 --- a/tasks/scale.js +++ b/tasks/scale.js @@ -9,7 +9,7 @@ var fetchres = require('fetchres'); module.exports = function(opts) { var source = opts.source || normalizeName(packageJson.name, { version: false }); - var target = opts.target || source; + var target = opts.target || normalizeName(packageJson.name); var overrides = {}; var token; @@ -20,11 +20,6 @@ module.exports = function(opts) { }); } - var authorizedPostHeaders = { - 'Accept': 'application/vnd.heroku+json; version=3', - 'Content-Type': 'application/json' - }; - function getProcessInfo(serviceData) { return serviceData && serviceData.versions && @@ -62,50 +57,14 @@ module.exports = function(opts) { size: processInfo[process].size, quantity: processInfo[process].scale }); - }; + } } // scale({ // token: token, - // app: normalizeName(packageJson.name), - // processProfiles: { - // updates: [ - - // ] - // } + // app: target + // processProfiles: processProfiles // }) - - - // desired["___WARNING___"] = "Don't edit config vars manually. Make PR to git.svc.ft.com/projects/NEXTPRIVATE/repos/config-vars/browse"; - // var patch = {}; - - // Object.keys(current).forEach(function(key) { - // patch[key] = null; - // }); - - // Object.keys(desired).forEach(function(key) { - // patch[key] = desired[key]; - // }); - - // Object.keys(overrides).forEach(function(key) { - // patch[key] = overrides[key]; - // }); - - // Object.keys(patch).forEach(function(key) { - // if (patch[key] === null) { - // console.log("Deleting config var: " + key); - // } else if (patch[key] !== current[key]) { - // console.log("Setting config var: " + key); - // } - // }); - - // console.log("Setting environment keys", Object.keys(patch)); - - // return fetch('https://api.heroku.com/apps/' + target + '/config-vars', { - // headers: authorizedPostHeaders, - // method: 'patch', - // body: JSON.stringify(patch) - // }); }) .then(function() { console.log(target + " config vars are set"); From 46d4ca5daf93110730269716a419e6a0df2f17b4 Mon Sep 17 00:00:00 2001 From: Arjun Gadhia Date: Tue, 5 May 2015 11:04:30 +0100 Subject: [PATCH 4/4] Make the actual scale work --- tasks/scale.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tasks/scale.js b/tasks/scale.js index 8f47a970..2a3cdf07 100644 --- a/tasks/scale.js +++ b/tasks/scale.js @@ -4,12 +4,12 @@ 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'); +var scale = require('haikro/lib/scale'); module.exports = function(opts) { var source = opts.source || normalizeName(packageJson.name, { version: false }); - var target = opts.target || normalizeName(packageJson.name); + var target = opts.target || packageJson.name; var overrides = {}; var token; @@ -27,6 +27,7 @@ module.exports = function(opts) { 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; @@ -60,13 +61,18 @@ module.exports = function(opts) { } } - // scale({ - // token: token, - // app: target - // processProfiles: processProfiles - // }) + return scale({ + token: token, + app: target, + processProfiles: processProfiles + }); + + }) - .then(function() { - console.log(target + " config vars are set"); + .then(function(processProfiles) { + console.log(target + " config vars are set to", processProfiles); + }) + .catch(function(err) { + console.log ('Error scaling processes - ' + err); }); };