-
Notifications
You must be signed in to change notification settings - Fork 28
/
cluster_connect
executable file
·164 lines (143 loc) · 6.06 KB
/
cluster_connect
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
#!/usr/bin/env python
#
# @author Couchbase <info@couchbase.com>
# @copyright 2011-2018 Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, sys, getopt, urllib2, urllib, json
from urllib2 import HTTPError
valid_bucket_types = ["ephemeral", "membase", "memcached"]
valid_service_types = {"kv", "n1ql", "index", "fts", "cbas", "example", "eventing"}
def usage():
print "usage: \n\
-n <number of nodes>\n\
-T <services to run. kv if unspecified> (eg: n0:kv,n1:index+n1ql+fts+eventing+cbas)\n\
-s <memory size (min 256) default: 256\n\
-I <index memory size> default: 256\n\
-t <bucket type> (ephemeral, membase, memcached) default: membase\n\
-r <num replicas> (max 3) default: 1 (Only for ephemeral or membase buckets!)\n\
-i (don't index replicas) default: replica index enabled\n\
-S <start index> default: 0\n\
-p <networking protocol to use> (ipv4, ipv6) default: ipv4"
class PasswordManager(urllib2.HTTPPasswordMgr):
def __init__(self, username, password):
self.auth = (username, password)
def find_user_password(self, realm, authuri):
return self.auth
def main():
try:
opts, args = getopt.getopt(sys.argv[1:],
"n:t:s:r:iS:T:I:p:", ["dont-rebalance"])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit()
nodes = 0
buckettype = "membase"
memsize = 256
indexmemsize = 256
replicas = 1
replica_index = True
start_index = 0
deploy = ['kv']
do_rebalance = True
protocol = "ipv4"
data_base_path = os.getcwd() + "/data"
for o, a in opts:
if o == "-n":
nodes = int(a)
elif o == "-t":
buckettype = a
elif o == "-s":
memsize = a
elif o == "-I":
indexmemsize = a
elif o == "-r":
replicas = a
elif o == "-i":
replica_index = False
elif o == "-S":
start_index = int(a)
elif o == "-T":
plan = a.replace(' ','').split(',')
if len(plan) == 1 and len(plan[0].split(':')) == 1:
deploy = plan[0].split('+')
else:
plan = dict(e.split(':') for e in plan)
deploy = dict([(k, v.split('+')) for k, v in plan.items()])
elif o == "-p":
protocol = a
elif o == "--dont-rebalance":
do_rebalance = False
else:
usage()
sys.exit()
if isinstance(deploy, list):
services = deploy
deploy = dict(("n%d" % i, services[:]) for i in xrange(nodes))
deploy["n0"] = deploy.get("n0", []) + ["kv"]
if nodes == 0 or buckettype not in valid_bucket_types or \
int(memsize) < 256 or int(replicas) > 3 or \
not set(deploy.keys()) <= set(["n" + str(i) for i in range(nodes)]) or \
not set(reduce(lambda x,y:x+y, deploy.values(), [])) <= valid_service_types:
usage()
sys.exit()
password_mgr = PasswordManager("Administrator", "asdasd")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
o = urllib2.build_opener(handler)
print "Connecting {0} nodes, bucket type {1}, mem size {2} " \
"with {3} replica copies, start index {4}, password asdasd, "\
"deployment plan {5}\n".format(nodes, buckettype,
memsize, replicas, start_index,
str(deploy))
base_port = 9000 + start_index
addr = "127.0.0.1" if protocol == "ipv4" else "[::1]"
services = deploy["n0"]
print "Connecting node 0 with services {0}".format(str(services))
o.open("http://{0}:{1}/node/controller/setupServices".format(addr, base_port),
"services={0}".format(",".join(services))).read()
o.open("http://{0}:{1}/pools/default".format(addr, base_port),
"memoryQuota=" + str(memsize) +
"&indexMemoryQuota=" + str(indexmemsize)).read()
o.open("http://{0}:{1}/pools/default/buckets".format(addr, base_port),
"name=default" +
"&authType=sasl" +
"&saslPassword=" +
"&bucketType=" + buckettype +
"&ramQuotaMB=" + str(memsize) +
"&replicaNumber=" + str(replicas) +
"&replicaIndex=" + bool_request_value(replica_index)).read()
o.open("http://{0}:{1}/settings/web".format(addr, base_port),
"port=SAME&username=Administrator&password=asdasd").read()
for i in range(1, nodes):
port = base_port + i
services = deploy.get("n" + str(i), [])
if not services: services = ["kv"]
print "Connecting node {0} with services {1}".format(i, str(services))
o.open("http://{0}:{1}/node/controller/doJoinCluster".format(addr, port),
"user=Administrator&password=asdasd&" +
"clusterMemberHostIp={0}".format(addr) +
"&clusterMemberPort={0}".format(base_port) +
"&services={0}".format(",".join(services))).read()
if do_rebalance:
print "Getting node list"
info = json.loads(o.open("http://{0}:{1}/nodeStatuses".format(addr, base_port)).read())
print "Servers added, triggering rebalance."
o.open("http://{0}:{1}/controller/rebalance".format(addr, base_port),
urllib.urlencode(
{'knownNodes': ",".join([info[k]['otpNode'] for k in info]),
'ejectedNodes': ''})).read()
def bool_request_value(Value):
return "1" if Value else "0"
if __name__ == '__main__':
main()