forked from geopsllc/core-control
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sh
664 lines (460 loc) · 17.5 KB
/
functions.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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/bin/bash
wrong_arguments () {
echo -e "\nMissing: arg1 [arg2]\n"
echo -e " ------------------------------------------------------------------------------"
echo -e "| arg1 | arg2 | Description |"
echo -e " ------------------------------------------------------------------------------"
echo -e "| install | core | Install Core |"
echo -e "| update | core / self / check | Update Core / Core-Control / Check |"
echo -e "| remove | core / self | Remove Core / Core-Control |"
echo -e "| secret | set / clear | Delegate Secret Set / Clear |"
echo -e "| start | relay / forger / all | Start Core Services |"
echo -e "| restart | relay / forger / all / safe | Restart Core Services |"
echo -e "| stop | relay / forger / all | Stop Core Services |"
echo -e "| status | relay / forger / all | Show Core Services Status |"
echo -e "| logs | relay / forger / all | Show Core Logs |"
echo -e "| snapshot | create / restore | Snapshot Create / Restore |"
echo -e "| system | info / update | System Info / Update |"
echo -e "| config | reset | Reset Config Files to Defaults |"
echo -e "| database | clear | Clear the Database |"
echo -e "| rollback | | Rollback to Specified Height |"
echo -e "| plugin | list / add / remove / update | Manage Core Plugins |"
echo -e " ------------------------------------------------------------------------------\n"
exit 1
}
pm2status () {
echo $(pm2 describe $1 2>/dev/null | grep "status")
}
git_check () {
git fetch > /dev/null 2>&1
loc=$(git rev-parse --short @)
rem=$(git rev-parse --short @{u})
if [ "$loc" = "$rem" ]; then
up2date="yes"
else
up2date="no"
fi
}
npm_check () {
if [ "$npmver" = "$corever" ]; then
up2date="yes"
else
up2date="no"
fi
}
setefile () {
local envFile="$config/.env"
if [ -f $envFile ]; then
rm $envFile
fi
touch "$envFile"
echo "CORE_LOG_LEVEL=$log_level" >> "$envFile" 2>&1
echo "CORE_LOG_LEVEL_FILE=$log_level" >> "$envFile" 2>&1
echo "CORE_DB_HOST=localhost" >> "$envFile" 2>&1
echo "CORE_DB_PORT=5432" >> "$envFile" 2>&1
echo "CORE_DB_USERNAME=$USER" >> "$envFile" 2>&1
echo "CORE_DB_PASSWORD=password" >> "$envFile" 2>&1
echo "CORE_DB_DATABASE=${name}_$network" >> "$envFile" 2>&1
echo "CORE_P2P_HOST=0.0.0.0" >> "$envFile" 2>&1
echo "CORE_P2P_PORT=$p2p_port" >> "$envFile" 2>&1
echo "CORE_API_HOST=0.0.0.0" >> "$envFile" 2>&1
echo "CORE_API_PORT=$api_port" >> "$envFile" 2>&1
echo "CORE_WEBHOOKS_HOST=0.0.0.0" >> "$envFile" 2>&1
echo "CORE_WEBHOOKS_PORT=$wh_port" >> "$envFile" 2>&1
echo "CORE_WALLET_API_HOST=0.0.0.0" >> "$envFile" 2>&1
echo "CORE_WALLET_API_PORT=$wapi_port" >> "$envFile" 2>&1
echo "CORE_EXCHANGE_JSON_RPC_HOST=0.0.0.0" >> "$envFile" 2>&1
echo "CORE_EXCHANGE_JSON_RPC_PORT=$rpc_port" >> "$envFile" 2>&1
}
start () {
local secrets=$(cat $config/delegates.json | jq -r '.secrets')
if [ "$1" = "all" ]; then
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [ "$rstatus" != "online" ]; then
pm2 --name "${name}-relay" start $core/core/bin/run -- relay:run --network $network --token $name > /dev/null 2>&1
else
echo -e "\n${red}Process relay already running. Skipping...${nc}"
fi
if [ "$secrets" = "[]" ]; then
echo -e "\n${red}Delegate secret is missing. Forger start aborted!${nc}"
elif [ "$fstatus" != "online" ]; then
pm2 --name "${name}-forger" start $core/core/bin/run -- forger:run --network $network --token $name > /dev/null 2>&1
else
echo -e "\n${red}Process forger already running. Skipping...${nc}"
fi
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [ "$rstatus" != "online" ]; then
echo -e "\n${red}Process startup failed.${nc}"
fi
else
local pstatus=$(pm2status "${name}-$1" | awk '{print $4}')
if [[ "$secrets" = "[]" && "$1" = "forger" ]]; then
echo -e "\n${red}Delegate secret is missing. Forger start aborted!${nc}"
elif [ "$pstatus" != "online" ]; then
pm2 --name "${name}-$1" start $core/core/bin/run -- ${1}:run --network $network --token $name > /dev/null 2>&1
else
echo -e "\n${red}Process $1 already running. Skipping...${nc}"
fi
local pstatus=$(pm2status "${name}-$1" | awk '{print $4}')
if [[ "$pstatus" != "online" && "$1" = "relay" ]]; then
echo -e "\n${red}Process startup failed.${nc}"
fi
fi
pm2 save > /dev/null 2>&1
}
restart () {
if [ "$1" = "all" ]; then
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [ "$rstatus" = "online" ]; then
pm2 restart ${name}-relay > /dev/null 2>&1
else
echo -e "\n${red}Process relay not running. Skipping...${nc}"
fi
if [ "$fstatus" = "online" ]; then
pm2 restart ${name}-forger > /dev/null 2>&1
else
echo -e "\n${red}Process forger not running. Skipping...${nc}"
fi
elif [ "$1" = "safe" ]; then
local api=$(curl -Is http://127.0.0.1:5001)
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [[ "$rstatus" != "online" || "$fstatus" != "online" ]]; then
echo -e "\n${red}Core forger is offline. Use '${cyan}ccontrol restart${red}' instead.${nc}\n"
exit 1
elif [ -z "$api" ]; then
echo -e "\n${red}Plugin round-monitor not active. Use '${cyan}ccontrol restart${red}' instead.${nc}\n"
exit 1
else
curl -X POST http://127.0.0.1:5001/restart > /dev/null 2>&1
echo -e "\n${green}Restart requested. Check logs to monitor progress.${nc}"
fi
else
local pstatus=$(pm2status "${name}-$1" | awk '{print $4}')
if [ "$pstatus" = "online" ]; then
pm2 restart ${name}-$1 > /dev/null 2>&1
else
echo -e "\n${red}Process $1 not running. Skipping...${nc}"
fi
fi
}
stop () {
if [ "$1" = "all" ]; then
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [ "$rstatus" = "online" ]; then
pm2 stop ${name}-relay > /dev/null 2>&1
else
echo -e "\n${red}Process relay not running. Skipping...${nc}"
fi
if [ "$fstatus" = "online" ]; then
pm2 stop ${name}-forger > /dev/null 2>&1
else
echo -e "\n${red}Process forger not running. Skipping...${nc}"
fi
else
local pstatus=$(pm2status "${name}-$1" | awk '{print $4}')
if [ "$pstatus" = "online" ]; then
pm2 stop ${name}-$1 > /dev/null 2>&1
else
echo -e "\n${red}Process $1 not running. Skipping...${nc}"
fi
fi
pm2 save > /dev/null 2>&1
}
status () {
echo -e -n "\n${cyan}${name}-core${nc} v${cyan}${corever}${nc} "
if [ "$1" = "all" ]; then
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [ "$rstatus" = "online" ]; then
echo -ne "relay: ${green}online${nc} "
else
echo -ne "relay: ${red}offline${nc} "
fi
if [ "$fstatus" = "online" ]; then
echo -e "forger: ${green}online${nc}\n"
else
echo -e "forger: ${red}offline${nc}\n"
fi
else
local pstatus=$(pm2status "${name}-$1" | awk '{print $4}')
if [ "$pstatus" = "online" ]; then
echo -e "$1: ${green}online${nc}\n"
else
echo -e "$1: ${red}offline${nc}\n"
fi
fi
}
install_deps () {
sudo timedatectl set-ntp no > /dev/null 2>&1
sudo apt install -y htop curl build-essential python git nodejs npm libpq-dev ntp gawk jq > /dev/null 2>&1
sudo npm install -g n grunt-cli pm2 yarn lerna > /dev/null 2>&1
sudo n 10 > /dev/null 2>&1
pm2 install pm2-logrotate > /dev/null 2>&1
local pm2startup="$(pm2 startup | tail -n1)"
eval $pm2startup > /dev/null 2>&1
pm2 save > /dev/null 2>&1
}
secure () {
sudo apt install -y ufw fail2ban > /dev/null 2>&1
sudo ufw allow 22/tcp > /dev/null 2>&1
sudo ufw allow ${p2p_port}/tcp > /dev/null 2>&1
sudo ufw allow ${api_port}/tcp > /dev/null 2>&1
sudo ufw allow ${wapi_port}/tcp > /dev/null 2>&1
sudo ufw --force enable > /dev/null 2>&1
sudo sed -i "/^PermitRootLogin/c PermitRootLogin prohibit-password" /etc/ssh/sshd_config > /dev/null 2>&1
sudo systemctl restart sshd.service > /dev/null 2>&1
}
install_db () {
sudo apt install -y postgresql postgresql-contrib > /dev/null 2>&1
sudo -u postgres psql -c "CREATE USER $USER WITH PASSWORD 'password' CREATEDB;" > /dev/null 2>&1
dropdb ${name}_$network > /dev/null 2>&1
createdb ${name}_$network > /dev/null 2>&1
}
install_core () {
if [ -d $HOME/.config ]; then
sudo chown -R $USER:$USER $HOME/.config > /dev/null 2>&1
else
mkdir $HOME/.config > /dev/null 2>&1
fi
yarn global add $repo/$package > /dev/null 2>&1
mkdir $data > /dev/null 2>&1
cp -rf "$core/core/bin/config/$network" "$data" > /dev/null 2>&1
setefile
echo 'export PATH=$(yarn global bin):$PATH' >> $HOME/.bashrc
}
update () {
yarn global add $repo/$package > /dev/null 2>&1
local api=$(curl -Is http://127.0.0.1:5001)
local added="$(cat $config/plugins.js | grep round-monitor)"
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
if [[ "$rstatus" = "online" && "$fstatus" = "online" && ! -z "$api" && ! -z "$added" ]]; then
curl -X POST http://127.0.0.1:5001/restart > /dev/null 2>&1
else
if [ "$rstatus" = "online" ]; then
pm2 restart ${name}-relay > /dev/null 2>&1
fi
if [ "$fstatus" = "online" ]; then
pm2 restart ${name}-forger > /dev/null 2>&1
fi
fi
}
remove () {
pm2 delete ${name}-forger > /dev/null 2>&1
pm2 delete ${name}-relay > /dev/null 2>&1
pm2 save > /dev/null 2>&1
yarn global remove $repo/core > /dev/null 2>&1
rm -rf $data > /dev/null 2>&1
rm -rf $HOME/.cache/${name}-core > /dev/null 2>&1
rm -rf $HOME/.local/share/${name}-core > /dev/null 2>&1
rm -rf $HOME/.local/state/${name}-core > /dev/null 2>&1
rm -rf /tmp/$USER/${name}-core > /dev/null 2>&1
dropdb ${name}_$network > /dev/null 2>&1
sudo ufw delete allow $p2p_port/tcp > /dev/null 2>&1
sudo ufw delete allow $api_port/tcp > /dev/null 2>&1
sudo ufw delete allow $wapi_port/tcp > /dev/null 2>&1
}
config_reset () {
stop all > /dev/null 2>&1
rm -rf $config > /dev/null 2>&1
cp -rf "$core/core/bin/config/$network" "$data" > /dev/null 2>&1
setefile
}
sysinfo () {
local sockets="$(lscpu | grep "Socket(s):" | head -n1 | awk '{ printf $2 }')"
local cps="$(lscpu | grep "Core(s) per socket:" | awk '{ printf $4 }')"
local tpc="$(lscpu | grep "Thread(s) per core:" | awk '{ printf $4 }')"
local os="$(lsb_release -d | awk '{ for (i=2;i<=NF;++i) printf $i " " }')"
local cpu="$(lscpu | grep "Model name" | awk '{ for (i=3;i<=NF;++i) printf $i " " }')"
local mhz="$(lscpu | grep "CPU MHz:" | awk '{ printf $3 }' | cut -f1 -d".")"
local maxmhz="$(lscpu | grep "CPU max MHz:" | awk '{ printf $4 }' | cut -f1 -d".")"
local hn="$(hostname --fqdn)"
local ips="$(hostname --all-ip-address)"
echo -e "\n${cyan}System: ${nc}$os"
w | head -n1
echo -e "\n${cyan}CPU(s): ${nc}${sockets}x ${cpu}with $cps Cores and $[cps*tpc] Threads"
echo -ne " ${cyan}Total: ${nc}$[sockets*cps] Cores and $[sockets*cps*tpc] Threads"
if [ -z "$maxmhz" ]; then
echo -e " @ ${mhz}MHz"
else
echo -e " @ ${maxmhz}MHz"
fi
echo -e "\n${cyan}Hostname: ${nc}$hn"
echo -e " ${cyan}IP(s): ${nc}$ips"
echo -e "${yellow}"
free -h
echo -e "${magenta}"
df -h /
echo -e "${nc}"
}
sysupdate () {
sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade -y
sudo apt-get autoremove -y
sudo apt-get autoclean -y
}
logs () {
if [ "$1" = "all" ]; then
pm2 logs
else
pm2 logs ${name}-$1
fi
}
secret () {
if [ "$1" = "set" ]; then
local scrt="$2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13}"
jq --arg scrt "$scrt" '.secrets = [$scrt]' $config/delegates.json > delegates.tmp
else
jq '.secrets = []' $config/delegates.json > delegates.tmp
fi
mv delegates.tmp $config/delegates.json
}
snapshot () {
if [ "$1" = "restore" ]; then
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
stop all > /dev/null 2>&1
dropdb ${name}_$network > /dev/null 2>&1
createdb ${name}_$network > /dev/null 2>&1
$core/core/bin/run snapshot:restore --network $network --token $name
if [ "$rstatus" = "online" ]; then
start relay > /dev/null 2>&1
fi
if [ "$fstatus" = "online" ]; then
start forger > /dev/null 2>&1
fi
else
$core/core/bin/run snapshot:dump --network $network --token $name
fi
}
selfremove () {
cd $HOME > /dev/null 2>&1
rm -rf $basedir > /dev/null 2>&1
sed -i '/ccontrol/d' $HOME/.bashrc > /dev/null 2>&1
sed -i '/cccomp/d' $HOME/.bashrc > /dev/null 2>&1
}
rollback () {
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
stop all > /dev/null 2>&1
$core/core/bin/run snapshot:rollback --height $1 --network $network --token $name
if [ "$rstatus" = "online" ]; then
start relay > /dev/null 2>&1
fi
if [ "$fstatus" = "online" ]; then
start forger > /dev/null 2>&1
fi
}
update_info () {
cd $basedir > /dev/null 2>&1
git_check
echo -e -n "\n${cyan}core-control${nc} v${cyan}${version}${nc} hash: ${cyan}${loc}${nc} status: "
if [ "$up2date" = "yes" ]; then
echo -e "${green}current${nc}\n"
else
echo -e "${red}stale${nc}\n"
fi
if [ -d $core ]; then
npm_check
echo -e -n "${cyan}${name}-core${nc} v${cyan}${corever}${nc} remote: v${cyan}${npmver}${nc} status: "
if [ "$up2date" = "yes" ]; then
echo -e "${green}current${nc}\n"
else
echo -e "${red}stale${nc}\n"
fi
fi
}
db_clear () {
local fstatus=$(pm2status "${name}-forger" | awk '{print $4}')
local rstatus=$(pm2status "${name}-relay" | awk '{print $4}')
stop all > /dev/null 2>&1
dropdb ${name}_$network > /dev/null 2>&1
createdb ${name}_$network > /dev/null 2>&1
if [ "$rstatus" = "online" ]; then
start relay > /dev/null 2>&1
fi
if [ "$fstatus" = "online" ]; then
start forger > /dev/null 2>&1
fi
}
plugin_list () {
echo -e "\nAvailable plugins:\n"
for plugin in $(ls plugins); do
. "plugins/$plugin"
if [ -z "$(cat $config/plugins.js | grep $plugin)" ]; then
echo -e "${cyan}$plugin${nc} - ${red}inactive${nc} [$desc]"
else
echo -e "${cyan}$plugin${nc} - ${green}active${nc} [$desc]"
fi
done
echo
}
plugin_manage () {
if [ ! -f plugins/$2 ]; then
echo -e "\n${red}Plugin $2 not found.${nc}\n"
exit 1
else
. "plugins/$2"
fi
added="$(cat $config/plugins.js | grep $2)"
lastline='};'
blockend='},'
stab=' '
if [[ "$1" = "add" && -z "$added" ]]; then
alen=${#options[@]}
insert="$stab\"$npmrepo\/$2\": {\n"
for i in ${!options[@]}; do
insert="$insert\t${options[$i]}"
comp=$((i+1))
if [ "$comp" -lt "$alen" ]; then
insert="$insert,\n"
else
insert="$insert\n"
fi
done
insert="$insert$stab$blockend\n"
sed -i "s/$lastline/$insert$lastline/" $config/plugins.js
yarn global add $npmrepo/$2 > /dev/null 2>&1 &
echo -ne "\n${cyan}Installing $2... ${red}"
while [ -d /proc/$! ]; do
printf "\b${sp:i++%${#sp}:1}" && sleep .1
done
echo -e "\b${green}Done${nc}"
echo -e "\n${green}Plugin $2 installed with default settings.${nc}\n"
echo -e "${red}Restart Core for the changes to take effect.${nc}\n"
echo -e "${cyan}For more information and custom configuration${nc}"
echo -e "${cyan}visit $gitrepo/$2${nc}\n"
elif [[ "$1" = "add" && ! -z "$added" ]]; then
echo -e "\n${red}Plugin $2 already installed.${nc}\n"
elif [[ "$1" = "remove" && ! -z "$added" ]]; then
sed -i "/$2/,/$blockend/d" $config/plugins.js
yarn global remove $npmrepo/$2 > /dev/null 2>&1 &
echo -ne "\n${cyan}Removing $2... ${red}"
while [ -d /proc/$! ]; do
printf "\b${sp:i++%${#sp}:1}" && sleep .1
done
echo -e "\b${green}Done${nc}\n"
echo -e "${red}Restart Core for the changes to take effect.${nc}\n"
elif [[ "$1" = "remove" && -z "$added" ]]; then
echo -e "\n${red}Plugin $2 not installed.${nc}\n"
elif [[ "$1" = "update" && ! -z "$added" ]]; then
rem=$(npm view $npmrepo/$2 version)
loc=$(cat $HOME/.config/yarn/global/node_modules/$npmrepo/$2/package.json | jq -r '.version')
if [ "$rem" = "$loc" ]; then
echo -e "Already up-to-date."
exit 1
fi
yarn global add $npmrepo/$2 > /dev/null 2>&1 &
echo -ne "\n${cyan}Installing $2... ${red}"
while [ -d /proc/$! ]; do
printf "\b${sp:i++%${#sp}:1}" && sleep .1
done
echo -e "\b${green}Done${nc}"
echo -e "\n${red}Restart Core for the changes to take effect.${nc}\n"
else
echo -e "\n${red}Plugin $2 not installed.${nc}\n"
fi
}