-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_rs.py
executable file
·32 lines (22 loc) · 971 Bytes
/
configure_rs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python3
import subprocess,os,sys
if len(sys.argv) < 2:
print("USAGE: ./script host1 host2 ... (host1 becomes primary)")
exit()
print("Configuring replica set...")
members = []
for i in range(1, len(sys.argv)):
prio = 1 if i == 1 else 0 #prioritize first member for the primary election
members.append("{_id: "+str(i-1)+", host: \'"+str(sys.argv[i])+"\', priority: "+str(prio)+"}")
#stringify members and prepare rs config
members = ', '.join(d for d in members)
cfg = "{_id: 'rs0', members: ["+members+"]}"
print(cfg)
#NOTE: I need the ugly \\\", or else the python interpreter won't keep \" as it is,
#causing a syntax error on bash
inner_cmd = "./mongo --host "+sys.argv[1]+" --eval \\\"JSON.stringify(db.adminCommand({'replSetInitiate' : "+cfg+"}))\\\""
outer_cmd = "sudo docker exec -it rt-mongod bash -c \""+inner_cmd+"\""
#execute
os.system(outer_cmd)
print("")
print(f"host {sys.argv[1]} has been prioritized for primary node election")