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