dropall.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var ports = [40001, 40002, 40011, 40012, 40013, 40021, 40022, 40023, 40041, 40101, 40102, 40103, 40201, 40202, 40203]
  2. var auth = [40002, 40103, 40203, 40031]
  3. var db1 = new Mongo("localhost:40001")
  4. if (db1.getDB("admin").serverBuildInfo().OpenSSLVersion) {
  5. ports.push(40003)
  6. auth.push(40003)
  7. }
  8. for (var i in ports) {
  9. var port = ports[i]
  10. var server = "localhost:" + port
  11. var mongo = new Mongo("localhost:" + port)
  12. var admin = mongo.getDB("admin")
  13. for (var j in auth) {
  14. if (auth[j] == port) {
  15. admin.auth("root", "rapadura")
  16. admin.system.users.find().forEach(function(u) {
  17. if (u.user == "root" || u.user == "reader") {
  18. return;
  19. }
  20. if (typeof admin.dropUser == "function") {
  21. mongo.getDB(u.db).dropUser(u.user);
  22. } else {
  23. admin.removeUser(u.user);
  24. }
  25. })
  26. break
  27. }
  28. }
  29. var result = admin.runCommand({"listDatabases": 1})
  30. for (var j = 0; j != 100; j++) {
  31. if (typeof result.databases != "undefined" || notMaster(result)) {
  32. break
  33. }
  34. result = admin.runCommand({"listDatabases": 1})
  35. }
  36. if (notMaster(result)) {
  37. continue
  38. }
  39. if (typeof result.databases == "undefined") {
  40. print("Could not list databases. Command result:")
  41. print(JSON.stringify(result))
  42. quit(12)
  43. }
  44. var dbs = result.databases
  45. for (var j = 0; j != dbs.length; j++) {
  46. var db = dbs[j]
  47. switch (db.name) {
  48. case "admin":
  49. case "local":
  50. case "config":
  51. break
  52. default:
  53. mongo.getDB(db.name).dropDatabase()
  54. }
  55. }
  56. }
  57. function notMaster(result) {
  58. return typeof result.errmsg != "undefined" && (result.errmsg.indexOf("not master") >= 0 || result.errmsg.indexOf("no master found"))
  59. }
  60. // vim:ts=4:sw=4:et