-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharonode
1881 lines (1651 loc) · 57.2 KB
/
aronode
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
#
# A useful script for managing an Arionum testnet and/or mainnet node.
# For more information see http://www.arionum.com
#
# Github: https://github.com/KyleFromOhio/arionum-scripts
# Docs: http://www.aro.wiki
# Example: bash aronode mainnet install
usage() {
echo "USAGE: "
echo " $ aronode <testnet|mainnet> <install|upgrade|reset|restart|status|...>"
echo " install -- install Arionum node and services from scratch"
echo " upgrade -- upgrade all node/server software; php/myql/nginx/packages/node"
echo " update -- upgrade Arionum node & aronode code; ie. git pull"
echo " remove -- purge nginx, php-fpm, mysql AND arionum content"
echo " additional options ..."
echo " backup -- will dump essential tables to a gzipped sql file"
echo " import -- will import a gzipped .sql file; 3rd argument (optional): <filename.sql.gz>"
echo " change -- change hostname ip/domain that peers see you as"
echo " diff -- view changes/code updates of Aronode's main repository"
echo " firewall -- configure UFW firewall. 3rd argument: <install|on|off|remove>"
echo " fix -- runs through some breaking change fix functions eg. fix020()"
echo " mysql -- update my.cnf file. Arguments: <get|set|restart> <setting_name> <value>"
echo " peers -- clear peers table. Arguments: <reset>"
echo " pop -- fix stuck syncing; 3rd argument: <integer> of blocks to delete"
echo " rebuild -- empty db; download remote .sql dump; import chain data; 3rd arg (optional): <latest>"
echo " require -- install aronode script requirements"
echo " reset -- set Aronode environment config to testnet or mainnet; then rebuild blockchain"
echo " restart -- restart all Arionum related services; php/nginx/mysql"
echo " resync-accounts -- check ALL account balances table; time consuming."
echo " stop -- stop public facing Arionum related services; php/nginx"
echo " status -- check status of services"
echo " sync -- runs sanity.php manually so you can monitor the sync; CTRL+C to kill"
}
# DEBUG: prevent chaos; halt on error
#set -e
# VARIABLES
ERROR=false
ERROR_CHAR=" ⌦ ERROR->"
CHECK_CHAR=" √"
INFO_CHAR=" INFO:"
TASK_CHAR=" ▷ TASK:"
LOG_CHAR=" LOG:"
BLANK_CHAR=" "
DOMAIN=""
DOMAINFOUND="unknown"
WEBROOT="/var/www"
SECUREOWNER="nobody"
WEBOWNER="www-data"
WEBGROUP="www-data"
ARONODE_PATH="/root/scripts"
WEBCONFIGFILE="config.inc.php"
WEBCONFIGFILESAMPLE="config-sample.inc.php"
WEBCONFIGPATH="include/config.inc.php"
WEBCONFIGPATHSAMPLE="include/config-sample.inc.php"
WEBCONFIGPATHBACKUP="include/config-backup.inc.php"
DBNAME="aronode"
DBUSER="aronode"
DBUSER_PASS=""
DBROOT_PASS=""
MYCNF_PATH="/root/.my.cnf"
MYSQL_CONFIG_PATH="/etc/mysql/my.cnf"
# import variables
BACKUP_FILENAME="aronode_dump"
# backup only backs up these tables
BLOCKCHAIN_TABLES="blocks accounts transactions mempool masternode"
# used in rebuild function; to re-download just delete LOCAL_SNAPSHOT
LOCAL_SNAPSHOT="arionum-daily.sql.gz"
LOCAL_SNAPTEMP="arionum-daily.sql"
REMOTE_SNAPSHOT="https://aro.pxgamer.xyz/daily/daily.sql.gz"
PACKAGES_GENERAL="git pigz dirmngr bc vim-nox htop ncdu cpulimit curl tar gzip wget zip unzip pv"
PHPv="7.3"
PHPFPM_SERVICE="php$PHPv-fpm"
PACKAGES_PHP="php$PHPv-cli php$PHPv-curl php$PHPv-mysql php$PHPv-mbstring php$PHPv-common php$PHPv-bcmath php$PHPv-gmp php$PHPv-gd php$PHPv-fpm"
DATER=`date +%Y%m%d`
TIMESTAMPER=`date '+%Y-%m-%d %H:%M:%S'`
LOG_FILE="$ARONODE_PATH/aronode_$DATER.log"
VHOSTS=()
# detect all folders in WEBDIR
while IFS= read -d $'\0' -r VHOST ; do
VHOSTS=("${VHOSTS[@]}" "$VHOST")
done < <(find $WEBROOT -maxdepth 1 -mindepth 1 -type d -print0)
# Setup logging
END_LOG_MSG=""
startLogging(){
exec > >(tee -ia $LOG_FILE)
exec 2>&1
echo "$TIMESTAMPER: start log"
END_LOG_MSG="$INFO_CHAR output logged to $LOG_FILE"
}
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
# Adding LC_ALL LANG and LANGUAGE to user profile
if [[ -f ~/.profile && ! "$(grep "en_US.UTF-8" ~/.profile)" ]]; then
{ echo "LC_ALL=en_US.UTF-8"; echo "LANG=en_US.UTF-8"; echo "LANGUAGE=en_US.UTF-8"; } >> ~/.profile
elif [[ -f ~/.bash_profile && ! "$(grep "en_US.UTF-8" ~/.bash_profile)" ]]; then
{ echo "LC_ALL=en_US.UTF-8"; echo "LANG=en_US.UTF-8"; echo "LANGUAGE=en_US.UTF-8"; } >> ~/.bash_profile
fi
# For root user only
if [[ $EUID -ne 0 ]]; then
echo "$ERROR_CHAR aronode must be run as root user. Type first: sudo su -"
ERROR=true
fi
UNAME=$(uname)-$(uname -m) # Linux-x86_64
OS_DISTRO=`lsb_release -i -s` # Ubuntu
OS_VERSION=`lsb_release -r -s` # 16.04
OS_NAME=`lsb_release -c -s` # xenial
# Only tested on these OS
if [ "$UNAME" != "Linux-x86_64" ]; then
echo "$ERROR_CHAR Must use a 64 bit Linux-x86_64 system. Wrong operating system."
ERROR=true
fi
if [ "$OS_DISTRO" != "Ubuntu" ]; then
echo "$ERROR_CHAR Script is written for Ubuntu xenial/bionic LTS only. Wrong operating system."
ERROR=true
fi
if [[
"$OS_NAME" != "xenial" &&
"$OS_NAME" != "bionic"
]]; then
echo "$ERROR_CHAR Script is written for Ubuntu xenial/bionic LTS only. Wrong operating system."
ERROR=true
fi
# Get version of aronode script
VERSION_ARONODE="0.1.0"
aronodeCheckVersion() {
if [ -f $ARONODE_PATH/VERSION ]; then
VERSION_ARONODE=`cat $ARONODE_PATH/VERSION`
fi
DATER=`date`
echo -e "$INFO_CHAR Aronode Script v$VERSION_ARONODE "
echo -e "$INFO_CHAR $DATER"
}
aronodeCheckVersion
# GET 1st argument: testnet | mainnet
ARG_CONFIG_MODE=$1
if [ -z "$ARG_CONFIG_MODE" ]; then
ARG_CONFIG_MODE="unknown"
fi
# allow help command
case "$ARG_CONFIG_MODE" in
usage|help|h|-h|--help)
usage
exit 0
;;
version)
echo -e "$INFO_CHAR Aronode v$VERSION_ARONODE"
exit 0
;;
esac
# if not help then only other option is testnet|mainnet
if [[ -z "$ARG_CONFIG_MODE" || ( "$ARG_CONFIG_MODE" != "testnet" && "$ARG_CONFIG_MODE" != "mainnet" ) ]]; then
echo "$ERROR_CHAR First argument must be either testnet or mainnet."
ERROR=true
fi
# GET 2nd argument: install | upgrade | remove
ARG_CALL=""
if [[ -z "2" || (
"$2" != "backup" &&
"$2" != "change" &&
"$2" != "diff" &&
"$2" != "firewall" &&
"$2" != "fix" &&
"$2" != "install" &&
"$2" != "import" &&
"$2" != "mysql" &&
"$2" != "peers" &&
"$2" != "pop" &&
"$2" != "rebuild" &&
"$2" != "remove" &&
"$2" != "require" &&
"$2" != "reset" &&
"$2" != "restart" &&
"$2" != "resync-accounts" &&
"$2" != "stop" &&
"$2" != "status" &&
"$2" != "sync" &&
"$2" != "upgrade" &&
"$2" != "update" &&
"$2" != "version"
) ]]; then
echo "$ERROR_CHAR Second argument is invalid."
ERROR=true
else
ARG_CALL="$2"
fi
# DEBUG: print variables
# ( set -o posix ; set ) | cat; exit 1;
exitAsError() {
sleep 1
exit 1 # halt script with 1 error
}
# EXIT the script if there's error
if [ $ERROR == true ]; then
usage
exitAsError
fi
# FUNCTIONS
# check if testnet or mainnet
nodeCheckConfigMode() {
#nodeFindDomainRoot
NODE_CONFIG_MODE="unknown"
if [[ -d "$DOMAINROOT" && -f "$DOMAINROOT/$WEBCONFIGPATH" ]]; then
# Determine if testnet or mainnet
CHECK_CONFIG=$(grep -Po "(?<=$_config\['testnet'\])[^;]*" "${DOMAINROOT}/$WEBCONFIGPATH")
# trim = and spaces
CHECK_CONFIG=$(echo -e "${CHECK_CONFIG//=}")
CHECK_CONFIG=$(echo -e "${CHECK_CONFIG}" | tr -d '[:space:]')
if [[ "$CHECK_CONFIG" == "false" ]]; then
NODE_CONFIG_MODE="mainnet"
fi
if [[ "$CHECK_CONFIG" == "true" ]]; then
NODE_CONFIG_MODE="testnet"
fi
fi
echo "$INFO_CHAR Network: $NODE_CONFIG_MODE"
}
generatePassword() {
PASS_LENGTH=16
echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $PASS_LENGTH | head -n 1)
}
# find something like /var/www/mydomain.com
nodeFindDomainRoot() {
DOMAINROOT="unknown"
if [ -d "$WEBROOT" ]; then
DOMAINROOT=$(find "$WEBROOT" -name "$WEBCONFIGFILE" | sed "s~/$WEBCONFIGPATH~~" | tail -n 1)
fi
# 2nd try if no custom config yet
if [ -f "$DOMAINROOT/$WEBCONFIGPATHSAMPLE" ]; then
DOMAINROOT=$(find "$WEBROOT" -name "$WEBCONFIGFILESAMPLE" | sed "s~/$WEBCONFIGPATHSAMPLE~~" | tail -n 1)
fi
if [ "$DOMAINROOT" == "" ]; then
DOMAINROOT="unknown"
fi
echo "$INFO_CHAR Webroot: $DOMAINROOT"
DOMAINFOUND=$(basename "$DOMAINROOT")
}
# apt upgrade
serverUpgrade() {
echo "$TASK_CHAR Updating server packages and installing required tools ..."
# backup my.cnf
if [ -f "/etc/mysql/my.cnf" ]; then
cp /etc/mysql/my.cnf /etc/mysql/my.cnf-BACKUP;
fi
export DEBIAN_FRONTEND=noninteractive
add-apt-repository universe -y
apt-get -qq update && apt-get -qq dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" && apt-get -qq autoremove -y
packageInstallGeneral
echo "$CHECK_CHAR Packages updated."
# apply any fixes
# delete vhost defaults (if any)
fix021
# set/fix mysql config
mysqlOptimizeConfig
}
# check php version
checkPhpVersion() {
PHP_VERSION_CLI=$(php -v | head -n1 | cut -d" " -f2 | cut -c1-3)
}
# upgrade php version
packageUpgradePhp() {
checkPhpVersion
# upgrade php fpm and vhost from 7.2 to $PHPv
if [ "$PHP_VERSION_CLI" == "7.2" ]; then
echo "$TASK_CHAR Upgrading PHP 7.2 to $PHPv ..."
# stop services
systemctl stop nginx
systemctl stop php7.2-fpm
# disable 7.2 service
systemctl disable php7.2-fpm
# install new php packages
packageInstallPhp
# enable/start new php fpm service
systemctl enable $PHPFPM_SERVICE
# make sure fix v0.2.0 is run first
fix020
# find arionum vhosts and update config to use new php version
for KEY in "${!VHOSTS[@]}";
do
DIR_NAME=${VHOSTS[$KEY]}
THISWEBNAME=$(basename $DIR_NAME)
# update vhost
THISVHOST="/etc/nginx/conf.d/$THISWEBNAME.conf"
sed -i "s/php7.2-fpm/php$PHPv-fpm/g" $THISVHOST
echo "$CHECK_CHAR Vhost updated: $THISVHOST"
done
# check ok?
nginx -t
# restart nginx and php fpm
nodeStart
fi
}
# create tmp/db-update for code to do a db update
flagDbUpdate(){
#nodeFindDomainRoot
if [ "$DOMAINROOT" != "unknown" ]; then
PWD=`pwd`
cd $DOMAINROOT
touch tmp/db-update
chown $WEBOWNER:$WEBGROUP tmp/db-update
chmod 775 tmp/db-update
echo "$CHECK_CHAR Database flagged for an update."
cd $PWD
else
echo "$ERROR_CHAR flagDbUpdate() Could not locate Arionum node software in '$DOMAINROOT'"
exitAsError
fi
}
# delete tmp/sanity-lock if exists
removeSanityLock(){
#nodeFindDomainRoot
if [ "$DOMAINROOT" != "unknown" ]; then
PWD=`pwd`
cd $DOMAINROOT
if [ -f $DOMAINROOT/tmp/sanity-lock ]; then
rm $DOMAINROOT/tmp/sanity-lock
echo "$CHECK_CHAR sanity-lock removed."
fi
cd $PWD
else
echo "$ERROR_CHAR removeSanityLock() Could not locate Arionum node software in '$DOMAINROOT'"
exitAsError
fi
}
# fix perms in $DOMAINROOT
fixNodePermissions() {
#nodeFindDomainRoot
echo "$TASK_CHAR Setting permissions..."
if [ "$DOMAINROOT" != "unknown" ]; then
PWD=`pwd`
cd $DOMAINROOT
chown -R $SECUREOWNER $DOMAINROOT/*
chown -R $SECUREOWNER $DOMAINROOT/.[^.]*
chgrp -R $WEBGROUP $DOMAINROOT/*
chgrp -R $WEBGROUP $DOMAINROOT/.[^.]*
if [ -d $DOMAINROOT/tmp ]; then
chmod 775 -R $DOMAINROOT/tmp
chown -R $WEBOWNER $DOMAINROOT/tmp/*
fi
if [ -f $DOMAINROOT/tmp/db-update ]; then
chmod 775 -R $DOMAINROOT/tmp/db-update
fi
echo "$CHECK_CHAR Permissions set."
cd $PWD
else
echo "$ERROR_CHAR fixNodePermissions() Could not locate Arionum node software in '$DOMAINROOT'"
exitAsError
fi
}
nodeRestart() {
nodeStop # stop nginx and php-fpm
nodeStopDb # stop mysql
nodeStart # start all
}
# stop nginx and php-fpm
nodeStop() {
echo "$TASK_CHAR Stopping Nginx, PHP ..."
killSanity
upNginx
if [ "$UP_NGINX" == "yes" ]; then
service nginx stop
fi
upPhp
if [ "$UP_PHP" == "yes" ]; then
service $PHPFPM_SERVICE stop
fi
}
# stop mysql
nodeStopDb() {
echo "$TASK_CHAR Stopping Mysql ..."
upMysql
if [ "$UP_MYSQL" == "yes" ]; then
service mysql stop
fi
}
# start nginx, php-fpm, mysql (if not running)
nodeStart() {
# BUG: check if my.cnf is missing OR is symlink; if so attempt restore to default
mysqlFixConfig
service $PHPFPM_SERVICE start
service nginx start
service mysql start
# check for errors
upMysql
upNginx
upPhp
if [ "$UP_NGINX" == "no" ]; then
echo "$ERROR_CHAR nodeStart() nginx service failed. Check nginx logs in /var/log/";
exitAsError
else
echo "$CHECK_CHAR nginx: started."
fi
if [ "$UP_PHP" == "no" ]; then
echo "$ERROR_CHAR nodeStart() php-fpm service failed. Check php-fpm logs in /var/log/";
exitAsError
else
echo "$CHECK_CHAR php fpm: started."
fi
if [ "$UP_MYSQL" == "no" ]; then
echo "$ERROR_CHAR nodeStart() mysql service failed. Check mysql logs in /var/log/";
exitAsError
else
echo "$CHECK_CHAR mysql: started."
fi
}
nodeRefresh() {
echo "$TASK_CHAR Refreshing node ..."
# documentation states to load node twice
curl -sSL -o /dev/null http://127.0.0.1:80/ 2>&1
curl -sSL -o /dev/null http://127.0.0.1:80/ 2>&1
curl -sSL -o /dev/null http://127.0.0.1:80/ 2>&1
echo "$CHECK_CHAR Node refreshed."
}
# check if mysql is up; NOTE: 0=true(no failures); 1=failure
upMysql() {
CMD="mysql";
UP=$(pgrep "$CMD" | wc -l);
UP_MYSQL="yes";
if [ $UP -eq 0 ]; then UP_MYSQL="no"; fi
}
# check if php-fpm is up; NOTE: 0=true(no failures); 1=failure
upPhp() {
CMD="php-fpm";
UP=$(pgrep "$CMD" | wc -l);
UP_PHP="yes";
if [ $UP -eq 0 ]; then UP_PHP="no"; fi
}
# check if nginx is up; NOTE: 0=true(no failures); 1=failure
upNginx() {
CMD="nginx";
UP=$(pgrep "$CMD" | wc -l);
UP_NGINX="yes";
if [ $UP -eq 0 ]; then UP_NGINX="no"; fi
}
# check if ntp is up; NOTE: 0=true(no failures); 1=failure
upNtp() {
CMD="ntp";
UP=$(pgrep "$CMD" | wc -l);
UP_NTP="yes";
if [ $UP -eq 0 ]; then UP_NTP="no"; fi
}
# check if ufw is up; NOTE: 0=true(no failures); 1=failure
upUfw() {
UP=$(ufw status | grep active | wc -l);
UP_UFW="yes";
if [ $UP -eq 0 ]; then UP_UFW="no"; fi
}
packageInstallNginx(){
echo "$TASK_CHAR packageInstallNginx() Installing Nginx ..."
upNginx
if [ "$UP_NGINX" == "no" ]; then
PWD=`pwd`
cd /tmp/ && wget -O nginx_signing.key http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
sh -c "echo 'deb http://nginx.org/packages/mainline/ubuntu/ '$(lsb_release -cs)' nginx' > /etc/apt/sources.list.d/nginx.list"
add-apt-repository ppa:ondrej/nginx-mainline -y
apt-get -qq update -y
apt-get -qq install nginx -y
service nginx restart
cd $PWD
else
echo "$INFO_CHAR Nginx detected as running already.";
fi
# check if nginx installed and running
upNginx
if [ "$UP_NGINX" == "no" ]; then
echo "$ERROR_CHAR packageInstallNginx() failed to start nginx service.";
exitAsError
else
echo "$CHECK_CHAR Nginx is installed and running."
fi
}
packageInstallPhp(){
echo "$TASK_CHAR packageInstallPhp() Installing PHP $PHPv and required modules ..."
apt-get -qq install software-properties-common -y
if [ $OS_NAME == "xenial" ]; then
apt-get -qq install python-software-properties -y
fi
upPhp
if [ "$UP_PHP" == "no" ]; then
add-apt-repository ppa:ondrej/php -y
apt-get -qq update -y
apt-get -qq install $PACKAGES_PHP -y
else
echo "$INFO_CHAR php-fpm detected as running already.";
fi
# check if php-fpm installed and running
upPhp
if [ "$UP_PHP" == "no" ]; then
echo "$ERROR_CHAR packageInstallPhp() failed to start php-fpm service.";
exitAsError
else
echo "$CHECK_CHAR PHP FPM $PHPv is installed and running."
fi
}
mysqlOptimizeConfig(){
if [ -f "$MYSQL_CONFIG_PATH" ]; then
# up max connections
MYSQL_VARIABLE="max_connections"
MYSQL_VALUE=1000
mysqlSetConfig
fi
}
mysqlFixConfig(){
# BuG: if apt upgrade broke config; fix it
if [[ -f "/etc/mysql/my.cnf.dpkg-new" && ! -e "$MYSQL_CONFIG_PATH" ]]; then
# remove symlink
if [ -L "$MYSQL_CONFIG_PATH" ]; then
unlink $MYSQL_CONFIG_PATH;
fi
# copy backup config OR new config
cp /etc/mysql/my.cnf.dpkg-new $MYSQL_CONFIG_PATH;
mysqlOptimizeConfig
fi
}
mysqlGetConfig(){
if [ -z "$MYSQL_VARIABLE" ]; then
echo "$ERROR_CHAR invalid request. missing mysql variable argument.";
return 0
fi
MYSQL_VALUE=$(awk -F "=" "/$MYSQL_VARIABLE/ {print \$2}" $MYSQL_CONFIG_PATH | tr -d ' ')
if [ -z "$MYSQL_VALUE" ]; then
echo "$ERROR_CHAR mysql variable not found in $MYSQL_CONFIG_PATH";
return 0
else
echo "$CHECK_CHAR Variable Found: $MYSQL_VARIABLE = $MYSQL_VALUE in $MYSQL_CONFIG_PATH";
fi
}
mysqlGetRootCnfPass(){
MYCNF_ROOTPASS=""
if [ -f "$MYCNF_PATH" ]; then
MYCNF_ROOTPASS=$(awk -F "=" "/password=/ {print \$2}" $MYCNF_PATH | tr -d ' ')
fi
}
mysqlSetConfig(){
if [ -z "$MYSQL_VARIABLE" ]; then
echo "$ERROR_CHAR invalid command. mysql variable not defined";
return 0
fi
if [ -z "$MYSQL_VALUE" ]; then
echo "$ERROR_CHAR invalid command mysql value not defined";
return 0
fi
if [ -f "$MYSQL_CONFIG_PATH" ]; then
sed -i "s/^\($MYSQL_VARIABLE\s*=\s*\).*\$/\1$MYSQL_VALUE/" $MYSQL_CONFIG_PATH;
echo "$INFO_CHAR set mysql variable $MYSQL_VARIABLE to $MYSQL_VALUE in $MYSQL_CONFIG_PATH";
fi
}
packageInstallMysql(){
echo "$TASK_CHAR packageInstallMysql() Installing Mysql (MariaDB) ..."
apt-get -qq install software-properties-common -y
apt-get -qq install apt-transport-https -y
upMysql
if [ "$UP_MYSQL" == "no" ]; then
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sh -c "echo 'deb [arch=amd64,i386] https://mirrors.evowise.com/mariadb/repo/10.2/ubuntu '$(lsb_release -cs)' main' > /etc/apt/sources.list.d/MariaDB-10.2.list"
apt-get -qq update -y
# skip password prompt; will set later
export DEBIAN_FRONTEND=noninteractive
apt-get -qq install mariadb-server mariadb-client -y
# set/fix mysql config
mysqlFixConfig
mysqlOptimizeConfig
else
echo "$INFO_CHAR mysql detected as running already.";
fi
# check if marioadb installed and running
upMysql
if [ "$UP_MYSQL" == "no" ]; then
echo "$ERROR_CHAR packageInstallMysql() failed to start mysql(mariadb) service.";
exitAsError
else
echo "$CHECK_CHAR Mysql (MariaDB) is installed and running."
fi
}
packagePurgeMysql() {
echo "$TASK_CHAR packagePurgeMysql() Purging mysql ..."
nodeStopDb
apt-get -qq --purge remove mariadb-server mariadb-client -y
apt-get -qq --purge remove mysql* -y
rm -rf /var/lib/mysql
rm -rf /var/log/mysql
if [ -f "$MYCNF_PATH" ]; then
rm "$MYCNF_PATH"
fi
apt-get -qq autoremove -y
apt-get -qq autoclean -y
echo "$CHECK_CHAR Mysql purged."
}
packageInstallNtp(){
echo "$TASK_CHAR packageInstallNtp() Installing Ntp; Syncing server time to UTC ..."
apt-get -qq install ntp ntpdate -yyq
service ntp stop
timedatectl set-timezone UTC
service ntp stop
ntpdate pool.ntp.org
service ntp start
# check if ntp installed and running and set to UTC
upNtp
if [ "$UP_NTP" == "no" ]; then
echo "$ERROR_CHAR packageInstallNtp() failed to start ntp service.";
exitAsError
else
echo "$CHECK_CHAR Date/Time updated."
fi
}
packageInstallGeneral() {
echo "$TASK_CHAR packageInstallGeneral() Installing required packages ..."
add-apt-repository universe -y
apt-get -qq install apt-transport-https -y
apt-get -qq install $PACKAGES_GENERAL -y
echo "$CHECK_CHAR Server packages installed."
}
setMysqlRootPass() {
mysqlGetRootCnfPass
if [ -f "$MYCNF_PATH" ]; then
sudo -H mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '${MYCNF_ROOTPASS}'";
sudo -H mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '${MYCNF_ROOTPASS}'";
sudo -H mysql -e "FLUSH PRIVILEGES;"
echo "$CHECK_CHAR root pass set and stored in $MYCNF_PATH"
fi
}
hardenMysql() {
sudo -H mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');"
sudo -H mysql -e "DELETE FROM mysql.user WHERE User='';"
sudo -H mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test_%';"
sudo -H mysql -e "FLUSH PRIVILEGES;"
setMysqlRootPass
}
hardenPHP() {
# TODO: php.ini, php-fpm conf
return 0
}
hardenNginx() {
# TODO: badbots, nginx.conf
return 0
}
hardenSSH() {
# TODO: ssh conf; change port; install fail2ban
return 0
}
checkSshPort() {
if [ -f /etc/ssh/sshd_config ]; then
SSH_PORT=$(awk '/^\s*Port\s+([0-9]{2,5})/{print}' /etc/ssh/sshd_config | cut -d'#' -f1 | cut -d';' -f1| tr -dc '0-9')
fi
if [ "$SSH_PORT" == "" ]; then
SSH_PORT=22
fi
}
hardenNetwork() {
# TODO: sysctl
return 0
}
hardenFirewall() {
echo "$TASK_CHAR hardenFirewall() Configuring UFW firewall ..."
apt-get -qq install ufw -y
service ufw restart
# check if ufw installed and running
upUfw
if [ "$UP_UFW" == "no" ]; then
echo "$ERROR_CHAR UFW firewall is not running.";
exitAsError
else
checkSshPort
ufw --force enable
ufw allow proto udp to 0.0.0.0/0 port 123 # ntp
ufw allow proto tcp to 0.0.0.0/0 port 22 # ssh
ufw allow proto tcp to 0.0.0.0/0 port 2222 # alternative ssh
if [ $SSH_PORT -gt 0 ]; then
ufw allow proto tcp to 0.0.0.0/0 port $SSH_PORT
fi
ufw allow proto tcp to 0.0.0.0/0 port 80 # http web
ufw allow proto tcp to 0.0.0.0/0 port 443 # https web
ufw default deny incoming
ufw default allow outgoing
service ufw restart
echo "$CHECK_CHAR UFW firewall configured."
fi
echo "$INFO_CHAR running # ufw status"
ufw status
}
enableFirewall() {
echo "$TASK_CHAR enableFirewall() Enabling UFW firewall ..."
# check if ufw installed and running
checkUfwInstalled
upUfw
if [ "$UP_UFW" == "no" ]; then
echo "$ERROR_CHAR UFW firewall is not running.";
exitAsError
else
ufw --force enable
echo "$CHECK_CHAR UFW firewall enabled."
echo "$INFO_CHAR running # ufw status"
ufw status
fi
}
disableFirewall() {
echo "$TASK_CHAR disableFirewall() Disabling UFW firewall ..."
# check if ufw installed and running
checkUfwInstalled
upUfw
if [ "$UP_UFW" == "no" ]; then
echo "$ERROR_CHAR UFW firewall is not running.";
exit 1 # halt script with error
else
ufw disable
echo "$CHECK_CHAR UFW firewall disabled."
fi
}
removeFirewall() {
echo "$TASK_CHAR removeFirewall() Removing UFW firewall ..."
# check if ufw installed and running
checkUfwInstalled
upUfw
if [ "$UP_UFW" == "yes" ]; then
service ufw stop
fi
apt-get -qq --purge remove ufw -y
echo "$CHECK_CHAR UFW firewall removed."
}
checkUfwInstalled() {
installedUFW="$(type -p "ufw")"
if [ ! installedUFW ]; then
echo "$ERROR_CHAR UFW not installed. To install try 'apt install ufw' instead."
exitAsError
fi
}
setupMysqlDatabase(){
echo "$TASK_CHAR Setting up mysql database..."
# check if config exists; if so use
if [ -f $MYCNF_PATH ]; then
DBROOT_PASS=$(grep -Po "(?<=password=)[^;]*" "$MYCNF_PATH")
DBUSER_PASS=$(grep -Po "(?<=aronode_pass=)[^;]*" "$MYCNF_PATH")
echo "$INFO_CHAR database config found in $MYCNF_PATH"
else
# Generate new user password(s)
DBROOT_PASS=$(generatePassword)
DBUSER_PASS=$(generatePassword)
# Reset root pass
echo "$INFO_CHAR setting root pass."
sudo -H mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '${DBROOT_PASS}'";
sudo -H mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '${DBROOT_PASS}'";
echo "$INFO_CHAR root pass set."
# Create, write to and save mysql config to .my.cnf file
echo "$INFO_CHAR saving root pass to $MYCNF_PATH"
touch $MYCNF_PATH
chmod 0600 $MYCNF_PATH
> $MYCNF_PATH
echo "[client]" >> $MYCNF_PATH
echo "user=root" >> $MYCNF_PATH
echo "password=$DBROOT_PASS" >> $MYCNF_PATH
echo "# aronode_user=$DBUSER" >> $MYCNF_PATH
echo "# aronode_pass=$DBUSER_PASS" >> $MYCNF_PATH
echo "$INFO_CHAR root pass saved to $MYCNF_PATH"
fi
# create db and user if not exists
if [ -d /var/lib/mysql/$DBNAME ] ; then
echo "$INFO_CHAR $DBNAME database already exists."
else
sudo -H mysql -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET utf8 */;"
echo "$TASK_CHAR create arionum database"
fi
USER_EXISTS="$(sudo -H mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"
if [ "$USER_EXISTS" = 1 ]; then
echo "$INFO_CHAR $DBUSER mysql user already exists."
else
echo "$TASK_CHAR create arionum database user"
sudo -H mysql -e "CREATE USER ${DBUSER}@localhost IDENTIFIED BY '${DBUSER_PASS}';"
sudo -H mysql -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${DBUSER}'@'localhost';"
fi
# flush permissions
sudo -H mysql -e "FLUSH PRIVILEGES;"
echo "$CHECK_CHAR Database setup finished."
}
setupNginxVhost() {
echo "$TASK_CHAR Setting up default website in Nginx ..."
# check if directories exists; create otherwise
DIR="/etc/nginx/sites-enabled"
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
DIR="/etc/nginx/sites-available"
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
DOMAINROOT=$WEBROOT/$DOMAIN
if [ ! -d $DOMAINROOT ]; then
mkdir -p $DOMAINROOT
fi
# secure domain root
PWD=`pwd`
cd $DOMAINROOT
chown $SECUREOWNER $WEBROOT
chgrp $WEBGROUP $WEBROOT
chmod 0755 $DOMAINROOT
cd $PWD
# update nginx user to WEBUSER:WEBGROUP:
sed -i "s/user nginx;/user $WEBOWNER $WEBOWNER;/g" /etc/nginx/nginx.conf
# delete vhost defaults
fix021
if [ ! -f $ARONODE_PATH/aronode.vhost ]; then
echo "$ERROR_CHAR setupNginxVhost() $ARONODE_PATH/aronode.vhost not found. Check github, download it, try again.";
exitAsError
fi
# enable vhost
VHOST_PATH_AVAILABLE="/etc/nginx/conf.d/$DOMAIN.conf"
cat $ARONODE_PATH/aronode.vhost > $VHOST_PATH_AVAILABLE
sed -i "s/__DOMAIN__/$DOMAIN/g" $VHOST_PATH_AVAILABLE
sed -i "s~__WEBROOT__~$WEBROOT~g" $VHOST_PATH_AVAILABLE
sed -i "s/__PHPVERSION__/$PHPv/g" $VHOST_PATH_AVAILABLE
if [ ! -L "$VHOST_PATH_ENABLED" ]; then
ln -s $VHOST_PATH_AVAILABLE $VHOST_PATH_ENABLED
echo "$CHECK_CHAR vhost setup in Nginx at $VHOST_PATH_AVAILABLE"
fi
service nginx restart
# check if nginx installed and running
upNginx
if [ "$UP_NGINX" == "no" ]; then
echo "$ERROR_CHAR setupNginxVhost() failed to start nginx service. Bad vhost config?";
exitAsError
else
echo "$CHECK_CHAR Nginx restarted and running."
fi
}
# v0.2.0 changes: move old sites-enabled/*vhosts to /etc/nginx/conf.d/*conf
fix020(){
echo "$TASK_CHAR Running fix for v0.2.0 ..."
if [ -f /etc/nginx/conf.d/vhosts.conf ]; then
rm /etc/nginx/conf.d/vhosts.conf
fi
if [ -L "/etc/nginx/sites-enabled/$DOMAINFOUND.vhost" ]; then
unlink /etc/nginx/sites-enabled/$DOMAINFOUND.vhost
fi
if [ -f /etc/nginx/sites-available/$DOMAINFOUND.vhost ]; then
mv /etc/nginx/sites-available/$DOMAINFOUND.vhost /etc/nginx/conf.d/$DOMAINFOUND.conf
echo "$INFO_CHAR Fixed $DOMAINFOUND"
fi
echo "$CHECK_CHAR Fix v0.2.0 complete."
}
# v0.2.1 changes: delete every kind of default vhost you can find
fix021(){
echo "$TASK_CHAR Running fix for v0.2.1 ..."
# delete default vhost file(s)
if [ -f /etc/nginx/conf.d/default.conf ]; then
rm /etc/nginx/conf.d/default.conf
fi
# delete default vhost file(s)
if [ -f /etc/nginx/conf.d/default ]; then
rm /etc/nginx/conf.d/default
fi
if [ -f /etc/nginx/sites-enabled/default ]; then
rm /etc/nginx/sites-enabled/default
fi
if [ -L /etc/nginx/sites-available/default ]; then
unlink /etc/nginx/sites-available/default
fi
if [ -L /etc/nginx/sites-available/default.conf ]; then
unlink /etc/nginx/sites-available/default.conf
fi
# delete old vhost config (if exists)
if [ -f /etc/nginx/conf.d/vhosts.conf ]; then
rm /etc/nginx/conf.d/vhosts.conf
fi
echo "$CHECK_CHAR Fix v0.2.1 complete."
}
gitInitNode() {
echo "$TASK_CHAR Installing Arionum node software ..."
if [[ -d "$DOMAINROOT" ]]; then
PWD=`pwd`
cd $DOMAINROOT
# check git exists so it won't halt process on error
if [[ ! -d .git ]]; then
git init
git remote add origin https://github.com/arionum/node.git
fi
git fetch --all --prune
git checkout master
git config core.fileMode false
if [ ! -f "$DOMAINROOT/$WEBCONFIGPATH" ]; then
cp -f $DOMAINROOT/$WEBCONFIGPATHSAMPLE $DOMAINROOT/$WEBCONFIGPATH
fi
echo "$CHECK_CHAR Download of Arionum node finished."
cd $PWD
else
echo "$ERROR_CHAR gitInitNode() $DOMAINROOT path not found.";
exitAsError
fi
}
gitPullAronode() {
PWD=`pwd`
cd $ARONODE_PATH
if [ -d .git ]; then
echo "$TASK_CHAR Updating aronode script ..."
git fetch --all
git reset --hard origin/master
chmod +x $ARONODE_PATH/aronode