Skip to content

Commit

Permalink
gains
Browse files Browse the repository at this point in the history
  • Loading branch information
generalhenry committed Jan 15, 2014
1 parent 9923dab commit 6221f71
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 114 deletions.
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@

"maxparams" : 8, // dang
"maxdepth" : 10, // oye
"maxstatements" : 96, // this needs to be brought WAY WAY down
"maxcomplexity" : 11 // this also needs to come down
"maxstatements" : 26, // this needs to be brought WAY WAY down
"maxcomplexity" : 10 // this also needs to come down
}
32 changes: 32 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var express = require('express');
var configs = require('./configs');
var app = module.exports = express();
app.use(require('./domains'));
if (configs.logExpress) {
app.use(express.logger());
}
app.use(express.json());
app.use(express.urlencoded());
app.use(require('./rest/users'));
app.use(require('./rest/impexp'));
app.use(require('./rest/runnables'));
app.use(require('./rest/channels'));
app.use(require('./rest/categories'));
app.use(require('./rest/specifications'));
app.use(require('./rest/implementations'));
app.use(require('./rest/campaigns'));
app.use(app.router);
if (configs.nodetime) {
app.use(require('nodetime').expressErrorHandler());
}
if (configs.rollbar) {
app.use(require('rollbar').errorHandler());
}
app.get('/cleanup', require('./cleanup'));
app.get('/cache', require('./models/caching').updateAllCaches);
app.get('/', function (req, res) {
res.json({ message: 'runnable api' });
});
app.all('*', function (req, res) {
res.json(404, { message: 'resource not found' });
});
5 changes: 0 additions & 5 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ function cleanupContainersNotIn (domain, whitelist, cb) {
], cb);
}
module.exports = function (req, res) {
function sendError (err) {
var status = err.status;
delete err.status;
res.json(status || 403, err);
}
async.series([
function (cb) {
if (req.query.firstRun) {
Expand Down
1 change: 0 additions & 1 deletion lib/domains.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var configs = require('./configs');
var domain = require('domain');
var dockerExp = /^HTTP response code is (\d\d\d) which indicates an error: (.+)$/;
module.exports = function (req, res, next) {
Expand Down
45 changes: 7 additions & 38 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
var caching = require('./models/caching');
var cleanup = require('./cleanup');
var configs = require('./configs');
var domains = require('./domains');
var express = require('express');
var http = require('http');
var mongoose = require('mongoose');
var nodetime = require('nodetime');
Expand All @@ -28,6 +24,7 @@ App.prototype.start = function (cb) {
cb();
} else {
this.listener = function (err) {
console.error('uncaughtException', err);
self.stop(function () {
self.cleanup();
});
Expand Down Expand Up @@ -62,29 +59,11 @@ App.prototype.stop = function (cb) {
};
App.prototype.create = function () {
var self = this;
var app = express();
app.use(domains);
if (configs.logExpress) {
app.use(express.logger());
}
app.use(express.json());
app.use(express.urlencoded());
app.use(require('./rest/users'));
app.use(require('./rest/impexp'));
app.use(require('./rest/runnables'));
app.use(require('./rest/channels'));
app.use(require('./rest/categories'));
app.use(require('./rest/specifications'));
app.use(require('./rest/implementations'));
app.use(require('./rest/campaigns'));
app.use(app.router);
if (configs.nodetime) {
app.use(nodetime.expressErrorHandler());
}
if (configs.rollbar) {
app.use(rollbar.errorHandler());
}
app.use(function (err, req, res, next) {
var app = require('./app');
app.use(errorMiddleware);
this.server = http.createServer(app);
return this.server;
function errorMiddleware (err, req, res, next) {
if (configs.logErrorStack && false) {
console.log(err.stack);
}
Expand All @@ -97,17 +76,7 @@ App.prototype.create = function () {
self.cleanup();
});
}
});
app.get('/cleanup', cleanup);
app.get('/cache', caching.updateAllCaches);
app.get('/', function (req, res) {
res.json({ message: 'runnable api' });
});
app.all('*', function (req, res) {
res.json(404, { message: 'resource not found' });
});
this.server = http.createServer(app);
return this.server;
}
};
App.prototype.cleanup = function () {
if (configs.nodetime) {
Expand Down
75 changes: 23 additions & 52 deletions lib/models/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,63 +272,34 @@ imageSchema.statics.createFromDisk = function (domain, owner, runnablePath, sync
image = new self();
encodedId = encodeId(image._id.toString());
tag = '' + configs.dockerRegistry + '/runnable/' + encodedId;
return buildDockerImage(domain, runnablePath, tag, function (err) {
var file, _i, _len, _ref;
if (err) {
return cb(err);
} else {
image.owner = owner;
image.name = runnable.name;
image.image = runnable.image;
image.dockerfile = dockerfile;
image.cmd = runnable.cmd;
if (runnable.description) {
image.description = runnable.description;
}
if (runnable.file_root_host) {
image.file_root_host = runnable.file_root_host;
}
if (runnable.file_root) {
image.file_root = runnable.file_root;
}
if (runnable.service_cmds) {
image.service_cmds = runnable.service_cmds;
}
if (runnable.start_cmd) {
image.start_cmd = runnable.start_cmd;
}
if (runnable.build_cmd) {
image.build_cmd = runnable.build_cmd;
}
image.port = runnable.port;
runnable.tags = runnable.tags || [];
_ref = runnable.files;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
image.files.push(file);
}
if (sync) {
return syncDockerImage(domain, image, function (err) {
if (err) {
return cb(err);
} else {
image.synced = true;
return image.save(domain.intercept(function () {
return cb(null, image, runnable.tags);
}));
}
});
} else {
return image.save(domain.intercept(function () {
return cb(null, image, runnable.tags);
buildDockerImage(domain, runnablePath, tag, domain.intercept(function () {
_.extend(image, runnable, {
owner: owner,
dockerfile: dockerfile
});
runnable.tags = runnable.tags || [];
var _ref = runnable.files;
for (var _i = 0, _len = _ref.length; _i < _len; _i++) {
var file = _ref[_i];
image.files.push(file);
}
if (sync) {
syncDockerImage(domain, image, domain.intercept(function () {
image.synced = true;
image.save(domain.intercept(function () {
cb(null, image, runnable.tags);
}));
}
}));
} else {
image.save(domain.intercept(function () {
cb(null, image, runnable.tags);
}));
}
});
}));
}
}));
});
return rendered.pipe(writestream);
rendered.pipe(writestream);
}
});
});
Expand Down
1 change: 0 additions & 1 deletion lib/rest/campaigns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var express = require('express');
var mailchimp = require('mailchimp');
var domains = require('../domains');
var Email = require('email').Email;
var configs = require('../configs');
var mailchimpApi = configs.mailchimp != null ? new mailchimp.MailChimpAPI(configs.mailchimp.key) : void 0;
Expand Down
1 change: 0 additions & 1 deletion lib/rest/categories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var categories = require('../models/categories');
var domains = require('../domains');
var express = require('express');
var app = module.exports = express();
app.post('/categories', function (req, res) {
Expand Down
12 changes: 3 additions & 9 deletions lib/rest/channels.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var configs = require('../configs');
var categories = require('../models/categories');
var channels = require('../models/channels');
var domains = require('../domains');
var express = require('express');
var app = module.exports = express();
app.post('/channels', function (req, res) {
Expand All @@ -15,13 +13,9 @@ app.post('/channels', function (req, res) {
});
app.get('/channels', function (req, res) {
var count;
var sendJSON = function (err, result) {
if (err) {
res.json(err.code, { message: err.msg });
} else {
res.json(result);
}
};
var sendJSON = req.domain.intercept(function (result) {
res.json(result);
});
if (req.query.name != null) {
channels.getChannelByName(req.domain, categories, req.query.name, sendJSON);
} else if (req.query.names != null) {
Expand Down
1 change: 0 additions & 1 deletion lib/rest/impexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var rimraf = require('rimraf');
var runnables = require('../models/runnables');
var tar = require('tar');
var zlib = require('zlib');
var domains = require('../domains');
var app = module.exports = express();
app.post('/runnables/import', function (req, res) {
req.pause();
Expand Down
1 change: 0 additions & 1 deletion lib/rest/implementations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var implementations = require('../models/implementations');
var domains = require('../domains');
var express = require('express');
var app = module.exports = express();
app.post('/users/me/implementations', function (req, res) {
Expand Down
1 change: 0 additions & 1 deletion lib/rest/runnables.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var channels = require('../models/channels');
var categories = require('../models/categories');
var configs = require('../configs');
var domains = require('../domains');
var express = require('express');
var users = require('../models/users');
var runnables = require('../models/runnables');
Expand Down
1 change: 0 additions & 1 deletion lib/rest/specifications.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var configs = require('../configs');
var specifications = require('../models/specifications');
var domains = require('../domains');
var error = require('../error');
var express = require('express');
var app = module.exports = express();
Expand Down
1 change: 0 additions & 1 deletion lib/rest/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var async = require('async');
var configs = require('../configs');
var domains = require('../domains');
var express = require('express');
var fs = require('fs');
var formidable = require('formidable');
Expand Down

0 comments on commit 6221f71

Please sign in to comment.