Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[ux] After "get"ing a database, print help with example usage #288

Merged
merged 3 commits into from

6 participants

@jfhbrook

When running `jitsu databases create ' and 'jitsu databases get ', jitsu will print help with examples on how to connect to the databases.

Redis:


josh@onix:~/dev/nodejitsu/jitsu$ jitsu databases create r testRedis
info:    Welcome to Nodejitsu jesusabdullah
info:    It worked if it ends with Nodejitsu ok
info:    Executing command databases create r testRedis
info:    A new redis has been created
data:    Database Type: redis
data:    Database Name: testRedis
data:    Connection host: chubb.redistogo.com
data:    Connection port: 9723
data:    Connection auth: f99172175be19664fdd1970acb6c5fab
help:    
help:    Connect with the `redis-cli` cli tool:
help:    
help:        $ redis-cli -h chubb.redistogo.com -p 9723 -a f99172175be19664fdd1970acb6c5fab
help:    
help:    Connect with the `redis` module:
help:    
help:        var redis = require('redis');
help:        var client = redis.createClient('chubb.redistogo.com', 9723);
help:        client.auth('f99172175be19664fdd1970acb6c5fab', function (err) {
help:          if (err) { throw err; }
help:          // You are now connected to your redis.
help:        });
help:    
info:    Nodejitsu ok
josh@onix:~/dev/nodejitsu/jitsu$ 

Mongo:

josh@onix:~/dev/nodejitsu/jitsu$ jitsu databases get testMongo
info:    Welcome to Nodejitsu jesusabdullah
info:    It worked if it ends with Nodejitsu ok
info:    Executing command databases get testMongo
data:    Database Type: mongo
data:    Database Name: testMongo
data:    Connection url: mongodb://nodejitsu:b94f0f3d4dc07b816a31521b672e23c3@staff.mongohq.com:10027/nodejitsudb42205449082
help:    
help:    Connect with the `mongo` cli client:
help:    
help:        $ mongo staff.mongohq.com:10027/nodejitsudb42205449082 -u nodejitsu -p b94f0f3d4dc07b816a31521b672e23c3
help:    
help:    Connect with the `mongodb-native module`:
help:    
help:        var mongodb = require('mongodb');
help:        var db = new mongodb.Db('nodejitsudb42205449082',
help:          new mongodb.Server('staff.mongohq.com', 10027, {})
help:        );
help:        db.open(function (err, db_p) {
help:          if (err) { throw err; }
help:          db.authenticate('nodejitsu', 'b94f0f3d4dc07b816a31521b672e23c3', function (err, replies) {
help:            // You are now connected and authenticated.
help:          });
help:        });
help:    
help:    Connect with the `mongoose` module:
help:    
help:        var mongoose = require('mongoose');
help:        mongoose.connect('mongodb://nodejitsu:b94f0f3d4dc07b816a31521b672e23c3@staff.mongohq.com:10027/nodejitsudb42205449082');
help:    
info:    Nodejitsu ok

Couch:

josh@onix:~/dev/nodejitsu/jitsu$ jitsu databases get testCouch
info:    Welcome to Nodejitsu jesusabdullah
info:    It worked if it ends with Nodejitsu ok
info:    Executing command databases get testCouch
data:    Database Type: couch
data:    Database Name: testCouch
data:    Connection url: http://nodejitsudb944957670256.iriscouch.com:5984
data:    SSL connection url: https://nodejitsudb944957670256.iriscouch.com:6984
help:    
help:    Connect with `curl`:
help:    
help:        $ curl http://nodejitsudb944957670256.iriscouch.com:5984
help:    
help:    Connect with the `futon` web app (open in your browser):
help:    
help:        http://nodejitsudb944957670256.iriscouch.com:5984/_utils
help:    
help:    Connect with the `nano` module:
help:    
help:        var nano = require('nano')('http://nodejitsudb944957670256.iriscouch.com:5984');
help:    
info:    Nodejitsu ok
josh@onix:~/dev/nodejitsu/jitsu$ 
@jfhbrook jfhbrook commented on the diff
lib/jitsu/commands/databases.js
@@ -15,7 +15,9 @@ databases.usage = [
'jitsu databases create <database type> <database name>',
'jitsu databases list',
'jitsu databases get <database name>',
- 'jitsu databases destroy <database name>'
+ 'jitsu databases destroy <database name>',
+ '',
+ 'Valid database types are: ' + 'couch'.magenta + ', ' + 'redis'.magenta + ', or ' + 'mongo'.magenta

Someone pointed out that this information wasn't here, so I added it while I was in the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jfhbrook jfhbrook commented on the diff
lib/jitsu/commands/databases.js
((6 lines not shown))
printBase(database);
- jitsu.log.data('Connection url: ' + ('redis://nodejitsu:' + password+ '@' + server + '.redistogo.com:' + port + '/').grey);
+ jitsu.log.data('Connection host: ' + server.grey);
+ jitsu.log.data('Connection port: ' + String(port).grey);

Redis doesn't have this concept of connection strings. It also doesn't have a concept of a username. So, I fixed this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jfhbrook

One other note: There's actually quite a bit of other metadata we could display, particularly for the redis instances (plan size, max memory, etc). I wouldn't be surprised if iriscouch and mongohq returned more data than we return in our api as well.

@cronopio
Collaborator

@jesusabdullah yes.

Actually, we have other open issues related to get more info about databases. Redis have that part well documented but for other providers the docs for espeific requesting more metada of size growing or memory use are not pretty clear.

There other way (only mongo), just a little trick in jitsu for when request metada, using some mongo client connect and send the db.stats() command. Should be a similar command or URL for hit in couch but I dont know.

@jfhbrook

I just added a commit that prints the code snippets without help: in front of it. I'm happy with this PR if everyone else is.

@mmalecki mmalecki merged commit adb7b59 into from
@mmalecki

Merged, thanks Josh.

@3rd-Eden

the mongodb driver also has a simple connect method that uses the mongodb:// string to connect. So you don't have to do all the db.open and auth madness.

require('mongodb').connect(string, {options}, function (err, db) {
   // authed and opened
});

See https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L1936-1950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 130 additions and 7 deletions.
  1. +128 −5 lib/jitsu/commands/databases.js
  2. +2 −2 package.json
View
133 lib/jitsu/commands/databases.js
@@ -15,7 +15,9 @@ databases.usage = [
'jitsu databases create <database type> <database name>',
'jitsu databases list',
'jitsu databases get <database name>',
- 'jitsu databases destroy <database name>'
+ 'jitsu databases destroy <database name>',
+ '',
+ 'Valid database types are: ' + 'couch'.magenta + ', ' + 'redis'.magenta + ', or ' + 'mongo'.magenta

Someone pointed out that this information wasn't here, so I added it while I was in the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
];
databases.available = ['couch', 'redis', 'mongo'];
@@ -161,6 +163,7 @@ databases.get = function (databaseName, callback) {
}
else {
printDatabase(results);
+ printDbHelp(results);
}
callback();
});
@@ -235,8 +238,12 @@ var printDatabase = function (database) {
case 'couch':
var subdomain = database.metadata.id.split('/')[1];
printBase(database);
- jitsu.log.data('Connection url: ' + ('http://' + subdomain + '.iriscouch.com:5984').grey);
- jitsu.log.data('SSL connection url: ' + ('https://' + subdomain + '.iriscouch.com:6984').grey);
+
+ var connUrl = 'http://' + subdomain + '.iriscouch.com:5984',
+ sslConnUrl = 'https://' + subdomain + '.iriscouch.com:6984';
+
+ jitsu.log.data('Connection url: ' + connUrl.grey);
+ jitsu.log.data('SSL connection url: ' + sslConnUrl.grey);
break;
case 'mongo':
@@ -247,10 +254,12 @@ var printDatabase = function (database) {
case 'redis':
var port = database.metadata.port,
password = database.metadata.password,
- server = database.metadata.label.split('-')[0];
+ server = database.metadata.label.split('-')[0] + '.redistogo.com';
printBase(database);
- jitsu.log.data('Connection url: ' + ('redis://nodejitsu:' + password+ '@' + server + '.redistogo.com:' + port + '/').grey);
+ jitsu.log.data('Connection host: ' + server.grey);
+ jitsu.log.data('Connection port: ' + String(port).grey);

Redis doesn't have this concept of connection strings. It also doesn't have a concept of a username. So, I fixed this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ jitsu.log.data('Connection auth: ' + password.grey);
break;
default:
@@ -259,3 +268,117 @@ var printDatabase = function (database) {
break;
}
};
+
+var printDbHelp = function (database) {
+
+ switch (database.type) {
+ case 'couch':
+ var subdomain = database.metadata.id.split('/')[1];
+
+ var connUrl = 'http://' + subdomain + '.iriscouch.com:5984',
+ sslConnUrl = 'https://' + subdomain + '.iriscouch.com:6984';
+
+ // TODO: Only write this if in some "verbose" mode.
+ [
+ '',
+ 'Connect with `' + 'curl'.magenta + '`:',
+ '',
+ ' $ curl ' + connUrl,
+ '',
+ 'Connect with the `' + 'futon'.magenta + '` web app (' + 'open in your browser'.cyan + '):',
+ '',
+ ' ' + connUrl + '/_utils',
+ '',
+ 'Connect with the `' + 'nano'.magenta + '` module:',
+ '',
+ ' var nano = require(\'nano\')(\'' + connUrl + '\');',
+ ''
+ ].forEach(printHelp);
+
+ break;
+
+ case 'mongo':
+ var parsed = require('url').parse(database.metadata.config.MONGOHQ_URL),
+ auth = parsed.auth.split(':');
+
+ // TODO: Only write this if in some "verbose" mode.
+ [
+ '',
+ 'Connect with the `' + 'mongo'.magenta + '` cli client:',
+ '',
+ ' $ mongo ' + parsed.host + parsed.path + ' -u ' + auth[0] + ' -p ' + auth[1],
+ '',
+ 'Connect with the `' + 'mongodb-native'.magenta + ' module`:',
+ '',
+ ' var mongodb = require(\'mongodb\');',
+ ' var db = new mongodb.Db(\'' + parsed.path.substr(1) + '\',',
+ ' new mongodb.Server(\'' + parsed.hostname + '\', ' + parsed.port +', {})',
+ ' );',
+ ' db.open(function (err, db_p) {',
+ ' if (err) { throw err; }',
+ ' db.authenticate(\'' + auth[0] + '\', \'' + auth[1] + '\', function (err, replies) {',
+ ' // You are now connected and authenticated.',
+ ' });',
+ ' });',
+ '',
+ 'Connect with the `' + 'mongoose'.magenta + '` module:',
+ '',
+ ' var mongoose = require(\'mongoose\');',
+ ' mongoose.connect(\'' + database.metadata.config.MONGOHQ_URL + '\');',
+ ''
+ ].forEach(printHelp);
+
+ break;
+
+ case 'redis':
+ var port = database.metadata.port,
+ password = database.metadata.password,
+ server = database.metadata.label.split('-')[0] + '.redistogo.com';
+
+ [
+ '',
+ 'Connect with the `' + 'redis-cli'.magenta + '` cli tool:',
+ '',
+ ' $ redis-cli -h ' + server + ' -p ' + port + ' -a ' + password,
+ '',
+ 'Connect with the `' + 'redis'.magenta + '` module:',
+ '',
+ ' var redis = require(\'redis\');',
+ ' var client = redis.createClient(\'' + server + '\', ' + port + ');',
+ ' client.auth(\'' + password + '\', function (err) {',
+ ' if (err) { throw err; }',
+ ' // You are now connected to your redis.',
+ ' });',
+ '',
+ ].forEach(printHelp);
+
+ break;
+
+ default:
+ break;
+ }
+
+ function printHelp(l) {
+ var lvl = 0,
+ helplvl = 6;
+
+ if (l.substr(0, 4) == ' ') {
+ // Only print help if the logger is configured such that help lvls
+ // should show
+ try {
+ lvl = jitsu.log.loggers.default.levels[
+ jitsu.log.loggers.default.level
+ ];
+ helplvl = jitsu.log.loggers.default.levels['help'];
+ }
+ finally {
+ if (lvl <= helplvl) {
+ console.log(' ' + l);
+ }
+ }
+ }
+ else {
+ jitsu.log.help(l);
+ }
+ }
+};
View
4 package.json
@@ -8,7 +8,7 @@
"platform-as-a-service",
"deployment"
],
- "version": "0.9.5",
+ "version": "0.9.6",
"author": "Nodejitsu Inc. <info@nodejitsu.com>",
"maintainers": [
"AvianFlu <avianflu@nodejitsu.com>",
@@ -29,7 +29,7 @@
"fstream": "0.1.18",
"fstream-npm": "0.1.0",
"npm": "1.1.16",
- "nodejitsu-api": "0.3.1",
+ "nodejitsu-api": "0.3.2",
"pkginfo": "0.2.3",
"request": "2.9.100",
"require-analyzer": "0.4.0-3",
Something went wrong with that request. Please try again.