summaryrefslogtreecommitdiffstats
path: root/aegir/tools/BOND.sh.txt
blob: 0a995bf2b360691462c03ca130be1f9048f39bda (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
#!/bin/bash


###----------------------------------------###
###
###  Barracuda-Octopus-Nginx-Drupal Tuner
###
###  Copyright (C) 2010-2014 Omega8.cc
###  noc@omega8.cc www.omega8.cc
###
###  This program is free software. You can
###  redistribute it and/or modify it under
###  the terms of the GNU GPL as published by
###  the Free Software Foundation, version 2
###  or later.
###
###  This program is distributed in the hope
###  that it will be useful, but WITHOUT ANY
###  WARRANTY; without even the implied
###  warranty of MERCHANTABILITY or FITNESS
###  FOR A PARTICULAR PURPOSE. See the GNU GPL
###  for more details.
###
###  You should have received a copy of the
###  GNU GPL along with this program.
###  If not, see http://www.gnu.org/licenses/
###
###  Code: https://code.aegir.cc/aegir
###
###----------------------------------------###


###----------------------------------------###
### HOW-TO: run it with bash, not with sh  ###
###----------------------------------------###
###
### $ bash BOND.sh.txt
###
### Note: to restore default values it is
###       enough to start this script with
###       any values defined below and answer
###       NO when it prompts for confirmation
###       "Are you ready to tune your Aegir".
###


###----------------------------------------###
### EDITME                                 ###
###----------------------------------------###
###
### Enter below the settings you wish to use.
###


###----------------------------------------###
### Hostmaster root directory - /var/aegir
###
### Note: most of values tuned by this script
###       are server-vide, while some, like
###       mod_evasive settings will affect
###       only sites hosted on the Aegir
###       Satellite Instance defined below.
###
_TUNE_HOSTMASTER=/data/disk/o1


###----------------------------------------###
### Nginx server mod_evasive - default ON
###
### Note: running verify task on any SITE
###       will restore default value ON
###       for that site only, while TUNER
###       will turn OFF/ON this feature
###       for all sites hosted on the
###       Hostmaster defined above.
###
_TUNE_NGINX_CONNECT=OFF


###----------------------------------------###
### Nginx server fastcgi timeout - default 180
###
### Note: running verify task on the SERVER
###       in the Hostmaster created
###       by Barracuda (not Octopus!)
###       will restore default value
###       for the server and all existing
###       Aegir Satellite Instances.
###
_TUNE_NGINX_TIMEOUT=3600


###----------------------------------------###
### Nginx server firewall limit - default 300
###
### Note: don't change the default value
###       if you are the only visitor, or
###       you will lock yourself easily.
###
###       The default value 300 means the
###       firewall limit is OFF because
###       it scans only the last 300 lines
###       of your web server log file.
###
###       If you will set this value to 100
###       then every visitor IP with more
###       than 100 out of the last 300
###       requests will be locked.
###
###       Only dynamic requests (pages) are
###       counted because static files like
###       images are generally not logged.
###
_TUNE_NGINX_FIREWALL=300


###----------------------------------------###
### Database server timeout - default 3600
###
_TUNE_SQL_TIMEOUT=3600


###----------------------------------------###
### PHP-FPM server timeout - default 180
###
_TUNE_PHP_FPM_TIMEOUT=3600


###----------------------------------------###
### PHP-CLI server timeout - default 3600
###
_TUNE_PHP_CLI_TIMEOUT=7200


###----------------------------------------###
### TUNER VERSION                          ###
###----------------------------------------###
###
### _AEGIR_VERSION=HEAD
### _AEGIR_VERSION=BOA-2.2.5
###
_AEGIR_VERSION=HEAD


###----------------------------------------###
### DON'T EDIT ANYTHING BELOW THIS LINE    ###
###----------------------------------------###

_INSTALLER_VERSION=BOA-2.2.6-dev
_NOW=`date +%y%m%d-%H%M`
_RAM=`free -mto | grep Mem: | awk '{ print $2 }'`
_THISHOST=`uname -n`
_SPINNER=YES
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
SHELL=/bin/bash
if [ -n "${STY+x}" ]; then
  _SPINNER=NO
fi
_PHP52_API=20060613
_PHP52_VERSION=5.2.17
_PHP53_API=20090626
_PHP53_VERSION=5.3.28
_PHP54_API=20100525
_PHP54_VERSION=5.4.29
_PHP55_API=20121212
_PHP55_VERSION=5.5.13


###---### Functions
#
# Noticeable messages.
msg () {
  echo "Tuner [`date`] ==> $*"
}
# Simple prompt.
prompt_yes_no () {
if [ "$_AUTOPILOT" = "YES" ] ; then
  return 0
else
  while true ; do
  printf "$* [Y/n] "
  read answer
  if [ -z "$answer" ] ; then
    return 0
  fi
  case $answer in
    [Yy]|[Yy][Ee][Ss])
    return 0
    ;;
    [Nn]|[Nn][Oo])
    return 1
    ;;
    *)
    echo "Please answer yes or no"
    ;;
  esac
 done
fi
}
#
# Silent runner.
st_runner () {
  CMD="$1"
  touch busy
  if [ "$_SPINNER" = "YES" ] ; then
    bash $_SRCDIR/spinner busy &
  fi
  if $CMD >> $_LOG; then
    rm busy
    sleep 1
    return 0
  else
    rm busy
    sleep 1
    echo "$CMD failed.  Error (if any): $?"
    echo " "
    echo "Displaying the last 15 lines of $_LOG to help troubleshoot this problem"
    echo "If you see any error with advice to run 'dpkg --configure -a', run this"
    echo "command first and choose default answer, then run this installer again"
    echo " "
    tail -15 $_LOG
    return 1
  fi
}
#
# Small spinner.
mrun () {
  CMD="$1"
  touch busy
  if [ "$_SPINNER" = "YES" ] ; then
    bash $_SRCDIR/spinner busy &
  fi
  if $CMD >> $_LOG; then
    rm busy
    sleep 1
    return 0
  fi
}
#
# Find correct IP.
find_correct_ip () {
  _LOC_IP=`echo $(getent ahostsv4 $_LOC_DOM) | cut -d: -f2 | awk '{ print $1}'`
}
#
# Fix php.ini files to remove suhosin.so
fix_php_ini_suhosin () {
  if [ -e "$_THIS_FILE" ] ; then
    _SUHOSIN_INI_TEST=$(grep "extension=suhosin.so" $_THIS_FILE)
    if [[ "$_SUHOSIN_INI_TEST" =~ "extension=suhosin.so" ]] ; then
      sed -i "s/.*suhosin.*//g" $_THIS_FILE &> /dev/null
    fi
  fi
}
#
# Fix php.ini files to add mailparse.so
fix_php_ini_mailparse () {
  if [ -e "$_THIS_FILE" ] ; then
    _MAILPARSE_INI_TEST=$(grep "extension=mailparse.so" $_THIS_FILE)
    if [[ "$_MAILPARSE_INI_TEST" =~ "extension=mailparse.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo "extension=mailparse.so" >> $_THIS_FILE
    fi
  fi
}
#
# Fix php.ini files to add jsmin.so
fix_php_ini_jsmin () {
  if [ -e "$_THIS_FILE" ] ; then
    _JSMIN_INI_TEST=$(grep "extension=jsmin.so" $_THIS_FILE)
    if [[ "$_JSMIN_INI_TEST" =~ "extension=jsmin.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo "extension=jsmin.so" >> $_THIS_FILE
    fi
  fi
}
#
# Fix php.ini files to add redis.so
fix_php_ini_redis () {
  if [ -e "$_THIS_FILE" ] ; then
    _REDIS_INI_TEST=$(grep "extension=redis.so" $_THIS_FILE)
    if [[ "$_REDIS_INI_TEST" =~ "extension=redis.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo "extension=redis.so" >> $_THIS_FILE
    fi
  fi
}
#
# Fix php.ini file to add newrelic.ini
fix_php_ini_newrelic () {
  _NR_TPL="/opt/tmp/$_BOA_REPO_NAME/aegir/conf/newrelic.ini"
  if [ -e "$_THIS_FILE" ] ; then
    _NEWRELIC_INI_TEST_A=$(grep "extension=newrelic.so" $_THIS_FILE)
    if [[ "$_NEWRELIC_INI_TEST_A" =~ "extension=newrelic.so" ]] ; then
      _DO_NOTHING=YES
    else
      cat $_NR_TPL >> $_THIS_FILE
    fi
    _NEWRELIC_INI_TEST_B=$(grep "newrelic.framework.drupal.modules" $_THIS_FILE)
    if [[ "$_NEWRELIC_INI_TEST_B" =~ "newrelic.framework.drupal.modules" ]] ; then
      _DO_NOTHING=YES
    else
      echo "newrelic.framework.drupal.modules = 1" >> $_THIS_FILE
    fi
    sed -i "s/REPLACE_WITH_REAL_KEY/$_NEWRELIC_KEY/g" $_THIS_FILE &> /dev/null
    sed -i "s/license_key=//g" $_THIS_FILE &> /dev/null
  fi
}
#
# Fix all php.ini files to add newrelic.ini
fix_php_ini_newrelic_all () {
  if [ -e "/etc/newrelic/newrelic.cfg" ] ; then
    if [ -z "$_NEWRELIC_KEY" ] ; then
      _NEWRELIC_KEY=`grep license_key /etc/newrelic/newrelic.cfg`
      _NEWRELIC_KEY=`echo -n $_NEWRELIC_KEY | tr -d "\n"`
    fi
    _THIS_FILE=/opt/php52/etc/php52.ini
    fix_php_ini_newrelic
    _THIS_FILE=/opt/php52/lib/php.ini
    fix_php_ini_newrelic

    _THIS_FILE=/opt/php53/etc/php53.ini
    fix_php_ini_newrelic
    _THIS_FILE=/opt/php53/lib/php.ini
    fix_php_ini_newrelic

    _THIS_FILE=/opt/php54/etc/php54.ini
    fix_php_ini_newrelic
    _THIS_FILE=/opt/php54/lib/php.ini
    fix_php_ini_newrelic

    _THIS_FILE=/opt/php55/etc/php55.ini
    fix_php_ini_newrelic
    _THIS_FILE=/opt/php55/lib/php.ini
    fix_php_ini_newrelic
  fi
}
#
# Fix FMP php.ini file to add opcache.so
fix_php_ini_opcache () {
  if [ -e "$_THIS_FILE" ] ; then
    if [ "$1" = "55" ] ; then
      if [[ "$_ZEND_OPCACHE_VERSION" =~ "master" ]] ; then
        _OPCACHE_SRC=YES
      else
        _OPCACHE_SRC=NO
      fi
    fi
    _OPCACHE_INI_TEST=$(grep "opcache.so" $_THIS_FILE)
    if [[ "$_OPCACHE_INI_TEST" =~ "opcache.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo ";"                                   >> $_THIS_FILE
      echo "; Zend OPcache"                      >> $_THIS_FILE
      if [ "$1" = "55" ] && [ "$_OPCACHE_SRC" = "NO" ] ; then
        echo "extension=opcache.so"              >> $_THIS_FILE
      else
        echo "zend_extension=\"$_OPCACHE_SO\""   >> $_THIS_FILE
      fi
      echo "opcache.enable=1"                    >> $_THIS_FILE
      echo "opcache.memory_consumption=181"      >> $_THIS_FILE
      echo "opcache.interned_strings_buffer=8"   >> $_THIS_FILE
      echo "opcache.max_accelerated_files=64000" >> $_THIS_FILE
      echo "opcache.revalidate_freq=60"          >> $_THIS_FILE
      echo "opcache.enable_file_override=1"      >> $_THIS_FILE
      echo "opcache.inherited_hack=1"            >> $_THIS_FILE
      echo "opcache.dups_fix=1"                  >> $_THIS_FILE
      echo "opcache.log_verbosity_level=0"       >> $_THIS_FILE
      echo "opcache.fast_shutdown=1"             >> $_THIS_FILE
      echo ";"                                   >> $_THIS_FILE
    fi
  fi
}
#
# Fix all FMP php.ini files to add Zend OPcache
fix_php_ini_opcache_all () {

  _THIS_FILE=/opt/php52/etc/php52.ini
  _OPCACHE_SO="/opt/php52/lib/php/extensions/no-debug-non-zts-$_PHP52_API/opcache.so"
  fix_php_ini_opcache "52"

  _THIS_FILE=/opt/php53/etc/php53.ini
  _OPCACHE_SO="/opt/php53/lib/php/extensions/no-debug-non-zts-$_PHP53_API/opcache.so"
  fix_php_ini_opcache "53"

  _THIS_FILE=/opt/php54/etc/php54.ini
  _OPCACHE_SO="/opt/php54/lib/php/extensions/no-debug-non-zts-$_PHP54_API/opcache.so"
  fix_php_ini_opcache "54"

  _THIS_FILE=/opt/php55/etc/php55.ini
  _OPCACHE_SO="/opt/php55/lib/php/extensions/no-debug-non-zts-$_PHP55_API/opcache.so"
  fix_php_ini_opcache "55"

}
#
# Fix php.ini file to add geos.so
fix_php_ini_geos () {
  if [ -e "$_THIS_FILE" ] ; then
    _GEOS_INI_TEST=$(grep "extension=geos.so" $_THIS_FILE)
    if [[ "$_GEOS_INI_TEST" =~ "extension=geos.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo "extension=geos.so" >> $_THIS_FILE
    fi
  fi
}
#
# Fix 5.3 php.ini files to add geos.so
fix_php_ini_geos_all () {
  if [ "$_PHP_GEOS" = "YES" ] || [[ "$_XTRAS_LIST" =~ "GEO" ]] ; then
    _THIS_FILE=/opt/php53/etc/php53.ini
    fix_php_ini_geos
    _THIS_FILE=/opt/php53/lib/php.ini
    fix_php_ini_geos
  fi
}
#
# Fix php.ini file to add mongo.so
fix_php_ini_mongo () {
  if [ -e "$_THIS_FILE" ] ; then
    _MONGODB_INI_TEST=$(grep "extension=mongo.so" $_THIS_FILE)
    if [[ "$_MONGODB_INI_TEST" =~ "extension=mongo.so" ]] ; then
      _DO_NOTHING=YES
    else
      echo "extension=mongo.so" >> $_THIS_FILE
    fi
  fi
}
#
# Fix 5.3 php.ini files to add mongo.so
fix_php_ini_mongo_all () {
  if [ "$_PHP_MONGODB" = "YES" ] || [[ "$_XTRAS_LIST" =~ "MNG" ]] ; then
    _THIS_FILE=/opt/php53/etc/php53.ini
    fix_php_ini_mongo
    _THIS_FILE=/opt/php53/lib/php.ini
    fix_php_ini_mongo
  fi
}
#
# Update PHP Config.
update_php_conf () {
  if [ -z "$_THISHTIP" ] ; then
    _LOC_DOM="$_THISHOST"
    find_correct_ip
    _THISHTIP="$_LOC_IP"
  fi
  if [ ! -e "/opt/etc/fpm" ] || [ ! -e "/opt/etc/fpm/fpm-pool-common.conf" ] ; then
    mkdir -p /opt/etc/fpm
  fi
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm-pool-common.conf /opt/etc/fpm/fpm-pool-common.conf

  if [ ! -e "/var/www/www55" ] ; then
    adduser --system --group --home /var/www/www55 www55 &> /dev/null
    usermod -aG www-data www55 &> /dev/null
  fi
  if [ ! -e "/opt/php55/etc/php55.ini" ] || [ ! -e "/opt/php55/etc/pool.d/www55.conf" ]  ; then
    mkdir -p /opt/php55/etc/pool.d
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini
  fi
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm55-pool-www.conf /opt/php55/etc/pool.d/www55.conf

  if [ ! -e "/var/www/www54" ] ; then
    adduser --system --group --home /var/www/www54 www54 &> /dev/null
    usermod -aG www-data www54 &> /dev/null
  fi
  if [ ! -e "/opt/php54/etc/php54.ini" ] || [ ! -e "/opt/php54/etc/pool.d/www54.conf" ] ; then
    mkdir -p /opt/php54/etc/pool.d
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini
  fi
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm54-pool-www.conf /opt/php54/etc/pool.d/www54.conf

  if [ ! -e "/var/www/www53" ] ; then
    adduser --system --group --home /var/www/www53 www53 &> /dev/null
    usermod -aG www-data www53 &> /dev/null
  fi
  if [ ! -e "/opt/php53/etc/php53.ini" ] || [ ! -e "/opt/php53/etc/pool.d/www53.conf" ] ; then
    mkdir -p /opt/php53/etc/pool.d
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini
  fi
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm53-pool-www.conf /opt/php53/etc/pool.d/www53.conf

  if [ ! -e "/opt/php52/etc/php52.ini" ] ; then
    mkdir -p /opt/php52/etc
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52.ini /opt/php52/etc/php52.ini
  fi
  if [ ! -e "/opt/php55/lib/php.ini" ] ; then
    mkdir -p /opt/php55/lib
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini
  fi
  if [ ! -e "/opt/php54/lib/php.ini" ] ; then
    mkdir -p /opt/php54/lib
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini
  fi
  if [ ! -e "/opt/php53/lib/php.ini" ] ; then
    mkdir -p /opt/php53/lib
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini
  fi
  if [ ! -e "/opt/php52/lib/php.ini" ] ; then
    mkdir -p /opt/php52/lib
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-cli.ini /opt/php52/lib/php.ini
  fi
  if [ "$_CUSTOM_CONFIG_PHP55" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
    cp -af /opt/php55/etc/php55.ini /var/backups/dragon/t/etc-php55.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini                     &> /dev/null
    cp -af /opt/php55/lib/php.ini /var/backups/dragon/t/lib-php55.ini-pre-$_INSTALLER_VERSION-$_NOW   &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini                   &> /dev/null
  fi
  if [ "$_CUSTOM_CONFIG_PHP54" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
    cp -af /opt/php54/etc/php54.ini /var/backups/dragon/t/etc-php54.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini                     &> /dev/null
    cp -af /opt/php54/lib/php.ini /var/backups/dragon/t/lib-php54.ini-pre-$_INSTALLER_VERSION-$_NOW   &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini                   &> /dev/null
  fi
  if [ "$_CUSTOM_CONFIG_PHP53" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
    cp -af /opt/php53/etc/php53.ini /var/backups/dragon/t/etc-php53.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini                     &> /dev/null
    cp -af /opt/php53/lib/php.ini /var/backups/dragon/t/lib-php53.ini-pre-$_INSTALLER_VERSION-$_NOW   &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini                   &> /dev/null
  fi
  if [ "$_CUSTOM_CONFIG_PHP52" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
    cp -af /opt/php52/etc/php52.ini /var/backups/dragon/t/etc-php52.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52.ini /opt/php52/etc/php52.ini                     &> /dev/null
    cp -af /opt/php52/lib/php.ini /var/backups/dragon/t/lib-php52.ini-pre-$_INSTALLER_VERSION-$_NOW   &> /dev/null
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-cli.ini /opt/php52/lib/php.ini                   &> /dev/null
  fi
  ###
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-fpm.conf /opt/php55/etc/php55-fpm.conf &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-fpm.conf /opt/php54/etc/php54-fpm.conf &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-fpm.conf /opt/php53/etc/php53-fpm.conf &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-fpm.conf /opt/php52/etc/php52-fpm.conf &> /dev/null
  ###
  sed -i "s/listen.allowed_clients =.*/listen.allowed_clients = 127.0.0.1,$_THISHTIP/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
  sed -i "s/>127.0.0.1</\>127.0.0.1,$_THISHTIP\</g"  /opt/php52/etc/php52-fpm.conf &> /dev/null
  sed -i "s/listen.mode =.*/listen.mode = 0660/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
  ###

  _THIS_FILE=/opt/php55/etc/php55.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse
  _THIS_FILE=/opt/php55/lib/php.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse

  _THIS_FILE=/opt/php54/etc/php54.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse
  _THIS_FILE=/opt/php54/lib/php.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse

  _THIS_FILE=/opt/php53/etc/php53.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse
  _THIS_FILE=/opt/php53/lib/php.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse

  _THIS_FILE=/opt/php52/etc/php52.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse
  _THIS_FILE=/opt/php52/lib/php.ini
  fix_php_ini_redis
  fix_php_ini_jsmin
  fix_php_ini_suhosin
  fix_php_ini_mailparse

  rm -f /etc/php5/conf.d/{opcache.ini,apc.ini,imagick.ini,memcached.ini,redis.ini,suhosin.ini,newrelic.ini} &> /dev/null
  fix_php_ini_newrelic_all
  fix_php_ini_geos_all
  fix_php_ini_mongo_all
  fix_php_ini_opcache_all

  ###
  sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php52/etc/php52.ini &> /dev/null
  sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php52/lib/php.ini   &> /dev/null
  sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php53/etc/php53.ini &> /dev/null
  sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php53/lib/php.ini   &> /dev/null
  ###
  sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php52/etc/php52.ini &> /dev/null
  sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php52/lib/php.ini   &> /dev/null
  sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php53/etc/php53.ini &> /dev/null
  sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php53/lib/php.ini   &> /dev/null
  ###
}
#
restore_default_php () {
  msg "INFO: Restoring default PHP configuration"
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini   &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini   &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini &> /dev/null
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini   &> /dev/null
}
#
tune_php () {
  msg "INFO: Tuning PHP configuration"
  if [ "$_TUNE_PHP_FPM_TIMEOUT" -lt "60" ] ; then
    _TUNE_PHP_FPM_TIMEOUT=60
  fi
  # PHP-FPM pools
  sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/pool.d/*.conf                                         &> /dev/null
  sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/php*-fpm.conf                                         &> /dev/null
  sed -i "s/180/$_TUNE_PHP_FPM_TIMEOUT/g" /opt/etc/fpm/fpm-pool-common.conf                                       &> /dev/null
  # PHP-FPM INI
  sed -i "s/^default_socket_timeout =.*/default_socket_timeout = $_TUNE_PHP_FPM_TIMEOUT/g" /opt/php*/etc/php*.ini &> /dev/null
  sed -i "s/^max_execution_time =.*/max_execution_time = $_TUNE_PHP_FPM_TIMEOUT/g" /opt/php*/etc/php*.ini         &> /dev/null
  sed -i "s/^max_input_time =.*/max_input_time = $_TUNE_PHP_FPM_TIMEOUT/g" /opt/php*/etc/php*.ini                 &> /dev/null
  # Redis config should sync with PHP-FPM
  sed -i "s/^timeout .*/timeout $_TUNE_PHP_FPM_TIMEOUT/g" /etc/redis/redis.conf                                   &> /dev/null
  # PHP-CLI INI
  sed -i "s/^max_execution_time =.*/max_execution_time = $_TUNE_PHP_CLI_TIMEOUT/g" /opt/php*/lib/php.ini          &> /dev/null
  sed -i "s/^max_input_time =.*/max_input_time = $_TUNE_PHP_CLI_TIMEOUT/g" /opt/php*/lib/php.ini                  &> /dev/null
  sed -i "s/^default_socket_timeout =.*/default_socket_timeout = $_TUNE_PHP_CLI_TIMEOUT/g" /opt/php*/lib/php.ini  &> /dev/null
}
#
restore_default_sql () {
  msg "INFO: Restoring default SQL configuration"
  sed -i "s/.*check_for_crashed_tables/#check_for_crashed_tables/g" /etc/mysql/debian-start &> /dev/null
  if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
    if [ "$_CUSTOM_CONFIG_SQL" = "YES" ] ; then
      _DO_NOTHING=YES
    else
      cp -af /etc/mysql/my.cnf /var/backups/dragon/t/my.cnf-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
      cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/my.cnf.txt /etc/mysql/my.cnf
      sed -i "s/.*slow_query_log/#slow_query_log/g"           /etc/mysql/my.cnf &> /dev/null
      sed -i "s/.*long_query_time/#long_query_time/g"         /etc/mysql/my.cnf &> /dev/null
      sed -i "s/.*slow_query_log_file/#slow_query_log_file/g" /etc/mysql/my.cnf &> /dev/null
      if [ ! -e "/etc/mysql/skip-name-resolve.txt" ] ; then
        sed -i "s/.*skip-name-resolve/#skip-name-resolve/g"   /etc/mysql/my.cnf &> /dev/null
      fi
    fi
  fi
  mv -f /etc/mysql/my.cnf-pre* /var/backups/dragon/t/ &> /dev/null
  sed -i "s/.*default-table-type/#default-table-type/g" /etc/mysql/my.cnf &> /dev/null
  sed -i "s/.*language/#language/g" /etc/mysql/my.cnf &> /dev/null
  _THIS_DB_SERVER_TEST=`mysql -V 2>&1`
  if [[ "$_THIS_DB_SERVER_TEST" =~ "5.5." ]] || [[ "$_THIS_DB_SERVER_TEST" =~ "10.0" ]] ; then
    _DO_NOTHING=YES
  else
    sed -i "s/.*lc_messages_dir /#lc_messages_dir /g" /etc/mysql/my.cnf &> /dev/null
    sed -i "s/.*lc_messages /#lc_messages /g" /etc/mysql/my.cnf &> /dev/null
  fi
  if [[ "$_THIS_DB_SERVER_TEST" =~ "MariaDB" ]] ; then
    sed -i "s/.*innodb_lazy_drop_table /#innodb_lazy_drop_table /g" /etc/mysql/my.cnf &> /dev/null
  fi
  if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
    if [ "$_DB_BINARY_LOG" = "NO" ] ; then
      bash /var/xdrago/purge_binlogs.sh                                  &> /dev/null
      sed -i "s/.*purge_binlogs.*//g" /var/spool/cron/crontabs/root      &> /dev/null
      sed -i "/^$/d" /var/spool/cron/crontabs/root                       &> /dev/null
      sed -i "s/^log_bin/#log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
      sed -i "s/^expire_logs_days/#expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
      sed -i "s/^max_binlog_size/#max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
    elif [ "$_DB_BINARY_LOG" = "YES" ] ; then
      sed -i "s/.*log_bin/log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
      sed -i "s/.*expire_logs_days/expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
      sed -i "s/.*max_binlog_size/max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
    fi
    if [ "$_DB_ENGINE" = "MyISAM" ] || [ "$_DB_ENGINE" = "InnoDB" ] ; then
      sed -i "s/^default_storage_engine.*/default_storage_engine  = $_DB_ENGINE/g" /etc/mysql/my.cnf &> /dev/null
    fi
  fi
}
#
tune_sql () {
  msg "INFO: Tuning SQL configuration"
  sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /etc/mysql/my.cnf     &> /dev/null
  sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /var/xdrago/minute.sh &> /dev/null
}
#
restore_default_nginx () {
  msg "INFO: Restoring default Nginx configuration"
  cd /var/xdrago/monitor/check
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/tools/system/monitor/check/scan_nginx ./
  if [ -d "$_TUNE_HOSTMASTER" ] ; then
    for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
    do
      sed -i "s/#limit_conn /limit_conn /g" $Files &> /dev/null
    done
  fi
  su -s /bin/bash - aegir -c "drush @server_master provision-verify &> /dev/null"
  mrun "sleep 8"
}
#
tune_nginx () {
  msg "INFO: Tuning Nginx configuration"
  sed -i "s/ = 300/ = $_TUNE_NGINX_FIREWALL/g" /var/xdrago/monitor/check/scan_nginx  &> /dev/null
  sed -i "s/60/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf    &> /dev/null
  sed -i "s/300/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf   &> /dev/null
  sed -i "s/180/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf   &> /dev/null
  if [ "$_TUNE_NGINX_CONNECT" = "OFF" ] ; then
    sed -i "s/limit_conn /#limit_conn /g" /var/aegir/config/server_master/nginx.conf &> /dev/null
    if [ -d "$_TUNE_HOSTMASTER" ] ; then
      for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
      do
        sed -i "s/limit_conn /#limit_conn /g" $Files &> /dev/null
      done
    fi
  fi
}
#
restart_services () {
  msg "INFO: Reloading services"
  if [ -e "/etc/init.d/php55-fpm" ] ; then
    mrun "service php55-fpm reload" &> /dev/null
  fi
  if [ -e "/etc/init.d/php54-fpm" ] ; then
    mrun "service php54-fpm reload" &> /dev/null
  fi
  if [ -e "/etc/init.d/php53-fpm" ] ; then
    mrun "service php53-fpm reload" &> /dev/null
  fi
  if [ -e "/etc/init.d/php52-fpm" ] ; then
    mrun "service php52-fpm reload" &> /dev/null
  fi
  mrun "service mysql restart" &> /dev/null
  mrun "service nginx reload" &> /dev/null
  mrun "service redis-server restart" &> /dev/null
  if [ -e "/usr/sbin/csf" ] && [ -e "/etc/csf/csf.deny" ] ; then
    mrun "csf -q" &> /dev/null
  fi
}
#
tune_web_server_config () {
  _LIM_FPM="$_L_PHP_FPM_WORKERS"
  if [ "$_LIM_FPM" -lt "24" ] ; then
    if [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
      _LIM_FPM=24
    fi
  fi
  let "_PROCESS_MAX_FPM = (($_LIM_FPM * 5))"
  let "_CHILD_MAX_FPM = (($_LIM_FPM * 2))"
  if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
    _DO_NOTHING=YES
  else
    _PHP_FPM_WORKERS=${_PHP_FPM_WORKERS//[^0-9]/}
    if [ ! -z "$_PHP_FPM_WORKERS" ] && [ "$_PHP_FPM_WORKERS" -gt "0" ] ; then
      _CHILD_MAX_FPM="$_PHP_FPM_WORKERS"
    fi
  fi
  let "_START_LEGACY_FPM = (($_LIM_FPM - 21))"
  if [ "$_START_LEGACY_FPM" -lt "3" ] ; then
    _START_LEGACY_FPM=3
  fi
  sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php55/etc/php55-fpm.conf    &> /dev/null
  sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php54/etc/php54-fpm.conf    &> /dev/null
  sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php53/etc/php53-fpm.conf    &> /dev/null
  sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
  sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
  sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
  sed -i "s/max_children\">.*</max_children\">$_START_LEGACY_FPM</g" /opt/php52/etc/php52-fpm.conf    &> /dev/null
  if [ ! -z "$_PHP_FPM_DENY" ] ; then
    sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
    sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
    sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
  fi
  if [ ! -z "$_L_NGINX_WORKERS" ] ; then
    sed -i "s/worker_processes.*/worker_processes  $_L_NGINX_WORKERS;/g" /etc/nginx/nginx.conf &> /dev/null
  else
    sed -i "s/worker_processes.*/worker_processes  4;/g" /etc/nginx/nginx.conf &> /dev/null
  fi
}
#
tune_sql_memory_limits () {
  if [ ! -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
    msg "INFO: Running MySQLTuner check on all databases..."
    msg "NOTE! This step may take a LONG time, please wait..."
    rm -f /var/opt/mysqltuner*
    ### curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 15 -A iCab "https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl" -o /var/opt/mysqltuner.pl
    curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
    if [ ! -e "/var/opt/mysqltuner.pl" ] ; then
      mrun "sleep 60"
      curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
    fi
    if [ -e "/var/opt/mysqltuner.pl" ] ; then
      perl /var/opt/mysqltuner.pl > /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt 2>&1
    fi
  fi
  if [ -e "/var/opt/mysqltuner.pl" ] && [ -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
    _REC_MYISAM_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in MyISAM tables" | cut -d: -f2 | awk '{ print $1}'`
    _REC_INNODB_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in InnoDB tables" | cut -d: -f2 | awk '{ print $1}'`
    ###--------------------###
    if [ ! -z "$_REC_MYISAM_MEM" ] ; then
      _RAW_MYISAM_MEM=`echo $_REC_MYISAM_MEM | sed "s/[A-Z]//g"`
      if [[ "$_REC_MYISAM_MEM" =~ "G" ]] ; then
        let "_RAW_MYISAM_MEM = (($_RAW_MYISAM_MEM * 1024))"
      fi
      if [ "$_RAW_MYISAM_MEM" -gt "$_USE_SQL" ] ; then
        _USE_MYISAM_MEM="$_USE_SQL"
      else
        _USE_MYISAM_MEM="$_RAW_MYISAM_MEM"
      fi
      _USE_MYISAM_MEM="${_USE_MYISAM_MEM}M"
      sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
    else
      _USE_MYISAM_MEM="64M"
      sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
    fi
    ###--------------------###
    if [ ! -z "$_REC_INNODB_MEM" ] ; then
      _RAW_INNODB_MEM=`echo $_REC_INNODB_MEM | sed "s/[A-Z]//g"`
      if [[ "$_REC_INNODB_MEM" =~ "G" ]] ; then
        let "_RAW_INNODB_MEM = (($_RAW_INNODB_MEM * 1024))"
      fi
      if [ "$_RAW_INNODB_MEM" -gt "$_USE_SQL" ] ; then
        _USE_INNODB_MEM="$_USE_SQL"
      else
        _USE_INNODB_MEM="$_RAW_INNODB_MEM"
      fi
      _INNODB_BPI="0"
      _INNODB_BPI=`echo "scale=0; $_USE_INNODB_MEM/1024" | bc`;
      if [ "$_INNODB_BPI" -gt "1" ] ; then
        sed -i "s/innodb_file_per_table   = 1/innodb_file_per_table   = 1\ninnodb_buffer_pool_instances = $_INNODB_BPI/g" /etc/mysql/my.cnf &> /dev/null
      fi
      _USE_INNODB_MEM="${_USE_INNODB_MEM}M"
      sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
    else
      _USE_INNODB_MEM="64M"
      sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
    fi
  else
    sed -i "s/= 181/= $_USE_SQL/g"  /etc/mysql/my.cnf &> /dev/null
  fi
}
#
tune_memory_limits () {
  msg "INFO: Default Memory Tuning"
  _AWS_TEST_A=$(grep cloudimg /etc/fstab)
  _AWS_TEST_B=$(grep cloudconfig /etc/fstab)
  _ETH_TEST=`ifconfig 2>&1`
  _VM_TEST=`uname -a 2>&1`
  if [[ "$_ETH_TEST" =~ "venet0" ]] ; then
    _VMFAMILY="VZ"
  elif [ -e "/proc/bean_counters" ] ; then
    _VMFAMILY="VZ"
  elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/menu.lst" ] ; then
    _VMFAMILY="TG"
  elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/grub.cfg" ] ; then
    _VMFAMILY="TG"
  else
    _VMFAMILY="XEN"
  fi
  if [[ "$_VM_TEST" =~ beng ]] ; then
    _VMFAMILY="VS"
  fi
  if [[ "$_AWS_TEST_A" =~ "cloudimg" ]] || [[ "$_AWS_TEST_B" =~ "cloudconfig" ]] ; then
    _VMFAMILY="AWS"
  fi
  _CPU_INFO=$(grep -c processor /proc/cpuinfo)
  _CPU_INFO=${_CPU_INFO//[^0-9]/}
  _NPROC_TEST=$(which nproc)
  if [ -z "$_NPROC_TEST" ] ; then
    _CPU_NR="$_CPU_INFO"
  else
    _CPU_NR=`nproc`
  fi
  _CPU_NR=${_CPU_NR//[^0-9]/}
  if [ ! -z "$_CPU_NR" ] && [ ! -z "$_CPU_INFO" ] && [ "$_CPU_NR" -gt "$_CPU_INFO" ] && [ "$_CPU_INFO" -gt "0" ] ; then
    _CPU_NR="$_CPU_INFO"
  fi
  if [ -z "$_CPU_NR" ] || [ "$_CPU_NR" -lt "1" ] ; then
    _CPU_NR=1
  fi
  let "_CPU_MX = (($_CPU_NR * 2))"
  if [ "$_CPU_MX" -lt "4" ] ; then
    _CPU_MX=4
  fi
  let "_CPU_TG = (($_CPU_NR / 2))"
  if [ "$_CPU_TG" -lt "4" ] ; then
    _CPU_TG=4
  fi
  let "_CPU_VS = (($_CPU_NR / 12))"
  if [ "$_CPU_VS" -lt "2" ] ; then
    _CPU_VS=2
  fi
  _RAM=`free -mto | grep Mem: | awk '{ print $2 }'`
  if [ "$_RESERVED_RAM" -gt "0" ] ; then
    let "_RAM = (($_RAM - $_RESERVED_RAM))"
  fi
  let "_USE = (($_RAM / 4))"
  if [ "$_VMFAMILY" = "VS" ] || [[ "$_THISHOST" =~ ".host8." ]] ; then
    if [ "$_VMFAMILY" = "VS" ] ; then
      if [ -e "/root/.tg.cnf" ] ; then
        let "_USE_SQL = (($_RAM / 12))"
      else
        let "_USE_SQL = (($_RAM / 24))"
      fi
    else
      let "_USE_SQL = (($_RAM / 8))"
    fi
  else
    let "_USE_SQL = (($_RAM / 8))"
  fi
  _TMP_SQL="${_USE_SQL}M"
  let "_SRT_SQL = (($_USE_SQL * 2))"
  _SRT_SQL="${_SRT_SQL}K"
  if [ "$_USE" -ge "512" ] && [ "$_USE" -lt "1024" ] ; then
    _USE_PHP=512
    _USE_OPC=512
    _PRC_SQL=20
    _QCE_SQL=128M
    _RND_SQL=8M
    _JBF_SQL=4M
    if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
      _L_PHP_FPM_WORKERS=12
    else
      _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
    fi
    if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
      _L_NGINX_WORKERS=$_CPU_MX
    else
      _L_NGINX_WORKERS=$_NGINX_WORKERS
    fi
  elif [ "$_USE" -ge "1024" ] ; then
    if [ "$_VMFAMILY" = "XEN" ] || [ "$_VMFAMILY" = "AWS" ] ; then
      _USE_PHP=512
      _USE_OPC=1024
      _PRC_SQL=30
      _QCE_SQL=128M
      _RND_SQL=8M
      _JBF_SQL=4M
      if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
        _L_PHP_FPM_WORKERS=24
      else
        _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
      fi
      if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
        _L_NGINX_WORKERS=$_CPU_MX
      else
        _L_NGINX_WORKERS=$_NGINX_WORKERS
      fi
    elif [ "$_VMFAMILY" = "VS" ] || [ "$_VMFAMILY" = "TG" ] ; then
      if [ -e "/boot/grub/grub.cfg" ] || [ -e "/boot/grub/menu.lst" ] || [ -e "/root/.tg.cnf" ] ; then
        _USE_PHP=1024
        _USE_OPC=1024
        _PRC_SQL=50
        _QCE_SQL=128M
        _RND_SQL=8M
        _JBF_SQL=4M
        if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
          _L_PHP_FPM_WORKERS=24
        else
          _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
        fi
        if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
          _L_NGINX_WORKERS=$_CPU_TG
        else
          _L_NGINX_WORKERS=$_NGINX_WORKERS
        fi
        sed -i "s/64000/128000/g"  /opt/php53/etc/php55.ini &> /dev/null
        sed -i "s/64000/128000/g"  /opt/php53/etc/php54.ini &> /dev/null
        sed -i "s/64000/128000/g"  /opt/php53/etc/php53.ini &> /dev/null
        sed -i "s/64000/128000/g"  /opt/php52/etc/php52.ini &> /dev/null
      else
        _USE_PHP=1024
        _USE_OPC=1024
        _PRC_SQL=25
        _QCE_SQL=64M
        _RND_SQL=2M
        _JBF_SQL=2M
        if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
          _L_PHP_FPM_WORKERS=6
        else
          _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
        fi
        if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
          _L_NGINX_WORKERS=$_CPU_VS
        else
          _L_NGINX_WORKERS=$_NGINX_WORKERS
        fi
      fi
    else
      _USE_PHP=256
      _USE_OPC=256
      _PRC_SQL=10
      _QCE_SQL=32M
      _RND_SQL=2M
      _JBF_SQL=2M
      if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
        _L_PHP_FPM_WORKERS=12
      else
        _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
      fi
      if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
        _L_NGINX_WORKERS=$_CPU_MX
      else
        _L_NGINX_WORKERS=$_NGINX_WORKERS
      fi
    fi
  else
    _USE_PHP="$_USE"
    _USE_OPC="$_USE"
    _PRC_SQL=10
    _QCE_SQL=32M
    _RND_SQL=1M
    _JBF_SQL=1M
    if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
      _L_PHP_FPM_WORKERS=6
    else
      _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
    fi
    if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
      _L_NGINX_WORKERS=$_CPU_MX
    else
      _L_NGINX_WORKERS=$_NGINX_WORKERS
    fi
  fi
  _USE_JETTY="-Xmx${_USE_OPC}m"
  if [ "$_VMFAMILY" = "VZ" ] ; then
    _USE_OPC=64
  fi
  let "_USE_FPM = (($_USE_PHP / 2))"
  if [ "$_USE_FPM" -lt "196" ] ; then
    _USE_FPM=196
  fi
  if [ "$_USE_PHP" -lt "196" ] ; then
    _USE_PHP=196
  fi
  if [ ! -e "/var/xdrago/conf/fpm-pool-foo.conf" ] ; then
    mkdir -p /var/xdrago/conf
  fi
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm-pool-foo.conf /var/xdrago/conf/fpm-pool-foo.conf
  if [ -e "/opt/etc/fpm/fpm-pool-common.conf" ] ; then
    sed -i "s/256/$_USE_FPM/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
  fi
  if [ -e "/opt/php55/etc/php55.ini" ] ; then
    sed -i "s/256/$_USE_FPM/g" /opt/php55/etc/php55.ini         &> /dev/null
    sed -i "s/181/$_USE_OPC/g" /opt/php55/etc/php55.ini         &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php55/lib/php.ini           &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
  fi
  if [ -e "/opt/php54/etc/php54.ini" ] ; then
    sed -i "s/256/$_USE_FPM/g" /opt/php54/etc/php54.ini         &> /dev/null
    sed -i "s/181/$_USE_OPC/g" /opt/php54/etc/php54.ini         &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php54/lib/php.ini           &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
  fi
  if [ -e "/opt/php53/etc/php53.ini" ] ; then
    sed -i "s/256/$_USE_FPM/g" /opt/php53/etc/php53.ini         &> /dev/null
    sed -i "s/181/$_USE_OPC/g" /opt/php53/etc/php53.ini         &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php53/lib/php.ini           &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
  fi
  if [ -e "/opt/php52/etc/php52.ini" ] ; then
    sed -i "s/256/$_USE_FPM/g" /opt/php52/etc/php52.ini         &> /dev/null
    sed -i "s/181/$_USE_OPC/g" /opt/php52/etc/php52.ini         &> /dev/null
    sed -i "s/256/$_USE_PHP/g" /opt/php52/lib/php.ini           &> /dev/null
  fi
  if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
    tune_sql_memory_limits
    sed -i "s/= 191/= $_PRC_SQL/g"                                               /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^tmp_table_size.*/tmp_table_size          = $_TMP_SQL/g"           /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^max_heap_table_size.*/max_heap_table_size     = $_TMP_SQL/g"      /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^myisam_sort_buffer_size.*/myisam_sort_buffer_size = $_SRT_SQL/g"  /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^query_cache_size.*/query_cache_size        = $_QCE_SQL/g"         /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^read_rnd_buffer_size.*/read_rnd_buffer_size    = $_RND_SQL/g"     /etc/mysql/my.cnf &> /dev/null
    sed -i "s/^join_buffer_size.*/join_buffer_size        = $_JBF_SQL/g"         /etc/mysql/my.cnf &> /dev/null
  fi
  _MAX_MEM_REDIS="${_USE_OPC}MB"
  sed -i "s/^maxmemory .*/maxmemory $_MAX_MEM_REDIS/g"  /etc/redis/redis.conf &> /dev/null
  if [ -e "/etc/default/jetty9" ] && [ -e "/opt/solr4" ] ; then
    sed -i "s/^JAVA_OPTIONS.*/JAVA_OPTIONS=\"-Xms8m $_USE_JETTY -Djava.awt.headless=true -Dsolr.solr.home=\/opt\/solr4 \$JAVA_OPTIONS\" # Options/g" /etc/default/jetty9 &> /dev/null
  fi
  if [ -e "/etc/default/jetty8" ] && [ -e "/opt/solr3" ] ; then
    sed -i "s/^JAVA_OPTIONS.*/JAVA_OPTIONS=\"-Xms8m $_USE_JETTY -Djava.awt.headless=true -Dsolr.solr.home=\/opt\/solr3 \$JAVA_OPTIONS\" # Options/g" /etc/default/jetty8 &> /dev/null
  fi
  if [ -e "/etc/default/jetty7" ] && [ -e "/opt/solr1" ] ; then
    sed -i "s/^JAVA_OPTIONS.*/JAVA_OPTIONS=\"-Xms8m $_USE_JETTY -Djava.awt.headless=true -Dsolr.solr.home=\/opt\/solr1 \$JAVA_OPTIONS\" # Options/g" /etc/default/jetty7 &> /dev/null
  fi
  tune_web_server_config
}
#
check_git_repos_status () {
  _GITHUB_WORKS=NO
  _DRUPAL_WORKS=NO
  _GITORIOUS_WORKS=NO
  if [ "$_FORCE_GIT_MIRROR" = "github" ] ; then
    msg "INFO: We will use forced GitHub repository without testing connection"
    _GITHUB_WORKS=YES
    _DRUPAL_WORKS=NO
    _GITORIOUS_WORKS=NO
    sleep 1
  elif [ "$_FORCE_GIT_MIRROR" = "drupal" ] ; then
    msg "INFO: We will use forced Drupal mirror without testing connection"
    _GITHUB_WORKS=NO
    _DRUPAL_WORKS=YES
    _GITORIOUS_WORKS=NO
    sleep 1
  elif [ "$_FORCE_GIT_MIRROR" = "gitorious" ] ; then
    msg "INFO: We will use forced Gitorious mirror without testing connection"
    _GITHUB_WORKS=NO
    _DRUPAL_WORKS=NO
    _GITORIOUS_WORKS=YES
    sleep 1
  else
    msg "INFO: Testing repository mirror servers availability..."
    sleep 1
    _GITHUB_WORKS=YES
    _DRUPAL_WORKS=YES
    _GITORIOUS_WORKS=YES
    if ! netcat -w 8 -z github.com 9418 ; then
      _GITHUB_WORKS=NO
      msg "WARN: The GitHub master repository server doesn't respond..."
    elif ! netcat -w 8 -z git.drupal.org 80 ; then
      _DRUPAL_WORKS=NO
      msg "WARN: The Drupal mirror repository server doesn't respond..."
    elif ! netcat -w 8 -z code.aegir.cc 9418 ; then
      _GITORIOUS_WORKS=NO
      msg "WARN: The Gitorious mirror repository server doesn't respond..."
    fi
  fi
  if [ "$_GITHUB_WORKS" = "YES" ] ; then
    _BOA_REPO_NAME="boa"
    _BOA_REPO_GIT_URL="git://github.com/omega8cc"
    msg "INFO: GitHub master repository will be used"
  elif [ "$_DRUPAL_WORKS" = "YES" ] ; then
    _BOA_REPO_NAME="barracuda"
    _BOA_REPO_GIT_URL="http://git.drupal.org/project"
    msg "INFO: Drupal mirror repository will be used"
  elif [ "$_GITORIOUS_WORKS" = "YES" ] ; then
    _BOA_REPO_NAME="barracuda-octopus"
    _BOA_REPO_GIT_URL="git://code.aegir.cc/aegir"
    msg "INFO: Gitorious mirror repository will be used"
  else
    cat <<EOF

    None of repository servers responded in 8 seconds,
    so we can't continue this installation.

    Please try again later or check if your firewall has port 9418 open.

    Bye.

EOF
    exit 1
  fi
  _BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
}


###---### init
#
touch /var/run/boa_run.pid
#
_BOA_REPO_NAME="boa"
_BOA_REPO_GIT_URL="git://github.com/omega8cc"
_BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
  check_git_repos_status
fi
#
#
if [ `whoami` = "root" ] ; then
  chmod a+w /dev/null
  if [ ! -e "/dev/fd" ] ; then
    if [ -e "/proc/self/fd" ] ; then
      rm -rf /dev/fd
      ln -s /proc/self/fd /dev/fd
    fi
  fi
  msg "INFO: This script is ran as a root user"
else
  msg "ERROR: This script should be ran as a root user - please `sudo -i` first"
  msg "Bye"
  exit 1
fi
#
#
if [ ! -f "/var/log/barracuda_log.txt" ] ; then
  msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
  msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
  msg "Bye"
  exit 1
else
  _VERSIONS_TEST=`cat /var/log/barracuda_log.txt`
  if [[ "$_VERSIONS_TEST" =~ "$_INSTALLER_VERSION" ]] ; then
    _VERSIONS_TEST_RESULT=OK
  else
    msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
    msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
    msg "Bye"
    exit 1
  fi
fi
#
#
rm -f /var/run/aegir_upgrade.pid
rm -f /opt/tmp/testecho*
_SRCDIR=/opt/tmp/files
mkdir -p $_SRCDIR
chmod -R 777 /opt/tmp &> /dev/null
cd /opt/tmp
rm -f -r /opt/tmp/$_BOA_REPO_NAME
if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
  git clone $_BOA_REPO_GIT_URL/$_BOA_REPO_NAME.git &> /dev/null
  if [ "$_FORCE_GIT_MIRROR" = "test" ] ; then
    cd /opt/tmp/$_BOA_REPO_NAME
    git checkout 2.0.5-dev &> /dev/null
    cd /opt/tmp
  fi
else
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/versions/stable/tar/boa-$_INSTALLER_VERSION.tar.gz" | tar -xzf -
  _BOA_REPO_NAME="boa"
fi
#
# Get spinner
cd $_SRCDIR
rm -f spinner*
cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/helpers/spinner ./
chmod +x spinner &> /dev/null
#
# Create tmp stuff
_LOG=/var/backups/bond-$_NOW.log
_SILENT=/opt/tmp/silent.log
#
#


###---### Tune Your Aegir Hosting System
#
echo " "
msg "TUNER START -> checkpoint: "
cat <<EOF

  * Aegir Satellite Instance to tune: $_TUNE_HOSTMASTER
  * Nginx server mod_evasive will be set to $_TUNE_NGINX_CONNECT
  * Nginx server fastcgi timeout will be set to $_TUNE_NGINX_TIMEOUT seconds
  * Nginx firewall limit of allowed requests will be set to $_TUNE_NGINX_FIREWALL/300
  * Database server timeout will be set to $_TUNE_SQL_TIMEOUT seconds
  * PHP-FPM server timeout will be set to $_TUNE_PHP_FPM_TIMEOUT seconds
  * PHP-CLI drush timeout will be set to $_TUNE_PHP_CLI_TIMEOUT seconds

EOF
echo " "
if prompt_yes_no "Are you ready to tune your Aegir Hosting System with above values?" ; then
  true
  if [ ! -e "/root/.upstart.cnf" ] ; then
    msg "INFO: We will stop cron and then wait 30 seconds..."
    service cron stop &> /dev/null
    mrun "sleep 30"
  fi
  msg "INFO: Tuning in progress, please wait..."
  if [ -e "/root/.barracuda.cnf" ] ; then
    source /root/.barracuda.cnf
  fi
  restore_default_php
  update_php_conf
  tune_php
  restore_default_sql
  tune_sql
  restore_default_nginx
  tune_nginx
  tune_memory_limits
  restart_services
  msg "INFO: Tuning completed"
else
  if [ ! -e "/root/.upstart.cnf" ] ; then
    msg "INFO: We will stop cron and then wait 30 seconds..."
    service cron stop &> /dev/null
    mrun "sleep 30"
  fi
  if [ -e "/root/.barracuda.cnf" ] ; then
    source /root/.barracuda.cnf
  fi
  restore_default_php
  update_php_conf
  restore_default_sql
  restore_default_nginx
  tune_memory_limits
  restart_services
  msg "INFO: Tuning stopped and default settings restored"
fi
rm -f -r /var/opt/*
rm -f -r /opt/tmp/*
rm -f /var/run/boa_run.pid
if [ ! -e "/root/.upstart.cnf" ] ; then
  service cron start &> /dev/null
fi
msg "INFO: Cron started again"
msg "BYE!"

###----------------------------------------###
###
###  Barracuda-Octopus-Nginx-Drupal Tuner
###  Copyright (C) 2010-2014 Omega8.cc
###  noc@omega8.cc www.omega8.cc
###
###----------------------------------------###