-
Notifications
You must be signed in to change notification settings - Fork 1
/
nearcore-updater.py
95 lines (74 loc) · 2.89 KB
/
nearcore-updater.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
#!/usr/bin/python3
import sys
import os
import time
import datetime
logFileName = 'nearcore-updater.log';
enableLog = True
def getFullPath(filename):
return os.path.dirname(os.path.realpath(__file__)) + '/' + filename
def getNowDateString():
now = datetime.datetime.now()
return f'{now.day}.{now.month}.{now.year} {now.hour}:{now.minute}:{now.second}:{now.microsecond}'
def logInFile(text):
if enableLog == False:
return
open(getFullPath(logFileName), 'a')
with open(getFullPath(logFileName), 'a') as file:
file.write(f'[{getNowDateString()}] {text}\r\n')
def printAndLog(text):
print(text)
logInFile(text)
def plexit():
printAndLog('exit()\r\n\r\n')
exit()
if len(sys.argv) < 2:
print('Missing argument <NETWORK>')
exit();
if len(sys.argv) < 3:
print('Missing argument <NEARCORE_DIR>')
exit();
if len(sys.argv) >= 4:
enableLog = bool(sys.argv[3])
# add nearup PATH
os.environ["PATH"] = f'{os.environ["HOME"]}/.local/bin:{os.environ["PATH"]}'
# add cargo PATH
os.environ["PATH"] = f'{os.environ["HOME"]}/.cargo/bin:{os.environ["PATH"]}'
network = sys.argv[1]
nearcoreDir = sys.argv[2]
printAndLog("Checking for updates")
currentVersion = os.popen(f'curl -s https://rpc.{network}.near.org/status | jq .version.version').read()
myVersion = os.popen('curl -s http://127.0.0.1:3030/status | jq .version.version').read()
if len(currentVersion):
currentVersion = currentVersion[:-1]
if len(myVersion):
myVersion = myVersion[:-1]
if currentVersion == myVersion:
printAndLog(f'Versions are equal (rpc {currentVersion}, local {myVersion})')
plexit()
printAndLog(f'Start update to {currentVersion}')
os.system(f'rm -rf {nearcoreDir}.backup');
os.system(f'mv {nearcoreDir} {nearcoreDir}.backup');
os.system(f'git clone -b {currentVersion} https://github.com/nearprotocol/nearcore.git {nearcoreDir}');
makeReleaseExitCode = os.system(f'cd {nearcoreDir} && make release');
if makeReleaseExitCode != 0:
printAndLog('Build failed')
plexit()
os.system('nearup stop');
os.system(f'nearup run localnet --binary-path {nearcoreDir}/target/release');
printAndLog('Run tests')
rpcVersion = os.popen(f'curl -s https://rpc.{network}.near.org/status | jq .version').read()
for count in range(4):
localVersion = os.popen(f'curl -s http://127.0.0.1:303{count}/status | jq .version').read()
if rpcVersion == localVersion:
printAndLog(f"Node {count} works")
else:
printAndLog(f"Node {count} don't works. Test failed.")
os.system(f'mv {nearcoreDir}.backup {nearcoreDir}')
os.system('nearup stop')
os.system(f'nearup run {network} --binary-path {nearcoreDir}/target/release/')
plexit()
printAndLog("Tests completed")
os.system('nearup stop')
os.system(f'nearup run {network} --binary-path {nearcoreDir}/target/release/')
printAndLog(f"Node updated. Current version {currentVersion} ({network})\r\n\r\n")