-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.sh
executable file
·275 lines (247 loc) · 6.49 KB
/
deploy.sh
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
#!/bin/bash
# Function: deploy bumo program on multual-trust node like bm01-bm03
workdir=/root/dpos
build_dir=$workdir/bumo
bumo=$workdir/bchain
keypair_prog=$workdir/model/dpos_test.py
hosts="bm01 bm02 bm03 bm04 bm05 bm06 bm07 bm08 bm09 bm10 bm11 bm12 bm13 bm14 bm15"
instance_per_node=2 # max number is 20, listen port from 36 01 1 - 36 20 1
start_idx=1
init_validator_num=4
dposd=$workdir/dposd
function usage
{
cat << EOF
Usage:
Options:
-h show help message
-c command mode
deploy: Deploy bumo to all nodes
mod: Modify and update configuration
init: Re-init and start bumo program
clean: Clean test environment
stop: stop all bumo instance
start: start all bumo instance
clean: clean all log and coredump files
drop: drop db for bumo instance
remove: remove bumo instance
-d bumo install directory, new instance will copy from here
-i bumo instance per node
-l node list to deploy
-g keypairs generation program, dpos_test.py
-b bumo build directory, binary file bin/bumo will update from here
Prepare:
Build ssh mutual-trust for all nodes and then download and compile bumo program.
Getting Started:
1. $0 -c deploy -d /root/kk/bchain -i 5 -l "bm01 bm02 bm03"
2. $0 -c mod -d /root/kk/bchain -i 5 -l "bm01 bm02 bm03" -g "/root/kk/dpos_test.py"
3. $0 -c init -d /root/kk/bchain -i 5 -l "bm01 bm02 bm03" -b "/root/kk/bumo-develop"
EOF
exit 0
}
# Prepare bumo program
function deploy_prog
{
for h in $hosts; do
for i in `seq -f %02g ${start_idx} $instance_per_node`; do
ssh ${h} "test -d ${bumo}${i}"
if [[ $? -ne 0 ]]; then
scp -r $bumo ${h}:${bumo}${i}
# update init scripts
cp $bumo/scripts/bumo /tmp/bumo
sed -i "/install_dir=/c install_dir=${bumo}${i}" /tmp/bumo
sed -i "/script_dir=/c script_dir=${bumo}${i}/scripts" /tmp/bumo
scp /tmp/bumo ${h}:${bumo}${i}/scripts/bumo
cp $bumo/scripts/bumod /tmp/bumod
sed -i "/install_dir=/c install_dir=${bumo}${i}" /tmp/bumod
scp /tmp/bumod ${h}:${bumo}${i}/scripts/bumod
else
scp $bumo/bin/bumo ${h}:${bumo}${i}/bin/bumo
fi
done
done
}
# Modify bumo.json
function modify_cfg
{
# generate keypairs for validator address
if [[ ! -f ${keypair_prog} ]]; then
echo "No such file $keypair_prog"
exit 1
fi
init_validators=""
seed_host=""
let cnt=0
[ -f ./keypairs ] && rm -f ./keypairs
for h in $hosts; do
tmp_keypairs="/tmp/keypairs-${h}"
if [[ ! -f /tmp/keypairs-${h} ]]; then
python $keypair_prog -u seed1.bumo.io:16002 -c genKeyPairs -n $instance_per_node
mv ./keypairs /tmp/keypairs-${h}
fi
if [[ -z "$seed_host" ]]; then
head -n 1 /tmp/keypairs-${h} > /tmp/init_validator
seed_host="\"${h}:36011\""
else
head -n 1 /tmp/keypairs-${h} >> /tmp/init_validator
seed_host="${seed_host}, \"${h}:36011\""
fi
let cnt+=1
done
init_validators=$(awk '{print $6}' /tmp/init_validator | head -n $init_validator_num | tr -d "}" | tr "\n" ",")
init_validators="${init_validators%,}"
for h in $hosts; do
tmp_keypairs="/tmp/keypairs-${h}"
for i in `seq -f %02g 1 $instance_per_node`; do
tmp_cfg="/tmp/bumo-${h}-${i}.json"
cp $bumo/config/bumo.json $tmp_cfg
listen_port="36${i}1"
web_addr="0.0.0.0:36${i}2"
ws_addr="0.0.0.0:36${i}3"
sed -i "/\"listen_port\":/c \"listen_port\": ${listen_port}," $tmp_cfg
sed -i "/\"listen_addresses\":/c \"listen_addresses\": \"${web_addr}\"," $tmp_cfg
sed -i "/\"listen_address\":/c \"listen_address\": \"${ws_addr}\"," $tmp_cfg
#peers=$(seq -f "\"${seed_host}:36%02g1\"" 1 $instance_per_node | tr "\n" ",")
sed -i "/\"known_peers\":/c \"known_peers\": [${seed_host}]" $tmp_cfg
addr=$(sed -n "${i}p" $tmp_keypairs |awk -F: '{print $4}'|cut -d\" -f2)
aes_priv=$(sed -n "${i}p" $tmp_keypairs |awk -F: '{print $3}'|cut -d\" -f2)
sed -i "/\"validation_address\":/c \"validation_address\": \"${addr}\"," $tmp_cfg
sed -i "/\"validation_private_key\":/c \"validation_private_key\": \"${aes_priv}\"," $tmp_cfg
sed -i "/\"validators\":/c \"validators\": [${init_validators}]" $tmp_cfg
scp $tmp_cfg ${h}:${bumo}${i}/config/bumo.json
done
done
}
# Re-deploy and then restart bumo program
function kill_prog
{
# Stop
for h in $hosts; do
for i in `seq -f %02g 1 $instance_per_node`; do
ssh $h "bash ${bumo}${i}/scripts/bumod stop"
[ $? -ne 0 ] && echo "Stop failed" || echo "Stopped"
done
sleep 2
ssh $h "ps aux |grep -v grep |grep bumo &>/dev/null"
if [ $? -eq 0 ]; then
bumod_pids=$(ssh $h "pidof bumod")
ssh $h "kill -9 $bumod_pids"
bumo_pids=$(ssh $h "pidof bumo")
ssh $h "kill -9 $bumo_pids"
fi
done
}
function init
{
# Stop
stop_prog
# Update bumo if the program has been modified
if [ -f "$build_dir/bin/bumo" ]; then
cp $build_dir/bin/bumo $bumo/bin/bumo
else
echo "No build directory found, use $bumo/bin/bumo"
fi
for h in $hosts; do
for i in `seq -f %02g 1 $instance_per_node`; do
ssh $h "${bumo}${i}/bin/bumo --dropdb"
scp $bumo/bin/bumo ${h}:${bumo}${i}/bin/bumo
done
done
# Start
sleep 2
start_prog
}
function start_prog
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd start $instance_per_node"
done
}
function stop_prog
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd stop $instance_per_node"
done
}
function remove
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd remove $instance_per_node"
done
}
function clean
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd clean $instance_per_node"
done
}
function drop
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd drop $instance_per_node"
done
}
function status_prog
{
for h in $hosts; do
echo "+++ $h +++"
ssh $h "bash $dposd status $instance_per_node"
done
}
cmds=""
while getopts 'hc:b:d:i:l:g:' arg ; do
case $arg in
h)
usage
;;
c)
cmds="$OPTARG"
;;
b)
build_dir=$OPTARG
;;
d)
bumo=$OPTARG
;;
i)
instance_per_node=$OPTARG
;;
l)
hosts="$OPTARG"
;;
g)
keypair_prog=$OPTARG
;;
?)
echo "Unknown options" && exit 1
;;
esac
done
for c in $cmds; do
if [[ $c == "deploy" ]]; then
deploy_prog
elif [[ $c == "mod" ]]; then
modify_cfg
elif [[ $c == "init" ]]; then
init
elif [[ $c == "remove" ]]; then
remove
elif [[ $c == "clean" ]]; then
clean
elif [[ $c == "drop" ]]; then
drop
elif [[ $c == "stop" ]]; then
stop_prog
elif [[ $c == "start" ]]; then
start_prog
elif [[ $c == "status" ]]; then
status_prog
else
echo "Unknown command $c"
fi
done