From 95b93e4119d97f6329949eece3f2055e82a190c4 Mon Sep 17 00:00:00 2001 From: Kristina Chodorow Date: Wed, 25 Aug 2010 11:35:44 -0400 Subject: [PATCH] add rs.remove helper SERVER-1676 --- shell/mongo_vstudio.cpp | 22 ++++++++++++++++++++-- shell/utils.js | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/shell/mongo_vstudio.cpp b/shell/mongo_vstudio.cpp index d75b968f3b6..e015986aca6 100644 --- a/shell/mongo_vstudio.cpp +++ b/shell/mongo_vstudio.cpp @@ -1265,6 +1265,7 @@ const StringData _jscode_raw_utils = "print(\"\\trs.add(membercfgobj) add a new member to the set with extra attributes\");\n" "print(\"\\trs.addArb(hostportstr) add a new member which is arbiterOnly:true\");\n" "print(\"\\trs.stepDown() step down as primary (momentarily)\");\n" +"print(\"\\trs.remove(hostportstr) remove a host from the replica set\");\n" "print(\"\\trs.conf() return configuration from local.system.replset\");\n" "print(\"\\trs.slaveOk() shorthand for db.getMongo().setSlaveOk()\");\n" "print();\n" @@ -1294,11 +1295,28 @@ const StringData _jscode_raw_utils = "}\n" "c.members.push(cfg);\n" "return db._adminCommand({ replSetReconfig: c });\n" -"}\n" -"rs.stepDown = function () { return db._adminCommand({ replSetStepDown:true}); }\n" +"};\n" +"rs.stepDown = function () { return db._adminCommand({ replSetStepDown:true}); };\n" "rs.addArb = function (hn) { return this.add(hn, true); }\n" "rs.conf = function () { return db.getSisterDB(\"local\").system.replset.findOne(); }\n" "\n" +"rs.remove = function (hn) {\n" +"var local = db.getSisterDB(\"local\");\n" +"assert(local.system.replset.count() <= 1, \"error: local.system.replset has unexpected contents\");\n" +"var c = local.system.replset.findOne();\n" +"assert(c, \"no config object retrievable from local.system.replset\");\n" +"c.version++;\n" +"\n" +"for (var i in c.members) {\n" +"if (c.members[i].host == hn) {\n" +"c.members.splice(i, 1);\n" +"return db._adminCommand({ replSetReconfig : c});\n" +"}\n" +"}\n" +"\n" +"return \"error: couldn't find \"+hn+\" in \"+tojson(c.members);\n" +"};\n" +"\n" "help = shellHelper.help = function (x) {\n" "if (x == \"connect\") {\n" "print(\"\\nNormally one specifies the server on the mongo shell command line. Run mongo --help to see those options.\");\n" diff --git a/shell/utils.js b/shell/utils.js index bea69a19d9c..8290fa63769 100644 --- a/shell/utils.js +++ b/shell/utils.js @@ -1260,6 +1260,7 @@ rs.help = function () { print("\trs.add(membercfgobj) add a new member to the set with extra attributes"); print("\trs.addArb(hostportstr) add a new member which is arbiterOnly:true"); print("\trs.stepDown() step down as primary (momentarily)"); + print("\trs.remove(hostportstr) remove a host from the replica set"); print("\trs.conf() return configuration from local.system.replset"); print("\trs.slaveOk() shorthand for db.getMongo().setSlaveOk()"); print(); @@ -1294,6 +1295,23 @@ rs.stepDown = function () { return db._adminCommand({ replSetStepDown:true}); } rs.addArb = function (hn) { return this.add(hn, true); } rs.conf = function () { return db.getSisterDB("local").system.replset.findOne(); } +rs.remove = function (hn) { + var local = db.getSisterDB("local"); + assert(local.system.replset.count() <= 1, "error: local.system.replset has unexpected contents"); + var c = local.system.replset.findOne(); + assert(c, "no config object retrievable from local.system.replset"); + c.version++; + + for (var i in c.members) { + if (c.members[i].host == hn) { + c.members.splice(i, 1); + return db._adminCommand({ replSetReconfig : c}); + } + } + + return "error: couldn't find "+hn+" in "+tojson(c.members); +}; + help = shellHelper.help = function (x) { if (x == "connect") { print("\nNormally one specifies the server on the mongo shell command line. Run mongo --help to see those options.");