-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from 5 commits
879e1a8
be81d19
4fb7458
61e969a
1cf7ad4
c91cfcd
6a2d27f
4a78d97
6962cca
0544020
382765e
a20c459
81233c5
b8f6bb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -62,45 +63,62 @@ 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 | ||
); | ||
|
||
|
||
// Added by PV | ||
|
||
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) { | ||
console.log(' program.password', program.password) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not output the password in the logs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great catch. I'll remove this line, was for test |
||
setCredentials(program.username, program.password); | ||
} | ||
else { | ||
startup(); | ||
} | ||
} | ||
else { | ||
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup); | ||
} | ||
|
||
|
||
//-- Added by PV | ||
|
||
|
||
} else { | ||
console.log("This directory does not contain a Deployd app!"); | ||
|
@@ -109,68 +127,69 @@ var start = function(file) { | |
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "deployd-cli", | ||
"version": "2.0.0", | ||
"version": "0.0.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, keep version 2.0.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok got it |
||
"license": "MIT", | ||
"description": "The deployd command line interface", | ||
"repository": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does that mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just note that where I made the change