forked from go140point6/xahl-node-1host2cnames
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.sh
2484 lines (2160 loc) · 86.6 KB
/
setup.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
version=0.96
###################################################################################
# setup message, variables, and functions for script.
#
# Set Color Vars
color() {
GREEN='\033[0;32m'
RED='\033[0;91m' # Intense Red
YELLOW='\033[0;33m'
BYELLOW='\033[1;33m'
BLUE='\033[0;94m'
NC='\033[0m' # No Color
YW=$(echo "\033[33m")
BL=$(echo "\033[36m")
RD=$(echo "\033[01;31m")
BGN=$(echo "\033[4;92m")
GN=$(echo "\033[1;92m")
DGN=$(echo "\033[32m")
CL=$(echo "\033[m")
CM="${GN}✓${CL}"
CROSS="${RD}✗${CL}"
BFR="\\r\\033[K"
HOLD=" "
}
export -f color
color
spinner() {
local chars="/-\|"
local spin_i=0
printf "\e[?25l"
while true; do
printf "\r \e[36m%s\e[0m" "${chars:spin_i++%${#chars}:1}"
sleep 0.1
done
}
export -f spinner
msg_info() {
if [[ -n "$SPINNER_PID" ]] && ps -p $SPINNER_PID >/dev/null 2>&1; then kill $SPINNER_PID > /dev/null && printf "\e[?25h"; fi
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg} "
spinner &
SPINNER_PID=$!
}
export -f msg_info
msg_ok() {
if [[ -n "$SPINNER_PID" ]] && ps -p $SPINNER_PID >/dev/null 2>&1; then kill $SPINNER_PID > /dev/null && printf "\e[?25h"; fi
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
}
export -f msg_ok
msg_error() {
if [[ -n "$SPINNER_PID" ]] && ps -p $SPINNER_PID >/dev/null 2>&1; then kill $SPINNER_PID > /dev/null && printf "\e[?25h"; fi
local msg="$1"
echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
}
export -f msg_error
# setup error catching
export SPINNER_PID=""
set -Eeuo pipefail
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
trap cleanup EXIT
trap SIGINT_EXIT SIGINT
error_handler() {
# clear
if [[ -n "$SPINNER_PID" ]] && ps -p $SPINNER_PID >/dev/null 2>&1; then kill $SPINNER_PID > /dev/null && printf "\e[?25h"; fi
local exit_code="$?"
local line_number="$1"
local command="$2"
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
echo -e "\n$error_message\n"
msg_error "an error occured, see above, cleared created temp directoy ($TEMP_DIR), and cleanly exiting..."
}
cleanup() {
if [[ -n "${SPINNER_PID// }" ]] && ps -p $SPINNER_PID >/dev/null 2>&1; then kill $SPINNER_PID > /dev/null && printf "\e[?25h"; fi
popd >/dev/null
sudo sh -c 'rm -f /etc/sudoers.d/node_setup'
sudo rm -rf $TEMP_DIR
stty sane
}
exit-script() {
cleanup
echo -e "⚠ User exited script \n"
exit
}
SIGINT_EXIT(){
cleanup
exit 1
}
FUNC_EXIT(){
cleanup
bash ~/.profile
sudo -u $USER_ID sh -c 'bash ~/.profile'
exit 0
}
FUNC_EXIT_ERROR(){
exit 1
}
INTEGER='^[0-9]+([.][0-9]+)?$'
FDATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
USER_ID=$(getent passwd $EUID | cut -d: -f1)
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR >/dev/null
cd ~
sudo mkdir -p xahl-node
cd ~/xahl-node
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
###################################################################################
# Authenticate sudo permissions, and extend time out before script execution to avoid timeouts or errors.
msg_info "checking privileges..."
if ! command -v sudo &> /dev/null; then
msg_error "sudo is not installed. Please install sudo and rerun the script. (to install sudo, you will need to be logged in as root user, then run this command \"apt update && apt install sudo -y\" )"
exit 1
fi
if [ "$(id -u)" -eq 0 ]; then
msg_ok "Privileges checked, user /"${USER_ID}/" has root privileges.${CL}"
elif sudo -l > /dev/null 2>&1; then
TIMEOUT=120 # Timeout in minutes
# Validate and apply sudoers timeout update
echo "Defaults:$USER_ID timestamp_timeout=$TIMEOUT" > "${TEMP_DIR}/node_setup"
if visudo -cf "${TEMP_DIR}/node_setup" >/dev/null 2>&1; then
sudo cp "${TEMP_DIR}/node_setup" /etc/sudoers.d/node_setup
msg_ok "${USER_ID} logged in, root privileges obtained, and timeout extended to $TIMEOUT minutes."
else
msg_error "${USER_ID} logged in, but an error occurred setting up sudoers configuration to extend timeout. Aborting."
sudo rm -f "${TEMP_DIR}/node_setup"
exit 1
fi
sudo rm -f "${TEMP_DIR}/node_setup"
else
msg_error "This script requires root or sudo privileges."
exit 1
fi
# Check for the .var file, if not present, generate a default one
FUNC_VARS_VARIABLE_CHECK(){
if [ ! -f $SCRIPT_DIR/xahl_node.vars ]; then
echo -e "$SCRIPT_DIR/xahl_node.vars file missing, generating a new one...${NC}"
mkdir -p $SCRIPT_DIR
sudo cat <<EOF > $SCRIPT_DIR/xahl_node.vars
vars_version="$version"
# These are the default variables for the setup.sh script to use.
# you can change these to suit you needs and environment.
# all saved question data is in .env file
# - for example,
# always_ask, will ask all question every time, with prompt of past answer, false skips if answered before
# install certbot, will stop the install of the cert bot, so it can be used without the need for SSL
# install landing page, having this on false, will prevent it deleting and re-installing the landing pages (if you have a custom one)
# install_toml, as above, you can force setup from messing with you .toml file
# variables for node setup
NODE_CONFIG_FILE="/opt/xahaud/etc/xahaud.cfg"
NODE_TYPE="node" # can be node, nodeHistory, validator, validatorHistory
NODE_SIZE="restricted" # used in history TYPEs and can either be, "full" to remove all size restriction, or "restricted" which will use NODE_LEDGER_HISTORY and NODE_ONLINE_DELETE values.
NODE_LEDGER_HISTORY="26000"
NODE_ONLINE_DELETE="26000"
NODE_CHAIN_NAME="mainnet" # can be either mainnet or testnet (aka VARVAL_CHAIN_NAME)
# variables for script choices
ALWAYS_ASK="true"
SCRIPT_DIR="$HOME/xahl-node" # by default this is now /home/, previously was always /root/ and not configurable
INSTALL_SYS_PACKAGES="true" # install and update all thats listed in system packages array below (this was called \$INSTALL_UPDATES in previous versions)
INSTALL_UFW="true"
INSTALL_CERTBOT_SSL="true"
INSTALL_LANDINGPAGE="true"
INSTALL_TOML="true"
DISPLAY_FULL_LOG="false"
# ipv6 can be set to auto (default), true or false, auto uses command \`ip a | grep -c 'inet6.*::1/128'\`
IPv6="auto"
# -------------------------------------------------------------------------------
# *** the following variables DO NOT need to be changed ***
# * these are for the script/nginx setups *
# system packages that the main script depends on;
SYS_PACKAGES=(net-tools git curl gpg nano node-ws python3 python3-requests python3-toml whois htop sysstat apache2-utils)
# variables for nginx
NGX_CONF_ENABLED="/etc/nginx/sites-enabled/"
NGX_CONF_NEW="/etc/nginx/sites-available/"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_ALLOWLIST_FILE="nginx_allowlist.conf"
NGINX_PROXY_IP="192.168.0.0/16"
# MAINNET
NGX_MAINNET_RPC="6007"
NGX_MAINNET_WSS="6009" # set to 6009 for admin port
XAHL_MAINNET_PEER="21337"
# TESTNET
NGX_TESTNET_RPC="5009"
NGX_TESTNET_WSS="6009"
XAHL_TESTNET_PEER="21338"
# variables for toml updater
TOMLUPDATER_URL=https://raw.githubusercontent.com/gadget78/ledger-live-toml-updating/node-dev/validator/update.py
# variables for XAHAUD AUTO UPDATER
AUTOUPDATE_XAHAUD="true"
AUTOUPDATE_CHECK_INTERVAL="24"
UPDATE_SCRIPT_NAME="xahaud-silent-update.sh"
UPDATE_SCRIPT_PATH="/usr/local/bin/\$UPDATE_SCRIPT_NAME"
LOG_DIR="/opt/xahaud/log"
LOG_FILE="\$LOG_DIR/update.log"
EOF
fi
source $SCRIPT_DIR/xahl_node.vars
mkdir -p $SCRIPT_DIR
touch $SCRIPT_DIR/.env
source $SCRIPT_DIR/.env
# check and update old .vars file if it already exists
if [ -z "$ALWAYS_ASK" ]; then
ALWAYS_ASK="true"
sudo sh -c "echo 'ALWAYS_ASK="true"' >> $SCRIPT_DIR/xahl_node.vars"
fi
if [ -z "$NGINX_PROXY_IP" ]; then
NGINX_PROXY_IP="192.168.0.0/16"
sed -i '/^NGIINX_PROXY_IP/d' $SCRIPT_DIR/xahl_node.vars
#sudo sh -c "echo 'NGINX_PROXY_IP="192.168.0.0/16"' >> $SCRIPT_DIR/xahl_node.vars"
sudo sed -i '/^NGINX_ALLOWLIST_FILE="nginx_allowlist.conf"/a\NGINX_PROXY_IP="192.168.0.0/16"' $SCRIPT_DIR/xahl_node.vars
echo -e "${GREEN}## ${YELLOW}xahl-node.vars file updated, by adding entry NGINX_PROXY_IP... ${NC}"
fi
if [ -z "$TOMLUPDATER_URL" ]; then
TOMLUPDATER_URL=https://raw.githubusercontent.com/gadget78/ledger-live-toml-updating/node-dev/validator/update.py
sudo sh -c "echo '\n# variables for toml updater' >> $SCRIPT_DIR/xahl_node.vars"
sudo sh -c "echo 'TOMLUPDATER_URL=https://raw.githubusercontent.com/gadget78/ledger-live-toml-updating/node-dev/validator/update.py' >> $SCRIPT_DIR/xahl_node.vars"
echo -e "${GREEN}## ${YELLOW}xahl-node.vars file updated, by adding entry TOMLUPDATER_URL... ${NC}"
fi
if [ -z "$IPv6" ]; then
sudo sed -i "/^INSTALL_TOML=*/a\\ \n# ipv6 can be set to auto (default), true or false, auto uses command \"ip a | grep -c 'inet6.*::1/128'\"\nIPv6=\"auto\"" $SCRIPT_DIR/xahl_node.vars
fi
if [ -z "$vars_version" ] || [ "$vars_version" == "0.8.7" ] || [ "$vars_version" == "0.8.8" ]; then
vars_version=0.89
sudo sed -i '/^vars_version/d' $SCRIPT_DIR/xahl_node.vars
sudo sh -c "sed -i '1i vars_version=$version' $SCRIPT_DIR/xahl_node.vars"
sudo sed -i "s/^NGX_MAINNET_WSS=.*/NGX_MAINNET_WSS=\"6009\"/" $SCRIPT_DIR/xahl_node.vars
sudo sed -i "s/^NGX_TESTNET_WSS=.*/NGX_TESTNET_WSS=\"6009\"/" $SCRIPT_DIR/xahl_node.vars
sudo sed -i "s/^NGX_TESTNET_RPC=.*/NGX_TESTNET_RPC=\"5009\"/" $SCRIPT_DIR/xahl_node.vars
sudo sed -i '/^SYS_PACKAGES/d' $SCRIPT_DIR/xahl_node.vars
sudo sed -i '/^# ubuntu packages that the main script depends on;/a\SYS_PACKAGES=(net-tools git curl gpg nano node-ws python3 python3-requests python3-toml whois htop sysstat apache2-utils)' $SCRIPT_DIR/xahl_node.vars
echo -e "${GREEN}## ${YELLOW}xahl-node.vars file updated to version 0.89... ${NC}"
fi
if echo "$vars_version" | awk '{ exit !($1 < 0.94) }'; then
echo -e "xahl_node.vars file needs updating, importing old variables...${NC}"
sudo rm -f $SCRIPT_DIR/xahl_node.vars
sudo cat <<EOF > $SCRIPT_DIR/xahl_node.vars
vars_version="$version"
# These are the default variables for the setup.sh script to use.
# you can change these to suit you needs and environment.
# all saved question data is in .env file
# - for example,
# always_ask, will ask all question every time, with prompt of past answer, false skips if answered before
# install certbot, will stop the install of the cert bot, so it can be used without the need for SSL
# install landing page, having this on false, will prevent it deleting and re-installing the landing pages (if you have a custom one)
# install_toml, as above, you can force setup from messing with you .toml file
# variables for node setup
NODE_CONFIG_FILE="/opt/xahaud/etc/xahaud.cfg"
NODE_TYPE="node" # can be node, nodeHistory, validator, validatorHistory
NODE_SIZE="restricted" # used in history TYPEs and can either be, "full" to remove all size restriction, or "restricted" which will use NODE_LEDGER_HISTORY and NODE_ONLINE_DELETE values.
NODE_LEDGER_HISTORY="26000"
NODE_ONLINE_DELETE="26000"
NODE_CHAIN_NAME="$VARVAL_CHAIN_NAME" # can be either mainnet or testnet (aka VARVAL_CHAIN_NAME)
# variables for script choices
ALWAYS_ASK="$ALWAYS_ASK"
SCRIPT_DIR="$SCRIPT_DIR" # this is now /home/xahl-node by default, previously was always /root/xahl-root and not configurable
INSTALL_SYS_PACKAGES="$INSTALL_UPDATES" # (aka SYS_UPDATES) install and update all thats listed in system packages array below
INSTALL_UFW="$INSTALL_UFW"
INSTALL_CERTBOT_SSL="$INSTALL_CERTBOT_SSL"
INSTALL_LANDINGPAGE="$INSTALL_LANDINGPAGE"
INSTALL_TOML="$INSTALL_TOML"
AUTOUPDATE_XAHAUD="true"
AUTOUPDATE_CHECK_INTERVAL="24"
DISPLAY_FULL_LOG="false"
# ipv6 can be set to auto (default), true or false, auto uses command \`ip a | grep -c 'inet6.*::1/128'\`
IPv6="auto"
# -------------------------------------------------------------------------------
# *** the following variables DO NOT need to be changed ***
# * these are for the script/nginx setups *
# system packages that the main script depends on;
SYS_PACKAGES=(net-tools git curl gpg nano node-ws python3 python3-requests python3-toml whois htop sysstat apache2-utils)
# variables for nginx
NGX_CONF_ENABLED="/etc/nginx/sites-enabled/"
NGX_CONF_NEW="/etc/nginx/sites-available/"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_ALLOWLIST_FILE="nginx_allowlist.conf"
NGINX_PROXY_IP="$NGINX_PROXY_IP"
# MAINNET
NGX_MAINNET_RPC="6007"
NGX_MAINNET_WSS="6009" # set to 6009 for admin port
XAHL_MAINNET_PEER="21337"
# TESTNET
NGX_TESTNET_RPC="5009"
NGX_TESTNET_WSS="6009"
XAHL_TESTNET_PEER="21338"
# variables for toml updater
TOMLUPDATER_URL=https://raw.githubusercontent.com/gadget78/ledger-live-toml-updating/node-dev/validator/update.py
# variables for XAHAUD AUTO UPDATER
UPDATE_SCRIPT_NAME="xahaud-silent-update.sh"
UPDATE_SCRIPT_PATH="/usr/local/bin/\$UPDATE_SCRIPT_NAME"
LOG_DIR="/opt/xahaud/log"
LOG_FILE="\$LOG_DIR/update.log"
EOF
fi
if [ "$vars_version" == "0.95" ]; then
vars_version="$version"
sudo sed -i '/^# ubuntu packages that the main script depends on;/a\SYS_PACKAGES=(net-tools git curl gpg nano node-ws python3 python3-requests python3-toml whois htop sysstat apache2-utils)' $SCRIPT_DIR/xahl_node.vars
fi
source $SCRIPT_DIR/xahl_node.vars
source $SCRIPT_DIR/.env
}
######## start of Functions
FUNC_PKG_CHECK(){
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## Check/install necessary updates, and Packages... ${NC}"
echo
# update and upgrade the system
if [ -z "$INSTALL_SYS_PACKAGES" ]; then
read -p "do you want to check, and install OS updates and dependencies? Enter true or false # " INSTALL_SYS_PACKAGES
sed -i "s/^INSTALL_SYS_PACKAGES=.*/INSTALL_SYS_PACKAGES=\"$INSTALL_SYS_PACKAGES\"/" $SCRIPT_DIR/xahl_node.vars
fi
if [ "$INSTALL_SYS_PACKAGES" == "true" ]; then
msg_info "carrying out apt-get update"
sudo apt-get update -y 2>&1 | awk '{ printf "\r\033[K checking updates.. "; printf "%s", $0; fflush() }'
msg_ok "apt-get updates finished"
msg_info "carrying out any needed upgrades"
sudo apt upgrade -y 2>&1 | awk '{ printf "\r\033[K checking upgrades.. "; printf "%s", $0; fflush() }'
msg_ok "all upgrades finished"
echo
echo -e "${GREEN}## cycle through packages in vars file, and install... ${NC}"
for a in "${SYS_PACKAGES[@]}"
do
if ! command -v $a &> /dev/null; then
msg_info "installing $a... "
sudo apt-get install -y "$a" 2>&1 | awk -v app="$a" '{ printf "\r\033[K installing %s.. ", app; printf "%s", $0; fflush() }'
msg_ok "$a installed."
else
msg_ok "$a was already installed."
fi
done
echo
else
echo -e "${GREEN}## ${YELLOW}INSTALL_SYS_PACKAGES set to false in var files, skipping... ${NC}"
fi
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
sleep 2s
}
FUNC_IPV6_CHECK(){
if [ "$IPv6" != "false" ]; then
if ! ping -c 1 -4 github.com &> /dev/null && ip a | grep -q 'inet6.*::1/128'; then
echo -e "${YELLOW}IPv6 environment detected, checking hosts file.${NC}"
IPv6="true"
if ! grep -q "github" /etc/hosts; then
echo '2001:67c:27e4:1064::140.82.121.3 github.com www.github.com' | sudo tee -a /etc/hosts
echo -e "${YELLOW}Updated hosts file.${NC}"
fi
elif [ "$IPv6" == "true" ]; then
echo -e "${YELLOW}IPv6 environment being forced by .var file, checking hosts file.${NC}"
if ! grep -q "github" /etc/hosts; then
echo '2001:67c:27e4:1064::140.82.121.3 github.com www.github.com' | sudo tee -a /etc/hosts
echo -e "${YELLOW}Updated hosts file.${NC}"
fi
else
echo -e "${YELLOW}Not an exclusive IPv6 enviroment.${NC}"
fi
fi
}
FUNC_SETUP_MODE(){
if [[ "$NODE_CHAIN_NAME" != "mainnet" ]] && [[ "$NODE_CHAIN_NAME" != "testnet" ]]; then
echo -e "${BLUE}NODE_CHAIN_NAME not set in $SCRIPT_DIR/xahl_node.vars"
echo "Please choose an option:"
echo "1. Mainnet = configures and deploys/updates xahau node for Mainnet"
echo "2. Testnet = configures and deploys/updates xahau node for Testnet"
read -p "Enter your choice [1-3] # " choice
case $choice in
1)
NODE_CHAIN_NAME="mainnet"
;;
2)
NODE_CHAIN_NAME="testnet"
;;
*)
echo "Invalid option. Exiting."
FUNC_EXIT
;;
esac
sed -i "s/^NODE_CHAIN_NAME=.*/NODE_CHAIN_NAME=\"$NODE_CHAIN_NAME\"/" $SCRIPT_DIR/xahl_node.vars
fi
if [ "$NODE_CHAIN_NAME" == "mainnet" ]; then
echo -e "${GREEN}### Configuring node for ${BYELLOW}Xahau $NODE_CHAIN_NAME${GREEN}... ${NC}"
VARVAL_CHAIN_RPC=$NGX_MAINNET_RPC
VARVAL_CHAIN_WSS=$NGX_MAINNET_WSS
VARVAL_CHAIN_REPO="mainnet-docker"
VARVAL_CHAIN_PEER=$XAHL_MAINNET_PEER
elif [ "$NODE_CHAIN_NAME" == "testnet" ]; then
echo -e "${GREEN}### Configuring node for ${BYELLOW}Xahau $NODE_CHAIN_NAME${GREEN}... ${NC}"
VARVAL_CHAIN_RPC=$NGX_TESTNET_RPC
VARVAL_CHAIN_WSS=$NGX_TESTNET_WSS
VARVAL_CHAIN_REPO="Xahau-Testnet-Docker"
VARVAL_CHAIN_PEER=$XAHL_TESTNET_PEER
fi
VARVAL_NODE_NAME="xahl_node_$(hostname -s)"
echo -e "Node name is :${BYELLOW} $VARVAL_NODE_NAME ${NC}"
echo -e "Local Node RPC port is :${BYELLOW} $VARVAL_CHAIN_RPC ${NC}"
echo -e "Local WSS port is :${BYELLOW} $VARVAL_CHAIN_WSS ${NC}"
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
}
FUNC_CLONE_NODE_SETUP(){
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Starting Xahau Node install... ${NC}"
echo
if [[ "$NODE_TYPE" != "node" ]] && [[ "$NODE_TYPE" != "history" ]] && [[ "$NODE_TYPE" != "validator" ]] && [[ "$NODE_TYPE" != "validatorHistory" ]] || [[ "$ALWAYS_ASK" == "true" ]]; then
echo -e "${BLUE}Please choose node type: (if you are unsure, choose option 1)"
echo -e "1. \"Submission Node\", this will setup the Node to use RAM for the database, so no need for a fast Solid State HDD, suitable for 8+GB RAM, 16+GB HDD"
echo -e "2. \"History Node\", this will use the Hard drive for the database storage (drive MUST be a Solid State type), so it is able to keep a history of ledgers, suitable for 16GB+ RAM (HDD space required depends on next question)"
echo -e "3. \"Validator\", this will setup a validator, suitable for 16+GB RAM, 64GB+ SSD+ ${NC}"
read -p "Enter your choice [1-3] # " choice
case $choice in
1)
NODE_TYPE="node"
;;
2)
NODE_TYPE="nodeHistory"
;;
3)
echo -e "${BLUE}Please choose validator type:"
echo -e "1. \"RAM\" type where no history is stored."
echo -e "2. \"history\" type where it uses the hard drive to store ledgers (space required depends on next question)${NC}"
read -p "Enter your choice [1-2] # " type
case $type in
1)
NODE_TYPE="validator"
;;
2)
NODE_TYPE="validatorHistory"
;;
*)
echo "Invalid option. Exiting."
FUNC_EXIT
;;
esac
;;
*)
echo "Invalid option. Exiting."
FUNC_EXIT
;;
esac
if sudo grep -q 'NODE_TYPE=' "$SCRIPT_DIR/.env"; then
sudo sed -i "s/^NODE_TYPE=.*/NODE_TYPE=\"$NODE_TYPE\"/" "$SCRIPT_DIR/.env"
else
sudo echo -e "NODE_TYPE=\"$NODE_TYPE\"" >> $SCRIPT_DIR/.env
fi
fi
if [[ "$NODE_TYPE" == "nodeHistory" && "$ALWAYS_ASK" == "true" ]] || [[ "$NODE_TYPE" == "validatorHistory" && "$ALWAYS_ASK" == "true" ]]; then
echo -e "${BLUE}Please choose the amount of history you want to save:"
echo -e "1. restricted = this will setup a restriction on hard drive use, so by default the limit will be a days worth of ledgers, which will needs roughly 64 GB of space"
echo -e "2. full = this will configure the settings so that there will be NO restriction of size, making it a full history node, be warned this will take up terabytes of hard drive space ${NC}"
read -p "Enter your choice [1-2] # " choice
case $choice in
1)
NODE_SIZE="restricted"
;;
2)
NODE_SIZE="full"
;;
*)
echo "Invalid option. Exiting."
FUNC_EXIT
;;
esac
sed -i "s/^NODE_SIZE=.*/NODE_SIZE=\"$NODE_SIZE\"/" $SCRIPT_DIR/xahl_node.vars
fi
if [ "$NODE_TYPE" == "validator" ] || [ "$NODE_TYPE" == "validatorHistory" ]; then
if [[ -f "/opt/xahaud/etc/xahaud.cfg" ]]; then
NODE_VALIDATOR_TOKEN=$(sed -n '/^\[validator_token\]/,/^$/ {/^$/q; /^\[validator_token\]/!{/^$/!p}}' /opt/xahaud/etc/xahaud.cfg)
fi
if [ -n "$NODE_VALIDATOR_TOKEN" ] && echo "$NODE_VALIDATOR_TOKEN" | grep -q '[^[:space:]]'; then
echo "found validator_token, $NODE_VALIDATOR_TOKEN"
else
printf "${BLUE}The [validator_token] section is empty, enter token here, or leave blank to generate one.${NC} # "
read -e -i "$NODE_VALIDATOR_TOKEN" NODE_VALIDATOR_TOKEN
echo
if [[ -z "$NODE_VALIDATOR_TOKEN" ]]; then
if ! [[ -f "/opt/xahaud/bin/validator-keys" ]]; then
echo "downloading key generator"
sudo wget -O /opt/xahaud/bin/validator-keys https://raw.githubusercontent.com/Xahau/mainnet-docker/main/utilities/validator-keys
sudo chmod +x /opt/xahaud/bin/validator-keys
fi
if ! [[ -f "~/.ripple/validator-keys.json" ]]; then
echo "generating keys"
/opt/xahaud/bin/validator-keys create_keys >/dev/null
fi
echo "generating token"
NODE_VALIDATOR_TOKEN=$(/opt/xahaud/bin/validator-keys create_token --keyfile ~/.ripple/validator-keys.json | sed -n '/^\[validator_token\]/,/^$/ {/^$/q; /^\[validator_token\]/!{/^$/!p}}')
fi
if sudo grep -q 'NODE_VALIDATOR_TOKEN=' "$SCRIPT_DIR/.env"; then
sudo sed -i "s/^NODE_VALIDATOR_TOKEN=.*/NODE_VALIDATOR_TOKEN=\"$NODE_VALIDATOR_TOKEN\"/" "$SCRIPT_DIR/.env"
else
sudo echo -e "NODE_VALIDATOR_TOKEN=\"$NODE_VALIDATOR_TOKEN\"" >> $SCRIPT_DIR/.env
fi
fi
fi
cd $SCRIPT_DIR
if [ ! -d "$VARVAL_CHAIN_REPO" ]; then
echo -e "Creating directory '$SCRIPT_DIR/$VARVAL_CHAIN_REPO' to use for xahaud installation..."
echo -e "Cloning repo https://github.com/Xahau/$VARVAL_CHAIN_REPO' ${NC}"
git clone https://github.com/Xahau/$VARVAL_CHAIN_REPO
else
echo "existing directory '$SCRIPT_DIR/$VARVAL_CHAIN_REPO' found, pulling updates..."
cd $SCRIPT_DIR/$VARVAL_CHAIN_REPO
git pull
fi
if [ -d "/opt/xahaud/" ]; then
echo "previous xahaud node install found,"
echo "will stop existing xahaud, and check for updates..."
sudo systemctl stop xahaud
fi
cd $SCRIPT_DIR/$VARVAL_CHAIN_REPO
sudo ./xahaud-install-update.sh
sudo rm -f /opt/xahaud/etc/xahaud.cfg > /dev/null
sudo rm -f -r /opt/xahaud/db > /dev/null
if [[ "$NODE_TYPE" == "nodeHistory" || "$NODE_TYPE" == "validatorHistory" ]]; then
echo
echo -e "setting up HDD node...${NC}"
echo
if [ "$NODE_SIZE" == "full" ]; then
NODE_LEDGER_HISTORY="full"
NODE_ONLINE_DELETE=""
fi
NODE_DB_TYPE="NuDB"
NODE_DB_PATH="path=/opt/xahaud/db/nudb"
NODE_DB_RELATIONAL="backend=sqlite"
else
echo
echo -e "setting up RAM type node...${NC}"
echo
NODE_LEDGER_HISTORY="256"
NODE_ONLINE_DELETE="256"
NODE_DB_TYPE="rwdb"
NODE_DB_PATH=""
NODE_DB_RELATIONAL="backend=rwdb"
fi
sudo cat <<EOF > /opt/xahaud/etc/xahaud.cfg
[peers_max]
20
[overlay]
ip_limit = 1024
[network_id]
21337
[server]
port_peer
port_rpc_admin_local
port_ws_admin_local
port_rpc_public
port_ws_public
[port_peer]
port = 21337
ip = 0.0.0.0
protocol = peer
[port_rpc_admin_local]
port = 5009
ip = 127.0.0.1
admin = 127.0.0.1
protocol = http
[port_ws_admin_local]
port = 6009
ip = 127.0.0.1
admin = 127.0.0.1
protocol = ws
[port_rpc_public]
port = 6007
ip = 127.0.0.1
protocol = http
secure_gateway = 127.0.0.1
[port_ws_public]
port = 6008
ip = 127.0.0.1
protocol = ws
secure_gateway = 127.0.0.1
limit = 50000
send_queue_limit = 20000
websocket_ping_frequency = 10
[node_size]
huge
[node_db]
advisory_delete=0
online_delete=$NODE_ONLINE_DELETE
type=$NODE_DB_TYPE
$NODE_DB_PATH
[ledger_history]
$NODE_LEDGER_HISTORY
[database_path]
/opt/xahaud/db
[debug_logfile]
/opt/xahaud/log/debug.log
[sntp_servers]
time.windows.com
time.apple.com
time.nist.gov
pool.ntp.org
[validators_file]
/opt/xahaud/etc/validators-xahau.txt
[rpc_startup]
{ "command": "log_level", "severity": "warn" }
[ssl_verify]
0
[peer_private]
0
[ips_fixed]
bacab.alloy.ee 21337
# For validators only
# Add validator token stanza etc after this. Don't forget to restart
[voting]
account_reserve = 1000000
owner_reserve = 200000
EOF
if [ "$NODE_TYPE" == "validator" ] || [ "$NODE_TYPE" == "validatorHistory" ]; then
echo "[validator_token]" >> /opt/xahaud/etc/xahaud.cfg
echo "$NODE_VALIDATOR_TOKEN" >> /opt/xahaud/etc/xahaud.cfg
fi
if [ "$IPv6" == "true" ]; then
echo -e "${YELLOW}applying IPv6 changes to xahaud.cfg file.${NC}"
sudo sed -i "s/0.0.0.0/::/g" /opt/xahaud/etc/xahaud.cfg
sudo sed -i "s/127.0.0.1/::1/g" /opt/xahaud/etc/xahaud.cfg
fi
echo "restarting xahaud service"
sudo systemctl restart xahaud.service
echo
echo -e "${GREEN}## Finished Xahau Node install ...${NC}"
echo
cd $SCRIPT_DIR
sleep 4s
}
FUNC_XAHAUD_UPDATER(){
# echo
# echo -e "${GREEN}#########################################################################${NC}"
# echo
# echo -e "${GREEN}## ${YELLOW}Setup: Install Xahaud Updater... ${NC}"
# echo
msg_info "checking and adding Auto Updater..."
if [[ "$AUTOUPDATE_XAHAUD" == "true" ]]; then
# Ensure the log directory exists
sudo mkdir -p "$LOG_DIR"
# Copy the provided update script to /usr/local/bin
sudo cat << 'EOF' > "$UPDATE_SCRIPT_PATH"
#!/bin/bash
# Copy this file to /usr/local/bin as root
# make it executable - chmod +x /usr/local/bin/root
# add the cron file
VERSION="latest"
SCREEN_OUTPUT=false
# Parse command-line options
while getopts "v:s" opt; do
case $opt in
v) VERSION=$OPTARG ;;
s) SCREEN_OUTPUT=true ;;
esac
done
RELEASE_TYPE="release"
URL="https://build.xahau.tech/"
BASE_DIR=/opt/xahaud
USER=xahaud
PROGRAM=xahaud
BIN_DIR=$BASE_DIR/bin
DL_DIR=$BASE_DIR/downloads
LOG_DIR=$BASE_DIR/log
SCRIPT_LOG_FILE=$LOG_DIR/update.log
SERVICE_NAME="$PROGRAM.service"
log() {
local message="$1"
echo "$(date +"%Y-%m-%d %H:%M:%S") $message" >> "$SCRIPT_LOG_FILE"
if [ "$SCREEN_OUTPUT" = true ]; then
echo "$message"
fi
}
# Ensure the script runs as root
[[ $EUID -ne 0 ]] && exit 1
# Fetch available versions
filenames=$(curl --silent "${URL}" | grep -Eo '>[^<]+<' | sed -e 's/^>//' -e 's/<$//' | grep -E '^\S+\+[0-9]{2,3}$' | grep -E "$RELEASE_TYPE")
if [[ "$VERSION" == "latest" ]]; then
version_filter="release"
else
version_filter=$VERSION
fi
# Sort using version comparison and fetch the latest one
version_file=$(echo "$filenames" | grep "$version_filter" | sort -V | tail -n 1)
if [[ -z "$version_file" ]]; then
log "No update found."
exit 0
fi
log "New Update: $version_file"
if [[ ! -f "$DL_DIR/$version_file" ]]; then
curl --silent --fail "${URL}${version_file}" -o "$DL_DIR/$version_file"
chmod +x "$DL_DIR/$version_file"
chown $USER:$USER "$DL_DIR/$version_file"
log "Downloaded $version_file"
fi
current_file=$(readlink "$BIN_DIR/$PROGRAM")
if [[ "$current_file" != "$DL_DIR/$version_file" ]]; then
ln -snf "$DL_DIR/$version_file" "$BIN_DIR/$PROGRAM"
log "Symlink updated to $version_file"
# Restart the service using systemctl
log "Restarting $SERVICE_NAME"
systemctl restart $SERVICE_NAME
log "Update available: Yes"
else
log "Update available: No"
fi
EOF
# Make the update script executable
sudo chmod +x "$UPDATE_SCRIPT_PATH"
# add to cronjob
cron_job="0 */${AUTOUPDATE_CHECK_INTERVAL} * * * root sleep \$((RANDOM*3540/32768)) && $UPDATE_SCRIPT_PATH >> $LOG_FILE 2>&1"
existing_crontab=$(crontab -l 2>/dev/null) || existing_crontab=""
if echo "$existing_crontab" | grep -q "$UPDATE_SCRIPT_PATH"; then
existing_crontab=$(echo "$existing_crontab" | grep -v "$UPDATE_SCRIPT_PATH")
existing_crontab="${existing_crontab}"$'\n'"${cron_job}"
echo "$existing_crontab" | crontab -
msg_ok "updated cron tab tasks, system will now check for updates every ${AUTOUPDATE_CHECK_INTERVAL} hours"
else
(sudo crontab -l 2>&1 | grep -v -E "^no crontab for|^sudo:" || true; echo "$cron_job") | sudo crontab -
msg_ok "added new entry to cron tab tasks, system will check for updates every ${AUTOUPDATE_CHECK_INTERVAL} hours"
fi
else
msg_error "NOT adding AutoUpdate functions, due to AUTOUPDATE_XAHAUD set to ${AUTOUPDATE_XAHAUD} in xahl_node.vars file"
fi
}
FUNC_UFW_SETUP(){
# Check UFW config, install/update
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Setup: Checking UFW... ${NC}"
echo
sudo ufw version
if [ $? = 0 ]; then
echo -e "${GREEN}UFW is ALREADY installed ${NC}"
echo
# Setup UFW
FUNC_SETUP_UFW_PORTS;
FUNC_ENABLE_UFW;
else
echo
echo -e "${GREEN}## ${YELLOW}UFW is not installed, checking config option... ${NC}"
echo
if [ -z "$INSTALL_UFW" ]; then
read -p "Do you want to install UFW (Uncomplicated Firewall) ? enter true or false #" INSTALL_UFW
sudo sed -i "s/^INSTALL_UFW=.*/INSTALL_UFW=\"$INSTALL_UFW\"/" $SCRIPT_DIR/xahl_node.vars
fi
if [ "$INSTALL_UFW" == "true" ]; then
echo
echo -e "${GREEN}## ${YELLOW}Setup: Installing UFW... ${NC}"
echo
msg_info "installing ufw..."
sudo apt-get update >/dev/null 2>&1
sudo apt-get install -y ufw 2>&1 | awk '{ printf "\r\033[K installing ufw.. "; printf "%s", $0; fflush() }'
msg_ok "ufw installed."
FUNC_SETUP_UFW_PORTS;
FUNC_ENABLE_UFW;
fi
fi
}
FUNC_SETUP_UFW_PORTS(){
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Setup: Configure Firewall...${NC}"
echo
echo "allowing Nginx through the firewall."
sudo ufw allow 'Nginx Full'
# Get current SSH and xahau node port number, and unblock them
SSH_PORT=$(sudo ss -tlpn | grep sshd | awk '{print$4}' | cut -d ':' -f 2 -s) || SSH_PORT=""
if [[ -n "$SSH_PORT" ]]; then
echo -e "current SSH port number detected as: ${BYELLOW}$SSH_PORT${NC}"
sudo ufw allow $SSH_PORT/tcp
else
echo -e "current SSH port NOT detected."
fi
echo -e "current Xahau Node port number detected as: ${BYELLOW}$VARVAL_CHAIN_PEER${NC}"
sudo ufw allow $VARVAL_CHAIN_PEER/tcp
sudo ufw status verbose --no-page
sleep 2s
}
FUNC_ENABLE_UFW(){
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Setup: Change UFW logging to ufw.log only${NC}"
echo
# source: https://handyman.dulare.com/ufw-block-messages-in-syslog-how-to-get-rid-of-them/
sudo sed -i -e 's/\#& stop/\& stop/g' /etc/rsyslog.d/20-ufw.conf
sudo cat /etc/rsyslog.d/20-ufw.conf | grep '& stop'
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Setup: (re)Enable Firewall...${NC}"
echo
sudo systemctl start ufw && sudo systemctl status ufw --no-page
echo "y" | sudo ufw enable
#sudo ufw enable
sudo ufw status verbose --no-page
sleep 2s
}
FUNC_CERTBOT_PRECHECK(){
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}Setup: Checking CERTBOT options... ${NC}"
echo
if [ -z "$INSTALL_CERTBOT_SSL" ]; then
read -e -p "Do you want to use install CERTBOT and use SSL? : true or false # " INSTALL_CERTBOT_SSL
sudo sed -i "s/^INSTALL_CERTBOT_SSL=.*/INSTALL_CERTBOT_SSL=\"$INSTALL_CERTBOT_SSL\"/" $SCRIPT_DIR/xahl_node.vars
fi
if [ "$INSTALL_CERTBOT_SSL" != "true" ]; then
echo
echo -e "${GREEN}## ${YELLOW}Setup: INSTALL_CERTBOT_SSL in .vars file set to Skip CERTBOT install... ${NC}"
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
return
fi
echo
# Install Let's Encrypt Certbot
msg_info "installing certbot..."
sudo apt-get update >/dev/null 2>&1
sudo apt-get install certbot python3-certbot-nginx -y 2>&1 | awk '{ printf "\r\033[K installing certbot.. "; printf "%s", $0; fflush() }'
msg_ok "certbot installed."
echo -e "${GREEN}#########################################################################${NC}"
echo
sleep 1s
}
FUNC_CERTBOT_REQUEST(){
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## ${YELLOW}CertBot: final setup and request, and restart nginx ...${NC}"
echo
if [ "$INSTALL_CERTBOT_SSL" == "true" ]; then
# Request and install a Let's Encrypt SSL/TLS certificate for Nginx
echo -e "${GREEN}## ${YELLOW}Setup: Request and install a Lets Encrypt SSL/TLS certificate for domain: ${BYELLOW} $USER_DOMAIN${NC}"
# make sure correct version is installed
#sudo pip install --upgrade twine requests-toolbelt
sudo certbot --nginx -m "$CERT_EMAIL" -n --agree-tos -d "$USER_DOMAIN"
else
echo -e "${GREEN}## ${YELLOW}Setup: skipping installing of Certbot certificate request.${NC}"
fi
echo
echo -e "${GREEN}#########################################################################${NC}"
sleep 2s
# Start/Reload Nginx to apply all the new configuration
if sudo systemctl is-active --quiet nginx; then
# Nginx is running, so reload its configuration
sudo systemctl reload nginx
echo "Nginx reloaded."
else
# Nginx is not running, starting it
sudo systemctl start nginx
echo "Nginx started."
fi
# and enable it to start at boot
sudo systemctl enable nginx
}
FUNC_PROMPTS_4_DOMAINS_EMAILS() {
if [ -z "${USER_DOMAIN-}" ] || [ "$ALWAYS_ASK" == "true" ]; then
printf "${BLUE}Enter your servers domain (e.g. mydomain.com or a subdomain like xahau.mydomain.com )${NC} # "
read -e -i "${USER_DOMAIN-}" USER_DOMAIN
if sudo grep -q 'USER_DOMAIN=' "$SCRIPT_DIR/.env"; then
sudo sed -i "s/^USER_DOMAIN=.*/USER_DOMAIN=\"$USER_DOMAIN\"/" "$SCRIPT_DIR/.env"
else