-
Notifications
You must be signed in to change notification settings - Fork 100
/
masternode.sh
905 lines (838 loc) · 32 KB
/
masternode.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
#!/bin/bash
# version v0.1
# date 2018-06-17
# function: Installation of an BitCore masternode with options to install sentinel
#
# Instructions:
# Run this script w/ the desired parameters. Leave blank or use -h for help.
#
#Platforms:
# - Linux Ubuntu 16.04/18.04 LTS ONLY on a Vultr, Hetzner or DigitalOcean VPS
# - Generic Ubuntu support will be added at a later point in time
#Discord: Jannas#8759
#LinkedIn: https://www.linkedin.com/in/jan-terblanche-29a01874/
#Credits: This script is a combination of different scripts, credit to these guys...
#Github https://github.com/marsmensch, https://gist.github.com/lukechilds, https://github.com/sucremoneda, https://github.com/chaoabunga/mnscripts
export LC_ALL=C
scriptversion="v0.1.2"
release=""
git="https://github.com/LIMXTEC/BitCore.git"
date="$(date +%y-%m-%d-%s)"
script=$( cd $(dirname ${BASH_SOURCE[0]}) || exit > /dev/null; pwd -P )
logfile="/tmp/install_${date}_out.log"
name="bitcore"
daemon_dir="/usr/local/bin"
daemon="bitcored"
cli_dir="/usr/local/bin"
cli="bitcore-cli"
ssh_port=${SSH_CLIENT##* }
port=8555
rpcport=8556
tport=19468
trpcport=19469
user="$name"
conf_dir="/home/$name/.$name"
data_dir="/var/lib/$name"
#menu="NodeManager"
sent_dir="/var/lib/$name/sentinel"
sent_env="/var/lib/$name/sentinel/venv"
systemd="/etc/systemd/system"
rpcuser=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w10 | head -n1)
rpcpassword=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w22 | head -n1)
function get_confirmation() {
# call when confirmation is required
read -r -p "${1:-Are you sure? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
function current_release () {
latest_release="$( bash <<EOF
curl --silent "https://api.github.com/repos/LIMXTEC/BitCore/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")'
EOF
)"
}
function check_distro() {
# Ubuntu 16.04 & 18.04
if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
if [[ "${VERSION_ID}" != "16.04" ]] && [[ "${VERSION_ID}" != "18.04" ]] ; then
echo "Only Ubuntu 16.04 & 18.04 LTS are supported, exiting."
exit 1
fi
else
echo "Only Ubuntu 16.04 & 18.04 LTS are supported, exiting."
exit 1
fi
}
function install_packages() {
# build packages
apt-get update && apt-get -y upgrade && apt-get -y dist-upgrade &>> ${logfile} &> /dev/null
echo "* Installing required packages, it may take some time to finish"
apt-get -qqy -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils libboost-all-dev jp2a imagemagick &>> ${logfile}
apt-get -qqy -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true install -y software-properties-common &>> ${logfile}
echo "* Adding bitcoin PPA repository"
{ apt-add-repository -yu ppa:bitcoin/bitcoin; apt-get -qq -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true update; } >> ${logfile}
apt-get -qqy -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true install libdb4.8-dev libdb4.8++-dev -y &>> ${logfile}
# only for 18.04 // openssl
if [[ "${VERSION_ID}" == "18.04" ]] ; then
apt-get -qqy -o=Dpkg::Use-Pty=0 install libssl1.0-dev &>> ${logfile}
fi
}
function swapcreate() {
echo -e "* Checking if swap space is needed."
PHYMEM=$(free -g|awk '/^Mem:/{print $2}')
if [ "$PHYMEM" -lt "2" ];
then
echo -e "* Server is running with less than 2G of RAM, creating 2G swap file"
{ dd if=/dev/zero of=/swapfile bs=1024 count=2M; chmod 600 /swapfile; mkswap /swapfile; swapon -a /swapfile; echo '/var/mnode_swap.img none swap sw 0 0' | tee -a /etc/fstab; echo 'vm.swappiness=10' | tee -a /etc/sysctl.conf; echo 'vm.vfs_cache_pressure=50' | tee -a /etc/sysctl.conf; } >> ${logfile}
else
echo -e "* Server running with at least 2G of RAM, no swap needed"
fi
}
function create_user() {
# our new mnode unpriv user acc is added
if id "${user}" >/dev/null 2>&1; then
echo "* User exists already, do nothing" &>> ${logfile}
else
echo "* Adding new system user ${user}"
adduser --disabled-password --gecos "" ${user} &>> ${logfile}
echo "bitcore ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
fi
}
function create_dirs() {
# individual data dirs for now to avoid problems
echo "* Creating masternode directories"
mkdir -p ${conf_dir}
if [ ! -d "${data_dir}/${name}" ]; then
echo "creating data directory ${data_dir}/${name}" &>> ${logfile}
mkdir -p ${data_dir}/${name} &>> ${logfile}
fi
}
function create_sentinel() {
apt-get -qq -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true update &>> ${logfile}
apt-get -qqy -o=Dpkg::Use-Pty=0 -o=Acquire::ForceIPv4=true install virtualenv python-virtualenv &>> ${logfile}
# if code directory does not exists, we create it clone the src
if [ ! -d ${sent_dir} ]; then
{ cd ${data_dir} || exit; git clone https://github.com/LIMXTEC/sentinel.git sentinel; cd sentinel || exit; } >> ${logfile}
else
echo "* Updating the existing sentinel GIT repo"
cd ${sent_dir} ||exit &>> ${logfile}
git pull &>> ${logfile}
fi
rm -f rm sentinel.conf &>> ${logfile}
rm -f rm /test/test_sentinel.conf &>> ${logfile}
# create a globally accessible venv and install sentinel requirements
echo "* Creating a globally accessible venv and install sentinel requirements"
cd ..
mkdir -p ${sent_env}
virtualenv --system-site-packages ${sent_env} &>> ${logfile}
${sent_env}/bin/pip install -r ${sent_dir}/requirements.txt &>> ${logfile}
# create sentinel config file
if [[ "$testnet" -eq 1 ]];
then
if [ ! -f "${sent_dir}/sentinel.conf" ]; then
echo "* Creating sentinel configuration for ${name} masternode on TESTNET" &>> ${logfile}
echo "dash_conf=${conf_dir}/${name}.conf"> ${sent_dir}/sentinel.conf
{ echo "network=testnet"; echo "db_name=${sent_dir}/database/sentinel.db"; echo "db_driver=sqlite"; } >> ${sent_dir}/sentinel.conf
echo "dash_conf=${conf_dir}/${name}.conf"> ${sent_dir}/test/test_sentinel.conf
{ echo "network=testnet"; echo "db_name=${sent_dir}/database/sentinel.db"; echo "db_driver=sqlite"; } >> ${sent_dir}/test/test_sentinel.conf
fi
else
if [ ! -f "${sent_dir}/sentinel.conf" ]; then
echo "* Creating sentinel configuration for ${name} masternode on MAINNET" &>> ${logfile}
echo "dash_conf=${conf_dir}/${name}.conf" > ${sent_dir}/sentinel.conf
{ echo "network=mainnet"; echo "db_name=${sent_dir}/database/sentinel.db"; echo "db_driver=sqlite"; } >> ${sent_dir}/sentinel.conf
echo "dash_conf=${conf_dir}/${name}.conf"> ${sent_dir}/test/test_sentinel.conf
{ echo "network=mainnet"; echo "db_name=${sent_dir}/database/sentinel.db"; echo "db_driver=sqlite"; } >> ${sent_dir}/test/test_sentinel.conf
fi
fi
sudo -u ${user} -- bash -c "cd ${sent_dir} && venv/bin/python bin/sentinel.py" | sudo tee -a ${logfile}
sudo -u ${user} -- bash -c '"* * * * * export SENTINEL_CONFIG=${sent_dir}/sentinel.conf; cd ${sent_dir} && venv/bin/python bin/sentinel.py 2>&1 >> /var/log/sentinel/sentinel-cron.log" | crontab -' | sudo tee -a ${logfile}
echo "* Sentinel contab has been created with ${user} account"
}
function configure_firewall() {
echo "* Configuring firewall rules"
# disallow everything except ssh and masternode inbound ports
{ ufw default deny; ufw logging on; ufw allow ${ssh_port}/tcp; } >> ${logfile}
if [[ "$testnet" -eq 1 ]]; then
{ ufw allow ${tport}/tcp; ufw allow ${trpcport}/tcp; ufw allow ${port}/tcp; ufw allow ${rpcport}/tcp; } >> ${logfile}
else
{ ufw allow ${port}/tcp; ufw allow ${rpcport}/tcp; } >> ${logfile}
fi
# This will only allow 6 connections every 30 seconds from the same IP address.
ufw limit OpenSSH &>> ${logfile}
ufw --force enable &>> ${logfile}
echo "* Firewall ufw is active and enabled on system startup"
}
function create_config() {
rm -f ${conf_dir}/${name}.conf &>> ${logfile}
if [[ "$testnet" -eq 1 ]]; then
echo -e "$name MasterNode will be configured for TESTNET"
cat << EOF >> ${conf_dir}/${name}.conf
rpcuser=$rpcuser
rpcpassword=$rpcpassword
rpcport=$trpcport
rpcallowip=127.0.0.1
listen=1
server=1
daemon=1
testnet=1
EOF
else
echo -e "$name MasterNode will be configured for MAINNET"
cat << EOF >> ${conf_dir}/${name}.conf
rpcuser=$rpcuser
rpcpassword=$rpcpassword
rpcport=$rpcport
rpcallowip=127.0.0.1
listen=1
server=1
daemon=1
EOF
fi
echo -e "* Enter your Masternode Private Key"
read -e KEY
if [[ -z "$KEY" ]]; then
sudo -u ${user} ${daemon_dir}/${daemon} -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name} | sudo tee -a ${logfile}
sleep 5
if [ -z $(pidof ${daemon}) ]; then
echo -e "* $name server couldn't start. Check /var/log/syslog for errors."
exit 1
fi
KEY=$(sudo -u ${user} -- bash -c "${daemon_dir}/${daemon} -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name} masternode genkey") &>> ${logfile}
kill $(pidof ${daemon}) &>> ${logfile}
fi
NODEIP=$(curl -s4 icanhazip.com) &>> ${logfile}
if [[ "$testnet" -eq 1 ]]; then
sudo iptables -t nat -I OUTPUT -d ${NODEIP} -p tcp --dport ${tport} -j REDIRECT --to-ports ${tport}
cat << EOF >> ${conf_dir}/${name}.conf
#externalip=${NODEIP}:${port}
logtimestamps=1
maxconnections=256
masternode=1
staking=0
gen=0
masternodeprivkey=$KEY
masternodeaddr=$NODEIP:$port
EOF
else
sudo iptables -t nat -I OUTPUT -d ${NODEIP} -p tcp --dport ${port} -j REDIRECT --to-ports ${port}
cat << EOF >> ${conf_dir}/${name}.conf
#externalip=${NODEIP}:${port}
logtimestamps=1
maxconnections=256
masternode=1
staking=0
gen=0
masternodeprivkey=$KEY
masternodeaddr=$NODEIP:$port
EOF
fi
}
function create_systemd() {
if [[ "$testnet" -eq 1 ]]; then
rm -f ${systemd}/${name}.testnet.service
echo "* Writing systemd config file ${systemd}/${name}.testnet.service" &>> ${logfile}
touch ${systemd}/${name}.testnet.service
chmod 664 ${systemd}/${name}.testnet.service
cat << EOF >> ${systemd}/${name}.testnet.service
[Unit]
Description=${name} Testnet Service
After=network.target
[Service]
User=${user}
Group=${user}
Type=forking
PIDFile=${data_dir}/${name}/testnet/${name}.pid
ExecStart=${daemon_dir}/${daemon} -daemon -testnet -port=8555 -pid=${data_dir}/${name}/testnet/${name}.pid -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}
ExecStop=${cli_dir}/${cli} stop -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}
Restart=always
RestartSec=5
PrivateTmp=true
TimeoutStopSec=60s
TimeoutStartSec=5s
StartLimitInterval=120s
StartLimitBurst=15
[Install]
WantedBy=multi-user.target
EOF
else
rm -f ${systemd}/${name}.service
echo "* Writing systemd config file ${systemd}/${name}.service" &>> ${logfile}
touch ${systemd}/${name}.service
chmod 664 ${systemd}/${name}.service
cat << EOF >> ${systemd}/${name}.service
[Unit]
Description=${name} Service
After=network.target
[Service]
User=${user}
Group=${user}
Type=forking
PIDFile=${data_dir}/${name}/${name}.pid
ExecStart=${daemon_dir}/${daemon} -daemon -pid=${data_dir}/${name}/${name}.pid -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}
ExecStop=${cli_dir}/${cli} stop -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}
Restart=always
RestartSec=5
PrivateTmp=true
TimeoutStopSec=60s
TimeoutStartSec=5s
StartLimitInterval=120s
StartLimitBurst=15
[Install]
WantedBy=multi-user.target
EOF
fi
}
function set_permissions() {
mkdir -p /var/log/sentinel &>> ${logfile}
chown -R ${user}:${user} ${conf_dir} ${data_dir} /var/log/sentinel ${sent_dir}/database &>> ${logfile}
${daemon_dir}/${daemon} $
# make group permissions same as user, so masternode user can be added to masternode group
chmod -R g=u ${conf_dir} ${data_dir} /var/log/sentinel &>> ${logfile}
}
function clear_all() {
echo "Deleting all ${name} related data!" &>> ${logfile}
if [[ "$testnet" -eq 1 ]]; then
systemctl stop ${name}.testnet.service &>> ${logfile}
rm -f ${systemd}/${name}.testnet.service &>> ${logfile}
else
systemctl stop ${name}.service &>> ${logfile}
rm -f ${systemd}/${name}.service &>> ${logfile}
fi
{ rm -f ${conf_dir}/${name}.conf; rm -rf ${data_dir}; rm -rf ${script:?}/${name}; rm -f ${daemon_dir}/${daemon}; rm -f ${cli_dir}/${cli}; } >> ${logfile}
echo "DONE!"
exit 0
}
function cleanup_after() {
{ apt-get -qqy -o=Dpkg::Use-Pty=0 --force-yes autoremove; apt-get -qqy -o=Dpkg::Use-Pty=0 --force-yes autoclean; rm -rf ${script:?}/${name}; } >> ${logfile}
}
function print_logo() {
cd ~/ || exit
wget -q https://raw.githubusercontent.com/LIMXTEC/BitCore/0.15/src/qt/res/icons/bitcore_logo_horizontal.png 2>&1
convert bitcore_logo_horizontal.png bitcore_logo_horizontal.jpg 2>&1
rm bitcore_logo_horizontal.png
mv bitcore_logo_horizontal.jpg /usr/local/bin/bitcore_logo_horizontal.jpg 2>&1
jp2a -b --colors --width=55 /usr/local/bin/bitcore_logo_horizontal.jpg 2>&1
}
function source_config() {
check_distro
if [[ "$testnet" -eq 1 ]]; then
echo "* Do you want to configure this $name MasterNode on TESTNET?"
echo "* Please type YES to confirm configuration for TESTNET or type anything else or just hit enter for MAINNET"
read -e answer
if [[ "${answer}" == YES ]]; then
testnet=1
else
testnet=0
fi
else
testnet=0
clear
fi
# main block of function logic starts here
if [[ "$update" -eq 1 ]]; then
if [[ ! -f "${daemon_dir}/${daemon}" ]]; then
echo "UPDATE FAILED! Daemon hasn't been found. Please try the normal installation process by omitting the upgrade parameter."
exit 1
fi
if [[ ! -f "${data_dir}/${name}.menu.sh" ]]; then
echo "UPDATE FAILED! Masternode activation file ${name}.menu.sh hasn't been found. Please try the normal installation process by omitting the upgrade parameter."
exit 1
fi
if [ ! -d ${data_dir} ]; then
echo "UPDATE FAILED! ${data_dir} hasn't been found. Please try the normal installation process by omitting the upgrade parameter."
exit 1
fi
fi
echo "************************* Installation Plan *****************************************"
echo ""
if [[ "${update}" -eq 1 ]]; then
echo "Your existing masternode will be updated with... "
echo "$(tput bold)$(tput setaf 2) => ${name} masternode in version ${release} $(tput sgr0)"
else
echo "Your masternode will be installed and configured with... "
echo "$(tput bold)$(tput setaf 2) => ${name} masternode in version ${release} $(tput sgr0)"
fi
#sentinel setup
if [[ "$sentinel" -eq 1 ]]; then
echo "Sentinel configuration will be created and applied."
fi
# start nodes after setup
if [[ "$startnode" -eq 1 ]]; then
echo "The masternode service will be started after the installation."
fi
echo ""
echo "A logfile for this run can be found at the following location:"
echo "${logfile}"
echo ""
echo "*************************************************************************************"
sleep 5
# main routine
if [[ "$update" -eq 0 ]]; then
swapcreate
fi
install_packages
print_logo
build_mn
if [[ "$update" -eq 0 ]]; then
create_user
create_dirs
#sentinel setup
if [[ "$sentinel" -eq 1 ]]; then
echo "* Sentinel setup chosen" &>> ${logfile}
create_sentinel
fi
configure_firewall
create_config
create_systemd
change_password
fi
set_permissions
cleanup_after
if [[ "$update" -eq 1 ]]; then
echo "I need to update the systemctl daemon now, else an error will occur when running 'systemctl enable' on a changed systemd process"
systemctl daemon-reload
fi
final_call
}
function start_daemon() {
if [[ "$testnet" -eq 1 ]]; then
systemctl daemon-reload
sleep 30
systemctl enable ${name}.testnet.service
systemctl start ${name}.testnet.service
sudo -u ${user} -- bash -c "${daemon_dir}/${daemon} -testnet -port=8555 -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}"
else
systemctl daemon-reload
sleep 30
systemctl enable ${name}.service
systemctl start ${name}.service
sudo -u ${user} -- bash -c "${daemon_dir}/${daemon} -conf=${conf_dir}/${name}.conf -datadir=${data_dir}/${name}"
fi
sleep 5
if [[ -z "$(pidof ${daemon})" ]]; then
if [[ "$testnet" -eq 1 ]]; then
echo -e "${name}.testnet.service is not running, please investigate. You should start by running the following commands as root:"
echo "systemctl start ${name}.testnet.service"
echo "systemctl status ${name}.testnet.service"
echo "less /var/log/syslog"
else
echo -e "${name}.service is not running, please investigate. You should start by running the following commands as root:"
echo "systemctl start ${name}.service"
echo "systemctl status ${name}.service"
echo "less /var/log/syslog"
fi
fi
clear
}
function change_password() {
clear
echo "Please enter a password for ${user} user"
passwd bitcore
clear
}
function build_mn() {
# daemon not found compile it
if [[ ! -f ${daemon_dir}/${daemon} ]] || [[ "$update" -eq 1 ]]; then
# if coin directory (name) exists, we remove it, to make a clean git clone
if [[ -d ${script}/${name} ]]; then
{ echo "Deleting ${script}/${name} for clean cloning"; rm -rf ${script:?}/${name}; echo "Stopping $(tput setaf 1)${daemon}$(tput setaf 7) daemon"; } >> ${logfile}
if [[ "$testnet" -eq 1 ]]; then
systemctl stop ${name}.testnet.service &>> ${logfile}
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -testnet stop -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
else
systemctl stop ${name}.service &>> ${logfile}
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli stop -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
fi
fi
cd ${script} || exit &>> ${logfile}
git clone ${git} ${name} &>> ${logfile}
echo " * Entering ${script}/${name} directory."
cd ${script}/${name} || exit &>> ${logfile}
echo "* Checking out desired GIT tag: ${release}"
git checkout ${release} &>> ${logfile}
if [[ "$update" -eq 1 ]]; then
echo "Updated source files, deleting the old daemon NOW!" &>> ${logfile}
rm -f ${daemon_dir}/${daemon}
# old daemon must be removed before compilation. Would be better to remove it afterwards, however not possible with current structure
if [ -f ${daemon_dir}/${daemon} ]; then
echo "UPDATE FAILED! Daemon ${daemon_dir}/${daemon} couldn't be removed. Please engage with us in Discord https://discord.gg/j9GJjdZ Thank you!"
exit 1
fi
fi
# compilation starts here
chmod u+x share/genbuild.sh
chmod u+x src/leveldb/build_detect_platform
chmod u+x ./autogen.sh && ./autogen.sh
./autogen.sh
./configure --without-gui
make
cd ${script}/${name}/src || exit
strip bitcored
strip bitcore-cli
chmod 755 bitcored bitcore-cli
mv bitcored ${daemon_dir}/${daemon}
mv bitcore-cli ${cli_dir}/${cli}
else
echo "* Daemon already in place at ${daemon_dir}/${daemon}, not compiling"
fi
# if it's not available after compilation, theres something wrong
if [[ ! -f ${daemon_dir}/${daemon} ]]; then
echo "COMPILATION FAILED! Please engage with us in Discord https://discord.gg/j9GJjdZ Thank you!"
exit 1
fi
}
#WIP
function create_script() {
if [[ "$testnet" -eq 1 ]]; then
(cat > ${data_dir}/${name}.menu.sh) << "EOF" &> /dev/null
#!/bin/bash
latest_release=$(curl --silent "https://api.github.com/repos/LIMXTEC/BitCore/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")')
pause(){
read -p "Press [Enter] key to continue..." fackEnterKey
}
one(){
echo "Start Masternode"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcored -testnet -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
pause
}
two(){
echo "Check systemd status"
systemctl status bitcore.testnet.service
pause
}
three(){
echo "Run Masternode GetInfo"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -testnet -getinfo -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
pause
}
four(){
echo "Edit Masternode config file"
nano /home/bitcore/.bitcore/bitcore.conf
pause
}
five(){
echo "Check Masternode Status"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore -testnet masternode status"
pause
}
six(){
echo "Check Masternode Sync Status"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore -testnet mnsync status"
pause
}
# seven(){
# echo "Check Masternode Debug"
# sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore -testnet masternode debug"
# pause
# }
# six(){
# echo "Activate Sentinel"
# sudo -u bitcore -- bash -c "SENTINEL_CONFIG=/var/lib/bitcore/sentinel/sentinel.conf; cd /var/lib/bitcore/sentinel && venv/bin/python /bin/sentinel.py"
# $(tput setaf 7)pause
# }
# seven(){
# echo "Edit Sentinel Config file"
# nano /var/lib/bitcore/sentinel/sentinel.conf
# }
# eight(){
# echo "Add Sentinel Cron Job"
# sudo -u bitcore -- bash -c '"* * * * * export SENTINEL_CONFIG=/var/lib/bitcore/sentinel/sentinel.conf; cd /var/lib/bitcore/sentinel && venv/bin/python /bin/sentinel.py 2>&1 >> /var/log/sentinel/sentinel-cron.log" | crontab -'
# pause
# }
show_menus() {
clear
jp2a -b --colors --width=55 /usr/local/bin/bitcore_logo_horizontal.jpg
echo "~~~~~~~~~~~~~~~~~~~~~"
echo " M A I N - M E N U - T E S T N E T"
echo "~~~~~~~~~~~~~~~~~~~~~"
echo "1. Start Masternode"
echo "2. Check systemd status"
echo "3. Run Masternode GetInfo"
echo "4. Edit Masternode config file"
echo "5. Check Masternode Status"
echo "6. Check Masternode Sync Status"
# echo "7. Check Masternode Debug"
# echo "9. Add Sentinel Cron Job"
echo "7. $(tput setaf 1)Exit$(tput setaf 7)"
}
read_options(){
local choice
read -p "Enter choice [ 1 - 7 ] " choice
case ${choice} in
1) one ;;
2) two ;;
3) three ;;
4) four ;;
5) five ;;
6) six ;;
# 7) seven ;;
# 8) eight ;;
7) exit 0;;
*) echo -e " $(tput setaf 1)Error...$(tput setaf 7)" && sleep 2
esac
}
trap '' SIGINT SIGQUIT SIGTSTP
while true
do
show_menus
read_options
done
EOF
else
(cat > ${data_dir}/${name}.menu.sh) << "EOF" &> /dev/null
#!/bin/bash
latest_release=$(curl --silent "https://api.github.com/repos/LIMXTEC/BitCore/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")')
pause(){
read -p "Press [Enter] key to continue..." fackEnterKey
}
one(){
echo "Start Masternode"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcored -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
pause
}
two(){
echo "Check systemd status"
systemctl status bitcore.service
pause
}
three(){
echo "Run Masternode GetInfo"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -getinfo -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore"
pause
}
four(){
echo "Edit Masternode config file"
nano /home/bitcore/.bitcore/bitcore.conf
pause
}
five(){
echo "Check Masternode Status"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore masternode status"
pause
}
six(){
echo "Check Masternode Sync Status"
sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore mnsync status"
pause
}
# seven(){
# echo "Check Masternode Debug"
# sudo -u bitcore -- bash -c "/usr/local/bin/bitcore-cli -conf=/home/bitcore/.bitcore/bitcore.conf -datadir=/var/lib/bitcore masternode debug"
# pause
# }
# six(){
# echo "Activate Sentinel"
# sudo -u bitcore -- bash -c "SENTINEL_CONFIG=/var/lib/bitcore/sentinel/sentinel.conf; cd /var/lib/bitcore/sentinel && venv/bin/python /bin/sentinel.py"
# $(tput setaf 7)pause
# }
# seven(){
# echo "Edit Sentinel Config file"
# nano /var/lib/bitcore/sentinel/sentinel.conf
# }
# eight(){
# echo "Add Sentinel Cron Job"
# sudo -u bitcore -- bash -c '"* * * * * export SENTINEL_CONFIG=/var/lib/bitcore/sentinel/sentinel.conf; cd /var/lib/bitcore/sentinel && venv/bin/python /bin/sentinel.py 2>&1 >> /var/log/sentinel/sentinel-cron.log" | crontab -'
# pause
# }
show_menus() {
clear
jp2a -b --colors --width=55 /usr/local/bin/bitcore_logo_horizontal.jpg
echo "~~~~~~~~~~~~~~~~~~~~~"
echo " M A I N - M E N U"
echo "~~~~~~~~~~~~~~~~~~~~~"
echo "1. Start Masternode"
echo "2. Check systemd status"
echo "3. Run Masternode GetInfo"
echo "4. Edit Masternode config file"
echo "5. Check Masternode Status"
echo "6. Check Masternode Sync Status"
# echo "7. Check Masternode Debug"
# echo "9. Add Sentinel Cron Job"
echo "7. $(tput setaf 1)Exit$(tput setaf 7)"
}
read_options(){
local choice
read -p "Enter choice [ 1 - 7 ] " choice
case ${choice} in
1) one ;;
2) two ;;
3) three ;;
4) four ;;
5) five ;;
6) six ;;
# 7) seven ;;
# 8) eight ;;
7) exit 0;;
*) echo -e " $(tput setaf 1)Error...$(tput setaf 7)" && sleep 2
esac
}
trap '' SIGINT SIGQUIT SIGTSTP
while true
do
show_menus
read_options
done
EOF
fi
}
function get_help(){
clear
echo "##############---BitCore Masternode Install Script Help---#########################"
echo "masternode.sh, version ${scriptversion}";
echo "Latest BitCore release is: $(tput setaf 1)${latest_release}$(tput setaf 7)";
echo "Usage example:";
echo "./masternode.sh (-r|--release) string [(-h|--help)] [(-c|--clear)] [(-s|--sentinel)] [(-n|--startnode)] [(-u|--update)] [(-t|--testnet)]";
echo "$(tput setaf 3)./masternode.sh -r ${latest_release} -s -n$(tput setaf 7) will install the masternode in the latest release, install sentinel and start the node for block syncing";
echo "Options:";
echo "-r or --release string: Release to be installed. REQUIRED.";
echo "-h or --help: Displays this information.";
echo "-c or --clear: Clear ALL ${name} masternode data";
echo "-s or --sentinel: Adds sentinel";
echo "-n or --startnode: Start masternode after installation to sync with blockchain";
echo "-u or --update: Updates the ${name} masternode deamon and cli";
echo "-t or --testnet: Configures the masternode for TESTNET";
exit 1;
}
function final_call() {
clear
echo "**************************! ALMOST DONE !******************************"
if [[ "$update" -eq 0 ]]; then
echo "Your $(tput setaf 1)${name}$(tput setaf 7) masternode installation and configuration is almost complete"
else
echo "Your $(tput setaf 1)${name}$(tput setaf 7) masternode daemon has been updated!"
fi
# config script - wip
if [[ "$update" -eq 0 ]]; then
create_script
fi
chmod u+x ${data_dir}/${name}.menu.sh
if [[ "$startnode" -eq 1 ]]; then
echo ""
echo "** Your node is starting up"
start_daemon
fi
if [[ "$testnet" -eq 1 ]]; then
mainnet=TESTNET
else
mainnet=MAINNET
fi
print_logo
echo "==================================================================================="
echo "$(tput setaf 1)${name}$(tput setaf 7) masternode service is up and running as user $(tput setaf 2)$user$(tput setaf 7)"
echo "and it is listening on RPC port $(tput setaf 3) $rpcport$(tput setaf 7) on ${mainnet}"
echo "Configuration file is: $(tput setaf 3) $conf_dir/${name}.conf$(tput setaf 7)"
echo "Sentinel Configuration file is: $(tput setaf 3)${sent_dir}/sentinel.conf$(tput setaf 7)"
echo "Sentinel Cron log file is: $(tput setaf 3)/var/log/sentinel/sentinel-cron.log$(tput setaf 7)"
echo "VPS_IP:PORT $(tput setaf 3)$NODEIP:$port$(tput setaf 7)"
echo "Masternode PRIVATEKEY is: $(tput setaf 3)${KEY}$(tput setaf 7)"
echo "Your RPC user is: $(tput setaf 3)$rpcuser$(tput setaf 7)"
echo "Your RPC password is: $(tput setaf 3)$rpcpassword$(tput setaf 7)"
echo "A logfile for this run can be found at the following location:"
echo "$(tput setaf 3)$logfile$(tput setaf 7)"
#echo "Run $(tput setaf 3)systemctl start ${name}.service$(tput setaf 7) as root to start the masternode service"
#echo "Run $(tput setaf 3)systemctl status ${name}.service$(tput setaf 7) as root to check if the service is running"
#echo "Run $(tput setaf 3)systemctl stop ${name}.service$(tput setaf 7) as root to stop the masternode service"
echo "$(tput setaf 1)Please run this script to check the health of your masternode$(tput setaf 7)"
echo "$(tput setaf 3) => $(tput setaf 7)bash $(tput setaf 3)${data_dir}/${name}.menu.sh$(tput setaf 7)"
echo "==================================================================================="
su -c bash bitcore
cd ~/ || exit
}
##################------------Menu()---------#####################################
# Declare vars. Flags initalizing to 0.
current_release
clear=0;
sentinel=0;
update=0;
debug=0;
startnode=0;
testnet=0;
# Execute getopt
ARGS=$(getopt -o "hr:csudnt" -l "help,release,clear,sentinel,update,debug,startnode,testnet" -n "masternode.sh" -- "$@");
#Bad arguments
if [ $? -ne 0 ];
then
get_help;
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-h|--help)
shift;
get_help;
;;
-r|--release)
shift;
if [ -n "$1" ];
then
release="$1";
shift;
fi
;;
-c|--clear)
shift;
clear="1";
;;
-s|--sentinel)
shift;
sentinel="1";
;;
-u|--update)
shift;
update="1";
;;
-d|--debug)
shift;
debug="1";
;;
-n|--startnodes)
shift;
startnode="1";
;;
-t|--testnet)
shift;
testnet="1";
;;
--)
shift;
break;
;;
esac
done
# Check required arguments
if [ -z "$release" ]
then
get_help;
fi
# Check required arguments
if [[ "$clear" -eq 1 ]]; then
get_confirmation "Would you really like to WIPE ALL DATA!? YES/NO y/n" && clear_all
exit 0
fi
if [[ $release != "$latest_release" ]]; then
get_confirmation "Would you really like to install and configure another release! The latest release is: ${latest_release}? YES/NO y/n" && source_config
exit 0
fi
main() {
echo "starting" &> ${logfile}
# debug
if [ "$debug" -eq 1 ]; then
echo "********************** VALUES AFTER CONFIG SOURCING: ************************"
fi
# configuration
source_config
# debug
if [ "$debug" -eq 1 ]; then
echo "********************** VALUES AFTER CONFIG SOURCING: ************************"
fi
}
main "$@"