Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add CLI params for authentication #12

Merged
merged 14 commits into from
Jul 19, 2017
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
cache:
directories:
- node_modules
sudo: false
services: mongodb
node_js:
- "7"
- "6"
- "5"
- "4"
- "0.12"
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,55 @@ Deployd requires a running MongoDB to start sucessfully. Check the [Deployd Requ
```bash
$ dpd create hello
$ cd hello
$ dpd -d
$ dpd
```
To start dpd with Database authentication:

```
dpd --host "127.0.0.1" -P '27017' -n "mymongodb" -u "myusername" -s "mypassword"

or

dpd --host "127.0.0.1" -P '27017' -n "mymongodb" -a "myusername:mypassword"

```

## dpd Command Options

```
Usage: dpd [options] [command]


Options:

-V, --version output the version number
-m, --mongod [path] path to mongod executable (defaults to `mongod`)
-p, --port [port] port to host server (defaults to 2403)
-w, --wait wait for input before exiting
-d, --dashboard start the dashboard immediately
-o, --open open in a browser
-e, --environment [env] defaults to development
-H, --host [host] specify host for mongo server
-P, --mongoPort [mongoPort] mongodb port to connect to
-n, --dbname [dbname] name of the mongo database
-a, --auth <auth> usesrname:password mongo server credentials
-u, --username <username> The user to authenticate as
-s, --password <password> The user's password
-c, --dbconn <dbconnectionstring> The MongoDB Connection String
--deploydPath [deploydPath] allow overriding the path to deployd main script
-h, --help output usage information


Commands:

create [project-name] create a project in a new directory
eg. `dpd create my-app`
keygen generate a key for remote access (./.dpd/keys.json)
showkey shows current key for connecting to remote dashboard (./.dpd/keys.json)
* [default] start the server in the current project in development mode
with an interactive shell/repl for interacting with the running server
e.g. dpd (starts server in current directory),
dpd my-app/app.dpd (starts app from file)
```

## License
Expand Down
10 changes: 8 additions & 2 deletions bin/dpd
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ cli.program
.option('-H, --host [host]', 'specify host for mongo server')
.option('-P, --mongoPort [mongoPort]', 'mongodb port to connect to')
.option('-n, --dbname [dbname]', 'name of the mongo database')
.option('-a, --auth', 'prompts for mongo server credentials')
.option('-a, --auth <auth>', ' usesrname:password mongo server credentials')
.option('-u, --username <username>', 'The user to authenticate as')
.option('-s, --password <password>', 'The user\'s password')
.option('-c, --dbconn <dbconnectionstring>', 'The MongoDB Connection String')
.option(' --deploydPath [deploydPath]', 'allow overriding the path to deployd main script');



cli.program
.command('create [project-name]')
.description('\tcreate a project in a new directory\n\teg. `dpd create my-app`')
Expand Down Expand Up @@ -52,4 +57,5 @@ cli.program

cli.program.parse(process.argv);

if (cli.program.args.length === 0) cli.start();
if (cli.program.args.length === 0) cli.start();

214 changes: 116 additions & 98 deletions lib/cli/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@
* External dependencies
*/
var http = require('http'),
program = require('commander'),
fs = require('fs'),
semver = require('semver'),
Step = require('step'),
shelljs = require('shelljs/global'),
path = require('path');
program = require('commander'),
fs = require('fs'),
semver = require('semver'),
Step = require('step'),
shelljs = require('shelljs/global'),
path = require('path');


/**
* Local modules
* Local modules
*/
var stop = require('./stop'),
mongod = require('../util/mongod'),
repl = require('../client/repl'),
packageInfo = require('../../package'),
latestversionFile = path.join(__dirname, '../../.latestversion'),
createServer = require('./createserver');
mongod = require('../util/mongod'),
repl = require('../client/repl'),
packageInfo = require('../../package'),
latestversionFile = path.join(__dirname, '../../.latestversion'),
createServer = require('./createserver');

/**
* Start the server
*/
var start = function(file) {
var start = function (file) {
var port = program.port,
host = program.host || '127.0.0.1',
dbname = program.dbname || '-deployd',
mongoPort = generatePort(),
env = program.environment || process.env.DPD_ENV || 'development',
retries = 0,
credentials;
host = program.host || '127.0.0.1',
dbname = program.dbname || '-deployd',
mongoPort = generatePort(),
env = program.environment || process.env.DPD_ENV || 'development',
retries = 0,
credentials = {};


if (!port) {
port = 2403;
retries = env === 'development' && 5;
}

if (program.mongoPort) {
mongoPort = Number(program.mongoPort);
mongoPort = Number(program.mongoPort);
}

if (file) {
Expand All @@ -62,115 +63,132 @@ var start = function(file) {
if (!test('-d', './data')) mkdir('-p', './data');

if (program.auth && program.host === undefined) {
console.error("Authentication requires the '-h' host flag... exiting.");
process.exit();
console.error("Authentication requires the '-h' host flag... exiting.");
process.exit();
}

if (program.host) {
if (program.auth) {
Step(function () {
var next = this;
credentials = {};
program.prompt('username: ', function(username){
if (username && username !== '') {
credentials.username = username;
next();
} else {
console.error('Username cannot be blank.');
process.exit();
}
});
},
function () {
var next = this;
program.password('Password: ', function(pass){
if (pass && pass !== '') {
credentials.password = pass;
next();
} else {
console.error('Password cannot be blank.');
process.exit();
}
});
},
startup
);




function setCredentials(username, password) {
Step(function () {
var next = this;
if (username && username !== '') {
credentials.username = username;
next();
} else {
startup();
console.error('Username cannot be blank.');
process.exit();
}
} else {
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup);
},
function () {
var next = this;
if (password && password !== '') {
credentials.password = password;
next();
} else {
console.error('Password cannot be blank.');
process.exit();
}
},
startup
);
}



if (program.host) {
if (program.auth) {
var auth = program.auth.split(":");
username = auth[0];
password = auth[1];
setCredentials(username, password);
} else if (program.username || program.password) {

setCredentials(program.username, program.password);
}
else {
startup();
}
}
else {
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup);
}




} else {
console.log("This directory does not contain a Deployd app!");
console.log("Use \"dpd create <appname>\" to create a new app");
console.log("or use \"dpd path/to/app.dpd\" to start an app in another directory");
stop(1);
}

function startup (err) {
if (err) {
console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)");
return stop(1);
}
function startup(err) {
if (err) {
console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)");
return stop(1);
}

var options = {port: port, env: 'development', db: {host: host, port: mongoPort, name: dbname}};

options.env = program.environment || process.env.DPD_ENV || options.env;
if(options.env !== 'development') console.log('starting in %s mode', options.env);

if(credentials !== undefined) options.db.credentials = credentials;
var dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
dpd.deploydPath = program.deploydPath;
function onListening () {
console.info('listening on port', options.port);
var commands = repl(dpd);
if (program.dashboard) {
commands.dashboard();
} else if (program.open) {
commands.open();
}
var options = { port: port, env: 'development', db: { host: host, port: mongoPort, name: dbname } };

options.env = program.environment || process.env.DPD_ENV || options.env;
if (options.env !== 'development') console.log('starting in %s mode', options.env);

if (credentials !== undefined) options.db.credentials = credentials;

var dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
dpd.deploydPath = program.deploydPath;

function onListening() {
console.info('listening on port', options.port);
var commands = repl(dpd);
if (program.dashboard) {
commands.dashboard();
} else if (program.open) {
commands.open();
}
}

function onError (err) {
if (err.code === "EADDRINUSE") {
console.error();
console.error("ERROR: port " + options.port + " is already in use");
if (retries > 0) {
options.port++;
console.log("Trying again on port " + options.port + "...");
console.log();
retries--;
dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
} else {
process.exit();
}
function onError(err) {
if (err.code === "EADDRINUSE") {
console.error();
console.error("ERROR: port " + options.port + " is already in use");
if (retries > 0) {
options.port++;
console.log("Trying again on port " + options.port + "...");
console.log();
retries--;
dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
} else {
console.error(err);
process.exit();
}
} else {
console.error(err);
process.exit();
}
}
}
}

/**
* Port generation
*/
function generatePort() {
var portRange = [ 3000, 9000 ];
var portRange = [3000, 9000];
return Math.floor(Math.random() * (portRange[1] - portRange[0])) + portRange[0];
}

function checkForUpdates() {
http.get('http://registry.npmjs.org/deployd-cli', function(err, res, body) {
http.get('http://registry.npmjs.org/deployd-cli', function (err, res, body) {
if (!err) {
var json;
try {
Expand Down
3 changes: 3 additions & 0 deletions lib/cli/stop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* Start the server
*/
var program = require('commander');


var stop = function(code) {
var fn = function() {
exit(code);
Expand Down