This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathARKcommander.sh
1423 lines (1259 loc) · 45.1 KB
/
ARKcommander.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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# #
# ARK Commander Script #
# by tharude a.k.a The Forging Penguin #
# thanks ViperTKD for the helping hand #
# 19/01/2017 ARK Team #
# #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
### Adding some color ###
# Line coloring functions
function red {
echo -e "$(tput bold; tput setaf 1)$1$(tput sgr0)"
}
function igreen {
echo -e "$(tput bold; tput setaf 0; tput setab 2)$1$(tput sgr0)"
}
function ired {
echo -e "$(tput bold; tput setaf 3; tput setab 1)$1$(tput sgr0)"
}
function green {
echo -e "$(tput bold; tput setaf 2)$1$(tput sgr0)"
}
function yellow {
echo -e "$(tput bold; tput setaf 3)$1$(tput sgr0)"
}
### Checking if the script is started as root ###
if [ "$(id -u)" = "0" ]; then
clear
echo -e "\n$(ired " !!! This script should NOT be started using sudo or as the root user !!! ") "
echo -e "\nUse $(green "bash ARKcommander.sh") as a REGULAR user instead"
echo -e "Execute ONCE $(green "chmod +x ARKcommander.sh") followed by $(green "ENTER")"
echo -e "and start it only by $(green "./arkcommander.sh") as regular user after\n"
exit 1
fi
### Checking the Virtualization Environment ###
if [ $(systemd-detect-virt -c) != "none" ]; then
clear
echo "$(ired " ")"
echo "$(ired " OpenVZ / LXC / Virtuoso Container detected! ")"
echo "$(ired " ")"
echo "$(ired " Running ARK Node on a Container based virtual system is not recommended! ")"
echo "$(ired " Please change your VPS provider with one that uses hardware Virtualization. ")"
echo "$(ired " ")"
echo "$(ired " This script will now exit! ")"
echo "$(ired " ")"
exit 1
fi
# TEMP N
# sudo apt-get install npm
# sudo npm install -g n
# sudo n 6.9.2
# ----------------------------------
# Variables
# ----------------------------------
EDIT=nano
GIT_ORIGIN=mainnet
LOC_SERVER="http://localhost:4001"
ADDRESS=""
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SNAPDIR="$HOME/snapshots"
SNAPURL="https://snapshots.ark.io/current"
re='^[0-9]+$' # For numeric checks
#pubkey="02a3e3e5fc36565ab4275ddfee1592667f6c46f5e9aa7528499511d65c5e82a7db"
# Logfile
log="install_ark.log"
# ----------------------------------
# Arrays
# ----------------------------------
# Install prereq packages array
declare -a array=("postgresql" "postgresql-contrib" "libpq-dev" "build-essential" "python" "git" "curl" "jq" "libtool" "autoconf" "locales" "automake" "locate" "wget" "zip" "unzip" "htop" "nmon" "iftop" "update-notifier")
# ----------------------------------
# Functions
# ----------------------------------
# ASCII Art function
function asciiart {
clear
tput bold; tput setaf 2
cat << "EOF"
{_ {_______ {__ {__
{_ __ {__ {__ {__ {__
{_ {__ {__ {__ {__ {__
{__ {__ {_ {__ {_ {_
{______ {__ {__ {__ {__ {__
{__ {__ {__ {__ {__ {__
{__ {__ {__ {__ {__ {__
___ __ __ __ __ __ __ __ _ __ ___ ___
/ _//__\| V | V |/ \| \| | _\| __| _ \
| \_| \/ | \_/ | \_/ | /\ | | ' | v | _|| v /
\__/\__/|_| |_|_| |_|_||_|_|\__|__/|___|_|_\
W E L C O M E A B O A R D !
EOF
tput sgr0
}
pause() {
read -p "$(yellow " Press [Enter] key to continue...")" fakeEnterKey
}
# Current Network Height
function net_height {
local heights=$(curl -s "$LOC_SERVER/api/peers" | jq -r '.peers[] | .height')
highest=$(echo "${heights[*]}" | sort -nr | head -n1)
}
# Find parent PID
function top_level_parent_pid {
# Look up the parent of the given PID.
pid=${1:-$$}
if [ "$pid" != "0" ]; then
stat=($(</proc/${pid}/stat))
ppid=${stat[3]}
# /sbin/init always has a PID of 1, so if you reach that, the current PID is
# the top-level parent. Otherwise, keep looking.
if [[ ${ppid} -eq 1 ]] ; then
echo ${pid}
else
top_level_parent_pid ${ppid}
fi
else
pid=0
fi
}
# Process management variables
function proc_vars {
node=`pgrep -a "node" | grep ark-node | awk '{print $1}'`
if [ "$node" == "" ] ; then
node=0
fi
# Is Postgres running
pgres=`pgrep -a "postgres" | awk '{print $1}'`
# Find if forever process manager is runing
frvr=`pgrep -a "node" | grep forever | awk '{print $1}'`
# Find the top level process of node
top_lvl=$(top_level_parent_pid $node)
# Looking for ark-node installations and performing actions
arkdir=`locate -b "\ark-node"`
# Getting the parent of the install path
parent=`dirname $arkdir 2>&1`
# Forever Process ID
forever_process=`forever --plain list | grep $node | sed -nr 's/.*\[(.*)\].*/\1/p'`
# Node process work directory
nwd=`pwdx $node 2>/dev/null | awk '{print $2}'`
}
#PSQL Queries
query() {
PUBKEY="$(psql -d ark_mainnet -t -c 'SELECT ENCODE("publicKey",'"'"'hex'"'"') as "publicKey" FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
DNAME="$(psql -d ark_mainnet -t -c 'SELECT username FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
PROD_BLOCKS="$(psql -d ark_mainnet -t -c 'SELECT producedblocks FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
MISS_BLOCKS="$(psql -d ark_mainnet -t -c 'SELECT missedblocks FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
#BALANCE="$(psql -d ark_mainnet -t -c 'SELECT (balance/100000000.0) as balance FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | sed -e 's/^[[:space:]]*//')"
BALANCE="$(psql -d ark_mainnet -t -c 'SELECT to_char(("balance"/100000000.0), '"'FM 999,999,999,990D00000000'"' ) as balance FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
FORGED="$(psql -d ark_mainnet -t -c 'SELECT to_char((("fees" + "rewards")/100000000.0), '"'FM 999,999,999,990D00000000'"' ) as total_forged FROM mem_accounts WHERE "address" = '"'"$ADDRESS"'"' ;' | xargs)"
HEIGHT="$(psql -d ark_mainnet -t -c 'SELECT height FROM blocks ORDER BY HEIGHT DESC LIMIT 1;' | xargs)"
RANK="$(psql -d ark_mainnet -t -c 'WITH RANK AS (SELECT DISTINCT "publicKey", "vote", "round", row_number() over (order by "vote" desc nulls last) as "rownum" FROM mem_delegates where "round" = (select max("round") from mem_delegates) ORDER BY "vote" DESC) SELECT "rownum" FROM RANK WHERE "publicKey" = '"'"$PUBKEY"'"';' | xargs)"
}
# Stats Address Change
change_address() {
DID_BREAK=0
echo -e "\n$(yellow " Press CTRL+C followed by ENTER to return to menu")\n"
echo "$(yellow " Enter your delegate address for Stats")"
echo "$(yellow " WITHOUT QUOTES, followed by 'ENTER'")"
trap "DID_BREAK=1" SIGINT
read -e -r -p "$(yellow " :") " inaddress
while [ ! "${inaddress:0:1}" == "A" ] ; do
if [ "$DID_BREAK" -eq 0 ] ; then
echo -e "\n$(yellow " Use Ctrl+C followed by ENTER to return to menu")\n"
echo -e "\n$(ired " Enter delegate ADDRESS, NOT the SECRET!")\n"
read -e -r -p "$(yellow " :") " inaddress
else
break
fi
done
if [ "$DID_BREAK" -eq 0 ] ; then
ADDRESS=$inaddress
# sed -i "s#\(.*ADDRESS\=\)\( .*\)#\1 "\"$inaddress\""#" $DIR/$BASH_SOURCE
sed -i "1,/\(.*ADDRESS\=\)/s#\(.*ADDRESS\=\)\(.*\)#\1"\"$inaddress\""#" $DIR/$BASH_SOURCE
fi
}
# Snapshot URL Change
change_snapurl() {
DID_BREAK=0
echo -e "\n$(yellow " Press CTRL+C followed by ENTER to return to menu")\n"
echo "$(yellow " Enter your snapshot URL")"
echo "$(yellow " WITHOUT QUOTES, followed by 'ENTER'")"
trap "DID_BREAK=1" SIGINT
read -e -r -p "$(yellow " :") " insnapurl
while [ ! "${insnapurl:0:4}" == "http" ] ; do
if [ "$DID_BREAK" -eq 0 ] ; then
echo -e "\n$(yellow " Use Ctrl+C followed by ENTER to return to menu")"
echo -e "\n $(ired " The URL must begin with 'http' ")\n"
read -e -r -p "$(yellow " :") " insnapurl
else
break
fi
done
if [ "$DID_BREAK" -eq 0 ] ; then
SNAPURL=$insnapurl
sed -i "1,/\(.*SNAPURL\=\)/s#\(.*SNAPURL\=\)\(.*\)#\1"\"$insnapurl\""#" $DIR/$BASH_SOURCE
fi
}
# Forging Turn
turn() {
# echo $DIR
# echo "$BASH_SOURCE"
# echo "$ADDRESS"
if [ "$ADDRESS" == "" ] ; then
change_address
# echo "$(yellow " Enter your delegate address for Stats")"
# echo "$(yellow " WITHOUT QUOTES, followed by 'ENTER'")"
# read -e -r -p "$(yellow " :") " inaddress
# while [ ! "${inaddress:0:1}" == "A" ] ; do
# echo -e "\n$(ired " Enter delegate ADDRESS, NOT the SECRET!")\n"
# read -e -r -p "$(yellow " :") " inaddress
# done
# ADDRESS=$inaddress
# sed -i "s#\(.*ADDRESS\=\)\( .*\)#\1 "\"$inaddress\""#" $DIR/$BASH_SOURCE
# sed -i "1,/\(.*ADDRESS\=\)/s#\(.*ADDRESS\=\)\(.*\)#\1"\"$inaddress\""#" $DIR/$BASH_SOURCE
fi
# pause
while true; do
# trap : INT
query
net_height
asciiart
proc_vars
queue=`curl --connect-timeout 3 -f -s $LOC_SERVER/api/delegates/getNextForgers?limit=51 | jq ".delegates"`
is_forging=`curl -s --connect-timeout 1 $LOC_SERVER/api/delegates/forging/status?publicKey=$PUBKEY 2>/dev/null | jq ".enabled"`
is_syncing=`curl -s --connect-timeout 1 $LOC_SERVER/api/loader/status/sync 2>/dev/null | jq ".syncing"`
BLOCK_SUM=$((MISS_BLOCKS+PROD_BLOCKS))
if ! [[ $BLOCK_SUM -eq 0 ]]; then
RATIO=$((20000 * PROD_BLOCKS / BLOCK_SUM % 2 + 10000 * PROD_BLOCKS / BLOCK_SUM))
[[ $PROD_BLOCKS == 0 ]] && RATIO=0 || RATIO=$(sed 's/..$/.&/;t;s/^.$/.0&/' <<< $RATIO)
else
RATIO=0
fi
pos=0
for position in $queue; do
position=`echo "$position" | tr -d '",'`
if [[ $PUBKEY == $position ]]; then
# echo "$position : $pos <=="
turn=$pos
fi
pos=`expr $pos + 1`
done
git_upd_check
echo -e "$(yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")"
echo -e "$(green " NODE STATS")"
echo -e "$(yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")"
echo
echo -e "$(green " Delegate : ")$(yellow "$DNAME")"
echo -e "$(green " Forging : ")$(yellow "$is_forging")"
echo -e "$(green " Current Rank : ")$(yellow "$RANK")"
echo -e "$(green " Forging Position : ")$(yellow "$turn")"
echo -e "$(green " Node Blockheight : ")$(yellow "$HEIGHT")"
echo -e "$(green " Net Height : ")$(yellow "$highest")"
# echo -e "$(green "Public Key:")\n$(yellow "$PUBKEY")\n"
echo -e "$(green " Forged Blocks : ")$(yellow "$PROD_BLOCKS")"
echo -e "$(green " Missed Blocks : ")$(yellow "$MISS_BLOCKS")"
echo -e "$(green " Productivity : ")$(yellow "$RATIO"%)"
echo -e "$(green " Total forged : ")$(yellow "$FORGED")"
echo -e "$(green " ARK Balance : ")$(yellow "$BALANCE")"
echo
echo -e "\n$(yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")"
if [ -e $arkdir/app.js ]; then
echo -e "\n$(green " ✔ ARK Node installation found!")\n"
if [ "$node" != "" ] && [ "$node" != "0" ]; then
echo -e "$(green " ARK Node process is running with:")"
echo -e "$(green " System PID: $node, Forever PID $forever_process")"
echo -e "$(green " and Work Directory: $arkdir")\n"
else
echo -e "\n$(red " ✘ No ARK Node process is running")\n"
fi
else
echo -e "\n$(red " ✘ No ARK Node installation is found")\n"
fi
echo -e "\n$(yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")"
echo -e "\n$(yellow " Press 'Enter' to terminate ")"
read -t 4 && break
# sleep 4
done
}
# Stats Display
function stats {
asciiart
proc_vars
is_forging=`curl -s --connect-timeout 1 $LOC_SERVER/api/delegates/forging/status?publicKey=$pubkey 2>/dev/null | jq ".enabled"`
is_syncing=`curl -s --connect-timeout 1 $LOC_SERVER/api/loader/status/sync 2>/dev/null | jq ".syncing"`
if [ "$node" != "" ] && [ "$node" != "0" ]; then
echo -e "$(green " Instance of ARK Node found with:")"
echo -e "$(green " System PID: $node, Forever PID $forever_process")"
echo -e "$(green " Directory: $arkdir")\n"
else
echo -e "\n$(red " ✘ ARK Node process is not running")\n"
pause
fi
}
# Updating the locate database
function db_up {
echo -e "$(red "Please enter your sudo password for user $USER")"
sudo updatedb
}
# Update and upgrade the OS
function os_up {
asciiart
echo -e "$(yellow " Checking for system updates...")\n"
sudo apt-get update >&- 2>&- #-yqq 2>/dev/null
avail_upd=`/usr/lib/update-notifier/apt-check 2>&1 | cut -d ';' -f 1`
sec_upd=`/usr/lib/update-notifier/apt-check 2>&1 | cut -d ';' -f 2`
if [ "$avail_upd" == 0 ]; then
echo -e "$(green " There are no updates available")\n"
sleep 1
else
echo -e "\n$(red " There are $avail_upd updates available")"
echo -e "$(red " $sec_upd of them are security updates")"
echo -e "\n$(yellow " Updating the system...")"
sudo apt-get upgrade -yqq >&- 2>&- #2>/dev/null
sudo apt-get dist-upgrade -yq >&- 2>&- #2>/dev/null
#sudo apt-get purge nodejs postgresql postgresql-contrib samba*
sudo apt-get autoremove -yyq >&- 2>&- #2>/dev/null
sudo apt-get autoclean -yq >&- 2>&- #2>/dev/null
echo -e "\n$(green " ✔ The system was updated!")"
echo -e "\n$(red " System restart is recommended!\n")"
fi
}
# Install prerequisites
function prereq {
# Get array length
arraylength=${#array[@]}
# Installation loop
echo -e "$(yellow "-----------------------------------------------")"
for (( i=1; i<${arraylength}+1; i++ )); do
asciiart;
echo -e "$(yellow " Installing prerequisites...") "
echo -e "$(yellow "-----------------------------------------------")" # added
echo -e "$(yellow " $i / ${arraylength} : ${array[$i-1]}")"
if [ $(dpkg-query -W -f='${Status}' ${array[$i-1]} 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
sudo apt-get install -yqq >&- 2>&- ${array[$i-1]};
else
echo "$(green " Package: ${array[$i-1]} is already installed!")"
fi
echo -e "$(yellow "-----------------------------------------------")"
sleep 0.5
clear
done
}
# Install and set locale
function set_locale {
# Checking Locale first
asciiart
if [ `locale -a | grep ^en_US.UTF-8` ] || [ `locale -a | grep ^en_US.utf8` ] ; then
echo -e "$(green " ✔ Locale en_US.UTF-8 is installed")\n"
echo -e "$(yellow " Checking if the locale is set in bashrc...")"
if `grep -E "(en_US.UTF-8)" $HOME/.bashrc` ; then
echo -e "\n$(green " ✔ bashrc is already set")"
else
# Setting the bashrc locale
echo -e "$(red " ✘ Not set yet. Setting the bashrc locale...")"
echo -e "export LC_ALL=en_US.UTF-8" >> $HOME/.bashrc
echo -e "export LANG=en_US.UTF-8" >> $HOME/.bashrc
echo -e "export LANGUAGE=en_US.UTF-8" >> $HOME/.bashrc
echo -e "$(green " ✔ bashrc locale was set")\n"
# Setting the current shell locale
echo -e "$(yellow " Setting current shell locale...")\n"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
echo -e "$(green " ✔ Shell locale was set")"
fi
else
# Install en_US.UTF-8 Locale
echo -e "$(red " ✘ Locale en_US.UTF-8 is not installed")\n"
echo -e "$(yellow " Generating locale en_US.UTF-8...")"
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8
echo -e "$(green " ✔ Locale generated successfully.")\n"
# Setting the current shell locale
echo -e "$(yellow " Setting current shell locale...")\n"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
echo -e "$(green " ✔ Shell locale was set")\n"
# Setting the bashrc locale
echo -e "$(yellow " Setting the bashrc locale...")\n"
echo "export LC_ALL=en_US.UTF-8" >> $HOME/.bashrc
echo "export LANG=en_US.UTF-8" >> $HOME/.bashrc
echo "export LANGUAGE=en_US.UTF-8" >> $HOME/.bashrc
echo -e "$(green " ✔ bashrc locale was set")"
fi
}
# Install and set NTP
function ntpd {
# Check if ve are running in a OpenVZ or LXC Container for NTP Install
if [ $(systemd-detect-virt) == "lxc" ] || [ $(systemd-detect-virt) == "openvz" ]; then
echo -e "Your host is running in LXC or OpenVZ container. NTP is not required. \n"
else
echo -e "Checking if NTP is running first... \n"
if ! sudo pgrep -x "ntpd" > /dev/null; then
echo -e "No NTP found. Installing... "
sudo apt-get install ntp -yyq &>> $log
sudo service ntp stop &>> $log
sudo ntpd -gq &>> $log
sleep 2
sudo service ntp start &>> $log
sleep 2
if ! sudo pgrep -x "ntpd" > /dev/null; then
echo -e "NTP failed to start! It should be installed and running for ARK.\n Check /etc/ntp.conf for any issues and correct them first! \n Exiting."
exit 1
fi
echo -e "NTP was successfully installed and started with PID:" `sudo pgrep -x "ntpd"`
else
echo "NTP is up and running with PID:" `sudo pgrep -x "ntpd"`
fi
fi
echo "-------------------------------------------------------------------"
}
# Logrotate for Ark Node logs
function log_rotate {
if [[ "$(uname)" == "Linux" ]]; then
if [ ! -f /etc/logrotate.d/ark-logrotate ]; then
echo -e " Setting up Logrotate for ARK node log files."
sudo bash -c "cat << 'EOF' >> /etc/logrotate.d/ark-logrotate
$arkdir/logs/ark.log {
size=50M
copytruncate
create 660 $USER $USER
missingok
notifempty
compress
delaycompress
daily
rotate 7
dateext
maxage 7
}
EOF"
else
echo -e "$(green " ✔ Logrotate file already exists!")\n"
fi
fi
}
# GIT Update Check
function git_upd_check {
if [ -d "$arkdir" ]; then
cd $arkdir
git remote update >&- 2>&-
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
cd $HOME
if [ "$LOCAL" == "$REMOTE" ]; then
echo -e " $(igreen " ARK Node is Up-to-date \n")"
UP_TO_DATE=1
elif [ "$LOCAL" == "$BASE" ]; then
echo -e " $(ired " Please Update! Press (3) \n")"
UP_TO_DATE=0
else
echo -e " $(ired " Diverged \n")"
fi
fi
}
# Install PostgreSQL
function inst_pgdb {
sudo apt install -yyq postgresql postgresql-contrib >&- 2>&-
}
# Purge the Postgres Database
function purge_pgdb {
if [ $(dpkg-query -W -f='${Status}' postgresql } 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
echo "$(green " Postgres is not installed, nothing to purge. Exiting.") "
else
echo -e " $(ired " ")"
echo -e " $(ired " WARNING! This option will stop all ")"
echo -e " $(ired " running ARK Node processes and will ")"
echo -e " $(ired " remove the databases and PostgreSQL ")"
echo -e " $(ired " installation! Are you REALLY sure? ")"
echo -e " $(ired " ")"
read -e -r -p "$(yellow "\n Type (Y) to proceed or (N) to cancel: ")" -i "N" YN
if [[ "$YN" =~ [Yy]$ ]]; then
echo -e "$(yellow "\n Proceeding with PostgreSQL removal... \n")"
forever --silent --plain stopall
sleep 1
drop_db
drop_user
# stop the DB if running first...
sudo service postgresql stop
sleep 1
sudo apt --purge remove -yq postgresql\* >&- 2>&-
sudo rm -rf /etc/postgresql/ >&- 2>&-
sudo rm -rf /etc/postgresql-common/ >&- 2>&-
sudo rm -rf /var/lib/postgresql/ >&- 2>&-
sudo userdel -r postgres >&- 2>&-
sudo groupdel postgres >&- 2>&-
echo -e "$(yellow "\n PostgreSQL has been removed\n")"
read -e -r -p "$(yellow "\n Proceed with PostgreSQL installation (Y/n): ")" -i "Y" YN
if [[ "$YN" =~ [Yy]$ ]]; then
echo -e "$(yellow "\n Proceeding with PostgreSQL installation... \n")"
inst_pgdb
create_db
echo -e "$(yellow "\n PostgreSQL has been installed and set.\n")"
pause
fi
fi
fi
}
function snap_menu {
if [ ! -d "$SNAPDIR" ]; then
mkdir -p $SNAPDIR
fi
if [ "$(ls -A $SNAPDIR)" ]; then
if [[ $(expr `date +%s` - `stat -c %Y $SNAPDIR/current`) -gt 900 ]]; then
echo -e "$(yellow " Existing Current snapshot is older than 15 minutes")"
read -e -r -p "$(yellow "\n Download from ${SNAPURL}? (Y) or use Local (N) ")" -i "Y" YN
if [[ "$YN" =~ [Yy]$ ]]; then
echo -e "$(yellow "\n Downloading latest snapshot\n")"
rm $SNAPDIR/current
wget -nv $SNAPURL -O $SNAPDIR/current
echo -e "$(yellow "\n Download finished\n")"
fi
fi
snapshots=( $(ls -t $SNAPDIR | xargs -0) )
echo -e "$(yellow "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")"
echo -e "$(green " List of local snapshots:")"
echo -e "$(yellow "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")"
for (( i=0; i<${#snapshots[*]}; i++ )); do
if [ $i -le 9 ]; then
echo " " $(($i+1)): ${snapshots[$i]}
else
echo " " $(($i+1)): ${snapshots[$i]}
fi
done
read -ep "$(yellow "\n Which snapshot to be restored? ")"
if [[ "${REPLY}" =~ $re ]]; then
# Numeric checks
if [ $REPLY -le ${#snapshots[*]} ]; then
echo -e "$(yellow "\n Restoring snapshot ${snapshots[$((REPLY-1))]}")\n"
pg_restore -O -j 8 -d ark_mainnet $SNAPDIR/${snapshots[$(($REPLY-1))]} 2>/dev/null
echo -e "$(green " Snapshot ${snapshots[$(($REPLY-1))]} was restored successfully")\n"
else
echo -e "$(red "\n Value is out of list range!\n")"
snap_menu
fi
else
echo -e "$(red "\n $REPLY is not a number!\n")"
snap_menu
fi
else
echo -e "$(red " No snapshots found in $SNAPDIR")"
read -e -r -p "$(yellow "\n Do you like to download the latest snapshot? (Y/n) ")" -i "Y" YN
if [[ "$YN" =~ [Yy]$ ]]; then
echo -e "$(yellow "\n Downloading current snapshot\n")"
wget -nv $SNAPURL -O $SNAPDIR/current
echo -e "$(yellow "\n Download finished\n")"
fi
if [[ $? -eq 0 ]]; then
read -e -r -p "$(yellow " Do you like to restore the snapshot now? (Y/n) ")" -i "Y" YN
if [[ "$YN" =~ [Yy]$ ]]; then
# here calling the db_restore function
echo -e "$(yellow "\n Restoring $SNAPDIR/current ... ")"
pg_restore -O -j 8 -d ark_mainnet $SNAPDIR/current 2>/dev/null
echo -e "$(green "\n Current snapshot has been restored\n")"
fi
else
echo -e "$(red "\n Error while retriving the snapshot")"
echo -e "$(red " Please check that the file exists on server")"
fi
fi
}
# Check if program is installed
function node_check {
# defaulting to 1
return_=1
# changing to 0 if not found
type $1 >/dev/null 2>&1 || { return_=0; }
# return value
# echo "$return_"
}
# Install NVM and node
function nvm {
node_check node
if [ "$return_" == 0 ]; then
echo -e "$(red " ✘ Node is not installed, installing...")"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh 2>/dev/null | bash >>install.log
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
### Installing node ###
nvm install 6.9.5 >>install.log
nvm use 6.9.5 >>install.log
nvm alias default 6.9.5 >>install.log
echo -e "$(green " ✔ Node `node -v` has been installed")"
else
echo -e "$(green " ✔ Node `node -v` is alredy installed")"
fi
node_check npm
if [ "$return_" == 0 ]; then
echo -e "$(red " ✘ NPM is not installed, installing...")"
### Install npm ###
npm install -g npm >>install.log 2>&1
echo -e "$(green " ✔ NPM `npm -v` has been installed")"
else
echo -e "$(green " ✔ NPM `npm -v` is alredy installed")"
fi
node_check forever
if [ "$return_" == 0 ]; then
echo -e "$(red " ✘ Forever is not installed, installing...")"
### Install forever ###
npm install forever -g >>install.log 2>&1
echo -e "$(green " ✔ Forever has been installed")"
else
echo -e "$(green " ✔ Forever is alredy installed")"
fi
# Setting fs.notify.max_user_watches
if grep -qi 'fs.inotify' /etc/sysctl.conf ; then
echo -e "\n$(green " fs.inotify.max_user_watches is already set")"
else
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
fi
echo -e "\n$(yellow "Check install.log for reported install errors")"
}
# Install ARK Node
function inst_ark {
# proc_vars
cd $HOME
mkdir ark-node
git clone https://github.com/ArkEcosystem/ark-node.git 2>/dev/null
cd ark-node
git checkout $GIT_ORIGIN 2>/dev/null
git pull origin $GIT_ORIGIN 2>/dev/null
npm install grunt-cli -g 2>/dev/null
npm install libpq 2>/dev/null
npm install secp256k1 2>/dev/null
npm install bindings 2>/dev/null
git submodule init 2>/dev/null
git submodule update 2>/dev/null
npm install 2>/dev/null
}
# Create ARK user and DB
function create_db {
# check if PG is running here if not Start.
if [ -z "$pgres" ]; then
sudo service postgresql start
fi
sleep 1
# sudo -u postgres dropdb --if-exists ark_mainnet
# sleep 1
# sudo -u postgres dropuser --if-exists $USER # 2>&1
# sleep 1
sudo -u postgres psql -c "update pg_database set encoding = 6, datcollate = 'en_US.UTF8', datctype = 'en_US.UTF8' where datname = 'template0';" >&- 2>&-
sudo -u postgres psql -c "update pg_database set encoding = 6, datcollate = 'en_US.UTF8', datctype = 'en_US.UTF8' where datname = 'template1';" >&- 2>&-
sudo -u postgres psql -c "CREATE USER $USER WITH PASSWORD 'password' CREATEDB;" >&- 2>&-
sleep 1
createdb ark_mainnet
}
# Check if DB exists
function db_exists {
# check if it's running and start if not.
if [ -z "$pgres" ]; then
sudo service postgresql start
fi
if [[ ! $(sudo -u postgres psql ark_mainnet -c '\q' 2>&1) ]]; then
read -r -n 1 -p "$(yellow " Database exists! Do you want to drop it? (y/n):") " YN
if [[ "$YN" =~ [Yy]$ ]]; then
drop_db;
fi
else
echo "Database not exist."
fi
}
# Check if User exists
function user_exists {
# check if it's running and start if not.
if [ -z "$pgres" ]; then
sudo service postgresql start
fi
if [[ $(sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$USER'" 2>&1) ]]; then
echo "User $USER exists";
read -r -n 1 -p "$(yellow " User $USER exists! Do you want to remove it? (y/n):") " YN
if [[ "$YN" =~ [Yy]$ ]]; then
sudo -u postgres dropuser --if-exists $USER
fi
else
echo "User $USER does not exist"
fi
}
# Drop ARK DB
function drop_db {
# check if it's running and start if not.
if [ -z "$pgres" ]; then
sudo service postgresql start
fi
dropdb --if-exists ark_mainnet
}
function drop_user {
if [ -z "$pgres" ]; then
sudo service postgresql start
fi
if [[ $(sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$USER'" 2>&1) ]]; then
sudo -u postgres dropuser --if-exists $USER
else
echo "DB User $USER does not exist"
fi
}
function update_ark {
if [ "$UP_TO_DATE" -ne 1 ]; then
cd $arkdir
# forever stop app.js
TMP_PASS=$(jq -r '.forging.secret | @csv' config.$GIT_ORIGIN.json)
mv config.mainnet.json ../
git pull origin $GIT_ORIGIN
git checkout $GIT_ORIGIN
npm install
sleep 1
if [ ! -e config.$GIT_ORIGIN.json ]; then
mv ../config.$GIT_ORIGIN.json .
else
jq -r '.forging.secret = ['"$TMP_PASS"']' config.$GIT_ORIGIN.json > config.$GIT_ORIGIN.tmp && mv config.$GIT_ORIGIN.tmp config.$GIT_ORIGIN.json
fi
unset TMP_PASS
# forever restart $forever_process
# forever start app.js --genesis genesisBlock.mainnet.json --config config.mainnet.json
else
echo "ARK Node is already up to date!"
sleep 2
fi
}
# Put the password in config.mainnet.json
function secret {
echo -e "\n"
#Put check if arkdir is empty, if it is stays only config.mainnet.json
echo -e "$(yellow " Enter (copy/paste) your private key (secret)")"
echo -e "$(yellow " (WITHOUT QUOTES!) followed by 'Enter'")"
read -e -r -p ": " secret
cd $arkdir
jq -r ".forging.secret = [\"$secret\"]" config.$GIT_ORIGIN.json > config.$GIT_ORIGIN.tmp && mv config.$GIT_ORIGIN.tmp config.$GIT_ORIGIN.json
}
### Menu Options ###
# Install ARK node
one() {
cd $HOME
proc_vars
if [ -e $arkdir/app.js ]; then
clear
asciiart
echo -e "\n$(green " ✔ ARK Node is already installed!")\n"
if [ "$node" != "" ] && [ "$node" != "0" ]; then
echo -e "$(green "A working instance of ARK Node is found with:")"
echo -e "$(green "System PID: $node, Forever PID $forever_process")"
echo -e "$(green "and Work Directory: $arkdir")\n"
fi
pause
else
clear
asciiart
echo -e "$(yellow " Installing ARK node....")"
create_db
inst_ark
clear
asciiart
echo -e "$(green " ✔ ARK node was installed")\n"
sudo updatedb
sleep 1
proc_vars
log_rotate
config="$parent/config.mainnet.json"
# echo "$config" 2>/dev/null
# pause
if [ ! -e $config ] ; then
read -e -r -p "$(yellow " Do you want to set your Secret Key now? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
five
fi
fi
fi
}
# Reinstall ARK Node
two() {
clear
asciiart
echo -e "$(ired "!!! This option will erase your DB and ARK Node installation !!!")\n"
read -e -r -p "$(red " Are you sure that you want to proceed? (Y/N): ")" -i "N" keys
if [ "$keys" == "Y" ]; then
proc_vars
if [ -e $arkdir/app.js ]; then
clear
asciiart
echo -e "\n$(green " ✔ ARK Node installation found in $arkdir")\n"
if [ "$node" != "" ] && [ "$node" != "0" ]; then
echo -e "$(green "A working instance of ARK Node is found with:")"
echo -e "$(green "System PID: $node, Forever PID $forever_process")"
echo -e "$(yellow " Stopping ARK node ...")\n"
cd $arkdir
forever --plain stop $forever_process >&- 2>&-
cd $parent
fi
echo -e "$(yellow " Backing up configuration file to $parent")\n"
sleep 1
if [ -e $parent/config.mainnet.json ] ; then
read -e -r -p "$(yellow " Backup file exists! Overwrite? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
cp $arkdir/config.mainnet.json $parent
cd $parent
fi
else
cp $arkdir/config.mainnet.json $parent
cd $parent
fi
echo -e "$(yellow " Removing ARK Node directory...")\n"
sleep 1
rm -rf $arkdir
drop_db
drop_user
one
echo ""
if [ -e $parent/config.mainnet.json ] ; then
read -e -r -p "$(yellow " Do you want to restore your config? (Y/N): ")" -i "Y" keys
# echo "Break1"; pause
if [ "$keys" == "Y" ]; then
cp $parent/config.mainnet.json $arkdir
echo -e "\n$(green " ✔ Config was restored in $arkdir")\n"
read -e -r -p "$(yellow " Do you want to start ARK Node now? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
start
fi
else
read -e -r -p "$(yellow " Do you want to start ARK Node now? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
start
fi
fi
fi
else
echo -e "\n$(green " ✔ Previous installation not found.")\n"
drop_db
drop_user
sleep 1
one
proc_vars
if [ -e $parent/config.mainnet.json ] ; then
read -e -r -p "$(yellow " Do you want to restore your config? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
cp $parent/config.mainnet.json $arkdir
echo -e "\n$(green " ✔ Config was restored in $arkdir")\n"
fi
else
echo -e "\n$(yellow " No backup config was found in $parent")\n"
read -e -r -p "$(yellow " Do you want to set your Secret Key now? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
secret
fi
fi
# echo "Break2"; pause
read -e -r -p "$(yellow " Do you want to start ARK Node now? (Y/N): ")" -i "Y" keys
if [ "$keys" == "Y" ]; then
start
fi
fi
fi
}
three() {
asciiart
proc_vars
if [ "$UP_TO_DATE" -ne 1 ]; then
if [ "$node" != "" ] && [ "$node" != "0" ]; then
echo -e "$(green " Instance of ARK Node found with:")"
echo -e "$(green " System PID: $node, Forever PID $forever_process")"
echo -e "$(green " Directory: $arkdir")\n"
echo -e "\n$(green " Updating ARK Node...")\n"
update_ark
echo -e "$(green " Restarting...")"
forever restart $forever_process >&- 2>&-
echo -e "\n$(green " ✔ ARK Node was successfully restarted")\n"
pause
else
echo -e "\n$(red " ✘ ARK Node process is not running")\n"
echo -e "$(green " Updating ARK Node...")\n"
update_ark