Skip to content

Commit

Permalink
Merge pull request #387 from nodejitsu/allowing-parameters
Browse files Browse the repository at this point in the history
Allowing parameters
  • Loading branch information
AvianFlu committed Feb 12, 2013
2 parents 640523b + a3b4dc1 commit 5688fe4
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 12 deletions.
129 changes: 120 additions & 9 deletions lib/jitsu/commands/apps.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -30,7 +30,7 @@
"fstream-npm": "0.1.1",
"ladder": "0.0.0",
"npm": "1.1.59",
"nodejitsu-api": "0.4.1",
"nodejitsu-api": "0.4.2",
"opener": "1.3.x",
"pkginfo": "0.2.3",
"progress": "0.1.0",
Expand Down
9 changes: 7 additions & 2 deletions test/commands/apps-test.js
Expand Up @@ -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),
Expand Down Expand Up @@ -875,4 +875,9 @@ shouldAcceptAllCloudOptions(
'deploy'
);

shouldAcceptAllCloudOptions(
suite,
'deploy joyent us-east-1'
);

suite.export(module);
2 changes: 2 additions & 0 deletions test/commands/snapshots-test.js
Expand Up @@ -103,6 +103,8 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.reply(200, '', { 'x-powered-by': 'Nodejitsu' })
.post('/apps/tester/application2/start', {})
.reply(200, '', { 'x-powered-by': 'Nodejitsu' })
.get('/endpoints')
.reply(200, endpoints, { 'x-powered-by': 'Nodejitsu' })
.get('/apps/tester/application2', '')
.reply(200, { app: {
subdomain: "tester.application2"
Expand Down

0 comments on commit 5688fe4

Please sign in to comment.