rs.add() trough pyhon

24 views
Skip to first unread message

Mario Splivalo

unread,
Apr 27, 2018, 2:38:04 PM4/27/18
to mongodb-user
Hi.

I'd like to add a new member into replicaset using python.

I used to do this via rs.add() called via mongo shell called from python via subprocess, something like this:

command = 'rs.add("%s")' % host
cmd_line = ['mongo', '--quiet', '--host', host, '--eval', 'printjson(%s)' % command]
cmd_output = subprocess.check_output(cmd_line)

In versions of MongoDB prior 3.6 rs.add() would return json, like this:
{"ok":1}

So, I would use something like this if member was added to replicaset:

if json.loads(cmd_output)['ok'] == 1:

Now, in Mongo 3.6, rs.add() returns BSON, looking like this:

{
       "ok" : 1,
       "operationTime" : Timestamp(1524851363, 1),
       "$clusterTime" : {
               "clusterTime" : Timestamp(1524851363, 1),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
               }
       }
}



Of course, parsing this using json module in python won't work. 
I even tried using python-bson, but it also fails. I understand that I need to somehow get output from rs.add() via pymongo.Connection(), but I don't know how (since rs.add() is a mongo shell wrapper).

I'd appreciate if anybody would have a suggestion on how to add member to replicaset using python. For now, the best thing do to is to implement rs.add() wrapper completely in python - getting the current replicaset configuration, adding new member to a python dictionary, and then passing that dictionary to replSetReconfig using MongoClient.admin.command.

But, is there a better/simpler solution? :)


Mario

shane....@10gen.com

unread,
Apr 28, 2018, 9:37:47 PM4/28/18
to mongodb-user
Hi Mario,

Instead of launching the mongo shell as a subproccess you can run the replSetReconfig command directly from PyMongo. Mongo-orchestration does this to set up and reconfigure replica sets: https://github.com/10gen/mongo-orchestration/blob/v0.6.11/mongo_orchestration/replica_sets.py#L220  

Best,
Shane

Mario Splivalo

unread,
Apr 30, 2018, 6:25:21 AM4/30/18
to mongod...@googlegroups.com
Hi, Shane!

Excellent, thank you - this is exactly what I had in mind.
I will go trough mongo-orchestration that you just showed me, but I am
assuming it is implementing exactly what rs.add() does - gets the
current configuration, increases the version of the config, adds new
node(s), and calls replSetReconfig with new configuration doc.

Thnx!

Mario

On 04/29/2018 03:37 AM, shane.harvey via mongodb-user wrote:
> Hi Mario,
>
> Instead of launching the mongo shell as a subproccess you can run
> the replSetReconfig command
> <https://docs.mongodb.com/manual/reference/command/replSetReconfig> directly
> from PyMongo. Mongo-orchestration does this to set up and reconfigure
> replica
> sets: https://github.com/10gen/mongo-orchestration/blob/v0.6.11/mongo_orchestration/replica_sets.py#L220  
>
> Best,
> Shane
>
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user"
> group.
>  
> For other MongoDB technical support options, see:
> https://docs.mongodb.com/manual/support/
> ---
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mongodb-user...@googlegroups.com
> <mailto:mongodb-user...@googlegroups.com>.
> To post to this group, send email to mongod...@googlegroups.com
> <mailto:mongod...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/fe9c8418-cc88-4d11-8a23-6a6241ad5557%40googlegroups.com
> <https://groups.google.com/d/msgid/mongodb-user/fe9c8418-cc88-4d11-8a23-6a6241ad5557%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages