-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
633 lines (432 loc) · 24 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import subprocess
from utils import create_logger, pretty_print
import config
import json
import version
import sys
import requests
import traceback
logger = create_logger(config.log_file_path, __name__ , config.log_level, True)
def check():
if config.binary_node != 'oraid':
raise Exception("This comd is only applicapble for orai network!")
def workspace_CUR():
"Directory where we currently store the blockchain data folder"
return "{}/workspace".format(config.volume_current)
def workspace_NEW():
"irectory where we plan to store going forward the blockchain data folder"
return "{}/workspace".format(config.volume_new)
def full_path_source_data():
"The full path to the folder to backup. Argument for the backup_script.sh"
return workspace_CUR()
def modifier_binary_name():
return config.binary_node[:-1] if config.binary_node in['junod', 'umeed'] else config.binary_node
def full_path_backup_name():
"""The full path of the backup folder. Argument for the backup_script.sh.
The zip file create by the backup script will be stored temporarily in volumne_new
"""
return "{}/{}".format(config.volume_new, config.binary_node)
def home_path_CUR():
"Path to the current blockchain datafolder"
return "{}/.{}".format(workspace_CUR(), modifier_binary_name())
def home_path_NEW():
"Path to the new blockchain datafolder"
return "{}/.{}".format(workspace_NEW(), modifier_binary_name())
def _create_home_path_symlink(home_path):
return "unlink /root/.{binary} ; ln -s {home_path} /root/.{binary}".format(binary=modifier_binary_name(),
home_path=home_path)
def create_home_path_symlink_CURR():
cmd_value = [_create_home_path_symlink(home_path_CUR())]
return cmd_format(cmd_value, 'create_home_path_symlink_CURR')
def create_home_path_symlink_NEW():
cmd_value = [_create_home_path_symlink(home_path_NEW())]
return cmd_format(cmd_value, 'create_home_path_symlink_CURR')
def backup_script(cleanup="cleanup?"):
""" This cmd is applicable only for all networks.
It upload source data to the digital ocean space.
- cleanup: true, false (default true)
-true: the local back file will be deleted
-false: the local back file will not be deleted.
"""
cmd_value = ["sh {script_path} {src} {space} {bkup} {cleanup}".format(script_path=config.backup_script_path,
space=config.digital_ocean_space,
src =full_path_source_data(),
bkup=full_path_backup_name(),
cleanup=cleanup)]
return cmd_format(cmd_value, 'backup_script')
def backup_script_and_delete_local_copy():
""" run the backup scrip to upload source to digital ocean and
delete the local copy in the new volume.
"""
return backup_script(cleanup="true")
def backup_script_and_keep_local_copy():
""" run the backup scrip to upload source to digital ocean and
keep a local copy in the new volume.
"""
return backup_script(cleanup="false")
def s3_download(source_file='source_file?'):
"""This cmd is applicable only for all networks.
It download source data file from the digital ocean space.
"""
cmd_value = ["cd {volume_new}; s3cmd get s3://{space}/{src} {src} ; tar -xzvf {src} ; rm {src}".format(volume_new=config.volume_new,
space=config.digital_ocean_space,
src =source_file)]
return cmd_format(cmd_value, 's3_download')
def s3_upload(source_tar_file='source_tar_file?', destination='destination?'):
"""This cmd is applicable only for all networks.
It download source data file from the digital ocean space.
"""
cmd_value = ["cd {volume_new}; s3cmd put {src} s3://{space}".format(volume_new=config.volume_new,
space=config.digital_ocean_space,
src =source_tar_file)]
return cmd_format(cmd_value, 's3_upload')
def escape_slash(name):
return name.replace("/", "\/")
def cmd_format(cmd_value, cmd_name):
return {'key': cmd_value, 'name': cmd_name.upper()}
def start_node():
"this cmd is applicable to all the networks except orai"
node_name = 'cosmovisor' if config.binary_node in [ 'junod', 'umeed'] else config.binary_node
cmd_value = ["sudo systemctl start {}".format(node_name)]
return cmd_format(cmd_value, 'start_node_CUR')
def _start_node(workspace):
"this cmd is applicable only for orai"
check()
return "cd {}; docker-compose restart orai && docker-compose exec -d orai bash -c 'oraivisor start --p2p.pex false'".format(workspace)
def start_node_CUR():
"this cmd is applicable only for orai"
cmd_value = [_start_node(workspace_CUR())]
return cmd_format(cmd_value, 'start_node_CUR')
def start_node_NEW():
"this cmd is applicable only for orai"
cmd_value = [_start_node(workspace_NEW())]
return cmd_format(cmd_value, 'start_node_NEW')
def stop_node():
"This cmd is applicable for all networks"
cmd_value = ""
if config.binary_node == 'oraid':
cmd_value = ["docker stop orai_node ; sleep 1s; docker rm orai_node; sleep 1s"]
else:
node_name = 'cosmovisor' if config.binary_node in ['junod', 'umeed'] else config.binary_node
cmd_value = ["sudo systemctl stop {}; sleep 2s".format(node_name)]
return cmd_format(cmd_value, 'stop_node')
def stop_remove_docker_container():
"This cmd is applicable only for orai"
check()
cmd_value = ["docker stop orai_node ; docker rm orai_node; sleep 2s"]
return cmd_format(cmd_value, 'stop_remove_docker_container')
def delete_priv_keys():
"This cmd is applicable for all networks"
node_key_file = "{}/config/node_key.json".format(home_path_CUR())
priv_key_file = "{}/config/priv_validator_key.json".format(home_path_CUR())
priv_key_state_file = "{}/data/priv_validator_state.json".format(home_path_CUR())
cmd_value = ["rm -f {}; rm -f {}; rm -f {}".format(node_key_file, priv_key_file, priv_key_state_file)]
return cmd_format(cmd_value, 'delete_priv_keys')
def remove_docker_container():
"This cmd is applicable only for orai"
cmd_value = ["docker rm orai_node"]
return cmd_format(cmd_value, 'remove_docker_container')
def force_recreate_docker_container(workspace):
"This cmd is applicable only for orai"
return "cd {} ; docker-compose pull && docker-compose up -d --force-recreate".format(workspace)
def force_recreate_docker_container_CUR():
"This cmd is applicable only for orai"
cmd_value = [force_recreate_docker_container(workspace_CUR())]
return cmd_format(cmd_value, 'force_recreate_docker_container_CUR')
def force_recreate_docker_container_NEW():
"This cmd is applicable only for orai"
cmd_value = [force_recreate_docker_container(workspace_NEW())]
return cmd_format(cmd_value, 'force_recreate_docker_container_NEW')
def start_alert():
"This cmd is applicable for all networks"
cmd_value = ["sudo systemctl start indep_node_alarm"]
return cmd_format(cmd_value, 'start_alert')
def stop_alert():
"This cmd is applicable for all networks"
cmd_value = ["sudo systemctl stop indep_node_alarm"]
return cmd_format(cmd_value, 'stop_alert')
def start_signctrl():
"This cmd is applicable only for all networks"
cmd_value = ["sudo systemctl start signctrl"]
return cmd_format(cmd_value, 'start_signctrl')
def stop_signctrl():
"This cmd is applicable only for all networks"
cmd_value = ["sudo systemctl stop signctrl"]
return cmd_format(cmd_value, 'stop_signctrl')
def delete_signctrl_state():
"This cmd is applicable only for all networks"
cmd_value = ["rm -f /home/signer/.signctrl/signctrl_state.json"]
return cmd_format(cmd_value, 'delete_signctrl_state')
def run_backup_delete_local_copy():
"This cmd is applicable only for all networks"
cmd_value = ['stop_node', 'stop_signctrl', 'delete_priv_keys', 'backup_script_and_delete_local_copy', 'delete_repo_outdated_files']
return cmd_format(cmd_value, 'run_backup')
def run_backup_keep_local_copy():
"""This cmd is applicable only for all networks.
Create a backup and keep a local copy in the volume_new folder.
"""
cmd_value = ['stop_node', 'stop_signctrl', 'delete_priv_keys', 'backup_script_and_keep_local_copy', 'unzip_backup_file', 'delete_repo_outdated_files']
return cmd_format(cmd_value, 'run_backup')
def restart_node():
"This cmd is applicable only for juno"
cmd_value = ['delete_signctrl_state', 'start_signctrl', 'start_node']
return cmd_format(cmd_value, 'restart_node')
def restart_node_NEW():
"This cmd is applicable for all networks "
cmd_value = ""
if config.binary_node == 'oraid':
cmd_value = ['delete_signctrl_state', 'start_signctrl', 'force_recreate_docker_container_NEW', 'start_node_NEW']
else:
cmd_value = ['create_home_path_symlink_NEW', 'restart_node']
return cmd_format(cmd_value, 'restart_node_NEW')
def restart_node_CUR():
"This cmd is applicable for all networks "
if config.binary_node == 'oraid':
cmd_value = ['delete_signctrl_state', 'start_signctrl', 'force_recreate_docker_container_CUR', 'start_node_CUR']
else:
cmd_value = ['create_home_path_symlink_CURR', 'restart_node']
return cmd_format(cmd_value, 'restart_node_CUR')
def restart_sentry_node_NEW():
"This cmd is applicable for all networks "
cmd_value = ""
if config.binary_node == 'oraid':
cmd_value = ['force_recreate_docker_container_NEW', 'start_node_NEW']
else:
cmd_value = ['create_home_path_symlink_NEW', 'start_node']
return cmd_format(cmd_value, 'restart_node_NEW')
def restart_sentry_node_CUR():
"This cmd is applicable for all networks "
if config.binary_node == 'oraid':
cmd_value = ['force_recreate_docker_container_CUR', 'start_node_CUR']
else:
cmd_value = ['create_home_path_symlink_CURR', 'start_node']
return cmd_format(cmd_value, 'restart_sentry_node_CUR')
def restart_node_without_signctrl_NEW():
"This cmd is applicable for all networks"
if config.binary_node == 'oraid':
cmd_value = ['config_node_without_signctrl_NEW', 'force_recreate_docker_container_NEW', 'start_node_NEW']
else:
cmd_value = ['config_node_without_signctrl_NEW', 'restart_node_NEW']
return cmd_format(cmd_value, 'restart_node_without_signctrl_NEW')
def restart_node_without_signctrl_CUR():
"This cmd is applicable for all networks"
if config.binary_node == 'oraid':
cmd_value = ['config_node_without_signctrl_CUR', 'force_recreate_docker_container_CUR', 'start_node_CUR']
else:
cmd_value = ['config_node_without_signctrl_CUR', 'restart_node_CUR']
return cmd_format(cmd_value, 'restart_node_without_signctrl_CUR')
def priv_validator_laddr_config_reset(home_path):
"This cmd is applicable for all networks"
return ["""sed -i "s/^priv_validator_laddr *=.*/priv_validator_laddr = \\"\\" /" {home_path}/config/config.toml""".format(home_path=home_path)]
def priv_validator_laddr_config_reset_NEW():
"This cmd is applicable for all networks"
cmd_value = priv_validator_laddr_config_reset(home_path_NEW())
return cmd_format(cmd_value, 'priv_validator_laddr_config_reset_NEW')
def priv_validator_laddr_config_reset_CUR():
"This cmd is applicable for all networks"
cmd_value = priv_validator_laddr_config_reset(home_path_CUR())
return cmd_format(cmd_value, 'priv_validator_laddr_config_reset_CUR')
def priv_validator_laddr_config_signctrl(home_path):
"This cmd is applicable for all networks"
return ["""sed -i "s/^priv_validator_laddr *=.*/priv_validator_laddr = \\"tcp:\\/\\/127.0.0.1:3000\\" /" {home_path}/config/config.toml""".format(home_path=home_path)]
def priv_validator_laddr_config_signctrl_NEW():
"This cmd is applicable for all networks"
cmd_value = priv_validator_laddr_config_signctrl(home_path_NEW())
return cmd_format(cmd_value, 'priv_validator_laddr_config_signctrl_NEW')
def priv_validator_laddr_config_signctrl_CUR():
"This cmd is applicable for all networks"
cmd_value = priv_validator_laddr_config_signctrl(home_path_CUR())
return cmd_format(cmd_value, 'priv_validator_laddr_config_signctrl_CUR')
def copy_priv_validator_key_to_home(home_path):
"This cmd is applicable for all networks"
return ["cp /home/signer/.signctrl/priv_validator_key.json {home_path}/config/; cp /home/signer/.signctrl/priv_validator_state.json {home_path}/data/".format(home_path=home_path)]
def copy_priv_validator_key_to_home_NEW():
"This cmd is applicable for all networks"
cmd_value = copy_priv_validator_key_to_home(home_path_NEW())
return cmd_format(cmd_value, 'copy_priv_validator_key_to_home_NEW')
def copy_priv_validator_key_to_home_CUR():
"This cmd is applicable for all networks"
cmd_value = copy_priv_validator_key_to_home(home_path_CUR())
return cmd_format(cmd_value, 'copy_priv_validator_key_to_home_CUR')
def config_node_without_signctrl_NEW():
"""This cmd is applicable for all networks.
- Move private keys from /home/signer/.signctrl to new validator home (e.g /mn/volumne_new/workspace/.noded)
- update the config.toml and set priv_validator_laddr = ""
"""
cmd_value = ["priv_validator_laddr_config_reset_NEW", "copy_priv_validator_key_to_home_NEW"]
return cmd_format(cmd_value, 'config_node_without_signctrl_NEW')
def config_node_without_signctrl_CUR():
"""This cmd is applicable for all networks.
- Move private keys from /home/signer/.signctrl to new validator home (e.g /mn/volumne_cur/workspace/.noded)
- update the config.toml and set priv_validator_laddr = ""
"""
cmd_value = ["priv_validator_laddr_config_reset_CUR", "copy_priv_validator_key_to_home_CUR"]
return cmd_format(cmd_value, 'config_node_without_signctrl_CUR')
def run_backup_and_restart_node_CUR():
"This cmd is applicable for all networks"
cmd_value = ['run_backup_delete_local_copy', 'restart_node_CUR']
return cmd_format(cmd_value, 'run_backup_and_restart_node_CUR')
def run_backup_and_restart_node_NEW():
"This cmd is applicable only for all network"
cmd_value = ['run_backup_keep_local_copy', 'restart_node_NEW']
return cmd_format(cmd_value, 'run_backup_and_restart_node_NEW')
def run_backup_and_restart_sentry_node_CUR():
"This cmd is applicable for all networks"
cmd_value = ['run_backup_delete_local_copy', 'restart_sentry_node_CUR']
return cmd_format(cmd_value, 'run_backup_and_restart_sentry_node_CUR')
def run_backup_and_restart_sentry_node_NEW():
"This cmd is applicable only for all network"
cmd_value = ['run_backup_keep_local_copy', 'restart_sentry_node_NEW']
return cmd_format(cmd_value, 'run_backup_and_restart_sentry_node_NEW')
def list_repository_files():
cmd_value = ["s3cmd ls s3://{space}/{binary}*".format(space=config.digital_ocean_space, binary=config.binary_node)]
return cmd_format(cmd_value, 'list_repository_files')
def delete_repo_file(file_name='file_name?'):
cmd_value = ['s3cmd rm s3://{space}/{file_name}'.format(space=config.digital_ocean_space, file_name=file_name)]
return cmd_format(cmd_value, 'delete_repo_file')
def delete_repo_outdated_files():
""" delete outdated files.
Keep only the last two backup
"""
backup_number = 2
cmd_value = ["""numberFiles=$(s3cmd ls s3://chandrodaya/{binary}* | wc -l) ; if (($numberFiles > {backup_number} )); then s3cmd ls s3://chandrodaya/{binary}* | sort -r | tail -n $(expr $numberFiles - {backup_number}) | grep -E -o "s3://chandrodaya/.*" | while read file; do s3cmd rm $file; done; else echo "there are NO files to delete"; fi""".format(binary=config.binary_node, backup_number=backup_number )]
return cmd_format(cmd_value, 'delete_outdate_repo_files')
def unzip_backup_file():
"""This cmd is applicable only for all networks.
"""
cmd_value = ["""cd {volume_new}; numberFiles=$(ls | wc -l); if (($numberFiles == 1 )); then fileName=`ls ${binary}*.gz`; tar -xzvf $fileName ; rm $fileName ; else echo "There are too many file to unzip or the folder is empty!" ;fi""".format(volume_new=config.volume_new,
binary=config.binary_node)]
return cmd_format(cmd_value, 'unzip_backup_file')
def EXIT():
"This cmd is applicable only for all network"
cmd_value = ["Exit from the program"]
return cmd_format(cmd_value, 'EXIT')
def display_cmd_value(cmd):
"""cmd: one of the cmd function. The output of such function is in this form:
{cmd_keys: ...., cmd_name: ...., cmd_level:}
See cmd_format fucntion.
"""
cmd_func = globals()[cmd]
cmd_value = cmd_func()['key']
return '; '.join(cmd_value)
CMD_KEY_INVARIANT = [ 'EXIT', 'delete_repo_file', 's3_download', 's3_upload', 'run_backup_and_restart_node_CUR', 'run_backup_and_restart_node_NEW',
'run_backup_keep_local_copy', 'run_backup_delete_local_copy', 'delete_signctrl_state',
'start_signctrl', 'stop_signctrl', 'start_alert',
'stop_alert', 'config_node_without_signctrl_NEW', 'config_node_without_signctrl_CUR',
'restart_node_without_signctrl_NEW', 'restart_node_without_signctrl_CUR',
'run_backup_and_restart_sentry_node_CUR', 'run_backup_and_restart_sentry_node_NEW']
def get_CMD_MAP():
CMD_MAP = {}
# common key
cmd_keys = CMD_KEY_INVARIANT + ['stop_node', 'stop_node', 'delete_signctrl_state',
'delete_priv_keys', 'restart_node_NEW', 'restart_node_CUR',
'restart_sentry_node_NEW', 'restart_sentry_node_CUR',
'list_repository_files', 'delete_repo_outdated_files',
'backup_script_and_keep_local_copy', 'backup_script_and_delete_local_copy',
'backup_script', 'unzip_backup_file', 'priv_validator_laddr_config_reset_NEW',
'priv_validator_laddr_config_reset_CUR', 'priv_validator_laddr_config_signctrl_NEW',
'priv_validator_laddr_config_signctrl_CUR', 'copy_priv_validator_key_to_home_NEW',
'copy_priv_validator_key_to_home_CUR',
]
# network specific key
if config.binary_node == 'oraid':
cmd_keys = cmd_keys + ['start_node_CUR', 'start_node_NEW', 'remove_docker_container',
'force_recreate_docker_container_CUR', 'force_recreate_docker_container_NEW']
else:
cmd_keys = cmd_keys + ['start_node', 'create_home_path_symlink_NEW', 'create_home_path_symlink_CURR',
'restart_node']
for cmd_key in cmd_keys:
CMD_MAP[cmd_key] = display_cmd_value(cmd_key)
return dict(sorted(CMD_MAP.items()))
def exec_shell_recursive_cmd(cmd_key):
""" output: 1, 0
- 1: the execution of the cmd failed
- 0: the execution of the cmd passed
"""
cmd = globals()[cmd_key]()
logger.info("************** START {} ***********************".format(cmd['name']))
if len(cmd['key']) == 1 and cmd['key'][0] not in list(get_CMD_MAP().keys()):
result = exec_shell_cmd(cmd['key'])
if result != 0 :
logger.info("************** {} FAILED! ***********************".format(cmd['name']))
return 1
else:
for _cmd_key in cmd['key']:
result = exec_shell_recursive_cmd(_cmd_key)
if result != 0 :
logger.info("************** {} FAILED! ***********************".format(cmd['name']))
return 1
logger.info("************** END {} ***********************".format(cmd['name']))
return 0
def exec_shell_cmd(cmd):
"cmd: shell command to execute"
result = None
try:
logger.info("EXEC CMD: {}".format(cmd))
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True, executable='/bin/bash')
logger.info(result.stdout.decode())
return result.returncode
except subprocess.CalledProcessError as exc:
logger.error("\n\n********** EXEC FAIL! {}*********".format(exc.stdout.decode()))
return (exc.returncode)
def repl():
while True:
logger.info("\n********** START CMD: version={}***************\n".format(version.number))
pretty_print(get_CMD_MAP())
print("\nENTER A CMD_KEY:")
cmd_key = input()
if cmd_key.lower() == 'exit':
break
if cmd_key not in list(get_CMD_MAP().keys()):
logger.error('Invalid CMD_KEY={}! Try again.'.format(cmd_key))
elif cmd_key == 's3_download':
print("ENTER source_file:")
source_file = input()
cmd = s3_download(source_file)
exec_shell_cmd(cmd['key'])
elif cmd_key == 's3_upload':
print("ENTER source_tar_file:")
source_tar_file = input()
cmd = s3_upload(source_tar_file)
exec_shell_cmd(cmd['key'])
elif cmd_key == 'backup_script':
print("ENTER cleanup (true/false):")
cleanup = input()
cmd = backup_script(cleanup)
exec_shell_cmd(cmd['key'])
elif cmd_key in ['config_node_without_signctrl_NEW', 'config_node_without_signctrl_CUR', 'restart_node_without_signctrl_NEW', 'restart_node_without_signctrl_CUR']:
print("The above cmd is dangerous. It may cause double signing. Are you sure, you want to continue? (yes/no):")
yes_or_no = input()
if yes_or_no == 'yes':
exec_shell_recursive_cmd(cmd_key)
else:
print('abort cmd!')
elif cmd_key == 'delete_repo_file':
print("ENTER file_name:")
file_name = input()
cmd = delete_repo_file(file_name)
exec_shell_cmd(cmd['key'])
else:
exec_shell_recursive_cmd(cmd_key)
def send_msg_to_telegram(msg):
try:
requestURL = "https://api.telegram.org/bot" + str(config.telegram_token) + "/sendMessage?chat_id=" + config.telegram_chat_id + "&text="
requestURL = requestURL + str(msg)
requests.get(requestURL, timeout=1)
except Exception:
error_msg = traceback.format_exc()
logger.error(error_msg)
if __name__ == "__main__":
nr_args = len(sys.argv)
if nr_args == 1:
repl()
elif nr_args == 2:
cmd_key = sys.argv[1]
if cmd_key not in list(get_CMD_MAP().keys()):
logger.error('Invalid cmd_key={}! Try again.'.format(cmd_key))
else:
res = exec_shell_recursive_cmd(cmd_key)
msg = "{}: {}".format(cmd_key.upper(), 'PASS' if res == 0 else 'FAIL')
send_msg_to_telegram(msg)
else:
logger.error('Too many arguments!')