-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaurad-status.py
More file actions
executable file
·288 lines (225 loc) · 7.3 KB
/
aurad-status.py
File metadata and controls
executable file
·288 lines (225 loc) · 7.3 KB
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
#!/usr/bin/env python3
import subprocess
import os
import time
import sys
import signal
import json
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
config_auto_restart = False
config_wait_before_restart = 0
config_rpc = ""
config_download_latest = False
restarts = 0
fetch_latest_version_counter = 0
version_status = ""
latest_version = ""
version = ""
status = ""
percentage_uptime = 0
percentage_downtime = 0
current_block_num = 0
last_line = ""
container_died = False
script_version = ""
was_online_once = False # Don't restart he node when it is syncing
is_online = False
offline_seconds = 0
last_run = time.time()
def read_config():
global config_auto_restart;
global config_wait_before_restart;
global config_download_latest;
global config_rpc;
try:
with open('aurad-status-settings.json', 'r') as f:
jobj = json.loads(f.read())
config_auto_restart = jobj["auto_restart"]
config_wait_before_restart = jobj["wait_before_restart"]
config_download_latest = jobj["download_latest"]
config_rpc = jobj["rpc"]
if config_auto_restart == True and config_wait_before_restart < 60:
print("Timeout before restart must be at least 60 seconds. Using default settings with auto-restart disabled.")
config_auto_restart = False
config_wait_before_restart = 0
config_rpc = ""
config_download_latest = False
time.sleep(3)
except:
print("Warning: Could not read config file! Continuing with default settings in 3 seconds.")
time.sleep(3)
def read_logs():
global fetch_latest_version_counter;
global version_status;
global latest_version;
global version;
global status;
global percentage_uptime;
global percentage_downtime;
global current_block_num;
global is_online;
global was_online_once;
global last_line;
global container_died;
global script_version;
dockerProc = subprocess.Popen(['docker','logs','docker_idexd_1'],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
online = 0
offline = 0
last_line_current_run = ""
while True:
line = dockerProc.stdout.readline().decode("utf-8")
if line != '':
last_line_current_run = line
if "No such container" in line:
container_died = True
break
elif "STAKING" in line:
status = ""
if "ONLINE" in line:
online += 1
status += bcolors.OKGREEN
if not was_online_once:
offline_seconds = 0
was_online_once = True
is_online = True
else:
offline += 1
status += bcolors.FAIL
is_online = False
status += line + bcolors.ENDC
elif "Processing blocks" in line:
if len(extract_integers(line)) >= 2:
current_block_num = extract_integers(line)[1]
elif "Waiting for" in line:
if len(extract_integers(line)) >= 1:
current_block_num = extract_integers(line)[0]
else:
break
# Check that the container has not died and is not stuck
if last_line_current_run == last_line:
is_online = False
last_line = last_line_current_run
if online + offline == 0:
percentage_uptime = 0
else:
percentage_uptime = (float(online) / (online + offline)) * 100
percentage_downtime = 100 - float(percentage_uptime)
auraProc = subprocess.Popen(['idex','status'],stdout=subprocess.PIPE)
version_line = auraProc.stdout.readline().decode("utf-8")
if version_line != '':
version = version_line.split()[1]
# we don't need to fetch the latest version every time
if fetch_latest_version_counter == 0 or fetch_latest_version_counter >= 50:
npmProc = subprocess.Popen(['npm','show','@idexio/idexd-cli','version'],stdout=subprocess.PIPE)
latest_version_line = npmProc.stdout.readline().decode("utf-8")
if latest_version_line != '':
latest_version = "v" + latest_version_line.rstrip()
local_commit_proc = subprocess.Popen(['git','rev-parse','master'],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
local_commit = local_commit_proc.stdout.readline().decode("utf-8")
remote_commit_proc = subprocess.Popen(['git','rev-parse','origin/master'],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
remote_commit = remote_commit_proc.stdout.readline().decode("utf-8")
if "not a git repository" in local_commit or "not a git repository" in remote_commit:
script_version = bcolors.FAIL + "Cannot check script version: Not a git repository" + bcolors.ENDC
else:
if remote_commit == local_commit:
script_version = bcolors.OKGREEN + "Script is up to date" + bcolors.ENDC
else:
script_version = bcolors.FAIL + "A new version of this script is available. Please run 'git pull' and restart this script" + bcolors.ENDC
fetch_latest_version_counter = 1
else:
fetch_latest_version_counter += 1
if latest_version != version:
version_status = bcolors.FAIL + bcolors.BOLD + "YOUR VERSION OF IDEXD IS OUTDATED" + bcolors.ENDC
else:
version_status = bcolors.OKGREEN + "UP TO DATE" + bcolors.ENDC
def check_for_restart():
global config_auto_restart;
global config_wait_before_restart;
global config_download_latest;
global config_rpc;
global offline_seconds;
global restarts;
global was_online_once;
global container_died;
if not config_auto_restart:
return
if offline_seconds > config_wait_before_restart or container_died:
print("Offline for " + str(int(offline_seconds)) + " seconds. Restarting.")
offline_seconds = 0
was_online_once = False
container_died = False
print("Stopping...")
subprocess.Popen(['idex','stop']).wait()
if config_download_latest:
print("Downloading latest version...")
subprocess.Popen(['npm','install','-g','@idexio/idexd-cli']).wait()
print("Starting...")
if config_rpc != "" and config_rpc != " ":
subprocess.Popen(['idex','start','--rpc',config_rpc]).wait()
else:
subprocess.Popen(['idex','start']).wait()
restarts += 1
os.system("clear")
def extract_integers(string):
return [int(s) for s in string.split() if s.isdigit()]
def wait():
progress_length = 20
sleep_dur = 5
for i in range(progress_length):
string = "Next update: ["
for j in range(i):
string += "="
for j in range(i,progress_length - 1):
string += "-"
string += "]"
print('{0}\r'.format(string), end='', flush=True)
time.sleep(float(sleep_dur)/progress_length)
def reset():
global version_status;
global version;
global status;
global percentage_uptime;
global percentage_downtime;
global current_block_num;
version_status = ""
version = ""
status = ""
percentage_uptime = 0
percentage_downtime = 0
current_block_num = 0
def exit_handler(sig, frame):
os.system("reset")
sys.exit(0)
signal.signal(signal.SIGINT, exit_handler)
#hide the cursor, restored on sigint
sys.stdout.write("\033[?25l")
sys.stdout.flush()
read_config()
while True:
reset()
read_logs()
os.system("clear")
current_time = time.time()
if (was_online_once and not is_online) or container_died:
offline_seconds += current_time - last_run
check_for_restart()
elif is_online:
offline_seconds = 0
last_run = current_time
print(script_version)
print("")
print("Your version: " + version + " | Latest version: " + latest_version + " | " + version_status)
print("Status: " + status+ "\n")
print("Uptime: {0:.2f}%\n".format(percentage_uptime))
print("Downtime: {0:.2f}%\n".format(percentage_downtime))
print("Restarts: " + str(restarts) + "\n")
print("Current block: " + str(current_block_num) + "\n")
wait()