From 131fb70c2ed544d3477d4aab46cff83d1be5cacc Mon Sep 17 00:00:00 2001 From: cronopio Date: Tue, 12 Feb 2013 16:34:01 -0500 Subject: [PATCH] [fix] final tweak to allow custom settings on bussiness plans --- lib/jitsu/commands/apps.js | 129 ++++++++++++++++++++++++++++++++++--- test/commands/apps-test.js | 9 ++- 2 files changed, 127 insertions(+), 11 deletions(-) diff --git a/lib/jitsu/commands/apps.js b/lib/jitsu/commands/apps.js index 8d97b75..48a2723 100644 --- a/lib/jitsu/commands/apps.js +++ b/lib/jitsu/commands/apps.js @@ -63,13 +63,51 @@ function handleStartError(err, name, callback) { // 5. Starts the application // apps.deploy = function (callback) { - var dir = process.cwd(), pkg; + var dir = process.cwd(), + args = utile.args(arguments), + cloud = {}, + pkg; // // Allows arbitrary amount of arguments to deploy // if (arguments.length) { - callback = utile.args(arguments).callback; + callback = args.callback; + } + + // + // Allow use provider name and datacenter name without flags (--provider, --datacenter) + // + if (arguments.length === 2) { + jitsu.log.error('Error: No datacenter name specified'); + jitsu.log.error('Please use a valid datacenter name.'); + + return jitsu.apps.endpoints(function (err, endpoints) { + if (err) return callback(err); + + if (endpoints) { + jitsu.log.info('You can use one of the following providers'); + Object.keys(endpoints).forEach(function (provider) { + var datacenters = endpoints[provider]; + Object.keys(datacenters).forEach(function (datacenter) { + jitsu.log.data('jitsu deploy ' + provider + ' ' + datacenter); + }); + }); + } + + return callback(new Error(), true); + }); + } + + if (arguments.length === 3) { + cloud.provider = args[0]; + cloud.datacenter = args[1]; + } + + // Setup of cloud parameters specified using (--) + if (jitsu.argv['provider'] || jitsu.argv['datacenter']) { + cloud.provider = jitsu.argv['provider']; + cloud.datacenter = jitsu.argv['datacenter']; } function promptLogin () { @@ -101,13 +139,46 @@ apps.deploy = function (callback) { // existing.state is thrown away in check app. // these closures would need to be rearanged so that this closure can see the result of view.. // - apps.start(existing.name, function (err) { + + // + // Enable custom drone number and memory ram + // + if (jitsu.argv['drones'] && typeof jitsu.argv['drones'] === 'number') { + cloud.drones = jitsu.argv['drones']; + } else if (cloud.provider && cloud.datacenter) { + // Default drones to 1 + cloud.drones = 1; + } + + if (jitsu.argv['ram'] && typeof jitsu.argv['ram'] === 'number') { + if ([1, 256, 512, 1024].indexOf(jitsu.argv['ram']) === -1) { + jitsu.log.warn('Invalid value of parameter: --ram, using 256 as default'); + cloud.ram = 256; + } else { + cloud.ram = jitsu.argv['ram']; + } + } else if (cloud.provider && cloud.datacenter) { + // Default ram to 256 + cloud.ram = 256; + } + + var next = function next (err) { if (err) { return callback(err); } jitsu.package.runScript(pkg, 'postdeploy', callback); - }); + }; + + // Only if all options are correct we pass cloud options + if (cloud.provider && cloud.datacenter && cloud.drones && cloud.ram) { + jitsu.log.info('Deploying application in cloud with:') + jitsu.inspect.putObject(cloud); + apps.start(existing.name, cloud, next); + } else { + apps.start(existing.name, next); + } + //} //else { // apps.start(existing.name, callback); @@ -574,7 +645,7 @@ apps.destroy.usage = [ // Starts the application specified by `name`. If no name is supplied // this will start the application in the current directory. // -apps.start = function (name, callback) { +apps.start = function (name, cloud, callback) { // // Allows arbitrary amount of arguments @@ -583,26 +654,66 @@ apps.start = function (name, callback) { var args = utile.args(arguments); callback = args.callback; name = args[0] || null; + cloud = args[1] || null; + } + + // Little check when call jitsu start directly + if (typeof cloud === 'string') { + cloud = { provider: cloud }; + if (args[2]) { + cloud.datacenter = args[2]; + } else { + jitsu.log.error('Error: No datacenter name specified'); + jitsu.log.error('Please use a valid datacenter name.'); + return callback(new Error(), true) + } } function executeStart() { jitsu.log.info('Starting app ' + name.magenta); - jitsu.apps.start(name, function (err) { + var showInfo = function showInfo (err, config) { if (err) { return handleStartError(err, name, callback); } - jitsu.apps.view(name, function (err, app) { + async.series({ + // + // 1. Fetch the endpoints so that we can properly + // tell the user what datacenter they are in later. + // + endpoints: function getEndpoints(next) { + jitsu.apps.endpoints(next); + }, + // + // 2. Get app information to show the proper subdomain. + // + app: function start(next) { + jitsu.apps.view(name, next); + } + }, function (err, result) { if (err) { return callback(err); } + var endpoints = result.endpoints, + tld = (config && config.provider && config.datacenter) + ? result.endpoints[config.provider][config.datacenter] + : 'api.jit.su', + subdomain = result.app.subdomain; + jitsu.log.info('App ' + name.magenta + ' is now started'); - jitsu.log.info(('http://' + app.subdomain + '.jit.su').magenta + ' on Port ' + '80'.magenta); + jitsu.log.info(('http://' + subdomain + tld.replace('api', '')).magenta + ' on Port ' + '80'.magenta); callback(); }); - }); + } + + if (cloud) { + return jitsu.apps.start(name, [cloud], showInfo); + } else { + return jitsu.apps.start(name, showInfo); + } + } if (!name) { diff --git a/test/commands/apps-test.js b/test/commands/apps-test.js index db67fea..4a7f055 100644 --- a/test/commands/apps-test.js +++ b/test/commands/apps-test.js @@ -42,13 +42,13 @@ function shouldAcceptAllCloudOptions(suite, command) { '--drones 2 --ram 512' ]; - if (command === 'deploy') { + if (/^deploy/.test(command)) { combinations.push('--provider joyent'); combinations.push('--datacenter us-east-1'); combinations.push('--provider joyent --datacenter us-east-1'); } - var setupFn = (command === 'deploy') ? setupDeploy : setupCloud; + var setupFn = (/^deploy/.test(command)) ? setupDeploy : setupCloud; combinations.forEach(function (argv) { var drones = /--drones\s(\d{1})/.exec(argv), @@ -875,4 +875,9 @@ shouldAcceptAllCloudOptions( 'deploy' ); +shouldAcceptAllCloudOptions( + suite, + 'deploy joyent us-east-1' +); + suite.export(module); \ No newline at end of file