-
Notifications
You must be signed in to change notification settings - Fork 28
/
cluster_run
executable file
·434 lines (366 loc) · 15.4 KB
/
cluster_run
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python
#
# @author Couchbase <[email protected]>
# @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, os.path, subprocess, sys, signal, atexit, socket, getopt, select, shlex
import platform, fnmatch
base_direct_port = 12000
base_api_port = 9000
base_couch_port = 9500
base_projector_port = 10000
base_xdcr_port = 13000
base_indexer_port = 9100
base_fts_port = 9200
base_eventing_port = 9300
base_cbas_port = 9600
LOGLEVELS = ["debug", "info", "warn", "error", "critical"]
def read_configuration():
with open("build/cluster_run.configuration") as f:
def fn(line):
k, v = line.strip().split('=')
return k, shlex.split(v)[0]
return dict(fn(line) for line in f.readlines())
config = read_configuration()
PREFIX = config['prefix']
def setup_path():
def ebin_search(path):
dirs = os.walk(path)
ebins = []
for d, _, _ in dirs:
if os.path.basename(d) == "ebin":
ebins.append(d)
return ebins
path = ebin_search(".")
couchpath = ebin_search("{0}/lib/couchdb/erlang/lib".format(PREFIX))
couch_plugins = ebin_search("{0}/lib/couchdb/plugins".format(PREFIX))
if len(couchpath) == 0:
sys.exit("Couch libs wasn't found.\nCan't handle it")
return couchpath + path + couch_plugins
def mk_node_couch_config(i):
try:
os.mkdir("couch")
except os.error:
pass
with open("couch/n_{0}_conf.ini".format(i), "w") as f:
f.write("[httpd]\n")
f.write("port={0}\n".format(base_couch_port + i))
f.write("[couchdb]\n")
f.write("database_dir={0}/data/n_{1}/data\n".format(os.getcwd(), i))
f.write("view_index_dir={0}/data/n_{1}/data\n".format(os.getcwd(), i))
f.write("max_dbs_open=10000\n")
f.write("[upr]\n")
f.write("port={0}\n".format(base_direct_port + i * 2))
f.write("[dcp]\n")
f.write("port={0}\n".format(base_direct_port + i * 2))
def couch_configs(i):
mk_node_couch_config(i)
return ["{0}/etc/couchdb/default.ini".format(PREFIX),
"{0}/etc/couchdb/default.d/capi.ini".format(PREFIX),
"{0}/etc/couchdb/default.d/geocouch.ini".format(PREFIX),
"couch/n_{0}_conf.ini".format(i)]
def os_specific(args, params):
"""Add os-specific junk to the cluster startup."""
if platform.system() == 'Windows':
args += ["dont_suppress_stderr_logger", "false"]
else:
args += ["dont_suppress_stderr_logger", "true"]
if platform.system() == 'Darwin':
import resource
## OS X has a pretty tiny default fd limit. Let's increase it (if it hasn't already been).
(soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
if soft < 2048:
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, 2048))
params['env'] = {"ERL_MAX_PORTS": "2048"}
params['env'].update(os.environ)
ebin_path = None
cluster_extra_args = None
cluster_args_prefix = None
def is_ipv6_setup():
return os.getenv("IPV6", "false") == "true"
def prepare_start_cluster(extra_args, args_prefix):
global cluster_args_prefix
global cluster_extra_args
global ebin_path
ebin_path = setup_path()
cluster_extra_args = extra_args
cluster_args_prefix = args_prefix
def quote_string_for_erl(s):
return '"' + s.replace("\\", "\\\\").replace("\"", "\\\"") + '"'
def erlang_args_for_node(i):
logdir = "logs/n_{0}".format(i)
args = cluster_args_prefix + ["erl", "+MMmcs" "30",
"+A", "16", "+sbtu",
"+P", "327680", "-pa"] + ebin_path
args += [
"-setcookie", "nocookie",
"-kernel", "inet_dist_listen_min", "21100",
"inet_dist_listen_max", "21199",
"error_logger", "false",
"-sasl", "sasl_error_logger", "false",
"-couch_ini"] + couch_configs(i)
datadir = os.path.abspath('data/n_{0}'.format(i))
tempdir = os.path.abspath('tmp/')
nodefile = os.path.join(datadir, "nodefile")
babysitternodefile = os.path.join(datadir, "couchbase-server.babysitter.node")
babysittercookiefile = os.path.join(datadir, "couchbase-server.babysitter.cookie")
args += [
"-name", "babysitter_of_n_{0}@::1".format(i) if ipv6 else "babysitter_of_n_{0}@127.0.0.1".format(i),
"-proto_dist", "inet6_tcp" if ipv6 else "inet_tcp",
"-hidden",
"-kernel", "global_enable_tracing", "true",
"-ns_babysitter", "cookiefile", quote_string_for_erl(babysittercookiefile),
"-ns_babysitter", "nodefile", quote_string_for_erl(babysitternodefile),
"-ns_server", "config_path", '"etc/static_config.in"',
"-ns_server", "ipv6", "true" if ipv6 else "false",
"error_logger_mf_dir", quote_string_for_erl(logdir),
"path_config_etcdir", '"priv"',
"path_config_bindir", quote_string_for_erl(PREFIX+"/bin"),
"path_config_libdir", quote_string_for_erl(PREFIX+"/lib"),
"path_config_datadir", quote_string_for_erl(datadir),
"path_config_tmpdir", quote_string_for_erl(tempdir),
"path_config_secdir", quote_string_for_erl(PREFIX+"/etc/security"),
"path_audit_log", quote_string_for_erl(logdir),
"rest_port", str(base_api_port + i),
"query_port", str(base_couch_port - 1 - i),
"ssl_query_port", str(10000 + base_couch_port - 1 - i),
"projector_port", str(base_projector_port + i),
"ssl_rest_port", str(10000 + base_api_port + i),
"capi_port", str(base_couch_port + i),
"ssl_capi_port", str(10000 + base_couch_port + i),
"memcached_port", str(base_direct_port + i * 2),
"moxi_port", str(base_direct_port + i * 2 + 1),
"memcached_dedicated_port", str(base_direct_port - 1 - i * 4),
"memcached_ssl_port", str(base_direct_port - 4 - i * 4),
"nodefile", quote_string_for_erl(nodefile),
"short_name", quote_string_for_erl('n_{0}'.format(i)),
"xdcr_rest_port", str(base_xdcr_port + i),
"indexer_admin_port", str(base_indexer_port + i * 6),
"indexer_scan_port", str(base_indexer_port + i * 6 + 1),
"indexer_http_port", str(base_indexer_port + i * 6 + 2),
"indexer_https_port", str(10000 + base_indexer_port + i * 6 + 2),
"indexer_stinit_port", str(base_indexer_port + i * 6 + 3),
"indexer_stcatchup_port", str(base_indexer_port + i * 6 + 4),
"indexer_stmaint_port", str(base_indexer_port + i * 6 + 5),
"fts_http_port", str(base_fts_port + i),
"fts_ssl_port", str(10000 + base_fts_port + i),
"eventing_http_port", str(base_eventing_port + i),
"eventing_https_port", str(10000 + base_eventing_port + i),
"cbas_http_port", str(base_cbas_port + i * 15),
"cbas_cc_http_port", str(base_cbas_port + i * 15 + 1),
"cbas_cc_cluster_port", str(base_cbas_port + i * 15 + 2),
"cbas_cc_client_port", str(base_cbas_port + i * 15 + 3),
"cbas_hyracks_console_port", str(base_cbas_port + i * 15 + 4),
"cbas_cluster_port", str(base_cbas_port + i * 15 + 5),
"cbas_data_port", str(base_cbas_port + i * 15 + 6),
"cbas_result_port", str(base_cbas_port + i * 15 + 7),
"cbas_messaging_port", str(base_cbas_port + i * 15 + 8),
"cbas_debug_port", str(base_cbas_port + i * 15 + 9),
"cbas_auth_port", str(base_cbas_port + i * 15 + 10),
"cbas_admin_port", str(base_cbas_port + i * 15 + 11),
"cbas_replication_port", str(base_cbas_port + i * 15 + 12),
"cbas_metadata_port", str(base_cbas_port + i * 15 + 13),
"cbas_metadata_callback_port", str(base_cbas_port + i * 15 + 14),
"cbas_ssl_port", str(10000 + base_cbas_port + i)
] + cluster_extra_args
return args
def start_cluster(num_nodes, start_index, host, extra_args, args_prefix):
prepare_start_cluster(extra_args, args_prefix)
def start_node(i):
logdir = "logs/n_{0}".format(i)
try:
os.makedirs(logdir)
except:
pass
args = erlang_args_for_node(i)
params = {}
os_specific(args, params)
if not params.has_key('env'):
params['env'] = {}
params['env'].update(os.environ)
path = params['env']['PATH']
path = (PREFIX+"/bin") + os.pathsep + path
if not params['env'].has_key('ERL_FULLSWEEP_AFTER'):
params['env']['ERL_FULLSWEEP_AFTER'] = '512'
params['env']['PATH'] = path
crash_dump_base = 'erl_crash.dump.n_%d' % i
params['env']['ERL_CRASH_DUMP_BASE'] = crash_dump_base
params['env']['ERL_CRASH_DUMP'] = crash_dump_base + '.babysitter'
params['env']['COUCHBASE_SMALLER_PKEYS'] = '1'
params['close_fds'] = True
if platform.system() == "Windows":
params['close_fds'] = False
w = None
if "-noinput" in args:
(r,w) = os.pipe()
params['stdin'] = r
if 'setpgrp' in os.__dict__ and params.get('close_fds'):
# this puts child out of our process group. So that
# Ctrl-C doesn't deliver SIGINT to it, leaving us
# ability to it shutdown carefully or otherwise
params['preexec_fn'] = os.setpgrp
pr = subprocess.Popen(args, **params)
if w != None:
os.close(r)
pr.write_side = w
return pr
return [start_node(i + start_index) for i in xrange(num_nodes)]
def usage():
sys.exit("Usage: {0} [--nodes=N] [--dont-rename] [--dont-start] "
"[--interactive] [--static-cookie] [--start-index=N] "
"[--static-cookie] [--host=H] [--loglevel=L] "
"[--pluggable-config=File] [--minified] [--disable-autocomplete]"
"[ns_server args]".format(sys.argv[0]))
def find_primary_addr():
Family = socket.AF_INET6 if ipv6 else socket.AF_INET
DnsAddr = "2001:4860:4860::8844" if ipv6 else "8.8.8.8"
s = socket.socket(Family, socket.SOCK_DGRAM)
try:
s.connect((DnsAddr, 53))
if ipv6:
addr, port, _, _ = s.getsockname()
else:
addr, port = s.getsockname()
return addr
except socket.error:
return None
finally:
s.close()
def main():
try:
optlist, args = getopt.gnu_getopt(sys.argv[1:], "hn:i",
["help", "start-index=", "nodes=",
"dont-rename", "interactive",
"static-cookie", "dont-start",
"host=", "loglevel=",
"prepend-extras", "pluggable-config=",
"minified", "disable-autocomplete",
"pretend-version="])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
global ipv6
ipv6 = is_ipv6_setup()
dont_rename = False
dont_start = False
static_cookie = False
start_index = 0
num_nodes = 1
prepend_extras = False
host = "127.0.0.1"
loglevel = 'debug'
pluggable_config = []
use_minified = False
disable_autocomplete = "{disable_autocomplete,false}"
pretend_version = None
for o, a in optlist:
if o in ("--nodes", "-n"):
num_nodes = int(a)
elif o == '--dont-start':
dont_start = True
elif o == '--host':
host = a
elif o == '--start-index':
start_index = int(a)
elif o == '--dont-rename':
dont_rename = True
elif o in ("--help", "-h"):
usage()
exit(0)
elif o in("--static-cookie"):
static_cookie = True
elif o == '--loglevel':
loglevel = a
elif o == "--prepend-extras":
prepend_extras = True
elif o == "--pluggable-config":
pluggable_config.append(a)
elif o == "--minified":
use_minified = True
elif o == "--disable-autocomplete":
disable_autocomplete = "{disable_autocomplete,true}"
elif o == "--pretend-version":
pretend_version = a
else:
assert False, "unhandled options"
nodes = []
terminal_attrs = None
def kill_nodes(*args):
for n in nodes:
if n.write_side != None:
print("Closing %d\n" % n.write_side)
# os.write(n.write_side, "shutdown\n") # this line does graceful shutdown versus quick
os.close(n.write_side)
else:
try:
n.kill()
except OSError:
pass
for n in nodes:
n.wait()
if terminal_attrs != None:
termios.tcsetattr(sys.stdin, termios.TCSANOW, terminal_attrs)
atexit.register(kill_nodes)
try:
import termios
terminal_attrs = termios.tcgetattr(sys.stdin)
except:
pass
extra_args = []
if not dont_rename:
primary_addr = find_primary_addr()
if primary_addr == None:
print("was unable to detect 'internet' address of this machine."
+ " node rename will be disabled")
else:
extra_args += ["rename_ip", '"' + primary_addr + '"']
extra_args += args[1:]
if prepend_extras:
prepend_args = args[0:]
else:
prepend_args = []
extra_args += args[0:]
if static_cookie:
extra_args += ["-ns_server", "dont_reset_cookie", "true"]
if dont_start:
extra_args += ["-run", "t", "fake_loggers"]
else:
extra_args += ["-noinput"]
extra_args += ["-run", "child_erlang", "child_start", "ns_babysitter_bootstrap"]
extra_args += ["-ns_babysitter", "handle_ctrl_c", "true"]
if loglevel not in LOGLEVELS:
print "Valid log levels are the following: %s" % ', '.join(LOGLEVELS)
sys.exit(1)
extra_args += ["-ns_server", "loglevel_stderr", loglevel]
plugins_dir = '../build/cluster_run_ui_plugins'
if os.path.isdir(plugins_dir):
for file in os.listdir(plugins_dir):
if fnmatch.fnmatch(file, 'pluggable-ui-*.cluster_run.json'):
pluggable_config.append(os.path.join(plugins_dir, file))
if pluggable_config:
extra_args += ["-ns_server", "ui_plugins",
quote_string_for_erl(','.join(pluggable_config))]
ui_env = [disable_autocomplete]
extra_args += ["-ns_server", "use_minified", "true" if use_minified else "false"]
extra_args += ["-ns_server", "ui_env", '[' + ','.join(ui_env) + ']']
if pretend_version is not None:
extra_args += ["-ns_server",
"pretend_version", '"{}"'.format(pretend_version)]
nodes = start_cluster(num_nodes, start_index, host, extra_args, prepend_args)
for node in nodes:
node.wait()
if __name__ == '__main__':
main()