Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 211 lines (176 sloc) 6.844 kb
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
1 #!/usr/bin/env python
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
2 import os, subprocess, sys, signal, termios, atexit, socket, getopt
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
3
d2f1015 @alk make sure erlang VMs get SIGHUP when cluster_run script dies
alk authored
4 def testament_child_death():
5 rv = os.fork()
6 if rv == 0:
7 os.kill(os.getpid(), signal.SIGSTOP)
8 os._exit(0)
9 os.waitpid(rv, os.WUNTRACED)
10
0963a0f @alk implemented make install with new directory layout
alk authored
11 PREFIX = subprocess.Popen(". $(pwd)/.configuration && echo $prefix", shell = True, stdout=subprocess.PIPE).communicate()[0].rstrip()
26cb6e5 @alk use couchdb symlink in lib
alk authored
12
3212a5d @alk enabled dev-mode clusters backed by couch
alk authored
13 base_direct_port = 12000
14 base_api_port = 9000
15 base_couch_port = 9500
16 base_mccouch_port = 13000
17
18
b92ce05 @alk don't fail if couch is not present
alk authored
19 def setup_path():
20 def ebin_search(path):
21 return subprocess.Popen(["find", path, "-name", "ebin", "-type", "d"],
22 stdout=subprocess.PIPE).communicate()[0].split()
23
24 path = ebin_search(".")
0963a0f @alk implemented make install with new directory layout
alk authored
25 couchpath = ebin_search("{0}/lib/couchdb/erlang/lib".format(PREFIX))
3212a5d @alk enabled dev-mode clusters backed by couch
alk authored
26 couch_plugins = ebin_search("{0}/lib/couchdb/plugins".format(PREFIX))
b92ce05 @alk don't fail if couch is not present
alk authored
27
28 if len(couchpath) == 0:
58b321f option to stop ns_server from starting
Dale Harvey authored
29 print("Couch libs wasn't found. Will disable couch support\n")
30 os.environ['DONT_START_COUCH'] = '1'
b92ce05 @alk don't fail if couch is not present
alk authored
31
3212a5d @alk enabled dev-mode clusters backed by couch
alk authored
32 return couchpath + path + couch_plugins
33
34 def mk_node_couch_config(i):
35 try:
36 os.mkdir("couch")
37 except os.error:
38 pass
39
40 with open("couch/n_{0}_conf.ini".format(i), "w") as f:
41 f.write("[httpd]\n")
42 f.write("port={0}\n".format(base_couch_port + i))
43 f.write("[couchdb]\n")
44 f.write("database_dir={0}/couch/{1}\n".format(os.getcwd(), i))
45 f.write("view_index_dir={0}/couch/{1}\n".format(os.getcwd(), i))
46 f.write("max_dbs_open=10000\n")
47 f.write("[log]\n")
48 f.write("file={0}/couch/{1}.log\n".format(os.getcwd(), i))
49 f.write("[daemons]\n")
50 f.write("mc_daemon={{mc_sup, start_link, [{0}]}}\n".format(base_mccouch_port+i))
b92ce05 @alk don't fail if couch is not present
alk authored
51
3212a5d @alk enabled dev-mode clusters backed by couch
alk authored
52 def couch_configs(i):
53 mk_node_couch_config(i)
54 return ["{0}/etc/couchdb/default.ini".format(PREFIX),
3a8884b @alk added initial CAPI db-frontend implementation
alk authored
55 "{0}/etc/couchdb/default.d/capi.ini".format(PREFIX),
3212a5d @alk enabled dev-mode clusters backed by couch
alk authored
56 "{0}/etc/couchdb/local.d/geocouch.ini".format(PREFIX),
57 "couch/n_{0}_conf.ini".format(i)]
b92ce05 @alk don't fail if couch is not present
alk authored
58
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
59 def start_cluster(num_nodes, start_index, host, extra_args):
b92ce05 @alk don't fail if couch is not present
alk authored
60 ebin_path = setup_path()
61
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
62 def start_node(i):
63 logdir = "logs/n_{0}".format(i)
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
64 try:
65 os.makedirs(logdir)
66 except:
67 pass
0963a0f @alk implemented make install with new directory layout
alk authored
68 try:
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
69 os.stat("data/n_{0}/mnesia".format(i))
0963a0f @alk implemented make install with new directory layout
alk authored
70 except:
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
71 os.makedirs("data/n_{0}/mnesia".format(i))
72 args = ["erl", "+A", "16", "-pa"] + ebin_path + [
de9fb03 @seanlynch Support forced renaming with cluster_run
seanlynch authored
73 "-setcookie", "nocookie",
74 "-kernel", "inet_dist_listen_min", "21100",
75 "inet_dist_listen_max", "21199",
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
76 "-couch_ini"] + couch_configs(i) + [
0963a0f @alk implemented make install with new directory layout
alk authored
77 "-ns_server", "config_path", '"etc/static_config.in"',
78 "error_logger_mf_dir", '"{0}"'.format(logdir),
de9fb03 @seanlynch Support forced renaming with cluster_run
seanlynch authored
79 "error_logger_mf_maxbytes", "10485760",
80 "error_logger_mf_maxfiles", "10",
ac839ad @alk don't disable stderr logging for cluster_run
alk authored
81 "dont_suppress_stderr_logger", "true",
0963a0f @alk implemented make install with new directory layout
alk authored
82 "path_config_etcdir", '"priv"',
83 "path_config_bindir", '"{0}"'.format(PREFIX+"/bin"),
84 "path_config_libdir", '"{0}"'.format(PREFIX+"/lib"),
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
85 "path_config_datadir", '"data/n_{0}"'.format(i),
0963a0f @alk implemented make install with new directory layout
alk authored
86 "path_config_tmpdir", '"tmp/"',
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
87 "rest_port", str(base_api_port + i),
88 "memcached_port", str(base_direct_port + i * 2),
89 "mccouch_port", str(base_mccouch_port + i),
90 "moxi_port", str(base_direct_port + i * 2 + 1),
91 "short_name", '"n_{0}"'.format(i)
92 ] + extra_args
93 if i == 0:
94 return subprocess.Popen(args)
ce519e6 @seanlynch Don't duplicate args
seanlynch authored
95 else:
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
96 return subprocess.Popen(args + ["-noshell"], stdin=subprocess.PIPE)
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
97
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
98 return [start_node(i + start_index) for i in xrange(num_nodes)]
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
99
100 def usage():
6aec6fc Disable interactive shell by default, enable with -i
Dale Harvey authored
101 sys.exit("Usage: {0} [--nodes=N] [--dont-rename] [--dont-start] [--interactive] [--static-cookie] [ns_server args]".format(sys.argv[0]))
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
102
95d03cf @alk [cluster_run] spawn epmd if it's not running
alk authored
103 def maybe_spawn_epmd():
104 try:
105 socket.create_connection(("127.0.0.1", 4369)).close()
106 except socket.error:
107 print("Spawning epmd...\n")
108 subprocess.Popen("erl -noshell -setcookie nocookie -sname init -run init stop 2>&1 > /dev/null",
109 shell = True).communicate()
110
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
111 def find_primary_addr():
112 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
113 try:
114 s.connect(("8.8.8.8", 56))
115 addr, port = s.getsockname()
116 return addr
117 except socket.error:
118 return None
119 finally:
120 s.close()
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
121
122 def main():
123 try:
4ede441 @alk Do rename dev-mode nodes.
alk authored
124 optlist, args = getopt.gnu_getopt(sys.argv[1:], "hn:i", ["help", "start-index=", "nodes=", "dont-rename", "interactive", "static-cookie", "dont-start", "host="])
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
125 except getopt.GetoptError, err:
126 # print help information and exit:
127 print str(err) # will print something like "option -a not recognized"
128 usage()
129 sys.exit(2)
130
4ede441 @alk Do rename dev-mode nodes.
alk authored
131 dont_rename = False
58b321f option to stop ns_server from starting
Dale Harvey authored
132 dont_start = False
9996ab9 add an option to not rename cookies
Dale Harvey authored
133 static_cookie = False
6aec6fc Disable interactive shell by default, enable with -i
Dale Harvey authored
134 interactive_shell = False
e6a9f27 allow independent cluster_runs in parallel
Dale Harvey authored
135 start_index = 0
136 num_nodes = 1
4ede441 @alk Do rename dev-mode nodes.
alk authored
137 host = "127.0.0.1"
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
138
139 for o, a in optlist:
e6a9f27 allow independent cluster_runs in parallel
Dale Harvey authored
140 if o in ("--nodes", "-n"):
141 num_nodes = int(a)
6aec6fc Disable interactive shell by default, enable with -i
Dale Harvey authored
142 elif o in ("--interactive", "-i"):
143 interactive_shell = True
e6a9f27 allow independent cluster_runs in parallel
Dale Harvey authored
144 elif o == '--dont-start':
58b321f option to stop ns_server from starting
Dale Harvey authored
145 dont_start = True
e6a9f27 allow independent cluster_runs in parallel
Dale Harvey authored
146 elif o == '--host':
147 host = a
148 elif o == '--start-index':
149 start_index = int(a)
4ede441 @alk Do rename dev-mode nodes.
alk authored
150 elif o == '--dont-rename':
151 dont_rename = True
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
152 elif o in ("--help", "-h"):
153 usage()
154 exit(0)
9996ab9 add an option to not rename cookies
Dale Harvey authored
155 elif o in("--static-cookie"):
156 static_cookie = True
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
157 else:
158 assert False, "unhandled options"
159
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
160 nodes = []
dbb3506 @alk [cluster_run] restore terminal attrs in atexit handler
alk authored
161 terminal_attrs = None
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
162
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
163 def kill_nodes(*args):
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
164 for n in nodes:
165 try:
166 n.kill()
ede590c @alk [cluster_run] wait childs death
alk authored
167 n.wait()
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
168 except OSError:
169 pass
dbb3506 @alk [cluster_run] restore terminal attrs in atexit handler
alk authored
170 if terminal_attrs != None:
171 termios.tcsetattr(sys.stdin, termios.TCSANOW, terminal_attrs)
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
172
eacc3de @alk Revert "Shutdown ns_server gracefully from within cluster_run."
alk authored
173 atexit.register(kill_nodes)
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
174
d2f1015 @alk make sure erlang VMs get SIGHUP when cluster_run script dies
alk authored
175 testament_child_death()
176
8b2ea0a @alk [cluster_run] don't screw up when stdin is not terminal
alk authored
177 try:
178 terminal_attrs = termios.tcgetattr(sys.stdin)
179 except:
180 pass
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
181
95d03cf @alk [cluster_run] spawn epmd if it's not running
alk authored
182 maybe_spawn_epmd()
183
4ede441 @alk Do rename dev-mode nodes.
alk authored
184 extra_args = []
185 if not dont_rename:
186 primary_addr = find_primary_addr()
187 if primary_addr == None:
188 print("was unable to detect 'internet' address of this machine."
189 + " node rename will be disabled")
190 else:
191 extra_args += ["rename_ip", '"' + primary_addr + '"']
192 extra_args += args[1:]
c0612ad @alk [cluster_run] always rename cluster nodes by default
alk authored
193
9996ab9 add an option to not rename cookies
Dale Harvey authored
194 if static_cookie:
195 extra_args += ["-ns_server", "dont_reset_cookie", "true"]
196
58b321f option to stop ns_server from starting
Dale Harvey authored
197 if not dont_start:
198 extra_args += ["-run", "ns_bootstrap"]
199
6aec6fc Disable interactive shell by default, enable with -i
Dale Harvey authored
200 if not interactive_shell:
201 extra_args += ["-noinput"]
202
e6a9f27 allow independent cluster_runs in parallel
Dale Harvey authored
203 nodes = start_cluster(num_nodes, start_index, host, extra_args)
2f7837d @alk [cluster_run] always kill erlangs and start shell on first
alk authored
204
5cf2edd @seanlynch Replace gen_cluster_scripts.rb with cluster_run.
seanlynch authored
205 for node in nodes:
206 node.wait()
207
208
209 if __name__ == '__main__':
210 main()
Something went wrong with that request. Please try again.