@@ -21,53 +21,57 @@ from docopt import docopt
2121from subprocess import call
2222from subprocess import Popen , PIPE
2323import os
24+ import subprocess
2425import sys
2526import getpass
27+ import time
2628
2729
28- def createNodeCommands (clustername , role , index = None , usessl = False , mx = False ):
29- cs = []
30+ def run (command , * args , ** kwargs ):
31+ print (command )
32+ result = subprocess .run (command , * args , check = True , shell = True , ** kwargs )
33+ print ()
34+ return result
35+
3036
37+ def createNodeCommands (clustername , role , index = None , usessl = False , mx = False ):
3138 nodename = role
3239 if index != None :
3340 nodename += "%d" % index
3441
3542 dir = "%s/%s" % (clustername , nodename )
36- cs . append ("initdb -D %s" % dir )
37- cs . append ("echo \" shared_preload_libraries = 'citus'\" >> %s/postgresql.conf" % dir )
38- cs . append ('echo "wal_level = logical" >> %s/postgresql.conf' % dir )
43+ run ("initdb -D %s" % dir )
44+ run ("echo \" shared_preload_libraries = 'citus,pg_stat_statements '\" >> %s/postgresql.conf" % dir )
45+ run ('echo "wal_level = logical" >> %s/postgresql.conf' % dir )
3946
4047 if usessl :
41- cs . append ('echo "ssl = on" >> %s/postgresql.conf' % dir )
42- cs . append (
48+ run ('echo "ssl = on" >> %s/postgresql.conf' % dir )
49+ run (
4350 "echo \" citus.node_conninfo = 'sslmode=require'\" >> %s/postgresql.conf"
4451 % dir
4552 )
46- cs . append (
53+ run (
4754 "openssl req -new -x509 -days 365 -nodes -text -out %s/server.crt -keyout %s/server.key -subj '/CN=%s'"
4855 % (dir , dir , nodename )
4956 )
50- cs . append ("chmod 0600 %s/server.key" % dir )
57+ run ("chmod 0600 %s/server.key" % dir )
5158
5259 if mx :
53- cs . append (
60+ run (
5461 "echo \" citus.replication_model = 'streaming'\" >> %s/postgresql.conf" % dir
5562 )
5663
57- return cs
58-
5964
6065def main (arguments ):
6166 print (arguments )
6267 if arguments ["make" ]:
63- cs = []
6468 if arguments ['--destroy' ]:
6569 name = arguments ["<name>" ]
6670 for role in getRoles (name ):
67- cs . append ("pg_ctl stop -D %s/%s" % (name , role ))
68- cs . append ('rm -rf %s' % (name ))
71+ run ("pg_ctl stop -D %s/%s || true " % (name , role ))
72+ run ('rm -rf %s' % (name ))
6973
70- cs += createNodeCommands (
74+ createNodeCommands (
7175 arguments ["<name>" ],
7276 "coordinator" ,
7377 usessl = arguments ["--use-ssl" ],
@@ -77,7 +81,7 @@ def main(arguments):
7781 size = int (arguments ["--size" ])
7882
7983 for i in range (size ):
80- cs += createNodeCommands (
84+ createNodeCommands (
8185 arguments ["<name>" ],
8286 "worker" ,
8387 i ,
@@ -89,15 +93,17 @@ def main(arguments):
8993
9094 cport = port
9195 role = "coordinator"
92- cs . append (
96+ run (
9397 'pg_ctl -D %s/%s -o "-p %d" -l %s_logfile start'
9498 % (arguments ["<name>" ], role , cport , role )
9599 )
96100 port += 1
97101
102+ worker_ports = []
98103 for i in range (size ):
99104 role = "worker%d" % i
100- cs .append (
105+ worker_ports .append (port )
106+ run (
101107 'pg_ctl start -D %s/%s -o "-p %d" -l %s_logfile'
102108 % (arguments ["<name>" ], role , port , role )
103109 )
@@ -106,93 +112,73 @@ def main(arguments):
106112
107113 if getpass .getuser () != 'postgres' and not os .getenv ('PGDATABASE' ):
108114 for i in range (size + 1 ):
109- cs . append ('createdb -p %d' % (port + i ))
115+ run ('createdb -p %d' % (port + i ))
110116
111117 if not arguments ["--no-extension" ]:
112118 for i in range (size + 1 ):
113- cs . append ('psql -p %d -c "CREATE EXTENSION citus;"' % (port + i ))
119+ run ('psql -p %d -c "CREATE EXTENSION citus;"' % (port + i ))
114120
115121 # If the cluster size is 0 we add the coordinator as the only node, otherwise we will add all other nodes
116122 if size == 0 :
117- cs . append (
123+ run (
118124 "psql -p %d -c \" SELECT * from master_add_node('localhost', %d);\" "
119125 % (port , port )
120126 )
121127 else :
122128 for i in range (size ):
123- cs . append (
129+ run (
124130 "psql -p %d -c \" SELECT * from master_add_node('localhost', %d);\" "
125131 % (port , port + 1 + i )
126132 )
127133 if arguments ["--mx" ]:
128- cs . append (
134+ run (
129135 "psql -p %d -c \" SELECT start_metadata_sync_to_node('localhost', %d);\" "
130136 % (port , port + 1 + i )
131137 )
132138
133- cs . append (
139+ run (
134140 'psql -p %d -c "SELECT * from master_get_active_worker_nodes();"'
135141 % (port )
136142 )
137143 if arguments ['--init-with' ]:
138- cs .append ('psql -p %d -f %s' % (cport , arguments ['--init-with' ]))
139-
140- for c in cs :
141- print (c )
142- os .system (c )
143- print ("" )
144+ run ('psql -p %d -f %s -v ON_ERROR_STOP=1' % (cport , arguments ['--init-with' ]))
144145
145146 elif arguments ["stop" ]:
146- cs = []
147147 name = arguments ["<name>" ]
148148 for role in getRoles (name ):
149- cs . append ("pg_ctl stop -D %s/%s" % (name , role ))
149+ run ("pg_ctl stop -D %s/%s" % (name , role ))
150150
151- for c in cs :
152- print (c )
153- os .system (c )
154- print ("" )
155151
156152 elif arguments ["start" ]:
157- cs = []
158153 name = arguments ["<name>" ]
159154 port = int (arguments ["--port" ])
160155 cport = port
161156 for role in getRoles (name ):
162- cs . append (
157+ run (
163158 'pg_ctl start -D %s/%s -o "-p %d" -l %s_logfile'
164159 % (name , role , cport , role )
165160 )
166161 cport += 1
167162
168- for c in cs :
169- print (c )
170- os .system (c )
171- print ("" )
172163
173164 elif arguments ["restart" ]:
174- cs = []
175165 name = arguments ["<name>" ]
176166 port = int (arguments ["--port" ])
177167 if arguments ["--watch" ]:
178- cs . append (
168+ run (
179169 "fswatch -0 '%s' | xargs -0 -n 1 -I{} citus_dev restart %s --port=%d"
180170 % (citus_so (), name , port )
181171 )
182172
183173 else :
184174 cport = port
185175 for role in getRoles (name ):
186- cs . append (
176+ run (
187177 'pg_ctl restart -D %s/%s -o "-p %d" -l %s_logfile'
188178 % (name , role , cport , role )
189179 )
190180 cport += 1
191181
192- for c in cs :
193- print (c )
194- os .system (c )
195- print ("" )
196182
197183 else :
198184 print ("unknown command" )
0 commit comments