-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.py
233 lines (173 loc) · 7.45 KB
/
run.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
import threading
import argparse
from crow.events.event_manager import Publisher
from crow.dashboard_utils import is_wasm_file, is_c_file, is_bc_file
from crow.monitor.minpeewee.readdb import DBNAME
from crow.events import SPAWN_COMMAND, SPAWN_QUEUE
from crow.events import CROW_HEARTBEAT_QUEUE
from crow.events.event_manager import Subscriber,subscriber_function
from crow.utils import updatesettings
from crow.monitor import life_status, dashboard
from crow.storage import manager as storagemanager
from crow.monitor import logger
from crow.settings import config
# Entrypoints
from crow.entrypoints import wasm2wat, bc2wasm, bc2candidates, fromll, split, fromc
import sys
import os
import time
import traceback
@subscriber_function(event_type=SPAWN_COMMAND)
def spawnnewworker(data):
print(data)
if "workername" in data:
if "bc2wasm" in data['workername']:
print("Spawning a bc2wasm worker")
bc2wasm.main()
elif "wasm2wat" in data['workername']:
print("Spawning a converter worker")
wasm2wat.main()
elif "storage" in data['workername']:
print("Spawning a storage worker")
storagemanager.main()
elif "explorer" in data['workername']:
print("Spawning a storage worker")
bc2candidates.main()
elif "extractor" in data['workername']:
print("Spawning a extractor worker")
bc2candidates.main()
def launch_spawner():
subscriber = Subscriber("spawner", SPAWN_QUEUE, f"crow.spawn", config["event"].getint("port"), spawnnewworker)
subscriber.setup()
if __name__ == '__main__':
# read from arguments and read legacy way
rs = []
notrs = []
i = 0
while i <= len(sys.argv[1:]):
arg = sys.argv[i]
if arg.startswith("%"):
rs.append(arg)
rs.append(sys.argv[i + 1])
i += 2
else:
notrs.append(arg)
i += 1
print(rs, notrs)
sys.argv = notrs
parser = argparse.ArgumentParser(description='CROW cli tool.')
parser.add_argument('--redis-host', metavar='rh', type=str, default="127.0.0.1", help='Redis host address')
parser.add_argument('--redis-port', metavar='rp', type=str, default="1010", help='Redis host port')
parser.add_argument('--redis-db', metavar='rd', type=str, default="0", help='Redis db')
parser.add_argument('--redis-pass', metavar='rs', type=str, default=os.environ.get("REDIS_PASS", ""), help='Redis pass')
parser.add_argument('--event-host', metavar='eh', type=str, default="127.0.0.1", help='Broker host')
parser.add_argument('--event-port', metavar='ep', type=str, default="5672", help='Broker port')
parser.add_argument('--dashboard-host', metavar='dh', type=str, default="127.0.0.1", help='Dashboard host')
parser.add_argument('--dashboard-port', metavar='dp', type=str, default="5000", help='Dashboard port')
# Debug in threading gets an error :)
parser.add_argument('--dashboard-debug', dest="dashboard_debug", default=False, action=argparse.BooleanOptionalAction)
parser.add_argument('--dashboard', default=True, action=argparse.BooleanOptionalAction)
parser.add_argument('--is-spawner', metavar='sp', type=bool, default=False, help='Launch CROW as an spawner process')
# Entrypoints
parser.add_argument('--no-wasm2wat', metavar='now2w', type=int, default=1, help='Number of wasm 2 wat workers')
parser.add_argument('--no-bc2wasm', metavar='nobc2w', type=int, default=1, help='Number of bc 2 wasm')
parser.add_argument('--no-explorers', metavar='now2ex', type=int, default=1, help='Number of explorers')
parser.add_argument('--no-llparsers', metavar='now2ll', type=int, default=1, help='Number of ll parsers')
parser.add_argument('--no-splitworkers', metavar='nosplitters', type=int, default=1, help='Number of split workers')
parser.add_argument('--in-file', metavar='f', type=str, help='Input file')
args = parser.parse_args()
print(args)
updatesettings([
"%cache.redis-host",
args.redis_host,
"%cache.redis-port",
args.redis_port,
"%event.host",
args.event_host,
"%event.port",
args.event_port,
] + rs)
threads = []
# listener to spawn is not specified, then run a thread as a standalone CROW
if args.is_spawner:
launch_spawner()
else:
# Create a new one
if os.path.exists(DBNAME):
print("Removing database")
os.remove(DBNAME)
from crow.monitor.minpeewee.readdb import init # to create the db
init()
else:
from crow.monitor.minpeewee.readdb import init # to create the db
init()
time.sleep(10)
print("Launching life status")
lifestatus_thread = threading.Thread(target=life_status.main)
lifestatus_thread.start()
threads.append(lifestatus_thread)
print("Launching storage manager")
storage_thread = threading.Thread(target=storagemanager.main, )
storage_thread.start()
threads.append(storage_thread)
print("Launching logger")
logger_thread = threading.Thread(target=logger.main, )
logger_thread.start()
threads.append(logger_thread)
print("Launching workers wasm2wat")
for _ in range(args.no_wasm2wat):
worker = threading.Thread(target=wasm2wat.main, )
worker.start()
threads.append(worker)
print("Launching bc2wasm")
for _ in range(args.no_bc2wasm):
worker = threading.Thread(target=bc2wasm.main, )
worker.start()
threads.append(worker)
print("Launching explorers")
for _ in range(args.no_explorers):
worker = threading.Thread(target=bc2candidates.main, )
worker.start()
threads.append(worker)
print("Launching workers fromll ")
for _ in range(args.no_llparsers):
worker = threading.Thread(target=fromll.main, )
worker.start()
threads.append(worker)
print("Launching workers fromll ")
for _ in range(args.no_splitworkers):
worker = threading.Thread(target=split.main, )
worker.start()
threads.append(worker)
time.sleep(2)
if args.dashboard:
print("Launching dashboard monitor")
if not args.dashboard_debug:
dashboard_thread = threading.Thread(target=dashboard.main, args=(
args.redis_host,
args.redis_port,
args.redis_db,
args.redis_pass,
"out",
args.dashboard_host,
args.dashboard_port,
False
))
dashboard_thread.start()
threads.append(dashboard_thread)
if args.in_file:
stream = open(args.in_file, 'rb').read()
filename = args.in_file.split("/")[-1]
publisher = Publisher()
if is_c_file(stream):
fromc.c2ll(filename, stream)
print("Enqueued C file")
elif is_bc_file(stream):
# Call bc2wasm event
bc2wasm.bc2wasm(publisher, stream, filename, explore=True)
print("BC file enqueued for diversification")
else:
print("Invalid file type")
exit(1)
for th in threads:
th.join()