forked from no112358/ALLinONE-nknnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnkndeploy.sh
1429 lines (1163 loc) · 42.4 KB
/
nkndeploy.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
method1(){
clear
cat << "EOF"
================================================================================
Setup: Download ChainDB from NKN.org and host it on THIS server
Hosting your own ChaindDB archive server will make making new NKN nodes faster.
To force exit this script press CTRL+C
================================================================================
EOF
if [[ $mode == "advanced" ]]; then
printf "\033[2A\033[2K"
cat << "EOF"
Requirements:
1. ChainDB HOST ONLY, need 25 GB+ storage space, 1+ cpu, 512+MB ram
2. ChainDB HOST + NKN node, need 40+ GB storage space, 1+ cpu, 1+GB ram
================================================================================
EOF
read -s -r -p "Press Enter to continue!"
printf "\r\033[K"
fi
# Install Apache web server
printf "Installing Apache Web Server............................................ "
apt-get install apache2 -y > /dev/null 2>&1
printf "DONE!\n"
# Configure Firewall and ports
printf "Configuring firewall.................................................... "
ufw allow 80 > /dev/null 2>&1
ufw allow 22 > /dev/null 2>&1
ufw allow 443 > /dev/null 2>&1
ufw --force enable > /dev/null 2>&1
printf "DONE!\n"
# Download the ChainDB archive from nkn.org
websource="https://nkn.org/ChainDB_pruned_latest.tar.gz" # NKN.org ChainDB URL
#websource="http://194.137.17.39/ChainDB.tar.gz" # TEST ChainDB URL
cd /var/www/html/ > /dev/null 2>&1 || exit
printf "Downloading ChainDB archive............................................. \n"
# Check URL
if curl --connect-timeout 5 --output /dev/null --silent --head --fail "$websource"; then
# URL OK
wget --quiet --continue --show-progress $websource
printf "Downloading ChainDB archive............................................. DONE!\n\n"
else
# URL FAIL / timeout
printf "URL: %s, does NOT exist. Server down?\n\n" "$websource"
read -s -r -p "Press Enter to go back to the menu!"
menu
fi
# cleanup
filename=${websource##*/} # read the URL filename
mv -f "$filename" ChainDB.tar.gz > /dev/null 2>&1 # fix the original filename to ChainDB.tar.gz
rm -f index.html > /dev/null 2>&1 # remove Apache auto generated index.html
# END output
printf "This is your new ChainDB archive URL use it to deploy your new NKN nodes.\n"
printf "Copy paste it somewhere safe!\n\n"
printf "%s" "$red"
printf "URL: http://%s/ChainDB.tar.gz\n\n" "$PUBLIC_IP"
printf "%s" "$normal"
read -s -r -p "Press Enter to continue!"
# "if from beginner menu" also install NKN node
if [[ $mode == "beginner" ]]; then
websource="http://$PUBLIC_IP/ChainDB.tar.gz" # set to local ChainDB file
installation="local"
userdata1
else
menu
fi
}
method2(){
clear
cat << "EOF"
================================================================================
Setup: Create ChainDB from own existing NKN node and host on the SAME server
To force exit this script press CTRL+C
Requirements:
1. NKN node syncState: "PERSIST_FINISHED"
2. ChainDB HOST + NKN node, need 40+ GB storage space, 1+ cpu, 1+GB ram
================================================================================
EOF
read -s -r -p "Press Enter to continue!"
printf "\r\033[K"
printf "Installing Apache Web Server............................................ "
apt-get install apache2 -y > /dev/null 2>&1
printf "DONE!\n"
printf "Stopping NKN node software.............................................. "
systemctl stop nkn-commercial.service > /dev/null 2>&1
sleep 5
printf "DONE!\n"
# find directory and change dir to it
cd "$(find / -type d -name "nkn-node" 2>/dev/null)" || exit
printf "Pruning ChainDB folder.................................................. "
./nknc pruning --pruning --lowmem > /dev/null 2>&1
printf "DONE!\n"
printf "Creating ChainDB archive................................................ \n"
tar cf - ./ChainDB -P | pv -s "$(du -sb ./ChainDB | awk '{print $1}')" | gzip > /var/www/html/ChainDB.tar.gz
printf "Create ChainDB archive.................................................. DONE!\n"
printf "Starting NKN node software.............................................. "
systemctl start nkn-commercial.service > /dev/null 2>&1
printf "DONE!\n\n"
rm -f /var/www/html/index.html > /dev/null 2>&1
#printf "\nYou can now start the script on NEW servers you wanna deploy a node on with:\n\n"
#printf "%s" "$red"
#printf "wget -O nkndeploy.sh 'http://107.152.46.244/nkndeploy.sh'; bash nkndeploy.sh\n\n"
#printf "%s" "$normal"
printf "Custom URL to the ChainDB archive. You will need this URL, make a copy of it!\n\n"
printf "%s" "$red"
printf "http://%s/ChainDB.tar.gz\n\n" "$PUBLIC_IP"
printf "%s" "$normal"
read -s -r -p "Press Enter to continue!"
}
method3(){
clear
cat << "EOF"
================================================================================
Setup: Create ChainDB from own node and host it on another server
To force exit this script press CTRL+C
================================================================================
________________ ________________
| ____________ | | ____________ |
| | | | | | | |
| | NKN | | | | WEB | |
| | NODE | | | | HOST | |
| | SERVER | | | | SERVER | |
| |____________| | | |____________| |
|________________| |________________|
_|________|_>>>>>>>>>>>>>>>>>>>>>>>>>>>>>_|________|_
/ ********** \ / ********** \
/ ************ \ / ************ \
-------------------- --------------------
READ CAREFULLY!
This process will make a ChainDB file on NKN node server and transfer it
to the web host server.
You need to provide a WEB HOST SERVER! Make another VPS server which you will
use to host the ChainDB file, so you can deploy your next NKN nodes faster!
Requirement: web host with 1 core, 512 MB RAM, 25+GB storage minimum.
EOF
read -s -r -p "Press Enter to continue!"
method3host
}
method3host(){
clear
cat << "EOF"
================================================================================
Setup: Create ChainDB and host it on another server.
To force exit this script press CTRL+C
================================================================================
We will now connect to the HOST server and configure it from this script.
After you put in the username and address, you will be asked to confirm the
ECDSA security. Type yes and hit enter. You will be asked for the password
to establish the SSH connection and to install software on the web host server.
Type in WEB HOST SERVER username:
EOF
read -r sshusername
printf "\nType in WEB HOST SERVER IP address:\n"
read -r sship
printf "\nConfiguring Web Host Server............................................. \n"
sudo ssh -t "$sshusername"@"$sship" 'sudo apt-get update -y > /dev/null 2>&1; sudo apt-get install apache2 -y > /dev/null 2>&1; sudo rm -f /var/www/html/index.html > /dev/null 2>&1; exit > /dev/null 2>&1'
method3node
}
method3node(){
printf "Configuring Web Host Server............................................. DONE!\n"
printf "Stopping NKN node software.............................................. "
systemctl stop nkn-commercial.service > /dev/null 2>&1
sleep 5
printf "DONE!\n"
# find directory and change dir to it
cd "$(find / -type d -name "nkn-node" 2>/dev/null)" || exit
printf "Pruning ChainDB folder.................................................. "
./nknc pruning --pruning --lowmem > /dev/null 2>&1
printf "DONE!\n"
printf "\nWe will now connect to the HOST server again and upload the ChainDB file.\n"
printf "You will be asked for the host server user password for the SSH connection\n\n"
tar zcf - ./ChainDB/ -P | pv -s "$(du -sb ./ChainDB | awk '{print $1}')" | ssh "$sshusername"@"$sship" "cat > /var/www/html/ChainDB.tar.gz"
printf "Upload complete......................................................... DONE!\n"
printf "Starting NKN node software.............................................. "
systemctl start nkn-commercial.service > /dev/null 2>&1
printf "DONE!\n\n"
#printf "You can now start the script on NEW servers you wanna deploy a node on with:\n\n"
#printf "%s" "$red"
#printf "wget -O nkndeploy.sh 'http://107.152.46.244/nkndeploy.sh'; bash nkndeploy.sh\n\n"
#printf "%s" "$normal"
printf "Custom URL to the ChainDB archive. You will need this URL, make a copy of it!\n\n"
printf "%s" "$red"
printf "http://%s/ChainDB.tar.gz\n\n" "$sship"
printf "%s" "$normal"
read -s -r -p "Press Enter to continue!"
menu
}
method4(){
clear
cat << "EOF"
================================================================================
Setup: Update existing ChainDB archive on THIS server
To force exit this script press CTRL+C
Requirement:
1. NKN node syncState: "PERSIST_FINISHED"
Previous ChainDB will be replaced
================================================================================
EOF
read -s -r -p "Press Enter to continue!"
printf "\r\033[K"
printf "Stopping NKN node software.............................................. "
systemctl stop nkn-commercial.service > /dev/null 2>&1
sleep 5
printf "DONE!\n"
printf "Pruning ChainDB folder.................................................. "
cd "$(find / -type d -name "nkn-node" 2>/dev/null)" || exit # find directory and change dir
./nknc pruning --pruning --lowmem > /dev/null 2>&1
printf "DONE!\n"
printf "Deleting OLD ChainDB archive............................................ "
rm -f Chain*.tar.gz > /dev/null 2>&1 # delete old file from previous versions of script
rm -f /var/www/html/Chain*.tar.gz > /dev/null 2>&1
printf "DONE!\n"
printf "Creating NEW ChainDB archive............................................ \n"
tar cf - ./ChainDB -P | pv -s "$(du -sb ./ChainDB | awk '{print $1}')" | gzip > /var/www/html/ChainDB.tar.gz
# bug somehow the tar/gzip process changes ownership of files ?? rechown
chown -R "$username":"$username" ChainDB/ > /dev/null 2>&1
printf "Create NEW ChainDB archive.............................................. DONE!\n"
printf "Starting NKN node software.............................................. "
sudo systemctl start nkn-commercial.service > /dev/null 2>&1
printf "DONE!\n\n"
printf "The ChainDB.tar.gz archive was updated.\n\n"
printf "Next time you install a node, it will use the updated database.\n\n"
read -s -r -p "Press Enter to continue!"
menu
}
method5(){
clear
cat << "EOF"
================================================================================
Setup: Download ChainDB from custom URL and host it on this server
To force exit this script press CTRL+C
Requirements:
1. Fresh Server only!
2. ChainDB HOST ONLY, need 25+ GB storage space, 1+ cpu, 512+ MB ram
3. ChainDB HOST + NKN node, need 40+ GB storage space, 1+ cpu, 1+ GB ram
================================================================================
EOF
read -s -r -p "Press Enter to continue!"
printf "\r\033[K"
printf "Enter the custom URL address where the ChainDB*.tar.gz is located at:\n"
read -r websource
printf "\n"
# URL CHECK
if curl --output /dev/null --silent --head --fail "$websource"; then
printf "URL OK: %s\n" "$websource"
sleep 4
#continue if URL ok
else
printf "ERROR URL does NOT exist: %s\n" "$websource"
sleep 4
method5
fi
printf "\nInstalling Apache Web Server............................................ "
apt-get install apache2 -y > /dev/null 2>&1
printf "DONE!\n"
# Configure Firewall and ports
printf "Configuring firewall.................................................... "
ufw allow 80 > /dev/null 2>&1
ufw allow 22 > /dev/null 2>&1
ufw allow 443 > /dev/null 2>&1
ufw --force enable > /dev/null 2>&1
printf "DONE!\n"
cd /var/www/html/ > /dev/null 2>&1 || exit
printf "Downloading ChainDB archive............................................. \n"
wget --quiet --continue --show-progress "$websource"
printf "Downloading ChainDB archive............................................. DONE!\n\n"
# cleanup
filename=${websource##*/}
mv -f "$filename" ChainDB.tar.gz > /dev/null 2>&1
rm -f index.html > /dev/null 2>&1
# NEW websource for the install
websource="http://$PUBLIC_IP/ChainDB.tar.gz"
# END output
printf "This is your new ChainDB archive URL use it to deploy your new NKN nodes.\n"
printf "Copy paste it somewhere safe!\n\n"
printf "%s" "$red"
printf "URL: http://%s/ChainDB.tar.gz\n\n" "$PUBLIC_IP"
printf "%s" "$normal"
# Question
read -r -p "Do you also want to install a NKN node on this server ? [y/n] " response
case "$response" in
[yY][eE][sS]|[yY])
# correct continue script
installation="local" ; userdata1 ;;
*)
# wrong exit
menuadvanced ;;
esac
}
function nodeWalletTransfer(){
clear
cat << "EOF"
================================================================================
Setup: Transfer NODE ID / wallet (NOT beneficiary wallet where you get paid)
To force exit this script press CTRL+C
================================================================================
This will copy the wallet files from the REMOTE server to THIS server!
Run this script on the NEW NKN server where you wanna restore the node ID to.
Requirement:
- NKN node installed on this server!
EOF
printf "REMOTE NKN server IP address:\n"
read -r remoteIP
printf "\nREMOTE NKN server username (NKN if installed with this script):\n"
printf "\n"
read -r remoteUsername
printf "\nLOCAL NKN server username (NKN if installed with this script):\n"
read -r localUsername
printf "\nYou will be asked for the REMOTE user password so the connection\n"
printf "can get established.\n\n"
read -s -r -p "Press Enter to continue!"
# check if rsync works or not
if rsync -a -I "$remoteUsername"@"$remoteIP":/home/"$remoteUsername"/nkn-commercial/services/nkn-node/wallet.json :/home/"$remoteUsername"/nkn-commercial/services/nkn-node/wallet.pswd /home/"$localUsername"/nkn-commercial/services/nkn-node/
then
printf "\nWallet files copied!\n"
systemctl restart nkn-commercial.service
printf "Local NKN node restarted!\n"
printf "Local NKN noded should start with the new ID.\n\n"
else
printf "\nError while running rsync\n\n"
fi
read -s -r -p "Press Enter to continue!"
menu
}
################################ user input ####################################
function userdata1(){
clear
cat << "EOF"
================================================================================
Setup: necessary data input
To force exit this script press CTRL+C
Enter the MAINNET! NKN address where you want to receive payments.
Example address: NKNFLRkm3uWZBxohoZAAfBgXPfs3Tp9oY4VQ
================================================================================
NKN Wallet address:
EOF
# Input beneficiary wallet adddress
read -r benaddress
# check wallet address lengh
walletlenght=${#benaddress}
if [[ $walletlenght == "36" ]]; then
# Continues script
userdata2
else
# restarts function F1
cat << "EOF"
NKN wallet address you entered is wrong. Use mainnet NKN wallet,
not ERC-20 wallet. NKN mainnet address starts with NKN*
EOF
read -s -r -p "Press Enter to continue!"
userdata1
fi
}
function userdata2(){
clear
cat << "EOF"
================================================================================
Setup: necessary data input
To force exit this script press CTRL+C
A new user will be created for security reasons.
Please use a strong password of choice.
================================================================================
Enter password:
EOF
printf "Pre-set Username: %s\n\n" "$username"
printf "Password:\n"
read -r userpassword
userdata3
}
function userdata3(){
if [[ $installtype == "custom" ]]; then
clear
cat << "EOF"
================================================================================
Setup: necessary data input
To force exit this script press CTRL+C
================================================================================
Enter the custom URL address where the ChainDB*.tar.gz is located at:
EOF
read -r websource
printf "\n"
if curl --output /dev/null --silent --head --fail "$websource"; then
printf "URL OK: %s\n" "$websource"
sleep 4
userdata4
else
printf "ERROR URL does NOT exist: %s\n" "$websource"
sleep 4
userdata3
fi
else
userdata4
fi
}
function userdata4(){
clear
cat << "EOF"
================================================================================
Setup: necessary data input
To force exit this script press CTRL+C
================================================================================
EOF
# Check data if true
printf "Check what you entered:\n\n"
printf "Wallet address: %s\n" "$benaddress"
printf "Username: %s\n" "$username"
printf "Password: %s\n" "$userpassword"
printf "Chain database source: %s\n\n" "$websource"
# Question
read -r -p "Are you sure this data is correct? [y/n] " response
case "$response" in
[yY][eE][sS]|[yY])
#correct continue script
install1 ;;
*)
#wrong restarts userdata input
userdata1 ;;
esac
}
############################# Firewall warning #################################
function firewallwarn(){
clear
# revert all changes
/home/"$username"/nkn-commercial/nkn-commercial uninstall > /dev/null 2>&1
cd / > /dev/null 2>&1
pkill -KILL -u "$username" > /dev/null 2>&1
deluser --remove-home "$username" > /dev/null 2>&1
printf "%s" "$red"
cat << "EOF"
A modem/router or VPS provided firewall is prohobiting access to the internet!
For home modem, configure the ports correctly.
For VPS, disable the VPS provider firewall and allow all internet through.
The system changes were REVERTED, once you fix the firewall settings
restart the server and just run the same script again
For info on how to do that visit:
https://forum.nkn.org/t/allinone-nknnode-script-deploy-nodes-faster-with-your-own-chaindb/2753
EOF
printf "%s" "$normal"
read -s -r -p "Press Enter to continue!"
menu
}
################################### Uninstall######################################
function uninstall(){
clear
# revert all changes
cat << "EOF"
================================================================================
Setup: Uninstall NKN node
To force exit this script press CTRL+C
================================================================================
EOF
read -s -r -p "Press Enter to continue!"
printf "\r\033[K"
# uninstall NKN miner, kill all user processes, remove user and userfolder
/home/"$username"/nkn-commercial/nkn-commercial uninstall > /dev/null 2>&1
cd / > /dev/null 2>&1
pkill -KILL -u "$username" > /dev/null 2>&1
deluser --remove-home "$username" > /dev/null 2>&1
printf "Uninstall complete!\n\n"
read -s -r -p "Press Enter to continue!"
menu
}
################################## Install #####################################
function install1(){
clear
IFS='' read -r -d '' fire <<"EOF"
( ,&&&.
) .,.&&
( ( \=__/
) ,'-'.
( ( ,, _.__|/ /|
) /\ -((------((_|___/ |
( // | (`' (( `'--|
_ -.;_/ \\--._ \\ \-._/.
(_;-// | \ \-'.\ <_,\_\`--'|
( `.__ _ ___,') <_,-'__,'
`'(_ )_)(_)_)'
================================================================================
This will take some time. Please be patient.
To force exit this script press CTRL+C
================================================================================
EOF
printf "%s\n" "$fire"
# disable firewall for the installation
ufw --force disable > /dev/null 2>&1
# Create a new SUDO user
printf "Creating a new Super User account....................................... "
pass=$(perl -e 'print crypt($ARGV[0], "password")' "$userpassword")
useradd -m -p "$pass" -s /bin/bash "$username"
usermod -a -G sudo "$username"
printf "DONE!\n"
# Install NKN node miner software
printf "Downloading NKN node software........................................... "
cd /home/"$username" || exit
wget --quiet --continue "$nknsoftwareURL"
printf "DONE!\n"
printf "Installing NKN node software............................................ "
# extract filename and extension from URL
filename=${nknsoftwareURL##*/}
unzip "$filename" > /dev/null 2>&1
rm -f "$filename"
# remove extension from filename
filename=${filename%.*}
mv "$filename" nkn-commercial
chown -R "$username":"$username" /home/"$username"
chmod -R 755 /home/"$username"
/home/"$username"/nkn-commercial/nkn-commercial -b "$benaddress" -d /home/"$username"/nkn-commercial/ -u "$username" install > /dev/null 2>&1
printf "DONE!\n"
# Wait for ChainDB DIR and wallet creation
DIR="/home/$username/nkn-commercial/services/nkn-node/"
# No ChainDB install
if [[ $database == "no" ]]; then
# script waits for wallet generation and then skips DB download and continues
printf "Waiting for NKN node software to start.................................. "
timestart=$(date +%s)
while [[ $(($(date +%s) - timestart)) -lt 300 ]]; do # 300sec 5 min
if [[ ! -f "$DIR"wallet.json ]]; then
# if file doesn't exist wait and repeat check
sleep 5
else
# when wallet.json file is detected
printf "DONE!\n"
install3
fi
done
# when timer runs out go to the firewall warning
firewallwarn
# ChainDB install
else
printf "Waiting for NKN node software to start.................................. "
timestart=$(date +%s)
while [[ $(($(date +%s) - timestart)) -lt 300 ]]; do # 300sec 5 min
if [[ ! -d "$DIR"ChainDB ]] && [[ ! -f "$DIR"wallet.json ]]; then
# if folder and file don't exist yet wait and repeat check
sleep 5
else
# when file is detected
sleep 5 > /dev/null 2>&1
systemctl stop nkn-commercial.service > /dev/null 2>&1
sleep 5 > /dev/null 2>&1
printf "DONE!\n"
install2
fi
done
# when timer runs out go to the firewall warning
firewallwarn
fi
}
function install2(){
printf "Downloading / Extracting NKN Chain database.............................\n"
cd "$DIR" > /dev/null 2>&1 || exit
rm -rf ChainDB/ > /dev/null 2>&1
# extract locally or download from websource
if [[ $installation == "local" ]]; then
cd /var/www/html/ || exit
pv ChainDB.tar.gz | tar xzf - -C "$DIR"
else
# internet download
wget -O - "$websource" -q --show-progress | tar -xzf -
fi
chown -R "$username":"$username" /home/"$username" > /dev/null 2>&1
chmod -R 755 /home/"$username" > /dev/null 2>&1
printf "Downloading / Extracting NKN Chain database............................. DONE!\n"
install3
}
function install3(){
# Configure Firewall / ports
printf "Configuring firewall.................................................... "
ufw allow 30001:30005/tcp > /dev/null 2>&1 # NKN node
ufw allow 30010/tcp > /dev/null 2>&1 # Tuna exit
ufw allow 30011/udp > /dev/null 2>&1 # Tuna exit
ufw allow 30020/tcp > /dev/null 2>&1 # Tuna reverse entry
ufw allow 30021/udp > /dev/null 2>&1 # Tuna reverse entry
ufw allow 32768:65535 > /dev/null 2>&1 # Tuna reverse entry
ufw allow 22 > /dev/null 2>&1 # SSH
ufw allow 80 > /dev/null 2>&1 # HTTP
ufw allow 443 > /dev/null 2>&1 # HTTPS
ufw --force enable > /dev/null 2>&1
systemctl start nkn-commercial.service > /dev/null 2>&1
printf "DONE!\n"
# Disable root password, to enable root again:
# sudo passwd root
# sudo passwd -u root
printf "Disabling Root account for security reasons............................. "
passwd --lock root > /dev/null 2>&1
printf "DONE!\n\n"
install4
}
function install4(){
printf "===============================================================================\n"
printf "Congratulations, you deployed a NKN node!\n"
printf "===============================================================================\n\n"
printf "%s" "$red"
printf "Check the status of your new node on www.nstatus.org\n"
printf "Use this server IP address: %s\n\n" "$PUBLIC_IP"
printf "The new NKN server will take some time to sync up, keep checking it\n"
printf "until the site gives you this error: No ID in this account...\n\n"
printf "Then send 10 NKN (mainnet token) to the address it provides, AKA:\n"
nodewallet=$(sed -r 's/^.*Address":"([^"]+)".*/\1/' "$DIR"wallet.json)
printf "%s\n\n" "$nodewallet"
printf "Keep checking on nstatus to see when the server activates.\n\n"
printf "%s" "$normal"
printf "%s" "$blue"
printf "NKN wallet (beneficiary adddress) where you will get paid to:\n"
printf "%s\n\n" "$benaddress"
printf "%s" "$normal"
printf "From now on use these settings to connect to your server:\n"
printf "If you're using AWS, Google Cloud, Azure... use the provided SSH keys to login.\n\n"
printf "Server IP: %s\n" "$PUBLIC_IP"
printf "SSH login: ssh %s@%s\n" "$username" "$PUBLIC_IP"
printf "Server username: %s\n" "$username"
printf "Server password: %s\n\n" "$userpassword"
printf "Thanks for using this script!\n\n"
read -s -r -p "Press enter to continue!"
menu
}
################################# NODE CHECKER #################################
addip(){
clear
printf "Enter NODE IP address to ADD:\n"
read -r addipaddress
printf "%s\n" >> IPs.txt "$addipaddress" # create/write file IPs.txt
}
removeip(){
clear
FILE="IPs.txt"
printf "Enter NODE IP address to REMOVE:\n"
read -r removeipaddress
# remove information from the file IPs.txt
if grep -Fxq "$removeipaddress" "$FILE"
then
# if found
sed -i /"$removeipaddress"/d "$FILE"
printf "\nIP address removed!\n\n"
read -s -r -p "Press enter to continue!"
else
# if not found
printf "\nERROR IP address not found!\n\n"
read -s -r -p "Press enter to continue!"
fi
}
showips(){
clear
FILE="IPs.txt"
# read file IPs.txt and print it out in terminal
printf "%s server IP addresses found in IPs.txt file.\n\n" "$(grep "" -c IPs.txt)"
printf "*** File - %s contents ***\n\n" "$FILE"
cat $FILE
printf "\n"
read -s -r -p "Press enter to continue!"
}
checknodes(){
clear
input="IPs.txt"
inputwallet="walletaddress.txt"
while :
do
clear
# check if file exists, if not skip the wallet part
if [[ ! -f walletaddress.txt ]]; then
printf "%s servers IP addresses found in IPs.txt file.\n\n" "$(grep "" -c IPs.txt)"
printf "IP: Status: Height: Version: Uptime:\n"
else
while IFS= read -r file; do # read the NKN wallet address from the walletaddress.txt file
walletaddress="$file"
# fetch wallet balance from nkn.org
getwalletinfo=$(curl -s -X GET \
-G "https://openapi.nkn.org/api/v1/addresses/$walletaddress" \
-H "Content-Type: application/json" \
-H "Accept: application/json")
walletoutput1=$(printf "%s" "$getwalletinfo" | sed -n -r 's/(^.*address":")([^"]+)".*/\2/p' | sed -e 's/[",]//g')
walletoutput2=$(printf "%s" "$getwalletinfo" | sed -E 's/(^.*balance":)([^",]+).*/\2/; s/[0-9]{8}$/.&/')
done < "$inputwallet"
printf "Wallet address: %s\n" "$walletoutput1"
printf "Wallet balance: %s NKN\n\n" "$walletoutput2"
printf "%s servers IP addresses found in IPs.txt file.\n\n" "$(grep "" -c IPs.txt)"
printf "IP: Status: Height: Version: Uptime: NKN mined:\n"
fi
# get blockworth from API
getlatestblock=$(curl -s -X GET \
-G "https://openapi.nkn.org/api/v1/statistics/counts" \
-H "Content-Type: application/json" \
-H "Accept: application/json")
latestblock=$(printf "%s" "$getlatestblock" | sed -E 's/(^.*blockCount":)([^",]+).*/\2/; s/[0-9]{8}$/.&/')
getblockworth=$(curl -s -X GET \
-G "https://openapi.nkn.org/api/v1/blocks/$latestblock" \
-H "Content-Type: application/json" \
-H "Accept: application/json")
blockworth=$(printf "%s" "$getblockworth" | sed -E 's/(^.*reward":)([^",]+).*/\2/; s/[0-9]{8}$/.&/; s/[}]//g')
# fetch the node data and process it
while IFS= read -r file; do
nkncOutput=$(./nknc --ip "$file" info -s)
if [[ $nkncOutput == *"error"* ]]
then
output1=$(printf "%s" "$nkncOutput" | sed -n -r 's/(^.*message": ")([^"]+)".*/\2/p')
printf "%-17s%s\n" "$file" "$output1"
else
output1=$(printf "%s" "$nkncOutput" | sed -n '/syncState/p' | cut -d' ' -f2 | sed -e 's/[",]//g')
output2=$(printf "%s" "$nkncOutput" | sed -n '/height/p' | cut -d' ' -f2 | sed -e 's/[",]//g')
output3=$(printf "%s" "$nkncOutput" | sed -n '/version/p' | cut -d' ' -f2 | sed -e 's/[",]//g' | sed 's/[-].*$//')
# convert seconds into days and hours
uptimeSec=$(printf "%s" "$nkncOutput" | sed -n '/uptime/p' | cut -d' ' -f2 | sed -e 's/[",]//g')
outputDays=$((uptimeSec / 86400))
outputHours=$(((uptimeSec / 3600) - (outputDays * 24)))
days="d "
hours="h"
output4="$outputDays$days$outputHours$hours"
# convert proposal blocks to NKN
howmanyblocks=$(printf "%s" "$nkncOutput" | sed -n '/proposalSubmitted/p' | cut -d' ' -f2 | sed -e 's/[",]//g')
worth=$(bc <<< "scale=2; $blockworth / 100000000 * $howmanyblocks")
nkn=" NKN"
output5="$worth$nkn"
# print out in colums
printf "%-17s%-18s%-9s%-10s%-10s%-10s\n" "$file" "$output1" "$output2" "$output3" "$output4" "$output5"
fi
done < "$input"
printf "\nRefresh every 2 minutes, press [ENTER] to exit to menu!\n"
read -r -s -N 1 -t 120 key
if [[ $key == $'\x0a' ]]; # exit loop if ENTER is pressed
then
menunodechecker
fi
done
}
walletbalance(){
clear
printf "Enter beneficiary wallet address:\n"
read -r walletaddress
# check wallet address lengh
walletlenght=${#walletaddress}
if [[ $walletlenght == "36" ]]; then
# Continues script
rm -f walletaddress.txt > /dev/null 2>&1
printf "%s\n" >> walletaddress.txt "$walletaddress" # write wallet address to file
else
# error wrong lenght of NKN address go back
cat << "EOF"
NKN wallet address you entered is wrong. Use mainnet NKN wallet,
not ERC-20 wallet. NKN mainnet address starts with NKN*
EOF
read -s -r -p "Press Enter to continue!"
walletbalance
fi
menunodechecker
}
################################### nWatch ####################################
nWatchInstall(){
clear
cat << "EOF"
================================================================================
Setup: nWatch installaion
Please be patient
To force exit this script press CTRL+C
================================================================================
EOF
if [[ ! -f /var/www/html/nodes-example.txt ]]; then
printf "Installing necessary software........................................... "
apt-get install apache2 php php-curl -y > /dev/null 2>&1
apt-get autoremove -y > /dev/null 2>&1
# Debian workaround to install locales
dpkg-reconfigure -f noninteractive tzdata > /dev/null 2>&1
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen > /dev/null 2>&1
sed -i -e 's/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/' /etc/locale.gen > /dev/null 2>&1
printf 'LANG="en_US.UTF-8"'>/etc/default/locale > /dev/null 2>&1
dpkg-reconfigure --frontend=noninteractive locales > /dev/null 2>&1
update-locale LANG=en_US.UTF-8 > /dev/null 2>&1
#
printf "DONE!\n"
printf "Downloading files....................................................... "
cd /var/www/html/ || exit
wget https://github.com/AL-dot-debug/nWatch/archive/refs/heads/main.zip > /dev/null 2>&1
printf "DONE!\n"
printf "Installing nWatch....................................................... "
rm -rf index.html > /dev/null 2>&1
unzip -u main.zip > /dev/null 2>&1
cp -rf nWatch-main/* . > /dev/null 2>&1
rm -rf nWatch-main/ > /dev/null 2>&1
rm -f main.zip > /dev/null 2>&1
find . -type f -name '*.png' -delete
chown -R www-data:www-data /var/www/html/ > /dev/null 2>&1
service apache2 restart > /dev/null 2>&1
printf "DONE!\n\n"
else
printf "Installing necessary software........................................... "
apt-get install apache2 php php-curl -y > /dev/null 2>&1
apt-get autoremove -y > /dev/null 2>&1
printf "DONE!\n"
printf "Downloading files....................................................... "
cd /var/www/html/ || exit
wget https://github.com/AL-dot-debug/nWatch/archive/refs/heads/main.zip > /dev/null 2>&1
printf "DONE!\n"
printf "Updating nWatch......................................................... "
unzip -u main.zip > /dev/null 2>&1
cp -rf nWatch-main/* . > /dev/null 2>&1
rm -rf nWatch-main/ > /dev/null 2>&1
rm -f main.zip > /dev/null 2>&1
find . -type f -name '*.png' -delete
chown -R www-data:www-data /var/www/html/ > /dev/null 2>&1
service apache2 restart > /dev/null 2>&1
printf "DONE!\n\n"
fi
printf "Access the nWatch website on this address, where you can set up your\n"
printf "server IP list and monitor all your nodes.\n"
printf "http://%s\n\n" "$PUBLIC_IP"
read -s -r -p "Press enter to continue!"