forked from vkarpov15/run-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·222 lines (195 loc) · 6.57 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
'use strict';
const ReplSet = require('mongodb-topology-manager').ReplSet;
const chalk = require('chalk');
const co = require('co');
const commander = require('commander');
const download = require('./src/download');
const execSync = require('child_process').execSync;
const fs = require('fs');
const moment = require('moment');
const mongodb = require('mongodb');
const options = require('./src/options');
const prettyjson = require('prettyjson');
const printHelp = require('./src/printHelp');
const spawn = require('child_process').spawn;
const os = require('os');
require('./src/topologyManagerPatch');
const ports = [];
const isWin = process.platform === 'win32';
let hostname = '';
for (const o of options) {
commander.option(o.option, o.description);
}
commander.parse(process.argv);
co(run).catch(error => console.error(chalk.red(error.stack)));
function* run() {
if (commander.rawArgs.indexOf('--help') > 0) {
printHelp();
return;
}
const options = {};
const rcfile = isWin ? `${process.cwd()}\\.run-rs.rc` : `${process.cwd()}/.run-rs.rc`;
if (fs.existsSync(rcfile)) {
Object.assign(options, JSON.parse(fs.readFileSync(rcfile, 'utf8')));
}
const version = typeof commander.version === 'string' ?
commander.version :
options.version || '4.0.12';
const n = parseInt(commander.number, 10) || 3;
const startingPort = parseInt(commander.portStart, 10) || 27017;
for (let i = 0; i < n; ++i) {
ports.push(startingPort + i);
}
if (commander.host) {
hostname = `${commander.host}`;
}
else {
hostname = isWin ? os.hostname() : 'localhost';
}
let mongod;
let mongo;
if (commander.mongod) {
mongod = typeof commander.mongod === 'string' ? commander.mongod : 'mongod';
mongo = typeof commander.mongod === 'string' ?
commander.mongod.replace(/mongod$/i, 'mongo') :
'mongo';
try {
const where = isWin ? 'where' : 'command -v';
execSync(`${where} ${mongod}`);
} catch (err) {
throw new Error(`No mongod process found at ${mongod}, check your --mongod option`, err);
}
} else {
mongod = isWin ? `${__dirname}\\${version}\\mongod.exe` : `${__dirname}/${version}/mongod`;
mongo = isWin ? `${__dirname}\\${version}\\mongo.exe` : `${__dirname}/${version}/mongo`;
if (!fs.existsSync(mongod)) {
console.log(`Downloading MongoDB ${version}`);
const path = download(version, commander.linux || 'ubuntu1604').path;
console.log(`Copied MongoDB ${version} to '${path}'`);
}
}
let dbPath;
if (typeof commander.dbpath === 'string') {
dbPath = `${commander.dbpath}` ;
}
else {
dbPath = isWin ? `${process.cwd()}\\data` : `${process.cwd()}/data`;
}
if (!fs.existsSync(`${dbPath}`)) {
execSync(isWin ? `md ${dbPath}` : `mkdir -p ${dbPath}`);
}
if (commander.keep) {
console.log(chalk.blue('Skipping purge'));
} else {
console.log(chalk.blue('Purging database...'));
execSync(isWin ? `del /S /Q ${dbPath}\\*` : `rm -rf ${dbPath}/*`);
}
ports.forEach((port) => {
const portDBPath = isWin ? `${dbPath}\\${port}` : `${dbPath}/${port}`;
if (!fs.existsSync(portDBPath)) {
execSync(isWin ? `md ${dbPath}\\${port}` : `mkdir -p ${dbPath}/${port}`);
}
});
console.log(`Running '${mongod}'`, ports);
const rs = new ReplSet(mongod,
ports.map(port => {
const options = {
port: port,
dbpath: isWin ? `${dbPath}\\${port}` : `${dbPath}/${port}`,
bind_ip: hostname
};
if (commander.bind_ip_all) {
options.bind_ip_all = null;
}
return { options };
}), { replSet: 'rs' });
if (commander.keep) {
console.log(chalk.blue('Restarting replica set...'));
for (const manager of rs.managers) {
yield manager.start();
}
const result = yield rs.managers[0].executeCommand('admin.$cmd', {
replSetGetStatus: 1
}, null, { ignoreError: true });
if (result.set) {
// There's already a replica set config, so don't initiate
yield rs.waitForPrimary();
} else {
// First time starting up, need to create a replica set config
for (const manager of rs.managers) {
yield manager.stop();
}
yield startRS(rs);
}
} else {
console.log(chalk.blue('Starting replica set...'));
yield startRS(rs);
}
const hosts = ports.map(port => `${hostname}:${port}`);
console.log(chalk.green(`Started replica set on "mongodb://${hosts.join(',')}?replicaSet=rs"`));
if (commander.shell) {
console.log(chalk.blue(`Running mongo shell: ${mongo}`));
const shellDefaultHost = (hosts[0].split(':'))[0];
const shellDefaultPort = (hosts[0].split(':'))[1];
spawn(mongo,
isWin ? ['--quiet','--port', shellDefaultPort, '--host', shellDefaultHost] : ['--quiet'],
{ stdio: 'inherit' }
);
} else if (!commander.quiet) {
const client = yield mongodb.MongoClient.connect(`mongodb://${hosts[0]}/test`, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const oplog = client.db('local').collection('oplog.rs').find({ ts: { $gte: new mongodb.Timestamp() } }, {
tailable: true,
awaitData: true,
oplogReplay: true,
noCursorTimeout: true,
numberOfRetries: Number.MAX_VALUE
}).stream();
console.log(chalk.green('Connected to oplog'));
oplog.on('end', () => {
console.log(moment().format('YYYY-MM-DD HH:mm:ss'), chalk.red('MongoDB oplog finished'));
});
oplog.on('data', data => {
if (['n'].includes(data.op)) {
return;
}
if (data.ns.startsWith('admin.') || data.ns.startsWith('config.')) {
return;
}
const ops = {
c: 'createCollection',
d: 'delete',
i: 'insert',
u: 'update'
};
const op = ops[data.op] || data.op;
let o = prettyjson.render(JSON.parse(JSON.stringify(data.o)));
if ('o2' in data) {
o = `${prettyjson.render(JSON.parse(JSON.stringify(data.o2)))} ${o}`;
}
console.log(chalk.blue(moment().format('YYYY-MM-DD HH:mm:ss')), data.ns, op);
console.log(o);
});
oplog.on('error', err => {
console.log(chalk.red(moment().format('YYYY-MM-DD HH:mm:ss')), chalk.red(`Oplog error: ${err.stack}`));
});
}
}
function startRS(rs) {
return co(function*() {
try {
yield rs.start();
} catch (err) {
if (err.message.includes('SocketException: Address already in use')) {
const match = err.message.match(/port: (\d+)/);
if (match != null) {
throw new Error(`Could not start mongod on port ${match[1]} because it is already in use`);
}
}
throw err;
}
});
}