Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[refactor] chroot_runner.js refactor to previous state, just logger f…

…unctionality added
  • Loading branch information...
commit e89e4efc30e907abddb35fdaca8bccf75b40c3c0 1 parent 5b41f2c
Alejandro Morales alejandro authored
Showing with 65 additions and 86 deletions.
  1. +65 −86 scripts/chroot_runner.js
151 scripts/chroot_runner.js
View
@@ -2,28 +2,25 @@
require.paths.unshift('/usr/lib/node_modules');
-var spawn = require('child_process').spawn
- , exec = require('child_process').exec
- , daemon = require('daemon')
- , fs = require('fs')
- , path = require('path')
- , net = require('net')
- , Logger = require('bunyan')
- , nodeVersions = require('../lib/lib').node_versions()
- , config = JSON.parse(fs.readFileSync(path.join('.nodester', 'config.json'),'utf8'))
- , cfg = require('../config').opt
- , newmask = 0000
- , log = process.log || new Logger({name: "nodester"})
- , run_max = 5
- , run_count = 0
- , LOG_STDOUT = 1
- , LOG_STDERR = 2
- , oldmask
+var spawn = require('child_process').spawn
+ , exec = require('child_process').exec
+ , daemon = require('daemon')
+ , fs = require('fs')
+ , path = require('path')
+ , net = require('net')
+ , node_versions = require('../lib/lib').node_versions()
+ , config = JSON.parse(fs.readFileSync(path.join('.nodester', 'config.json'), encoding = 'utf8'))
+ , cfg = require('../config').opt
+ , oldmask, newmask = 0000
;
oldmask = process.umask(newmask);
-
-log.info('Changed umask from: ' + oldmask.toString(8) + ' to ' + newmask.toString(8));
+console.log('Changed umask from: ' + oldmask.toString(8) + ' to ' + newmask.toString(8));
+var run_max = 5
+ , run_count = 0
+ , LOG_STDOUT = 1
+ , LOG_STDERR = 2
+ ;
var env = {
PATH: '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',
@@ -38,33 +35,24 @@ if (config.env) {
env.app_port = parseInt(config.port, 10);
env.app_host = config.ip;
-
-var args = ['/app/' + config.start]
- , chroot_res = daemon.chroot(config.appchroot)
- ;
-
+var args = ['/app/' + config.start];
+var chroot_res = daemon.chroot(config.appchroot);
if (chroot_res !== true) {
log_line('chroot_runner', 'Failed to chroot to ' + config.apphome, LOG_STDERR);
pre_shutdown();
process.exit(1);
}
-
var ch_uid = daemon.setreuid(config.userid);
-
if (ch_uid !== true) {
log_line.call('chroot_runner', 'Failed to change user to ' + config.userid, LOG_STDERR);
pre_shutdown();
process.exit(2);
}
-
var child = null;
var child_watcher_time = null;
var log_lines = [];
var myPid = daemon.start();
-
(function () {
-
- // Default log writer
var log_listen = function (p, cb) {
var srv = net.createServer(function (conn) {
var srvLog = new Logger({name:'nodester',stream:conn});
@@ -82,7 +70,6 @@ var myPid = daemon.start();
});
srv.listen(p, cb);
}
-
var log_line = function (line, stdout) {
if (!stdout) var stdout = 1;
if (typeof this == 'string')
@@ -91,14 +78,12 @@ var myPid = daemon.start();
if (log_lines.length > 250)
log_lines.shift();
};
-
log_line.call('chroot_runner', 'New PID: ' + myPid.toString());
if (path.existsSync('/.nodester/pids/runner.pid')) fs.unlinkSync('/.nodester/pids/runner.pid');
fs.writeFileSync('/.nodester/pids/runner.pid', myPid.toString());
-
var log_sock_path = path.join('/', '.nodester', 'logs.sock');
log_listen(log_sock_path, function () {
- log_line.call('chroot_runner', 'log_listen\'ing', LOG_STDERR);
+ log_line('chroot_runner', 'log_listen\'ing', LOG_STDERR);
try {
fs.chmodSync(log_sock_path, '0777');
} catch (e) {
@@ -114,7 +99,6 @@ var myPid = daemon.start();
process.exit();
}
});
-
process.on('SIGTERM', function () {
log_line.call('chroot_runner', 'SIGTERM recieved, sending SIGTERM to children.');
if (child !== null) {
@@ -125,55 +109,52 @@ var myPid = daemon.start();
process.exit();
}
});
-
var start_child = function () {
- var pack = {};
- // 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(packPath, 'utf8'));
- } catch(e){
- // Set default to the parent node version
- pack['node'] = process.version;
- }
- // 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();
-
- if (node_versions.indexOf(version) !== -1) {
- // 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, {
- env: env
- });
- log_line.call('Watcher', 'Running node v-' + version, LOG_STDERR);
- child.stdout.on('data', log_line.bind('stdout'));
- child.stderr.on('data', log_line.bind('stderr'));
- child.on('exit', function (code) {
- if (code > 0 && run_count > run_max) {
- log_line.call('Watcher', 'Error: Restarted too many times, bailing.', LOG_STDERR);
- clearInterval(child_watcher_timer);
- } else if (code > 0) {
- log_line.call('Watcher', 'Process died with exit code ' + code + '. Restarting...', LOG_STDERR);
- child = null;
- } else {
- log_line.call('Watcher', 'Process exited cleanly. Dieing.', LOG_STDERR);
- clearInterval(child_watcher_timer);
- }
- });
- } else {
- log_line.call('Watcher', 'Process exited cleanly. node.js Version:'+version + ' not avaiable', LOG_STDERR);
- clearInterval(child_watcher_timer);
- }
- };
+ var pack = {};
+ // 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(packPath, 'utf8'));
+ } catch (e) {
+ // Set default to the parent node version
+ pack['node'] = process.version;
+ }
+ // 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();
+ // Insert node-watcher code and link the dependency
+ if (node_versions.indexOf(version) !== -1) {
+ // 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, {
+ env: env
+ });
+ log_line.call('Watcher', 'Running node v-' + version, LOG_STDERR);
+ child.stdout.on('data', log_line.bind('stdout'));
+ child.stderr.on('data', log_line.bind('stderr'));
+ child.on('exit', function (code) {
+ if (code > 0 && run_count > run_max) {
+ log_line.call('Watcher', 'Error: Restarted too many times, bailing.', LOG_STDERR);
+ clearInterval(child_watcher_timer);
+ } else if (code > 0) {
+ log_line.call('Watcher', 'Process died with exit code ' + code + '. Restarting...', LOG_STDERR);
+ child = null;
+ } else {
+ log_line.call('Watcher', 'Process exited cleanly. Dieing.', LOG_STDERR);
+ clearInterval(child_watcher_timer);
+ }
+ });
+ } else {
+ log_line.call('Watcher', 'Process exited cleanly. node.js Version:' + version + ' not avaiable', LOG_STDERR);
+ clearInterval(child_watcher_timer);
+ }
+ };
var child_watcher = function () {
if (child === null) {
start_child();
@@ -182,6 +163,4 @@ var myPid = daemon.start();
};
child_watcher_timer = setInterval(child_watcher, 750);
});
-
-})();
-/* TODO */
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.