Skip to content
Browse files

[master] ¿Ready for merge? I've added/changed a few things that I was

doing wrong, speacilly at reading the package.json, since we are dealing
at different moments with chrooted and not chrooted dirs, so that change
a little, I've added also the create_version script, and fixed some
JSON.parse try/catch since we don't know when the package.json can be
malformed.
  • Loading branch information...
1 parent 04409ce commit 3fff3d0c0513c86a3e0e61ef27b3220ae8893411 @alejandro alejandro committed Feb 27, 2012
Showing with 61 additions and 20 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 app.js
  3. +5 −2 bin/create_versions.js
  4. +1 −2 lib/app.js
  5. +1 −1 lib/chroot.js
  6. +12 −1 lib/lib.js
  7. +11 −3 lib/npm.js
  8. +16 −6 scripts/chroot_runner.js
  9. +12 −5 scripts/launch_chrooted_app.js
View
2 .gitignore
@@ -7,3 +7,5 @@ bin/.nodester.config
.DS_Store
*.log
node_modules
+test
+*.un~
View
1 app.js
@@ -151,6 +151,7 @@ myapp.get('/applogs/:appname', middle.authenticate, middle.authenticate_app, app
// curl -u PUT -u "testuser:123" -d "appname=test&key=NODE_ENV&value=production" http://localhost:4001/env
// curl -u DELETE -u "testuser:123" -d "appname=test&key=NODE_ENV" http://localhost:4001/env
myapp.get('/env/:appname', middle.authenticate, middle.authenticate_app, app.env_get);
+myapp.get('/env/version', app.env_version);
myapp.put('/env', middle.authenticate, middle.authenticate_app, app.env_put);
myapp.del('/env/:appname/:key', middle.authenticate, middle.authenticate_app, app.env_delete);
View
7 bin/create_versions.js
@@ -12,8 +12,11 @@ var installVersion = function(_version){
var execute = function(v){
console.log('[INFO] installing node-v'+ v.trim());
var installprocess = exec('n ' + v.trim());
- installprocess.stdout.on('end', function(){
- console.log('[INFO] Installed node-v'+v.trim());
+ installprocess.stderr.on('end', function(data){
+ console.log('[INFO] stderr node-v'+v.trim()+' :' + data);
+ });
+ installprocess.stdout.on('end', function(data){
+ console.log('[INFO] stdout Installed node-v'+v.trim()+' :' +data);
});
installprocess.on('exit', function(code) {
if (code !== 0) {
View
3 lib/app.js
@@ -405,8 +405,7 @@ module.exports = {
gitrepo: config.opt.git_user + '@' + config.opt.git_dom + ':' + path.join(config.opt.git_home_dir, req.app.username, req.app.repo_id + '.git'),
start: req.app.start,
running: req.app.running,
- pid: req.app.pid,
- 'node-version':req.app['node-version'] || '0.4.9'
+ pid: req.app.pid
}));
},
post: function (req, res, next) {
View
2 lib/chroot.js
@@ -36,7 +36,7 @@
};
chroot.prototype.stop = function(cb) {
if (this.mounted === true) {
- var arr = ['sudo -i umount ' + this.dev_path, 'sudo -i umount ' + this.proc_path];
+ var arr = ['sudo umount ' + this.dev_path, 'sudo umount ' + this.proc_path];
lib.exec_array(arr, function() {
lib.is_mounted(this.dev_path, function(respA) {
if (respA !== false) {
View
13 lib/lib.js
@@ -308,4 +308,15 @@ var setup_unionfs_chroot = function (chroot_base, git_home, rw_folder, final_chr
});
};
-exports.setup_unionfs_chroot = setup_unionfs_chroot;
+ exports.setup_unionfs_chroot = setup_unionfs_chroot;
+
+ var check_node_version = function(ver){
+ exec('n use ' + ver, function(e,d){
+ if (e){
+ cb(false);
+ } else {
+ cb(true);
+ }
+ });
+ }
+ exports.check_node_version = check_node_version;
View
14 lib/npm.js
@@ -34,10 +34,18 @@ module.exports = {
p.forEach(function(v, k) {
p[k] = lib.escape_packages(v);
});
- var packageJSON = fs.readFileSync(app_user_home + '/package.json', 'utf8');
- var node_version = JSON.parse(packageJSON)['node-version'] || '0.4.9';
+ var node_version;
+ // in the package what type of data is? #TODO
+ // always a try/catch for avoid mistakes and exceptions on the package.json read operation
+ try {
+ node_version = JSON.parse(fs.readFileSync(app_user_home + '/package.json', 'utf8'))['node'];
+ } catch(e){
+ node_version = process.version;
+ }
+ node_version = node_version['node'] === undefined ? process.version : node_version;
package = p.join(' ');
- var cmd = 'cd ' + app_user_home + '; if [ ! -d node_modules ]; then mkdir node_modules; fi; n npm '+node_version + action + ' ' + package;
+ var cmd = 'cd ' + app_user_home + '; if [ ! -d node_modules ]; then mkdir node_modules; fi; n npm '+ node_version
+ + ' ' + action + ' ' + package;
var pr = exec(cmd, function(err, stdout, stderr) {
res.send({
status: 'success',
View
22 scripts/chroot_runner.js
@@ -108,15 +108,25 @@ var myPid = daemon.start();
var start_child = function () {
var pack = {};
- // we don't know what kind of package.json are we dealing
+ // normalize path, since args contain the node-executable pop that value
+ // and replace it with `package.json`
+ // I'm not a RegExp guru so this is my solution ;)
+ var packPath = args[0].split('/');
+ packPath[packPath.length-1] = 'package.json';
+ packPath = packPath.join('/');
+ // we don't know what kind of package.json are we dealing with
try {
- pack = JSON.parse(fs.readFileSync(path.join(args,'package.json'), 'utf8'));
+ pack = JSON.parse(fs.readFileSync(packPath, 'utf8'));
} catch(e){
- // Set default to the parent version
- pack['node-version'] = process.version;
+ // Set default to the parent node version
+ pack['node'] = process.version;
}
- // n handles only number paths without v0.x.x => 0.x.x
- var version = pack['node-version'].replace('v','');
+ // What if the try/catch read the package but there is no `node`?
+ var version = pack['node'] === undefined ? process.version : pack['node'];
+ // n dir only handles number paths without v0.x.x => 0.x.x
+ version = version.replace('v','').trim();
+ // The spawn process only works with absolute paths, and by default n'd saved every
+ // version of node in /usr/local/n/version
child = spawn((path.extname(args[0]) == '.coffee'
? '/usr/bin/coffee'
: '/usr/local/n/versions/' + version +'/bin/node'), args, {
View
17 scripts/launch_chrooted_app.js
@@ -16,9 +16,16 @@ var creds = crypto.createCredentials();
var config = JSON.parse(fs.readFileSync(path.join('.nodester', 'config.json'), encoding = 'utf8'));
// Read the package.json
var packageJSON = {};
-var appdir = fs.readdirSync(config.appdir);
+// asuming that here we are already chrooted, so the app path is '/'
+var appdir = fs.readdirSync('/');
if (appdir.indexOf('package.json') !== -1 ) {
- packageJSON = JSON.parse(fs.readFileSync(path.join(config.appdir,'package.json'),'utf8'));
+ try {
+ packageJSON = JSON.parse(fs.readFileSync(path.join('/','package.json'),'utf8'));
+ } catch(exp) {
+ packageJSON = {
+ node:process.version
+ }
+ }
}
config.userid = parseInt(config.userid);
@@ -99,8 +106,8 @@ daemon.daemonize(path.join('.nodester', 'logs', 'daemon.log'), path.join('.nodes
sandbox.process.installPrefix = '/';
// Run a specified node version using the `n` module from TJ, if no version is
// found, set default to node v0.4.9, `n` can handle 0.4.9 as v0.4.9
- var _version = sandbox.module["node-version"] = packageJSON["node-version"] || '0.4.9';
- sandbox.process.ARGV = ['n use ' + _version, config.start];
+ var version = sandbox.module["node"] = packageJSON["node"] || '0.4.9';
+ sandbox.process.ARGV = ['n use ' + version, config.start];
sandbox.process.argv = sandbox.process.ARGV;
var env = sandbox.process.env = sandbox.process.ENV = {
// defaults which can be overriden
@@ -176,7 +183,7 @@ daemon.daemonize(path.join('.nodester', 'logs', 'daemon.log'), path.join('.nodes
if (port !== app_port) {
console.log('[ERROR] You asked to listen on port', port, 'but nodester will use port', app_port, 'instead..');
} else {
- console.log('[INFO] Nodester running node ' + _version);
+ console.log('[INFO] Nodester running node ' + version);
console.log('[INFO] Nodester listening on port:', app_port);
}
_listen.call(h, app_port);

0 comments on commit 3fff3d0

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