forked from AschPlatform/asch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
aschd
executable file
·244 lines (213 loc) · 5.71 KB
/
aschd
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
#!/bin/bash
readonly PROG_DIR=$(pwd)
readonly PID_FILE=$PROG_DIR/asch.pid
readonly IPS=("https://www.asch.io/downloads-International" "http://125.32.110.138:9999/downloads-Chinese" "http://39.107.52.143-Chinese")
function read_port() {
echo `cat $PROG_DIR/config.json |grep '"port"'|head -n 1| awk -F "[:,]" '{print$2}'|tr -d ' '`
}
function is_running() {
test -f $PID_FILE && ps -p $(cat $PID_FILE) > /dev/null
}
function status() {
if is_running; then
echo "Asch server is running"
else
echo "Asch server is not running"
fi
}
function start() {
if is_running; then
echo "Asch server is already started"
else
rm -f $PROG_DIR/asch.pid
node $PROG_DIR/app.js --base $PROG_DIR --daemon $@
fi
}
function stop() {
local pid
if test -f $PID_FILE; then
pid=$(cat $PID_FILE)
fi
if [ -n "$pid" ] && ps -p "$pid" > /dev/null; then
kill $pid
sleep 1
i=1
while ps -p $pid > /dev/null; do
if [ $i == 5 ]; then
kill -9 $pid
echo "Asch server killed"
fi
echo "Still waiting for asch server to stop ..."
sleep 1
((i++))
done
echo "Asch server stopped"
else
echo "Asch server is not running"
fi
rm -f $PID_FILE
}
function restart() {
stop
start
}
function ismainnet(){
magic=$(cat $PROG_DIR/config.json | grep magic | awk -F: '{print $2}' | cut -d \" -f2)
net="testnet"
echo "net is $net"
}
function chosenode(){
read -p "Please input your choice [default 0] : " num
num=${num:-0}
if [ $num -ge $length ];then
echo "You chosen wrong number,please run aschd again and chose the other node!"
exit 1
fi
if [ i${status[$num]} = i"N" ];then
echo "This node has no data,please run aschd again and chose the other node!"
exit 2
fi
ip=$(echo ${IPS[$num]} | awk -F- '{print $1}')
var=$(curl -sL -m 10 "$ip/$meta_file")
Name=$(echo $var| awk '{print $1}')
Md5=$(echo $var| awk '{print $2}')
}
function rebuild() {
ismainnet
meta_file=metadata_rebuild_$net.txt
echo "Please select one of the following sources to use"
length=${#IPS[@]}
for i in $(seq 0 $[ $length-1 ])
do
# echo ${IPS[$i]}
ip=$(echo ${IPS[$i]} | awk -F- '{print $1}')
country=$(echo ${IPS[$i]} | awk -F- '{print $2}')
memo="$country users recommended"
cnt=1
var=$(curl -sL -m 10 $ip/$meta_file)
if [ $? -eq 0 ];then
cnt=$(echo $var | grep html | wc -l)
fi
if [ $cnt -eq 0 ];then
status[$i]="Y"
Name=$(echo $var| awk '{print $1}')
Md5=$(echo $var| awk '{print $2}')
Date=$(echo $var| awk '{print $3}')
Height=$(echo $var| awk '{print $4}')
Size=$(echo $var| awk '{print $5}')
DB=$(echo $var| awk '{print $6}')
echo $i. $ip [$memo] "Height:"$Height "Date:"$Date "Size:"$Size
else
status[$i]="N"
memo="No data,do not use."
echo $i. $ip [$memo]
fi
done
chosenode
echo "Downloading blockchain snapshot $Name..."
/bin/rm $Name
wget "$ip/$Name"
newMd5=$(md5sum $Name | awk '{print $1}')
if [ i$newMd5 = i$Md5 ];then
echo "Check md5 passed!"
else
echo "Check md5 failed,please run again!"
exit
fi
./aschd stop
tar zxf $Name
rm $Name
mv ./$DB ./blockchain.db
./aschd start
}
function version() {
node $PROG_DIR/app.js --version
}
function check_os() {
os_num=`cat /etc/os-release | grep '\"Ubuntu\"' | wc -l`
if [ $os_num -ne 1 ];then
echo "Linux is not Ubuntu, please configure manually!" && exit 1
fi
}
function configure() {
check_os
sudo bash $PROG_DIR/init/install_deps.sh
sudo bash $PROG_DIR/init/config_ntp.sh
sudo bash $PROG_DIR/init/config_monitor.sh
}
function upgrade() {
ismainnet
meta_file=metadata_upgrade_$net.txt
echo "Please select one of the following sources to use"
length=${#IPS[@]}
for i in $(seq 0 $[ $length-1 ])
do
# echo ${IPS[$i]}
ip=$(echo ${IPS[$i]} | awk -F- '{print $1}')
country=$(echo ${IPS[$i]} | awk -F- '{print $2}')
memo="$country users recommended"
#echo $ip $net
cnt=1
var=$(curl -sL -m 10 "$ip/$meta_file")
if [ $? -eq 0 ];then
cnt=$(echo $var | grep html | wc -l)
fi
if [ $cnt -eq 0 ];then
status[$i]="Y"
Name=$(echo $var| awk '{print $1}')
Md5=$(echo $var| awk '{print $2}')
Date=$(echo $var| awk '{print $3}')
Version=$(echo $var| awk '{print $4}')
Size=$(echo $var| awk '{print $5}')
echo $i. $ip [$memo] "Version:"$Version "Date:"$Date "Size:"$Size
else
status[$i]="N"
memo="No data,do not use."
echo $i. $ip [$memo]
fi
done
# echo "status",${status[*]}
chosenode
mkdir -p tmp
wget "$ip/$Name" -O tmp/$Name
newMd5=$(md5sum tmp/$Name | awk '{print $1}')
if [ i$newMd5 = i$Md5 ];then
echo "Check md5 passed!"
else
echo "Check md5 failed,please run again!"
exit
fi
echo "Extracting new package ..."
tar zxf tmp/$Name -C tmp/
extractedDir=`find tmp -maxdepth 1 -type d | grep asch |head -n 1`
currentVersion=`./aschd version`
chmod u+x $extractedDir/aschd
./aschd stop
echo "Copying new files ..."
for file in `ls $extractedDir`
do
if [ "$file" != "config.json" -a "$file" != "blockchain.db" ]; then
echo "copy $extractedDir/$file ..."
cp -r $extractedDir/$file ./
fi
done
rm -rf $extractedDir tmp/$latestName
newVersion=`./aschd version`
echo "Upgrade to $newVersion done."
./aschd start
}
function enable() {
local secret="$@"
local port=`read_port`
curl -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$secret"'"}' localhost:$port/api/delegates/forging/enable
}
function main() {
export PATH=$PROG_DIR/bin:$PATH
local cmdType=`type -t $1`
if [ $cmdType == "function" ]; then
eval $@
else
echo "Command not supported"
fi
}
main $@