-
Notifications
You must be signed in to change notification settings - Fork 1
/
extendcluster.py
executable file
·174 lines (137 loc) · 4.94 KB
/
extendcluster.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
import json
import sys
import getopt
import copy
import time
from pprint import pprint
import re
#Read an automation config and extend it with a second shard on same hosts as
#Original - NOT SOMETHIGN YOU SHOULD DO - in any normal case.
autoconfigfile = ""
def logmsg(x):
#pprint(x)
pass
def print_usage_message():
print ("usage: " + sys.argv[0] + " [-h,--help] [-c,--config CurrentConfig] ")
def parse_args(argv):
global autoconfigfile
try:
opts, args = getopt.getopt(argv,"c:h",["config=","help"])
except getopt.GetoptError:
print_usage_message()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_usage_message()
sys.exit()
elif opt in ("-c", "--config"):
autoconfigfile = arg
if autoconfigfile == "":
print("You must specify an input config")
sys.exit(1)
def read_existing(name):
with open(autoconfigfile) as autofile:
data = json.load(autofile)
return data
def add_shards(config,nnewshards):
sharding = config['sharding']
shards = sharding[0]['shards']
firstshard = shards[0]
nshards = len(shards)
logmsg( "Cluster currently has " + str(nshards) + " shards")
shardname = firstshard['_id'].split('_')[0]
logmsg( "Prefix = " + shardname)
#Find highest port for each host
processes = config['processes']
hosts = {}
mongodhosts = {}
newprocesses = []
for p in processes:
hostname = p['hostname']
port = p["args2_6"]["net"]["port"]
if hostname in hosts:
if port > hosts[hostname]:
hosts[hostname]=port
else:
hosts[hostname]=port
#Which have a mongod on - no install with mongos!
if p['processType'] == "mongod":
#verify its not a config server
isconfig = False
try:
if p['args2_6']['sharding']['clusterRole'] == 'configsvr':
isconfig = True
except:
pass
#in case we have dedicated config server hosts
if isconfig == False:
mongodhosts[hostname] = True
example_mongod = p
#Add Processes for our new shards
host = 0
oneup = 0
newrepsets=[]
stime = int(time.time())
newshards = []
for s in range(0,nnewshards):
shardno = nshards + s
logmsg("Adding shard " + str(shardno))
repsetmembers = []
for r in range(0,3):
newprocess = copy.deepcopy(example_mongod)
newname = shardname+"_"+str(shardno)+"_"+str(stime)+"_"+str(oneup)
hostname = list(mongodhosts)[host]
port = hosts[hostname]+1
hosts[hostname]=port
oneup=oneup+1
host=(host+1) % len(mongodhosts)
dbpath = example_mongod['args2_6']['storage']['dbPath']
matchObj = re.match(r'^(.*/)',dbpath)
if matchObj == None:
logmsg("Could not extract dbpath")
exit(1)
dbpath = matchObj.group(1)+newname
replsetname = shardname+"_"+str(shardno)
newprocess['args2_6']['net']['port'] = port
newprocess['args2_6']['replication']['replSetName']=replsetname
newprocess['args2_6']['storage']['dbPath'] = dbpath
#DIFFERENT FOR EBPI (TODO)
newprocess['args2_6']['systemLog']['path'] = dbpath + "/mongodb.log"
newprocess['hostname'] = hostname
newprocess['name'] = newname
newprocesses.append(newprocess)
logmsg(newprocess)
if r == 2:
isArbiter =True
else:
isArbiter = False
repsetconfig = { "_id" : r,
"arbiterOnly" : isArbiter,
"hidden" : False,
"priority" : 1.0,
"slaveDelay": 0,
"votes" : 1,
"host" : newname }
repsetmembers.append(repsetconfig)
newrepsets.append({"_id":shardname+"_"+str(shardno),
"members" : repsetmembers})
newshards.append({"_id":shardname+"_"+str(shardno),
"rs" : shardname+"_"+str(shardno),
"tags" : []
})
logmsg(newrepsets)
#Now add all this
for p in newprocesses:
config['processes'].append(p)
for r in newrepsets:
config['replicaSets'].append(r)
for s in newshards:
config["sharding"][0]["shards"].append(s)
logmsg(config['sharding'])
print(json.dumps(config ,indent=4, separators=(',', ': ')))
if __name__ == "__main__":
logmsg("Reads an automation config and modifies it as needed")
parse_args(sys.argv[1:])
existing = read_existing(autoconfigfile)
add_shards(existing,3)