init.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //var settings = {heartbeatSleep: 0.05, heartbeatTimeout: 0.5}
  2. var settings = {};
  3. // We know the master of the first set (pri=1), but not of the second.
  4. var rs1cfg = {_id: "rs1",
  5. members: [{_id: 1, host: "127.0.0.1:40011", priority: 1, tags: {rs1: "a"}},
  6. {_id: 2, host: "127.0.0.1:40012", priority: 0, tags: {rs1: "b"}},
  7. {_id: 3, host: "127.0.0.1:40013", priority: 0, tags: {rs1: "c"}}],
  8. settings: settings}
  9. var rs2cfg = {_id: "rs2",
  10. members: [{_id: 1, host: "127.0.0.1:40021", priority: 1, tags: {rs2: "a"}},
  11. {_id: 2, host: "127.0.0.1:40022", priority: 1, tags: {rs2: "b"}},
  12. {_id: 3, host: "127.0.0.1:40023", priority: 1, tags: {rs2: "c"}}],
  13. settings: settings}
  14. var rs3cfg = {_id: "rs3",
  15. members: [{_id: 1, host: "127.0.0.1:40031", priority: 1, tags: {rs3: "a"}},
  16. {_id: 2, host: "127.0.0.1:40032", priority: 1, tags: {rs3: "b"}},
  17. {_id: 3, host: "127.0.0.1:40033", priority: 1, tags: {rs3: "c"}}],
  18. settings: settings}
  19. for (var i = 0; i != 60; i++) {
  20. try {
  21. db1 = new Mongo("127.0.0.1:40001").getDB("admin")
  22. db2 = new Mongo("127.0.0.1:40002").getDB("admin")
  23. rs1a = new Mongo("127.0.0.1:40011").getDB("admin")
  24. rs2a = new Mongo("127.0.0.1:40021").getDB("admin")
  25. rs3a = new Mongo("127.0.0.1:40031").getDB("admin")
  26. break
  27. } catch(err) {
  28. print("Can't connect yet...")
  29. }
  30. sleep(1000)
  31. }
  32. function hasSSL() {
  33. return Boolean(db1.serverBuildInfo().OpenSSLVersion)
  34. }
  35. rs1a.runCommand({replSetInitiate: rs1cfg})
  36. rs2a.runCommand({replSetInitiate: rs2cfg})
  37. rs3a.runCommand({replSetInitiate: rs3cfg})
  38. function configShards() {
  39. cfg1 = new Mongo("127.0.0.1:40201").getDB("admin")
  40. cfg1.runCommand({addshard: "127.0.0.1:40001"})
  41. cfg1.runCommand({addshard: "rs1/127.0.0.1:40011"})
  42. cfg2 = new Mongo("127.0.0.1:40202").getDB("admin")
  43. cfg2.runCommand({addshard: "rs2/127.0.0.1:40021"})
  44. cfg3 = new Mongo("127.0.0.1:40203").getDB("admin")
  45. cfg3.runCommand({addshard: "rs3/127.0.0.1:40031"})
  46. }
  47. function configAuth() {
  48. var addrs = ["127.0.0.1:40002", "127.0.0.1:40203", "127.0.0.1:40031"]
  49. if (hasSSL()) {
  50. addrs.push("127.0.0.1:40003")
  51. }
  52. for (var i in addrs) {
  53. print("Configuring auth for", addrs[i])
  54. var db = new Mongo(addrs[i]).getDB("admin")
  55. var v = db.serverBuildInfo().versionArray
  56. var timedOut = false
  57. if (v < [2, 5]) {
  58. db.addUser("root", "rapadura")
  59. } else {
  60. try {
  61. db.createUser({user: "root", pwd: "rapadura", roles: ["root"]})
  62. } catch (err) {
  63. // 3.2 consistently fails replication of creds on 40031 (config server)
  64. print("createUser command returned an error: " + err)
  65. if (String(err).indexOf("timed out") >= 0) {
  66. timedOut = true;
  67. }
  68. }
  69. }
  70. for (var i = 0; i < 60; i++) {
  71. var ok = db.auth("root", "rapadura")
  72. if (ok || !timedOut) {
  73. break
  74. }
  75. sleep(1000);
  76. }
  77. if (v >= [2, 6]) {
  78. db.createUser({user: "reader", pwd: "rapadura", roles: ["readAnyDatabase"]})
  79. } else if (v >= [2, 4]) {
  80. db.addUser({user: "reader", pwd: "rapadura", roles: ["readAnyDatabase"]})
  81. } else {
  82. db.addUser("reader", "rapadura", true)
  83. }
  84. }
  85. }
  86. function countHealthy(rs) {
  87. var status = rs.runCommand({replSetGetStatus: 1})
  88. var count = 0
  89. var primary = 0
  90. if (typeof status.members != "undefined") {
  91. for (var i = 0; i != status.members.length; i++) {
  92. var m = status.members[i]
  93. if (m.health == 1 && (m.state == 1 || m.state == 2)) {
  94. count += 1
  95. if (m.state == 1) {
  96. primary = 1
  97. }
  98. }
  99. }
  100. }
  101. if (primary == 0) {
  102. count = 0
  103. }
  104. return count
  105. }
  106. var totalRSMembers = rs1cfg.members.length + rs2cfg.members.length + rs3cfg.members.length
  107. for (var i = 0; i != 60; i++) {
  108. var count = countHealthy(rs1a) + countHealthy(rs2a) + countHealthy(rs3a)
  109. print("Replica sets have", count, "healthy nodes.")
  110. if (count == totalRSMembers) {
  111. configShards()
  112. configAuth()
  113. quit(0)
  114. }
  115. sleep(1000)
  116. }
  117. print("Replica sets didn't sync up properly.")
  118. quit(12)
  119. // vim:ts=4:sw=4:et