mongodb-prebuilt
Advanced tools
Comparing version 4.5.4 to 4.5.5
244
index.js
@@ -1,3 +0,4 @@ | ||
'use strict'; | ||
var fs = require('fs') | ||
"use strict"; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -12,7 +13,7 @@ var install = require('./install'); | ||
module.exports = { | ||
"bin_path": bin_path, | ||
"dist_path": dist_path, | ||
"active_version": active_version, | ||
"install": install, | ||
"start_server": start_server | ||
"bin_path": bin_path, | ||
"dist_path": dist_path, | ||
"active_version": active_version, | ||
"install": install, | ||
"start_server": start_server | ||
}; | ||
@@ -22,6 +23,6 @@ | ||
mongodb_logs("Shutting down"); | ||
if (e) { | ||
if (typeof e === "function") { | ||
throw e; | ||
} | ||
} | ||
}; | ||
@@ -32,46 +33,45 @@ process.on('uncaughtException', shutdown); | ||
function start_server(opts, callback) { | ||
emitter.once('mongoStarted', callback); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
emitter.once('mongoStarted', callback); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
var args = build_args(opts); | ||
var bpath = bin_path(opts.version); | ||
if (! bpath ) { | ||
install(opts.version, function(err) { | ||
if ( err ) { | ||
callback(err); | ||
} else { | ||
bpath = bin_path(opts.version); | ||
start(); | ||
} | ||
}); | ||
} else { | ||
start(); | ||
} | ||
var args = build_args(opts); | ||
var bpath = bin_path(opts.version); | ||
if (!bpath) { | ||
install(opts.version, function(err) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
bpath = bin_path(opts.version); | ||
start(); | ||
} | ||
}); | ||
} else { | ||
start(); | ||
} | ||
function start() { | ||
debug("spawn", bpath + "mongod", args.join(' ')) | ||
var child = proc.spawn(bpath + "mongod", args); | ||
var killer = proc.spawn("node", | ||
[path.join(__dirname, "binjs", "mongokiller.js"), process.pid, child.pid], | ||
{ stdio: 'ignore' } | ||
); | ||
killer.unref(); | ||
child.on('error', function (err) { | ||
debug('Failed to start child process.', err); | ||
callback(err); | ||
}); | ||
function start() { | ||
debug("spawn", bpath + "mongod", args.join(' ')); | ||
var child = proc.spawn(bpath + "mongod", args); | ||
var killer = proc.spawn("node", [path.join(__dirname, "binjs", "mongokiller.js"), process.pid, child.pid], { | ||
stdio: 'ignore' | ||
}); | ||
killer.unref(); | ||
child.on('error', function(err) { | ||
debug('Failed to start child process.', err); | ||
callback(err); | ||
}); | ||
child.on('close', function (code) { | ||
debug('child process exited with code ' + code); | ||
if ( opts.exit_callback ) { | ||
opts.exit_callback(code); | ||
} | ||
}); | ||
child.on('close', function(code) { | ||
debug('child process exited with code ' + code); | ||
if (opts.exit_callback) { | ||
opts.exit_callback(code); | ||
} | ||
}); | ||
emitter.once('mongoShutdown', function() { | ||
child.kill('SIGTERM'); | ||
}); | ||
emitter.once('mongoShutdown', function() { | ||
child.kill('SIGTERM'); | ||
}); | ||
// this type of redirect is causing uncaught exception even with try/catch | ||
@@ -82,96 +82,94 @@ // when process exits with non zero error code, even tho error handler | ||
var started = 0; | ||
child.stdout.on('data', function(data) { | ||
if ( opts.logs_callback ) { | ||
opts.logs_callback(data); | ||
} | ||
if (! started ) { | ||
// log message indicating succesful start | ||
if ( /waiting for connections on port/.test(data.toString())) { | ||
started = 1; | ||
emitter.emit('mongoStarted'); | ||
} | ||
if ( /errno:48 Address already in use/.test(data.toString())) { | ||
emitter.emit('mongoStarted', "EADDRINUSE"); | ||
} | ||
} | ||
mongodb_logs(data.toString().slice(0, -1)); | ||
}); | ||
var started = 0; | ||
child.stdout.on('data', function(data) { | ||
if (opts.logs_callback) { | ||
opts.logs_callback(data); | ||
} | ||
if (!started) { | ||
// log message indicating succesful start | ||
if (/waiting for connections on port/.test(data.toString())) { | ||
started = 1; | ||
emitter.emit('mongoStarted'); | ||
} | ||
if (/errno:48 Address already in use/.test(data.toString())) { | ||
emitter.emit('mongoStarted', "EADDRINUSE"); | ||
} | ||
} | ||
mongodb_logs(data.toString().slice(0, -1)); | ||
}); | ||
if (opts.auto_shutdown) { | ||
// override shutdown function with sigterm | ||
shutdown = function(e) { | ||
child.kill('SIGTERM'); | ||
if (e) { | ||
throw e; | ||
} | ||
}; | ||
} | ||
} | ||
return emitter; | ||
if (opts.auto_shutdown) { | ||
// override shutdown function with sigterm | ||
shutdown = function(e) { | ||
child.kill('SIGTERM'); | ||
if (e) { | ||
throw e; | ||
} | ||
}; | ||
} | ||
} | ||
return emitter; | ||
} | ||
function dir_exists(dir) { | ||
try { | ||
var stats = fs.lstatSync(dir); | ||
if (stats.isDirectory()) { | ||
return true; | ||
} | ||
} | ||
catch (e) { | ||
debug("error from lstat:", e); | ||
return false; | ||
} | ||
try { | ||
var stats = fs.lstatSync(dir); | ||
if (stats.isDirectory()) { | ||
return true; | ||
} | ||
} catch (e) { | ||
debug("error from lstat:", e); | ||
return false; | ||
} | ||
} | ||
function build_args(opts) { | ||
var args = []; | ||
if (!opts.args) return []; | ||
var args = []; | ||
if (!opts.args) return []; | ||
Object.keys(opts.args).forEach(function(mongo_key) { | ||
if ( opts.args[mongo_key] ) { | ||
args.push("--" + mongo_key); | ||
if ( opts.args[mongo_key] !== true ) { | ||
args.push(opts.args[mongo_key]); | ||
} | ||
} | ||
}); | ||
return args; | ||
Object.keys(opts.args).forEach(function(mongo_key) { | ||
if (opts.args[mongo_key]) { | ||
args.push("--" + mongo_key); | ||
if (opts.args[mongo_key] !== true) { | ||
args.push(opts.args[mongo_key]); | ||
} | ||
} | ||
}); | ||
return args; | ||
} | ||
function bin_path (version) { | ||
var dpath = dist_path(); | ||
if (!version) { | ||
version = active_version(); | ||
} | ||
function bin_path(version) { | ||
var dpath = dist_path(); | ||
if (!version) { | ||
version = active_version(); | ||
} | ||
var bpath = path.join(dpath, version, '/bin/'); | ||
debug("bin path: %s", bpath); | ||
var bpath = path.join(dpath, version, '/bin/'); | ||
debug("bin path: %s", bpath); | ||
if ( dir_exists(bpath) ) { | ||
return bpath; | ||
} else { | ||
debug("version %s is not installed", version); | ||
return; | ||
} | ||
if (dir_exists(bpath)) { | ||
return bpath; | ||
} else { | ||
debug("version %s is not installed", version); | ||
return; | ||
} | ||
} | ||
function dist_path() { | ||
return fs.readFileSync(path.join(__dirname, 'dist_path.txt'), 'utf-8'); | ||
return fs.readFileSync(path.join(__dirname, 'dist_path.txt'), 'utf-8'); | ||
} | ||
function active_version() { | ||
return fs.readFileSync(path.join(__dirname, 'active_version.txt'), 'utf-8'); | ||
return fs.readFileSync(path.join(__dirname, 'active_version.txt'), 'utf-8'); | ||
} | ||
function install(version, callback) { | ||
var bin_path = bin_path(version); | ||
if ( dir_exists(bin_path) ) { | ||
callback(null); | ||
} else { | ||
install(version, function(err) { | ||
callback(err); | ||
}); | ||
} | ||
} | ||
var bin_path = bin_path(version); | ||
if (dir_exists(bin_path)) { | ||
callback(null); | ||
} else { | ||
install(version, function(err) { | ||
callback(err); | ||
}); | ||
} | ||
} |
#!/usr/bin/env node | ||
'use strict'; | ||
(function(){ | ||
var fs = require('fs') | ||
var os = require('os') | ||
var path = require('path') | ||
"use strict"; | ||
(function() { | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var path = require('path'); | ||
var Decompress = require('decompress'); | ||
@@ -18,5 +19,7 @@ var download = require('mongodb-download'); | ||
} | ||
debug("installing version: %s", version); | ||
debug("installing version: %s", version); | ||
// downloads if not cached | ||
download({version: version}, function(err, archive) { | ||
download({ | ||
version: version | ||
}, function(err, archive) { | ||
if (err) { | ||
@@ -29,7 +32,7 @@ return callback(err); | ||
function extractFile (archive, version, callback) { | ||
function extractFile(archive, version, callback) { | ||
var dist_path = path.join(__dirname, './dist/'); | ||
fs.writeFileSync(path.join(__dirname, 'active_version.txt'), version); | ||
fs.writeFile(path.join(__dirname, 'dist_path.txt'), dist_path, function (err) { | ||
fs.writeFile(path.join(__dirname, 'dist_path.txt'), dist_path, function(err) { | ||
if (err) { | ||
@@ -39,15 +42,19 @@ return callback(err); | ||
var archive_type; | ||
if ( /\.zip$/.test(archive) ) { | ||
archive_type = "zip"; | ||
if (/\.zip$/.test(archive)) { | ||
archive_type = "zip"; | ||
} else { | ||
archive_type = "targz"; | ||
archive_type = "targz"; | ||
} | ||
debug("archive type selected %s", archive_type); | ||
new Decompress({mode: '755'}) | ||
.src(archive) | ||
.dest(path.join(__dirname, 'dist', version)) | ||
.use(Decompress[archive_type]({strip: 1})) | ||
.run(function(err, files) { | ||
if (! err ) { | ||
new Decompress({ | ||
mode: '755' | ||
}) | ||
.src(archive) | ||
.dest(path.join(__dirname, 'dist', version)) | ||
.use(Decompress[archive_type]({ | ||
strip: 1 | ||
})) | ||
.run(function(err, files) { | ||
if (!err) { | ||
callback(); | ||
@@ -57,5 +64,5 @@ } else { | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -67,11 +74,11 @@ } | ||
if (!module.parent) { | ||
install(null, function(err) { | ||
if ( err ) { | ||
console.log('Error during installation:', err); | ||
} else { | ||
console.log('Done installing MongoDB'); | ||
} | ||
}); | ||
install(null, function(err) { | ||
if (err) { | ||
console.log('Error during installation:', err); | ||
} else { | ||
console.log('Done installing MongoDB'); | ||
} | ||
}); | ||
} | ||
})(); |
{ | ||
"name": "mongodb-prebuilt", | ||
"version": "4.5.4", | ||
"version": "4.5.5", | ||
"description": "Install MongoDB prebuilt binaries via npm", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
14121
23
270