-
Notifications
You must be signed in to change notification settings - Fork 5
/
prb_post_request.py
339 lines (291 loc) · 10.5 KB
/
prb_post_request.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
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
class PrbPostRequest:
"""
"""
def __init__(self, ip_port):
"""
:param ip_port:
"""
self.url = f'http://{ip_port}/api/query_manager'
self.headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Content-Type": "application/json;charset=UTF-8",
"Connection": "keep-alive",
"Host": f"{ip_port}",
"Origin": f"http://{ip_port}",
"Referer": f"http://{ip_port}/workers",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
}
def get_resp_data(self, post_data):
"""
:param post_data:
:return:
"""
try:
resp = requests.post(self.url, data=json.dumps(post_data), headers=self.headers, timeout=120)
resp_data = resp.json()
resp.close()
return resp_data
except Exception as post_err:
return ['filed', post_err]
@staticmethod
def get_uuid(uuid_data):
"""
:param uuid_data:
:return:
"""
uuid_dict = {}
uuid_list = []
for data in uuid_data:
uuid_dict['uuid'] = data['uuid']
uuid_list.append(uuid_dict.copy())
return uuid_list
class PrbPostRequestForFetcher(PrbPostRequest):
"""
"""
def __init__(self, ip_port):
super().__init__(ip_port)
self.url = f'http://{ip_port}/api/query_fetcher'
self.headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Content-Type": "application/json;charset=UTF-8",
"Connection": "keep-alive",
"Host": f"{ip_port}",
"Origin": f"http://{ip_port}",
"Referer": f"http://{ip_port}/fetcher",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
}
class PrbDataMethod:
@staticmethod
def get_fetcher_data():
"""
请求prb所有fetcher信息
:return:
"""
return {"callOnlineFetcher": {}}
@staticmethod
def get_workers_pools_data():
"""
请求prb所有workers和pools信息
:return:
"""
return {"callOnlineLifecycleManager": {}}
# @staticmethod
# def get_workers_pools_data_with_uuid(uuid):
# return {'callOnlineLifecycleManager': {'requests': [{'id': {'uuid': f'{uuid}'}}]}}
@staticmethod
def get_request_start_worker_lifecycle(uuid):
"""
restart worker的lifecycle
:param uuid:
:return:
"""
return {
"requestStartWorkerLifecycle": {
"requests": [{"id": {"uuid": f"{uuid}"}}]
}
}
@staticmethod
def get_request_kick_worker(uuid):
"""
kill worker的pruntime,等于重启pruntime
:return:
"""
return {
"requestKickWorker": {"requests": [{"id": {"uuid": f"{uuid}"}}]}}
@staticmethod
def get_request_update_worker(uuid):
"""
update worker的相关信息
:param uuid:
:return:
"""
return {
"requestUpdateWorker": {
"items": [
{"id": {"uuid": f"{uuid}"},
"worker": {
"uuid": f"{uuid}",
"pid": "1472",
"name": "192.168.50.101",
"endpoint": "http://192.168.50.100:8000",
"enabled": True,
"deleted": False,
"stake": "10000000000000"
}
}
]
}
}
# @staticmethod
# def get_request_update_worker_delete(uuid):
# """Delete worker信息"""
# return {
# "requestUpdateWorker": {
# "items": [
# {"id": {"uuid": f"{uuid}"},
# "worker": {
# "uuid": "18571db4-abac-46ba-a647-cbccad424d08",
# "pid": "88",
# "name": "192.168.5.74",
# "endpoint": "http://192.168.5.74:8000",
# "enabled": true,
# "deleted": true,
# "stake": "10000000000000",
# "status": "S_ERROR",
# "worker": {
# "uuid": "18571db4-abac-46ba-a647-cbccad424d08",
# "pid": "88", "name": "192.168.5.74",
# "endpoint": "http://192.168.5.74:8000",
# "stake": "10000000000000"},
# "lastMessage": "1636889212205 - xxxxxxxxxx}",
# "minerInfoJson": "{}", "minerInfo": {}
# }
# }
# ]
# }
# }
@staticmethod
def get_request_create_pool(pools_list):
"""
请求创建pool质押池信息
:param pools_list:
[
{
"name": f"{pool_name}",
"pid": f"{pid}",
"enabled": True,
"owner": {"mnemonic": f"{mnemonic}"}
}
]
:return:
"""
return {"requestCreatePool": {"pools": pools_list}}
@staticmethod
def get_request_create_worker(workers_list):
"""
请求创建worker信息
:param workers_list:
[
{
"enabled": True,
"pid": f"{pid}",
"name": f"{name}",
"endpoint": f"http://{name}:8000",
"stake": f"{stake}"
}
]
:return:
"""
return {"requestCreateWorker": {"workers": workers_list}}
class GetPrbWorkersPoolsData:
def __init__(self, ip_port):
self.req = PrbPostRequest(ip_port)
# self.post_data = PrbDataMethod.get_fetcher_data()
self.post_data = PrbDataMethod.get_workers_pools_data()
self.post_result = self.req.get_resp_data(self.post_data)
def get_pools_data(self):
"""
:return:
"""
pools_data = self.post_result
if type(pools_data) is list:
pass
try:
uuid_data = pools_data['content']['lifecycleManagerStateUpdate']['pools']
# print(uuid_data)
# uuid_result = PrbPostRequest.get_uuid(uuid_data)
# print(uuid_result)
except KeyError as e:
pass
except TypeError as e:
pass
else:
return uuid_data
def get_workers_data(self):
"""
:return:
"""
workers_data = self.post_result
# print(workers_data)
# fetcher_cls = GetPrbFetcherData(ip__port)
# manager_data = manager_cls.post_result
# fetcher_data = fetcher_cls.post_data
# print(manager_data)
# print(fetcher_data)
# exit()
if type(workers_data) is list:
pass
try:
uuid_data = workers_data['content']['lifecycleManagerStateUpdate']['workers']
except KeyError as e:
pass
except TypeError as e:
pass
else:
uuid_result = PrbPostRequest.get_uuid(uuid_data)
post_data_query = {"queryWorkerState": {"ids": uuid_result}}
prb_data = self.req.get_resp_data(post_data_query)['content']['workerStateUpdate']['workerStates']
# print(prb_data)
return prb_data
class GetPrbFetcherData(object):
def __init__(self, ip_port):
self.req = PrbPostRequestForFetcher(ip_port)
self.post_data = PrbDataMethod.get_fetcher_data()
self.post_result = self.req.get_resp_data(self.post_data)
class PostPrbRestartLifecycle(object):
def __init__(self):
pass
@staticmethod
def post_prb_restart_lifecycle(ip_port, uuid):
req = PrbPostRequest(ip_port)
post_data = PrbDataMethod.get_request_start_worker_lifecycle(uuid)
req.get_resp_data(post_data)
class AddWorkerAndPoolsToPrb(object):
def __init__(self, ip_port):
self.req = PrbPostRequest(ip_port)
def add_pools_to_prb(self, test):
post_data = PrbDataMethod.get_request_create_pool(test)
post_result = self.req.get_resp_data(post_data)
# print(post_result)
return post_result
def add_workers_to_prb(self, workers_list):
post_data = PrbDataMethod.get_request_create_worker(workers_list)
post_result = self.req.get_resp_data(post_data)
# print(post_result)
return post_result
if __name__ == '__main__':
pass
# '''示例代码'''
# # prb-monitor的地址和端口
# ip_port = '192.168.2.100:3000'
# # 获取prb中的pool信息
# a = GetPrbWorkersPoolsData(ip_port).get_pools_data()
# # 获取prb中的worker信息
# b = GetPrbWorkersPoolsData(ip_port).get_workers_data()
# print(a)
# print(b)
# # 创建pools,需要app面板对应pid的,可以同时添加多个pid
# mnemonic_for_83 = '83号pid,对应的app中Gas账号的助记词'
# mnemonic_for_xx = 'xx号pid,对应的app中Gas账号的助剂词'
# pools = [{"name": "83", "pid": "83", "enabled": True, "owner": {"mnemonic": f"{mnemonic_for_83}"}},
# {"name": "xx", "pid": "xx", "enabled": True, "owner": {"mnemonic": f"{mnemonic_for_xx}"}},
# ]
# a = AddWorkerAndPoolsToPrb(ip_port)
# a.add_pools_to_prb(pools)
# # 创建workers
# workers = [{"enabled": True,
# "pid": "83",
# "name": "192.168.2.1",
# "endpoint": "http://192.168.2.1:8000",
# "stake": "10000000000000"}]
# a.add_workers_to_prb(workers)