-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtools.sh
executable file
·541 lines (471 loc) · 16.3 KB
/
tools.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
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
#!/bin/bash
source /opt/crust/crust-node/scripts/utils.sh
tools_help()
{
cat << EOF
Crust tools usage:
help show help information
space-info show information about data folders
rotate-keys generate session key of chain node
upgrade-image {chain|api|smanager|ipfs|c-gen|sw} upgrade one docker image
sworker-ab-upgrade {code} sworker AB upgrade
workload show workload information
file-info {all|valid|lost|pending|{cid}} {output-file} show file information
delete-file {cid} delete one file
change-srd {number} change sworker's srd capacity(GB), for example: 'change-srd 100', 'change-srd -50'
ipfs {...} ipfs command, for example 'ipfs pin ls', 'ipfs swarm peers'
watch-chain generate watch chain node docker-compose file and show help
set-sworker-debug {true|false} set sworker debug
EOF
}
space_info()
{
local data_folder_info=(`df -h /opt/crust/data | sed -n '2p'`)
cat << EOF
>>>>>> Base data folder <<<<<<
Path: /opt/crust/data
File system: ${data_folder_info[0]}
Total space: ${data_folder_info[1]}
Used space: ${data_folder_info[2]}
Avail space: ${data_folder_info[3]}
EOF
local has_disks=false
for i in $(seq 1 128); do
local disk_folder_info=(`df -h /opt/crust/disks/${i} | sed -n '2p'`)
if [ x"${disk_folder_info[0]}" != x"${data_folder_info[0]}" ]; then
printf "\n>>>>>> Storage folder ${i} <<<<<<\n"
printf "Path: /opt/crust/disks/${i}\n"
printf "File system: ${disk_folder_info[0]}\n"
printf "Total space: ${disk_folder_info[1]}\n"
printf "Used space: ${disk_folder_info[2]}\n"
printf "Avail space: ${disk_folder_info[3]}\n"
has_disks=true
fi
done
if [ "$has_disks" == false ]; then
log_err "Please mount the hard disk to storage folders, paths is from: /opt/crust/disks/1 ~ /opt/crust/disks/128"
return 1
fi
cat << EOF
PS:
1. Base data folder is used to store chain and db, 2TB SSD is recommended, you can mount SSD on /opt/crust/data
2. Please mount the hard disk to storage folders, paths is from: /opt/crust/disks/1 ~ /opt/crust/disks/128
3. SRD will not use all the space, it will reserve 50G of space
EOF
}
rotate_keys()
{
check_docker_status crust
if [ $? -ne 0 ]; then
log_info "Service chain is not started or exited now"
return 0
fi
local res=`curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys", "params":[]}' http://localhost:19933 2>/dev/null`
session_key=`echo $res | jq .result`
if [ x"$session_key" = x"" ]; then
log_err "Generate session key failed"
return 1
fi
echo $session_key
}
change_srd()
{
if [ x"$1" == x"" ] || [[ ! $1 =~ ^[1-9][0-9]*$|^[-][1-9][0-9]*$|^0$ ]]; then
log_err "The input of srd change must be integer number"
tools_help
return 1
fi
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_info "Service crust sworker is not started or exited now"
return 0
fi
if [ ! -f "$builddir/sworker/sworker_config.json" ]; then
log_err "No sworker configuration file"
return 1
fi
local base_url=`cat $builddir/sworker/sworker_config.json | jq .base_url`
base_url=${base_url%?}
base_url=${base_url:1}
curl -XPOST ''$base_url'/srd/change' -H 'backup: '$backup'' --data-raw '{"change" : '$1'}'
}
workload()
{
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_info "Service crust sworker is not started or exited now"
return 0
fi
local base_url=`cat $builddir/sworker/sworker_config.json | jq .base_url`
base_url=${base_url%?}
base_url=${base_url:1}
curl $base_url/workload
}
file_info()
{
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_info "Service crust sworker is not started or exited now"
return 0
fi
local base_url=`cat $builddir/sworker/sworker_config.json | jq .base_url`
base_url=${base_url%?}
base_url=${base_url:1}
if [ x"$1" == x"" ]; then
tools_help
return 1
fi
local output=""
if [ x"$2" != x"" ]; then
output="--output $2"
fi
if [ ${#1} -eq 46 ];then
curl -X GET ''$base_url'/file/info?cid='$1'' $output
return $?
fi
if [ x"$1" != x"all" ] && [ x"$1" != x"valid" ] && [ x"$1" != x"lost" ] && [ x"$1" != x"pending" ]; then
tools_help
return 1
fi
curl -X GET ''$base_url'/file/info_by_type?type='$1'' $output
return $?
}
delete_file()
{
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_info "Service crust sworker is not started or exited now"
return 0
fi
local base_url=`cat $builddir/sworker/sworker_config.json | jq .base_url`
base_url=${base_url%?}
base_url=${base_url:1}
curl --request POST ''$base_url'/storage/delete' --header 'Content-Type: application/json' --data-raw '{"cid":"'$1'"}'
}
set_sworker_debug()
{
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_info "Service crust sworker is not started or exited now"
return 0
fi
if [ x"$1" != x"true" ] && [ x"$1" != x"false" ]; then
tools_help
return 1
fi
local base_url=`cat $builddir/sworker/sworker_config.json | jq .base_url`
base_url=${base_url%?}
base_url=${base_url:1}
curl --request POST ''$base_url'/debug' --header 'Content-Type: application/json' --data-raw '{"debug":'$1'}'
}
upgrade_image()
{
if [ x"$1" == x"chain" ]; then
upgrade_docker_image crust $2
if [ $? -ne 0 ]; then
return 1
fi
elif [ x"$1" == x"api" ]; then
upgrade_docker_image crust-api $2
if [ $? -ne 0 ]; then
return 1
fi
elif [ x"$1" == x"smanager" ]; then
upgrade_docker_image crust-smanager $2
if [ $? -ne 0 ]; then
return 1
fi
elif [ x"$1" == x"ipfs" ]; then
upgrade_docker_image go-ipfs $2
if [ $? -ne 0 ]; then
return 1
fi
elif [ x"$1" == x"c-gen" ]; then
upgrade_docker_image config-generator $2
if [ $? -ne 0 ]; then
return 1
fi
elif [ x"$1" == x"sw" ]; then
upgrade_docker_image crust-sworker $2
if [ $? -ne 0 ]; then
return 1
fi
else
tools_help
fi
}
ipfs_cmd()
{
check_docker_status ipfs
if [ $? -ne 0 ]; then
log_info "Service ipfs is not started or exited now"
return 0
fi
docker exec -i ipfs ipfs $@
}
sworker_ab_upgrade()
{
# Check input
if [ x"$1" == x"" ]; then
log_err "Please give sWorker code."
return 1
fi
if [ ${#1} -ne 64 ];then
log_err "Please give right sWorker code."
return 1
fi
local code=$1
# Check sworker
local a_or_b=`cat $basedir/etc/sWorker.ab`
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_err "Service crust sWorker is not started or exited now"
return 1
fi
log_info "Start sworker A/B upgragde...."
# Get configurations
local config_file=$builddir/sworker/sworker_config.json
if [ x"$config_file" = x"" ]; then
log_err "please give right config file"
return 1
fi
api_base_url=`cat $config_file | jq .chain.base_url`
sworker_base_url=`cat $config_file | jq .base_url`
if [ x"$api_base_url" = x"" ] || [ x"$sworker_base_url" = x"" ]; then
log_err "please give right config file"
return 1
fi
api_base_url=`echo "$api_base_url" | sed -e 's/^"//' -e 's/"$//'`
sworker_base_url=`echo "$sworker_base_url" | sed -e 's/^"//' -e 's/"$//'`
log_info "Read configurations success."
if [ x"$2" != x"--offline" ]; then
# Check chain
while :
do
system_health=`curl --max-time 30 $api_base_url/system/health 2>/dev/null`
if [ x"$system_health" = x"" ]; then
log_err "Service crust chain or api is not started or exited now"
return 1
fi
is_syncing=`echo $system_health | jq .isSyncing`
if [ x"$is_syncing" = x"" ]; then
log_err "Service crust api dose not connet to crust chain"
return 1
fi
if [ x"$is_syncing" = x"true" ]; then
printf "\n"
for i in $(seq 1 60); do
printf "Crust chain is syncing, please wait 60s, now is %s\r" "${i}s"
sleep 1
done
continue
fi
break
done
fi
# Get id_info from sworker
local id_info=`curl --max-time 30 $sworker_base_url/enclave/id_info 2>/dev/null`
if [ x"$id_info" = x"" ]; then
log_err "Please check sworker logs to find more information"
return 1
fi
# Check attestation mode compatibility
local running_attestation_mode=`echo $id_info | jq .attestation_mode`
if [ x"$running_attestation_mode" = x"" ] || [ x"$running_attestation_mode" = x"null" ]; then
running_attestation_mode="epid"
fi
local config_attestation_mode=$(getSGXAttestationMode)
if [ x"$running_attestation_mode" != x"$config_attestation_mode" ]; then
log_err "Cannot run sworker-ab-upgrade between EPID mode and ECDSA mode, because SGX OOT driver and DCAP driver cannot run simultaneously."
log_err "Please check your sWorker.attestation_mode configuration."
return 1
fi
# Get code from sworker
local mrenclave=`echo $id_info | jq .mrenclave`
if [ x"$mrenclave" = x"" ] || [ ! ${#mrenclave} -eq 66 ]; then
log_err "Please check sworker logs to find more information"
return 1
fi
mrenclave=`echo ${mrenclave: 1: 64}`
log_info "sWorker self code: $mrenclave"
if [ x"$mrenclave" == x"$code" ]; then
log_success "sWorker is already latest"
while :
do
check_docker_status crust-sworker-a
local resa=$?
check_docker_status crust-sworker-b
local resb=$?
if [ $resa -eq 0 ] && [ $resb -eq 0 ] ; then
sleep 10
continue
fi
break
done
check_docker_status crust-sworker-a
if [ $? -eq 0 ]; then
local aimage=(`docker ps -a | grep 'crust-sworker-a'`)
aimage=${aimage[1]}
if [ x"$aimage" != x"crustio/crust-sworker:latest" ]; then
docker tag $aimage crustio/crust-sworker:latest
fi
fi
check_docker_status crust-sworker-b
if [ $? -eq 0 ]; then
local bimage=(`docker ps -a | grep 'crust-sworker-b'`)
bimage=${bimage[1]}
if [ x"$bimage" != x"crustio/crust-sworker:latest" ]; then
docker tag $bimage crustio/crust-sworker:latest
fi
fi
return 0
fi
# Upgrade sworker images
local old_image=(`docker images | grep '^\b'crustio/crust-sworker'\b ' | grep 'latest'`)
old_image=${old_image[2]}
local region=`cat $basedir/etc/region.conf`
local docker_org="crustio"
if [ x"$region" == x"cn" ]; then
docker_org=$aliyun_address/$docker_org
fi
local res=0
docker pull $docker_org/crust-sworker:$code
res=$(($?|$res))
docker tag $docker_org/crust-sworker:$code crustio/crust-sworker:latest
if [ $res -ne 0 ]; then
log_err "Download sworker docker image failed"
return 1
fi
local new_image=(`docker images | grep '^\b'crustio/crust-sworker'\b ' | grep 'latest'`)
new_image=${new_image[2]}
if [ x"$old_image" = x"$new_image" ]; then
log_info "The current sworker docker image is already the latest"
return 1
fi
# Start A/B
if [ x"$a_or_b" = x"a" ]; then
a_or_b='b'
else
a_or_b='a'
fi
check_docker_status crust-sworker-a
local resa=$?
check_docker_status crust-sworker-b
local resb=$?
if [ $resa -eq 0 ] && [ $resb -eq 0 ] ; then
log_info "sWorker A/B upgrade is already in progress"
else
# The new sworker should run under the same attestion mode as the old sworker during ab-upgrade
local ecdsa_option=""
if [ x"$running_attestation_mode" = x"ecdsa" ] ; then
ecdsa_option="--ecdsa"
fi
docker stop crust-sworker-$a_or_b &>/dev/null
docker rm crust-sworker-$a_or_b &>/dev/null
shift
EX_SWORKER_ARGS="--upgrade $ecdsa_option $@" docker-compose -f $composeyaml up -d crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
log_err "Setup new sWorker failed"
docker tag $old_image crustio/crust-sworker:latest
return 1
fi
fi
# Change back to older image
docker tag $old_image crustio/crust-sworker:latest
log_info "Please do not close this program and wait patiently, ."
log_info "If you need more information, please use other terminal to execute 'sudo crust logs sworker-a' and 'sudo crust logs sworker-b'"
# Check A/B status
local acc=0
while :
do
printf "Sworker is upgrading, please do not close this program. Wait %s\r" "${acc}s"
((acc++))
sleep 1
# Get code from sworker
local id_info=`curl --max-time 30 $sworker_base_url/enclave/id_info 2>/dev/null`
if [ x"$id_info" != x"" ]; then
local mrenclave=`echo $id_info | jq .mrenclave`
if [ x"$mrenclave" != x"" ]; then
mrenclave=`echo ${mrenclave: 1: 64}`
if [ x"$mrenclave" == x"$code" ]; then
break
fi
fi
fi
# Check upgrade sworker status
check_docker_status crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
printf "\n"
log_err "Sworker update failed, please use 'sudo crust logs sworker-a' and 'sudo crust logs sworker-b' to find more details"
return 1
fi
done
# Set new information
docker tag $new_image crustio/crust-sworker:latest
if [ x"$a_or_b" = x"a" ]; then
sed -i 's/b/a/g' $basedir/etc/sWorker.ab
else
sed -i 's/a/b/g' $basedir/etc/sWorker.ab
fi
printf "\n"
log_success "Sworker update success, setup new sworker 'crust-sworker-$a_or_b'"
}
watch_chain()
{
cp $basedir/etc/watch-chain.yaml watch-chain.yaml
cat << EOF
The 'watch-chain.yaml' file has been generated your current path, use docker-compose to start the watch chain node
PS:
1. Watch chain node can provide ws and rpc services, please open 30888, 19933 and 19944 ports
2. You can edit 'watch-chain.yaml' to customize your watch chain
3. The simplest startup example: 'sudo docker-compose -f watch-chain.yaml up -d'
4. With external connect chain configuration, a topology structure where one chain node serves multiple members can be realized
EOF
}
tools()
{
case "$1" in
space-info)
space_info
;;
change-srd)
change_srd $2
;;
rotate-keys)
rotate_keys
;;
workload)
workload
;;
file-info)
file_info $2
;;
delete-file)
delete_file $2
;;
set-sworker-debug)
set_sworker_debug $2
;;
upgrade-image)
upgrade_image $2 $3
;;
sworker-ab-upgrade)
shift
sworker_ab_upgrade $@
;;
watch-chain)
watch_chain
;;
ipfs)
shift
ipfs_cmd $@
;;
*)
tools_help
esac
}