Skip to content
Browse files

error checking

  • Loading branch information...
1 parent 317c3bc commit ba98b981e809a227d2313763e89e3b977bcd7bf4 @bradleyg committed Nov 29, 2011
Showing with 106 additions and 163 deletions.
  1. +93 −140 bin/nserv
  2. +1 −1 placeholders/helloworld.js
  3. +12 −22 placeholders/post-receive
View
233 bin/nserv
@@ -25,34 +25,32 @@ switch(choice){
case 'remove': remove(); break;
case 'list': list(); break;
case 'help': help(); break;
- default: sendError("Please provide a valid parameter. eg. 'create', 'remove', 'list', 'help', [sudo] 'start', [sudo] 'stop'"); break
+ default: error('undefined', "Please provide a valid parameter. eg. 'create', 'remove', 'list', 'help', [sudo] 'start', [sudo] 'stop'"); break
}
function start(){
fs.stat(nservDir + 'domains.json', function(err, data){
- if(err){
+ if (err) {
fs.mkdir(nservDir, '0777', function(err){
- if (err) throw err;
+ error(err, "Could not create directory.");
fs.readFile(placeholders + 'domains.json', function (err, data) {
- if (err) throw err;
+ error(err, "Could not read placeholder domains.json.");
fs.writeFile(nservDir + 'domains.json', data, function (err) {
- if (err) throw err;
- fs.chmodSync(nservDir + 'domains.json', '0777');
- testSudo(function(err){
- if(err){
- sendError("Are you sure you are running with sudo permissions?", err);
- }
- startServer();
- });
+ error(err, "Could not write domains.json.");
+ fs.chmod(nservDir + 'domains.json', '0777', function(err){
+ error(err, "Could not write domains.json permissions.");
+ testSudo(function(err){
+ error(err, "Are you sure you have sudo permissions?.");
+ startServer();
+ });
+ });
});
});
});
}
else {
testSudo(function(err){
- if(err){
- sendError("Are you sure you are running with sudo permissions?", err);
- }
+ error(err, "Are you sure you have sudo permissions?.");
startServer();
});
}
@@ -61,32 +59,22 @@ function start(){
function stop(){
testSudo(function(err){
- if(err){
- sendError("Are you sure you are running with sudo permissions?", err);
- }
+ error(err, "Are you sure you have sudo permissions?.");
stopServer();
});
}
function startServer(){
exec('forever start ' + base + 'lib/www.js', function(err, stdout, stderr){
- if(err){
- sendError("Could not start nserv server.", err);
- }
- else {
- sendOutput('Nserv started on port 80.')
- }
+ error(err, "Could not start main nserv server.");
+ success('Nserv started on port 80.');
});
}
function stopServer(){
exec('forever stop ' + base + 'lib/www.js', function(err, stdout, stderr){
- if(err){
- sendError("Could not stop nserv server.", err);
- }
- else {
- sendOutput('Nserv server stopped.')
- }
+ error(err, "Could not stop nserv server.");
+ success('Nserv server stopped.');
});
}
@@ -109,38 +97,32 @@ function testSudo(cb){
function create(){
if(typeof process.argv[3] === 'undefined') {
- sendError("You must provide a domain name when running create.", null);
+ error('undefined', 'You must provide a domain name when running create.');
}
var domain = process.argv[3];
getApps(function(apps){
checkForExisting(domain, apps, function(loc){
- if(loc){
- sendError("Application already exists.");
- }
- else {
- addApp(domain, function(fullPath){
- sendOutput("Domain successfully added.".green + "\ndata:".grey + " Add a git remote to make changes:".green + "\ndata:".grey + " $ git remote add nserv ssh://".grey + "<HOSTNAME>".red + fullPath.grey + "/.git".grey);
- });
- };
+ error(loc, "Application already exists.");
+ addApp(domain, function(loc){
+ success("Domain successfully added.".green + "\ndata:".green + " Add a git remote to make changes:".green + "\ndata:".grey + " $ git remote add nserv ssh://".grey + "<HOSTNAME>".red + loc.grey + "/.git".grey);
+ });
});
});
}
function remove() {
if(typeof process.argv[3] === 'undefined') {
- sendError("You must provide a domain name when running delete.", null);
+ error('undefined', "You must provide a domain name when running delete.");
}
var domain = process.argv[3];
getApps(function(apps){
checkForExisting(domain, apps, function(loc){
- if(loc){
- deleteApp(domain, loc, function(){
- sendOutput("Application deleted");
- });
+ if (loc === null){
+ error('Missing', "That application is not currently installed.");
}
- else {
- sendError("That application is not currently installed.", null);
- }
+ deleteApp(domain, loc, function(){
+ success("Application deleted");
+ });
});
});
}
@@ -151,82 +133,62 @@ function list(){
var domainList = "";
for (app in apps) {
domainList += "data: ".grey + app.green + " - git remote add nserv ssh://".grey + "<HOSTNAME>".red + apps[app].loc.grey + "/".grey + ".git".grey + '\n';
- };
+ }
}
else {
- domainList = "data: No applications installed.\n";
+ domainList = "data: No applications installed.".red + "\n";
}
- sendOutput("Installed applications/domains.\n".magenta + domainList.slice(0, -1).cyan);
+ message("Installed applications/domains.\n".cyan + domainList.slice(0, -1));
});
}
function help() {
- var message = "" +
- "Options: \n".magenta +
- " [sudo] start \n".cyan +
- " [sudo] stop \n".cyan +
- " remove (domain) \n".cyan +
- " create (domain) \n".cyan +
- " remove (domain) \n".cyan +
- " list \n".cyan +
- " help ".cyan;
+ var help = "" +
+ "Options: \n".cyan +
+ "data: [sudo] start \n".grey +
+ "data: [sudo] stop \n".grey +
+ "data: remove (domain) \n".grey +
+ "data: create (domain) \n".grey +
+ "data: remove (domain) \n".grey +
+ "data: list \n".grey +
+ "data: help ".grey;
- sendOutput(message);
+ message(help);
}
-function addApp(domain, callback){
- var appBase = domain + '/',
- appRepo = appBase + ".git/",
- fullPath = process.cwd() + '/' + domain;
+function addApp(domain, cb){
+ var app = domain + '/',
+ repo = app + ".git/",
+ loc = process.cwd() + '/' + domain;
getPort(function(port){
- fs.mkdir(appBase, '755', function(err){
- if(err){
- sendError("Could not make base application directory.", err);
- }
- fs.mkdir(appRepo, '755', function(err){
- if(err){
- sendError("Could not make git repo directory.", err);
- }
- exec("cd " + appRepo + " && exec git init --bare", function(err, stdout, stderr){
- if(err){
- sendError("Could create application git repo.", err);
- }
+ fs.mkdir(app, '755', function(err){
+ error(err, "Could not make base application directory.");
+ fs.mkdir(repo, '755', function(err){
+ error(err, "Could not make git repo directory.");
+ exec("cd " + repo + " && exec git init --bare", function(err, stdout, stderr){
+ error(err, "Could create application git repo.");
fs.readFile(placeholders + 'post-receive', function(err, data){
- if(err){
- sendError("Could not read post-receive hook.", err);
- }
- fs.writeFile(appRepo + 'hooks/post-receive', data, function(){
- if(err){
- sendError("Could not write post-receive hook.", err);
- }
- fs.chmod(appRepo + 'hooks/post-receive', '755', function(err){
- if(err){
- sendError("Could change file permissions for post-receive hook.", err);
- }
- fs.link(placeholders + 'helloworld.js', appBase + 'server.js' , function(err){
- if(err){
- sendError("Could not create placeholder application.", err);
- }
+ error(err, "Could not read post-receive hook.");
+ fs.writeFile(repo + 'hooks/post-receive', data, function(err){
+ error(err, "Could not write post-receive hook.");
+ fs.chmod(repo + 'hooks/post-receive', '755', function(err){
+ error(err, "Could change file permissions for post-receive hook.");
+ fs.link(placeholders + 'helloworld.js', app + 'server.js' , function(err){
+ error(err, "Could not create placeholder application.");
fs.readFile(nservDir + 'domains.json', function (err, data) {
- if(err){
- sendError("Could not read proxy table.", err);
- }
+ error(err, "Could not read proxy table.");
var config = JSON.parse(data);
config[domain] = {
port: port,
- loc: fullPath
+ loc: loc
}
newConfig = JSON.stringify(config);
- exec('PORT=' + port + ' forever start ' + fullPath + '/server.js', function(err){
- if(err){
- sendError("Could not start application.", err);
- }
+ exec('PORT=' + port + ' forever start ' + loc + '/server.js', function(err){
+ error(err, "Could not start application.");
fs.writeFile(nservDir + 'domains.json', newConfig, function (err) {
- if(err){
- sendError("Could not write proxy table.", err);
- }
- callback(fullPath);
+ error(err, "Could not write proxy table.");
+ cb(loc);
});
});
});
@@ -241,85 +203,76 @@ function addApp(domain, callback){
}
function deleteApp (domain, loc, callback) {
- console.log('data: '.grey);
- commander.confirm('data:'.grey + ' Are you sure you want to delete this domain? (y/n):'.magenta + " ", function(ok){
+ commander.confirm('data: Are you sure you want to delete this domain? (y/n):'.magenta + " ", function(ok){
process.stdin.destroy(); // needed to kill commander process
if(ok === true) {
exec("rm -r " + loc, function (err) {
- if(err){
- sendError("Could not remove application directory.", err);
- }
+ error(err, "Could not remove application directory.");
fs.readFile(nservDir + 'domains.json', function (err, data) {
- if(err){
- sendError("Could read proxy table.", err);
- }
+ error(err, "Could read proxy table.");
var config = JSON.parse(data);
delete config[domain];
var newConfig = JSON.stringify(config);
fs.writeFile(nservDir + 'domains.json', newConfig, function (err) {
- if(err){
- sendError("Could write proxy table.", err);
- }
+ error(err, "Could write proxy table.");
exec("forever stop " + loc + "/server.js", function(err){
- if(err){
- sendError("Could not write proxy table.", err);
- }
+ error(err, "Could not write proxy table.");
callback();
});
});
});
});
}
else {
- sendOutput("Domain not deleted.");
+ success("Domain not deleted.");
}
});
}
-function getApps(callback){
- var apps = new Array();
+function getApps(cb){
fs.readFile(nservDir + 'domains.json', function(err, data){
+ error(err, 'Could not read domains.json.');
var apps = JSON.parse(data);
- callback(apps);
+ cb(apps);
});
}
-function checkForExisting(domain, apps, callback) {
+function checkForExisting(domain, apps, cb) {
for (app in apps) {
if(app == domain){
var loc = apps[app].loc;
- return callback(loc);
+ return cb(loc);
}
}
- callback(false);
+ cb(null);
}
-function getPort(callback){
+function getPort(cb){
var port = Math.floor(Math.random() * 1001 + 9000);
exec("netstat -tulpn | grep 0.0.0.0:" + port, function(err, stdout, stderr){
if(stdout == ""){
- return callback(port);
+ return cb(port);
}
- getPort(callback);
+ getPort(cb);
});
}
String.prototype.trim = function () {
- return this.replace(/^\s*/, "").replace(/\s*$/, "");
+ return this.replace(/^\s*/, "").replace(/\s*$/, "");
+}
+
+function error (err, message) {
+ if (err) {
+ console.log("data: ".red + message.red);
+ console.log("data: ".red + err.red);
+ process.exit();
+ }
}
-function sendOutput(message){
- console.log('data:'.grey);
- console.log("data: ".grey + message.green);
- console.log('data:'.grey);
+function success (message) {
+ console.log("data: ".green + message.green)
}
-function sendError(message, err){
- console.log('data:'.grey);
- console.log("data: ".grey + message.red);
- if(err) {
- console.log(err);
- }
- console.log('data:'.grey);
- process.exit();
+function message (message) {
+ console.log("data: ".cyan + message.cyan)
}
View
2 placeholders/helloworld.js
@@ -3,4 +3,4 @@ http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Yes! successfully added. Commit changes to update you application.');
res.end();
-}).listen(process.env.PORT || 9001);
+}).listen(process.env.PORT);
View
34 placeholders/post-receive
@@ -13,37 +13,27 @@ var appPath = path.resolve(__dirname + '/../../'),
domain = pathSegments[pathSegments.length - 1];
exec("export GIT_WORK_TREE=" + appPath + " && git checkout -f", function(err, stdout){
- if(err){
- sendError("Could not checkout git tree.", err);
- }
+ error(err, "Could not checkout git tree.");
fs.readFile(configfile, function (err, data) {
- if(err){
- sendError("Could not read config file.", err);
- }
+ error(err, "Could not read config file.");
var config = JSON.parse(data);
var port = config[domain].port;
exec("PORT=" + port + " forever restart " + appPath + "/server.js", function(err, stdout){
- if(err){
- sendError("Could not restart server.", err);
- }
+ error(err, "Could not restart server.");
console.log(stdout);
- sendOutput("Application updated.");
+ success("Application updated.");
});
});
});
-function sendOutput(message){
- console.log('----------------------------------------');
- console.log(message);
- console.log('----------------------------------------');
+function success(message){
+ console.log("data: " + message);
}
-function sendError(message, err){
- console.log('----------------------------------------');
- console.log(message);
- if(err) {
- console.log(err);
+function error (err, message) {
+ if (err) {
+ console.log("data: " + message);
+ console.log("data: " + err);
+ process.exit();
}
- console.log('----------------------------------------');
- process.exit();
-}
+}

0 comments on commit ba98b98

Please sign in to comment.
Something went wrong with that request. Please try again.