-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.py
243 lines (202 loc) · 6.96 KB
/
start.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
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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import subprocess
HOSTNAME = "127.0.0.1"
PORT = "2345"
USERNAME = "root"
PASSWORD = "123qwe"
SERVERID = ""
COOKIE = "server"
erl = "erl "
def get_deps():
'''
get all deps
'''
command = 'rebar get-deps'
os.system( command )
def get_proto_files_list():
files = os.listdir("proto")
def f(x):
s = x.split('.')
if len(s) == 1:
return False
else:
filename, extension = s
if extension == "proto":
return True
else:
return False
return filter(f, files)
def generate_pb_list():
files = get_proto_files_list()
r = ""
for file in files:
file = file.split('.')
name,extension = file
pb_name = (name+"_pb")
load_pb = "code:load_file("+pb_name+")"
r += ( name + "," +load_pb+"," )
return r
pb_list = generate_pb_list()
def get_dep_ebin_dirs():
'''
get all deps' ebin directories
'''
ebinList = [ 'ebin/' ]
ebinStringPipe = os.popen( 'find deps -type d | grep ebin' )
for line in ebinStringPipe:
ebinList.append( line.strip('\n') )
dependencyDirectory = ' '
for e in ebinList:
dependencyDirectory += (' '+ e)
return dependencyDirectory
dep_ebin_dirs = get_dep_ebin_dirs()
def get_apps_ebin_dirs():
'''
get all apps' ebin directories
'''
ebinList = [ 'ebin/' ]
ebinStringPipe = os.popen( 'find apps -type d | grep ebin' )
for line in ebinStringPipe:
ebinList.append( line.strip('\n') )
dependencyDirectory = ' '
for e in ebinList:
dependencyDirectory += (' '+ e)
return dependencyDirectory
apps_ebin_dirs = get_apps_ebin_dirs()
def build_src_files():
r = ""
for root, dirs, files in os.walk("src", True):
for name in files:
filename, extension = name.split('.')
if extension == "erl":
r += (" " + os.path.join(root, name))
return r
def build():
'''
run "rabar compile" directly
'''
command = 'rebar compile'
os.system( command )
def proto():
'''
proceed .proto files to generate _pb.erl and _pb.hrl files
'''
command = "erlc -I src/tools/ -o ebin/ src/tools/proto.erl"
subprocess.call(command, shell=True)
command = erl + ' -pa ' + dep_ebin_dirs + ' -eval \'proto:compile_all()\''
subprocess.call(command, shell=True)
def rebuild():
'''the first time run "rebar compile" will get some error like "cannot find files", just pass it'''
command = ''' rm -fr ebin && mkdir ebin '''
os.system( command )
get_deps()
subprocess.call('rebar compile', shell=True)
'''proto() needs module(protobuffs) that generated by "rebar compile"'''
proto()
'''after run proto(), it will generate some *_pb.hrl and *_pb.erl files, run "rebar compile" again'''
subprocess.call('rebar compile', shell=True)
'''build lib/ proto/ test/ tools/'''
src_list = build_src_files()
command = "erlc -o ebin/ " + src_list
os.system( command )
def get_db_game_sql():
'''
load src/tools/db_game.sql
'''
f = open('src/tools/db_game.sql')
sql_text = ''
try:
sql_text = f.read()
finally:
f.close()
return sql_text
db_sql = get_db_game_sql()
def reset_db():
command = 'mysql -u' + USERNAME + ' -h' + HOSTNAME + ' -p' + PORT + ' -p123qwe -e '
command += '"'
command += db_sql
command += '"'
os.system( command )
print('reset success!')
def start_game(detach=False):
nodeName = SERVERID + 'game' + '@' + HOSTNAME
command1 = erl + '-setcookie ' + COOKIE
if detach == True:
command1 += " -detached"
command2 = " -s lager" +" -pa " + dep_ebin_dirs + " " + apps_ebin_dirs + " -name " + nodeName
command3 = " -port " + PORT + " -eval \'" + pb_list+ "application:ensure_all_started(game).\'"
command = command1 + command2 + command3
os.system(command)
def start_db(detach=False):
nodeName = SERVERID + 'db_session' + '@' + HOSTNAME
command1 = erl + '-setcookie ' + COOKIE
if detach == True:
command1 += " -detached"
command2 = " -s lager" + " -pa " + dep_ebin_dirs + " " + apps_ebin_dirs + " -name " + nodeName
command3 = " -port" + PORT + " -eval \'" + pb_list+ "application:ensure_all_started(db_session).\'"
command = command1 + command2 + command3
os.system(command)
def start_client():
'''
start client
'''
#nodeName = 'client' + '@' + HOSTNAME
command1 = erl + '-setcookie ' + COOKIE + " -s lager" + " -pa " + dep_ebin_dirs + " " + apps_ebin_dirs
command2 = " -port " + PORT + " -host " + HOSTNAME + " -eval \'" + pb_list+ " application:ensure_all_started(client).\' "
command = command1+command2
os.system(command)
def start():
start_db(True)
start_game(True)
def start_release():
command1 = "apps/db_session/rel/db_session/bin/db_session start" + " -port " + PORT + " -host " + HOSTNAME
command2 = "apps/game/rel/game/bin/game start" + " -port " + PORT + " -host " + HOSTNAME
os.system(command1)
os.system(command2)
def stop_release():
command1 = "apps/db_session/rel/db_session/bin/db_session stop"
command2 = "apps/game/rel/game/bin/game stop"
os.system(command1)
os.system(command2)
def attach_rel_game():
os.system("apps/game/rel/game/bin/game attach")
def attach_rel_db():
os.system("apps/db_session/rel/db_session/bin/db_session attach")
def stop():
command = "ps aux|grep db_session|awk '{print $2}'|xargs kill -9"
os.system(command)
command = "ps aux|grep game|awk '{print $2}'|xargs kill -9"
os.system(command)
def generate():
subprocess.call('rebar compile generate', shell=True)
def clean():
os.system("rebar clean")
def help():
helpMessage = '''
Execute this script to generate protocol files,
build, and start the erlang game server.
Usage:
./start build #get all deps and build all project files
./start rebuild #get all deps and generate *_pb.hrl files, then build all project files
./start reset_db #reset mysql and import init sql files with src/tools/db_game.sql
./start start #start this game server
./stop kill all node process
'''
print(helpMessage)
if __name__ == '__main__':
commandFunMapper = { 'build':build, 'start':start, 'start_release':start_release, 'stop':stop, 'generate':generate,
'start_client':start_client, 'start_game':start_game, 'start_db':start_db, 'reset_db':reset_db,
'rebuild':rebuild, 'proto':proto, 'stop_release':stop_release, 'clean':clean, 'attach_rel_game':attach_rel_game,
'attach_rel_db':attach_rel_db, 'help':help }
try:
command = sys.argv[1]
commandFunMapper[command]()
except IndexError:
commandFunMapper['help']()
exit(1)
except KeyError:
commandFunMapper['help']()
exit(1)