-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_actions.py
241 lines (212 loc) · 12.3 KB
/
request_actions.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
import const
import json
import requests
import subprocess
import time
import config
import logging
# Global variables
token = config.token
network = config.network
indexer_management_url = config.indexer_management_url
indexer_graph = config.indexer_graph
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
def print_hi():
print(f'\n\n************* This is a tool that will help the indexer manage allocations on the website '
f'*************')
print(f'Starting indexer_script version ' + const.VERSION + "\n\n")
logging.basicConfig(filename='indexer_script.log', level=logging.INFO)
logging.info("version: " + const.VERSION)
def request_update_exe_status(id):
try:
params = {
'id': id,
'token': token,
'network': network
}
response = requests.post(const.API_UPDATE_EXE_STATUS, data=params, headers=headers)
if response.status_code == 200 and response.json()["code"] == 0:
print("************ UPDATE EXE STATUS SUCCESS ***********\n")
logging.info("UPDATE EXE STATUS SUCCESS " + str(id))
else:
print("************ UPDATE EXE STATUS ERROR ***********\n")
logging.info("UPDATE EXE STATUS SUCCESS " + str(id) + ":" + response.status_code)
logging.info("UPDATE EXE STATUS SUCCESS " + str(id) + ":" + str(response.json()["code"]))
except Exception as e:
print(str(e))
logging.error("request_update_exe_status:" + str(e))
def request_update_time(token):
try:
params = {
'token': token,
'timeUpdate': time.time()
}
response = requests.post(const.API_UPDATE_TIME, data=params, headers=headers)
if response.status_code == 200 and response.json()["code"] == 0:
print("************ UPDATE TIME SUCCESS ***********\n")
logging.info("UPDATE TIME SUCCESS ")
else:
print("************ UPDATE TIME ERROR ***********\n")
logging.info("UPDATE TIME ERROR " + str(response.status_code))
logging.info("UPDATE TIME ERROR " + str(response.json()["code"]))
except Exception as e:
print(str(e))
logging.error("request_update_time:" + str(e))
def request_actions(url):
try:
params = {
'token': token,
'network': network,
}
response = requests.post(url, data=params, headers=headers)
if response.status_code == 200:
print(" ======================================================")
print("Request actions success , here is the list actions : \n")
actions_json = response.json()
responseCode = actions_json["code"]
actions_data = actions_json["data"]
executeCount = 0
if responseCode == 0 and len(actions_data) > 0:
for action in actions_data:
try:
logging.info(action)
print("------------------------------")
serverId = action["id"]
ipfsHash = action["ipfsHash"]
status = action["status"]
networkId = action["networkId"]
allocatedTokens = action["allocatedTokens"]
actionId = action["action"]
if actionId == const.CLOSE_ACTION or actionId == const.RE_ALLOCATION_ACTION:
actionMsg = "CLOSE"
allocateId = action["allocateId"]
poi = action["poi"]
print("Valid POI : ", poi)
print("Allocate Id : ", allocateId)
else:
actionMsg = "OPEN"
print("Deployment : ", ipfsHash)
print("Status : ", status)
print("Network : ", networkId)
print("Allocated : ", allocatedTokens)
print("Action : ", actionMsg)
if status == const.STATUS_APPROVE:
executeCount = executeCount + 1
logging.info(ipfsHash + " : " + str(serverId))
if actionId == const.CLOSE_ACTION:
print(" ************************************* ")
print(" Start close this subgraph !")
if poi == 'NULL':
cmd_unallocate_action = f"{indexer_graph} indexer actions queue unallocate {ipfsHash} {allocateId} --output=json --network={config.agent_network}"
else:
cmd_unallocate_action = f"{indexer_graph} indexer actions queue unallocate {ipfsHash} {allocateId} {poi} true --output=json --network={config.agent_network}"
logging.info(cmd_unallocate_action)
print("Execute cmd : " + cmd_unallocate_action)
process = subprocess.run([cmd_unallocate_action], shell=True, check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
close_output = process.stdout
logging.info("unallocate success for deployment : " + ipfsHash)
print("Close action status : " + close_output)
elif actionId == const.OPEN_ACTION:
print(" ************************************* ")
print(" Start open this subgraph !")
cmd_allocate_action = f"{indexer_graph} indexer actions queue allocate {ipfsHash} {allocatedTokens} --output=json --network={config.agent_network}"
logging.info(cmd_allocate_action)
print("Execute cmd : " + cmd_allocate_action)
process = subprocess.run([cmd_allocate_action], shell=True, check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
close_output = process.stdout
logging.info("allocate success for deployment : " + ipfsHash)
print("OPEN action status : " + close_output)
elif actionId == const.RE_ALLOCATION_ACTION:
print(" ************************************* ")
print(" Start re-allocation this subgraph !")
if poi == 'NULL':
cmd_reallocate_action = f"{indexer_graph} indexer actions queue reallocate {ipfsHash} {allocateId} {allocatedTokens} --output=json --network={config.agent_network}"
else:
cmd_reallocate_action = f"{indexer_graph} indexer actions queue reallocate {ipfsHash} {allocateId} {allocatedTokens} {poi} true --output=json --network={config.agent_network}"
logging.info(cmd_reallocate_action)
print("Execute cmd : " + cmd_reallocate_action)
process = subprocess.run([cmd_reallocate_action], shell=True, check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
re_allocation_output = process.stdout
logging.info("reallocate success for deployment : " + ipfsHash)
print("Close action status : " + re_allocation_output)
# update exe status on server to avoid dupplicate action
request_update_exe_status(serverId)
request_update_time(token)
else:
print("====> Status is Queue -> No execute !")
# For offchain dont need approve
if actionId == const.OFFCHAIN_ACTION:
print(" ************************************* ")
print(" Start sync offchain this subgraph !")
cmd_offchain = f"{indexer_graph} indexer rules set {ipfsHash} decisionBasis offchain --output=json --network={config.agent_network}"
logging.info(cmd_offchain)
print("Execute cmd : " + cmd_offchain)
process = subprocess.run([cmd_offchain], shell=True, check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
offchain_output = process.stdout
print("Offchain action status : " + offchain_output)
logging.info("Offchain success for deployment : " + ipfsHash)
# update exe status on server to avoid dupplicate action
request_update_exe_status(serverId)
request_update_time(token)
except Exception as e:
print("Exception: " + str(e))
logging.error("Exception actions_data: " + str(e))
request_update_exe_status(serverId)
request_update_time(token)
# end for
if executeCount > 0:
try:
print(f"=====> Total actions need to approve : {executeCount}")
cmd_approve_all_queue = f"{indexer_graph} indexer actions approve queued --output=json --network={config.agent_network}"
logging.info(cmd_approve_all_queue)
process = subprocess.run([cmd_approve_all_queue], shell=True, check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
approve_output = process.stdout
logging.info("approve success for all queued actions !")
print("Approve all actions : " + approve_output)
except subprocess.CalledProcessError as subError:
print(subError)
logging.error("Approve queue exception : " + subError)
else:
print("There is no action to execute !")
except Exception as e:
print(str(e))
logging.error("request_actions:" + str(e))
def verify_config():
try:
cmd_connect_indexer_manager = f"{indexer_graph} indexer connect {indexer_management_url}"
print("Start connect indexer manager: " + cmd_connect_indexer_manager)
process = subprocess.run([cmd_connect_indexer_manager], shell=True, check=True, stdout=subprocess.PIPE,
universal_newlines=True)
connect_indexer_manager_output = process.stdout
if connect_indexer_manager_output.startswith(
f"Indexer management API URL configured as \"{indexer_management_url}\""):
print("** Connected success to indexer manager ! **")
cmd_get_graph_vs = f"{indexer_graph} --version"
process = subprocess.run([cmd_get_graph_vs], shell=True, check=True, stdout=subprocess.PIPE,
universal_newlines=True)
graph_version = process.stdout
print("Indexer graph version : " + graph_version)
return True
except Exception as e:
print(e)
logging.error(str(e))
return False
def start_request_actions():
while True:
try:
request_actions(const.API_GET_CLOSE_ACTIONS)
request_actions(const.API_GET_OPEN_ACTIONS)
except Exception as e:
print(e)
logging.error("start_request_actions:" + str(e))
time.sleep(20)