forked from mongodb-js/mongodb-prebuilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (147 loc) · 4.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"use strict";
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var proc = require('child_process');
var spawnSync = require('spawn-sync');
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('mongodb-prebuilt');
var mongodb_logs = require('debug')('mongodb');
var os = require('os');
module.exports = {
"bin_path": bin_path,
"dist_path": dist_path,
"active_version": active_version,
"install": install,
"start_server": start_server,
"shutdown": shutdown
};
// persist created child pid
var child_pid = 0;
var killer;
function shutdown (e) {
if (child_pid !== 0) {
debug('killing mongod process: %d', child_pid);
process.removeListener('exit', shutdown);
process.kill(child_pid);
killer.kill();
}
};
process.on('exit', shutdown);
function start_server(opts, callback) {
if (!opts) {
opts = {};
}
if (!opts.args) {
opts.args = {};
}
if (opts.args.fork === undefined) {
opts.args.fork = true;
}
if (!opts.args.logpath) {
var log_file = path.join(os.tmpdir(), 'mongodb-prebuilt-' + (new Date()).getTime() + '.log');
debug('logpath is', log_file);
opts.args.logpath = log_file;
}
var args = build_args(opts);
var bpath = bin_path(opts.version);
if (!bpath) {
return install(opts.version, function(err) {
if (err) {
callback(err);
} else {
bpath = bin_path(opts.version);
return start();
}
});
} else {
return start();
}
function start() {
debug("spawn", bpath + "mongod", args.join(' '));
var child = spawnSync(bpath + "mongod", args);
mongodb_logs(child.stdout.toString());
mongodb_logs(child.stderr.toString());
// error
if (child.status !== 0) {
if (opts.exit_callback) {
opts.exit_callback(child.status);
}
callback(child.status);
return child.status;
}
// need to catch child pid
var child_pid_match = child.stdout.toString().match(/forked process:\s+(\d+)/i);
child_pid = child_pid_match[1];
// if mongod started, spawn killer
if (child.status === 0) {
debug('starting mongokiller.js, ppid:%d\tmongod pid:%d', process.pid, child_pid);
killer = proc.spawn("node", [path.join(__dirname, "binjs", "mongokiller.js"), process.pid, child_pid], {
stdio: 'ignore',
detached: true
});
killer.unref();
}
return child.status;
}
}
function dir_exists(dir) {
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 [];
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();
}
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;
}
}
function dist_path() {
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');
}
function install(version, callback) {
var bin_path = bin_path(version);
if (dir_exists(bin_path)) {
callback(false);
} else {
var spawn_opts = [];
if (version) {
spawn_opts.push('--version', version);
}
if (process.env.https_proxy) {
spawn_opts.push('--https-proxy', process.env.https_proxy);
}
var install_out = child_process.spawnFileSync('./install.js', spawn_opts);
callback(!!install_out.status);
}
}