-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
355 lines (309 loc) · 9.89 KB
/
main.ts
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env -S deno run --allow-all
import "https://deno.land/std/log/mod.ts";
import { Select } from "https://deno.land/x/[email protected]/prompt/select.ts";
import { Confirm } from "https://deno.land/x/[email protected]/prompt/confirm.ts";
import "https://deno.land/x/[email protected]/dist/lodash.js";
import * as log from "https://deno.land/[email protected]/log/mod.ts";
import config from "./config.json" with { type: "json" };
import $ from "https://deno.land/x/dax/mod.ts";
// now `_` is imported in the global variable, which in deno is `self`
// deno-lint-ignore no-explicit-any
const _ = (self as any)._;
import {
blue,
bold,
dim,
gray,
green,
red,
} from "https://deno.land/std/fmt/colors.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";
const PARSED_ARGS = parse(Deno.args, {
boolean: ["version", "debug"],
string: ["env"],
default: { env: "local", "debug": false },
});
let logDefault: log.LoggerConfig = { level: "INFO", handlers: ["jsonStdout"] };
if (PARSED_ARGS.debug) {
logDefault = { level: "DEBUG", handlers: ["jsonStdout"] };
}
await log.setup({
//define handlers
handlers: {
jsonStdout: new log.ConsoleHandler("DEBUG", {
formatter: log.formatters.jsonFormatter,
useColors: false,
}),
},
//assign handlers to loggers
loggers: {
default: logDefault,
},
});
const logger = log.getLogger();
import { Document, MongoClient } from "npm:[email protected]";
if (Deno.env.has("MSH_ENV_VAR_OVERRIDE")) {
const overrides = JSON.parse(Deno.env.get("MSH_ENV_VAR_OVERRIDE") || "{}");
const uOverride = overrides["MONGO_USER"];
const pOverride = overrides["MONGO_PASSWORD"];
if (uOverride) {
Deno.env.set("MONGO_USER", Deno.env.get(uOverride) || "");
}
if (pOverride) {
Deno.env.set("MONGO_PASSWORD", Deno.env.get(pOverride) || "");
}
}
const MONGO_USER = Deno.env.get("MONGO_USER") || "";
const MONGO_PASSWORD = Deno.env.get("MONGO_PASSWORD") || "";
const MONGO_AUTH_DB = Deno.env.get("MONGO_AUTH_DB") || "admin";
type Node = {
rs: string;
name: string;
state: string;
};
const buildAuthURI = (user: string, password: string) => {
if (user === "") return "";
return [
user,
":",
password,
"@",
].join("");
};
/*
mongos> db.getSiblingDB('config').mongos.find({}, {_id: 1})
{ "_id" : "enterprise.gear.xargs.io:27017" }
mongos> db.getSiblingDB('config').shards.find({}, {host: 1})
{ "_id" : "shard01", "host" : "shard01/localhost:27018,localhost:27019,localhost:27020" }
{ "_id" : "shard02", "host" : "shard02/localhost:27021,localhost:27022,localhost:27023" }
{ "_id" : "shard03", "host" : "shard03/localhost:27024,localhost:27025,localhost:27026" }
mongos> db.adminCommand("getShardMap").map
ping each mongo shard for membership
db.getSiblingDB("admin").runCommand({replSetGetStatus: 1}).members
*/
const getMongosByEnv = (envName: string) => {
if (!Deno.env.has("MONGOS_BY_ENV")) {
throw new Error("Missing MONGOS_BY_ENV variable which is json encoded");
}
const MONGOS_BY_ENV = JSON.parse(Deno.env.get("MONGOS_BY_ENV") || "{}");
return MONGOS_BY_ENV[envName];
};
const getShardMap = async (envName: string) => {
const uri = `mongodb://${buildAuthURI(MONGO_USER, MONGO_PASSWORD)}${
getMongosByEnv(envName)
}?authSource=${MONGO_AUTH_DB}`;
logger.debug("getShardMap", { fn: "getShardMap", uri });
let result;
try {
const client = new MongoClient(uri);
result = await client.db("admin").command({ getShardMap: 1 });
} catch (error) {
logger.warn(`Error on ${envName}`, { error, MONGO_USER, MONGO_AUTH_DB });
}
return result?.map;
};
const mongosh = async (args: string[]) => {
const cmd = new Deno.Command("mongo", { args: args });
logger.debug("mongosh", { cmd, args });
const child = cmd.spawn();
await child.status;
};
const mainPrompted = async (envName: string) => {
const shardMap: Record<string, string> = await getShardMap(envName);
const nodes = Object.entries(shardMap).filter(([k, v]) => {
return k !== v;
}).filter(([k, v]) => {
// Nodes lack a /
if (k.indexOf("/") === -1 && k.indexOf(":") !== -1) {
return true;
}
if (v.startsWith(k)) {
return false;
}
return false;
});
type Shard = {
rs: string;
connection: string;
};
const allShards = nodes.map(([_k, v]) => {
// "rs-N/rs1-0:27017,rs1-1:27017,rs1-2:27017"
const [rs, connection] = v.split("/");
return { rs, connection } as Shard;
});
const shardURIs = _.uniqBy(allShards, (s: Shard) => (s.rs));
const nodeRespondedOnPort = async (node: string, port: string) => {
const result = await $`nc -z ${node} ${port}`.stdout("piped").noThrow()
.quiet();
if (result.code === 0) {
return true;
}
return false;
};
// Fails if any of the nodes is unreachable on the port
// So we work around that by trying one node at a time
// first with netcat and then with the actual connection
// See issue: https://github.com/denoland/deno/issues/11595
const replSetGetStatus = async ({ rs, connection }: Shard) => {
const oneNode = await connection.split(",").find(async (c) => {
const [node, port] = c.split(":");
return await nodeRespondedOnPort(node, port);
});
// NOTE: ?authenticationDatabase=admin is equivalent to authSource when using driver :shrug:
const uri = `mongodb://${
buildAuthURI(MONGO_USER, MONGO_PASSWORD)
}${oneNode}/?authSource=${MONGO_AUTH_DB}&directConnection=true&replicaSet=${rs}`;
logger.debug("mainPrompt looping over nodes", { uri });
try {
const client = new MongoClient(uri);
logger.debug("mainPrompt client instantiated", { uri });
const db = client.db("admin");
logger.debug("mainPrompt db instance");
return await db.command({ replSetGetStatus: 1 });
} catch (error) {
logger.error(error);
}
};
const allNodes: Node[] = [];
for await (const n of shardURIs) {
const result = await replSetGetStatus(n).catch((e) =>
logger.error("Error getting connection", e)
) as Document;
// Try to guard against a single node going down and breaking connectivity
if (result === undefined) {
continue;
}
// deno-lint-ignore no-explicit-any
const all = result.members.map((e: any) => {
return { rs: n.rs, name: e.name as string, state: e.stateStr };
});
allNodes.push(all);
}
// Insert mongos back into the total possible set
allNodes.push(
{ rs: "mongos", name: getMongosByEnv(envName), state: "none" } as Node,
);
const colorByState = (state: string) => {
switch (state) {
case "PRIMARY":
return bold(red(state));
case "SECONDARY":
return bold(green(state));
default:
return bold(gray(state));
}
};
const allNodesDeduplicated = _.chain(allNodes).flatten().uniqBy("name")
.value();
const uniqNodes = _.chain(allNodes).flatten().uniqBy("name").map(
(e: Node) => {
return {
name: `${blue(e.name)} in shard: ${dim(blue(e.rs))} with state: ${
colorByState(e.state)
}`,
value: e.name,
};
},
).value();
const server: string = await Select.prompt({
message: "Pick a server",
info: true,
options: uniqNodes,
search: true,
});
return { server: server, nodes: allNodesDeduplicated };
};
const connect = async () => {
// Prompt for either failover against a shard OR connect a shell
const choice: string = await Select.prompt({
message: "Operation to execute?",
info: true,
options: ["failover", "mongoshell"],
search: true,
});
let server = PARSED_ARGS._[0] as string;
let nodes: Node[] = [];
if (PARSED_ARGS["env"]) {
({ server, nodes } = await mainPrompted(PARSED_ARGS.env));
}
switch (choice) {
case "failover":
await runFailover(server, nodes);
break;
case "mongoshell":
await runMongoShell(server);
break;
default:
await runMongoShell(server);
break;
}
Deno.exit(0);
};
const runFailover = async (server: string, nodes: Node[]) => {
const match = nodes.find((n) => n.name === server);
if (match === undefined) {
throw Error("Unable to find matching server in our node set");
}
const primary = nodes.find((n) => n.rs === match.rs && n.state === "PRIMARY");
if (primary === undefined) {
throw Error("Unable to find matching server primary in our node set");
}
// TODO: add in failover mechanism
// https://www.mongodb.com/docs/manual/reference/command/replSetStepDown/
const confirmed: boolean = await Confirm.prompt({
message: `Failover: ${primary.name} ${primary.state} in ${primary.rs}?`,
});
if (!confirmed) {
throw Error("Denied confirmation exiting");
}
const uri = `mongodb://${
buildAuthURI(MONGO_USER, MONGO_PASSWORD)
}${primary.name}?authSource=${MONGO_AUTH_DB}`;
const client = new MongoClient(uri);
const _result = await client.db("admin").command({ replSetStepDown: 60 })
.catch((e) => logger.info(`Failed over: `, e));
logger.info(`Failed over: `, primary);
logger.info(`Fetching replicaset state in 5s `);
await new Promise((resolve) => setTimeout(resolve, 5 * 1000));
const clientv2 = new MongoClient(uri);
const result = await clientv2.db("admin").command({ replSetGetStatus: 1 });
logger.info(
`Result after failover `,
result.members.map((n: Document[string]) => ({
name: n.name,
state: n.stateStr,
})),
);
};
const runMongoShell = async (server: string) => {
Deno.addSignalListener("SIGINT", () => {
console.log("interrupted!");
Deno.exit();
});
if (MONGO_USER !== "") {
await mongosh([
`mongodb://${server}`,
`--username`,
MONGO_USER,
`--password`,
MONGO_PASSWORD,
`--authenticationDatabase`,
MONGO_AUTH_DB,
]);
} else {
await mongosh([
`mongodb://${server}`,
]);
}
};
const version = () => {
console.info(`msh version ${config.version}`);
};
const main = () => {
if (PARSED_ARGS.version) {
version();
} else {
connect();
}
};
await main();