Force MariaDB 5.5 re-install if installed version doesn't match latest version for...
[project/barracuda.git] / aegir / tools / BOND.sh.txt
1 #!/bin/bash
2
3
4 ###----------------------------------------###
5 ###
6 ###  Barracuda-Octopus-Nginx-Drupal Tuner
7 ###
8 ###  Copyright (C) 2010-2014 Omega8.cc
9 ###  noc@omega8.cc www.omega8.cc
10 ###
11 ###  This program is free software. You can
12 ###  redistribute it and/or modify it under
13 ###  the terms of the GNU GPL as published by
14 ###  the Free Software Foundation, version 2
15 ###  or later.
16 ###
17 ###  This program is distributed in the hope
18 ###  that it will be useful, but WITHOUT ANY
19 ###  WARRANTY; without even the implied
20 ###  warranty of MERCHANTABILITY or FITNESS
21 ###  FOR A PARTICULAR PURPOSE. See the GNU GPL
22 ###  for more details.
23 ###
24 ###  You should have received a copy of the
25 ###  GNU GPL along with this program.
26 ###  If not, see http://www.gnu.org/licenses/
27 ###
28 ###  Code: https://code.aegir.cc/aegir
29 ###
30 ###----------------------------------------###
31
32
33 ###----------------------------------------###
34 ### HOW-TO: run it with bash, not with sh  ###
35 ###----------------------------------------###
36 ###
37 ### $ bash BOND.sh.txt
38 ###
39 ### Note: to restore default values it is
40 ###       enough to start this script with
41 ###       any values defined below and answer
42 ###       NO when it prompts for confirmation
43 ###       "Are you ready to tune your Aegir".
44 ###
45
46
47 ###----------------------------------------###
48 ### EDITME                                 ###
49 ###----------------------------------------###
50 ###
51 ### Enter below the settings you wish to use.
52 ###
53
54
55 ###----------------------------------------###
56 ### Hostmaster root directory - /var/aegir
57 ###
58 ### Note: most of values tuned by this script
59 ###       are server-vide, while some, like
60 ###       mod_evasive settings will affect
61 ###       only sites hosted on the Aegir
62 ###       Satellite Instance defined below.
63 ###
64 _TUNE_HOSTMASTER=/data/disk/o1
65
66
67 ###----------------------------------------###
68 ### Nginx server mod_evasive - default ON
69 ###
70 ### Note: running verify task on any SITE
71 ###       will restore default value ON
72 ###       for that site only, while TUNER
73 ###       will turn OFF/ON this feature
74 ###       for all sites hosted on the
75 ###       Hostmaster defined above.
76 ###
77 _TUNE_NGINX_CONNECT=OFF
78
79
80 ###----------------------------------------###
81 ### Nginx server fastcgi timeout - default 180
82 ###
83 ### Note: running verify task on the SERVER
84 ###       in the Hostmaster created
85 ###       by Barracuda (not Octopus!)
86 ###       will restore default value
87 ###       for the server and all existing
88 ###       Aegir Satellite Instances.
89 ###
90 _TUNE_NGINX_TIMEOUT=3600
91
92
93 ###----------------------------------------###
94 ### Nginx server firewall limit - default 300
95 ###
96 ### Note: don't change the default value
97 ###       if you are the only visitor, or
98 ###       you will lock yourself easily.
99 ###
100 ###       The default value 300 means the
101 ###       firewall limit is OFF because
102 ###       it scans only the last 300 lines
103 ###       of your web server log file.
104 ###
105 ###       If you will set this value to 100
106 ###       then every visitor IP with more
107 ###       than 100 out of the last 300
108 ###       requests will be locked.
109 ###
110 ###       Only dynamic requests (pages) are
111 ###       counted because static files like
112 ###       images are generally not logged.
113 ###
114 _TUNE_NGINX_FIREWALL=300
115
116
117 ###----------------------------------------###
118 ### Database server timeout - default 3600
119 ###
120 _TUNE_SQL_TIMEOUT=3600
121
122
123 ###----------------------------------------###
124 ### PHP-FPM server timeout - default 180
125 ###
126 _TUNE_PHP_FPM_TIMEOUT=3600
127
128
129 ###----------------------------------------###
130 ### PHP-CLI server timeout - default 3600
131 ###
132 _TUNE_PHP_CLI_TIMEOUT=7200
133
134
135 ###----------------------------------------###
136 ### TUNER VERSION                          ###
137 ###----------------------------------------###
138 ###
139 ### _AEGIR_VERSION=HEAD
140 ### _AEGIR_VERSION=BOA-2.2.3
141 ###
142 _AEGIR_VERSION=HEAD
143
144
145 ###----------------------------------------###
146 ### DON'T EDIT ANYTHING BELOW THIS LINE    ###
147 ###----------------------------------------###
148
149 _INSTALLER_VERSION=BOA-2.2.4-dev
150 _NOW=`date +%y%m%d-%H%M`
151 _RAM=`free -mto | grep Mem: | awk '{ print $2 }'`
152 _THISHOST=`uname -n`
153 _SPINNER=YES
154 PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
155 SHELL=/bin/bash
156 if [ -n "${STY+x}" ]; then
157   _SPINNER=NO
158 fi
159 if [ "$_PHP_FPM_VERSION" = "5.5" ] ; then
160   _PHP_API=20121212
161 elif [ "$_PHP_FPM_VERSION" = "5.4" ] ; then
162   _PHP_API=20100525
163 elif [ "$_PHP_FPM_VERSION" = "5.3" ] ; then
164   _PHP_API=20090626
165 elif [ "$_PHP_FPM_VERSION" = "5.2" ] ; then
166   _PHP_API=20060613
167 fi
168
169
170 ###---### Functions
171 #
172 # Noticeable messages.
173 msg () {
174   echo "Tuner [`date`] ==> $*"
175 }
176 # Simple prompt.
177 prompt_yes_no () {
178 if [ "$_AUTOPILOT" = "YES" ] ; then
179   return 0
180 else
181   while true ; do
182   printf "$* [Y/n] "
183   read answer
184   if [ -z "$answer" ] ; then
185     return 0
186   fi
187   case $answer in
188     [Yy]|[Yy][Ee][Ss])
189     return 0
190     ;;
191     [Nn]|[Nn][Oo])
192     return 1
193     ;;
194     *)
195     echo "Please answer yes or no"
196     ;;
197   esac
198  done
199 fi
200 }
201 #
202 # Silent runner.
203 st_runner () {
204   CMD="$1"
205   touch busy
206   if [ "$_SPINNER" = "YES" ] ; then
207     bash $_SRCDIR/spinner busy &
208   fi
209   if $CMD >> $_LOG; then
210     rm busy
211     sleep 1
212     return 0
213   else
214     rm busy
215     sleep 1
216     echo "$CMD failed.  Error (if any): $?"
217     echo " "
218     echo "Displaying the last 15 lines of $_LOG to help troubleshoot this problem"
219     echo "If you see any error with advice to run 'dpkg --configure -a', run this"
220     echo "command first and choose default answer, then run this installer again"
221     echo " "
222     tail -15 $_LOG
223     return 1
224   fi
225 }
226 #
227 # Small spinner.
228 mrun () {
229   CMD="$1"
230   touch busy
231   if [ "$_SPINNER" = "YES" ] ; then
232     bash $_SRCDIR/spinner busy &
233   fi
234   if $CMD >> $_LOG; then
235     rm busy
236     sleep 1
237     return 0
238   fi
239 }
240 #
241 # Find correct IP.
242 find_correct_ip () {
243   _LOC_IP=`echo $(getent ahostsv4 $_LOC_DOM) | cut -d: -f2 | awk '{ print $1}'`
244 }
245 #
246 # Fix php.ini files to remove suhosin.so
247 fix_php_ini_suhosin () {
248   if [ -e "$_THIS_FILE" ] ; then
249     _SUHOSIN_INI_TEST=$(grep "extension=suhosin.so" $_THIS_FILE)
250     if [[ "$_SUHOSIN_INI_TEST" =~ "extension=suhosin.so" ]] ; then
251       sed -i "s/.*suhosin.*//g" $_THIS_FILE &> /dev/null
252     fi
253   fi
254 }
255 #
256 # Fix php.ini files to add mailparse.so
257 fix_php_ini_mailparse () {
258   if [ -e "$_THIS_FILE" ] ; then
259     _MAILPARSE_INI_TEST=$(grep "extension=mailparse.so" $_THIS_FILE)
260     if [[ "$_MAILPARSE_INI_TEST" =~ "extension=mailparse.so" ]] ; then
261       _DO_NOTHING=YES
262     else
263       echo "extension=mailparse.so" >> $_THIS_FILE
264     fi
265   fi
266 }
267 #
268 # Fix php.ini files to add jsmin.so
269 fix_php_ini_jsmin () {
270   if [ -e "$_THIS_FILE" ] ; then
271     _JSMIN_INI_TEST=$(grep "extension=jsmin.so" $_THIS_FILE)
272     if [[ "$_JSMIN_INI_TEST" =~ "extension=jsmin.so" ]] ; then
273       _DO_NOTHING=YES
274     else
275       echo "extension=jsmin.so" >> $_THIS_FILE
276     fi
277   fi
278 }
279 #
280 # Fix php.ini files to add redis.so
281 fix_php_ini_redis () {
282   if [ -e "$_THIS_FILE" ] ; then
283     _REDIS_INI_TEST=$(grep "extension=redis.so" $_THIS_FILE)
284     if [[ "$_REDIS_INI_TEST" =~ "extension=redis.so" ]] ; then
285       _DO_NOTHING=YES
286     else
287       echo "extension=redis.so" >> $_THIS_FILE
288     fi
289   fi
290 }
291 #
292 # Fix php.ini file to add newrelic.ini
293 fix_php_ini_newrelic () {
294   _NR_TPL="/opt/tmp/$_BOA_REPO_NAME/aegir/conf/newrelic.ini"
295   if [ -e "$_THIS_FILE" ] ; then
296     _NEWRELIC_INI_TEST_A=$(grep "extension=newrelic.so" $_THIS_FILE)
297     if [[ "$_NEWRELIC_INI_TEST_A" =~ "extension=newrelic.so" ]] ; then
298       _DO_NOTHING=YES
299     else
300       cat $_NR_TPL >> $_THIS_FILE
301     fi
302     _NEWRELIC_INI_TEST_B=$(grep "newrelic.framework.drupal.modules" $_THIS_FILE)
303     if [[ "$_NEWRELIC_INI_TEST_B" =~ "newrelic.framework.drupal.modules" ]] ; then
304       _DO_NOTHING=YES
305     else
306       echo "newrelic.framework.drupal.modules = 1" >> $_THIS_FILE
307     fi
308     sed -i "s/REPLACE_WITH_REAL_KEY/$_NEWRELIC_KEY/g" $_THIS_FILE &> /dev/null
309     sed -i "s/license_key=//g" $_THIS_FILE &> /dev/null
310   fi
311 }
312 #
313 # Fix all php.ini files to add newrelic.ini
314 fix_php_ini_newrelic_all () {
315   if [ -e "/etc/newrelic/newrelic.cfg" ] ; then
316     if [ -z "$_NEWRELIC_KEY" ] ; then
317       _NEWRELIC_KEY=`grep license_key /etc/newrelic/newrelic.cfg`
318       _NEWRELIC_KEY=`echo -n $_NEWRELIC_KEY | tr -d "\n"`
319     fi
320     _THIS_FILE=/opt/php52/etc/php52.ini
321     fix_php_ini_newrelic
322     _THIS_FILE=/opt/php52/lib/php.ini
323     fix_php_ini_newrelic
324
325     _THIS_FILE=/opt/php53/etc/php53.ini
326     fix_php_ini_newrelic
327     _THIS_FILE=/opt/php53/lib/php.ini
328     fix_php_ini_newrelic
329
330     _THIS_FILE=/opt/php54/etc/php54.ini
331     fix_php_ini_newrelic
332     _THIS_FILE=/opt/php54/lib/php.ini
333     fix_php_ini_newrelic
334
335     _THIS_FILE=/opt/php55/etc/php55.ini
336     fix_php_ini_newrelic
337     _THIS_FILE=/opt/php55/lib/php.ini
338     fix_php_ini_newrelic
339   fi
340 }
341 #
342 # Fix FMP php.ini file to add opcache.so
343 fix_php_ini_opcache () {
344   if [ -e "$_THIS_FILE" ] ; then
345     if [ "$1" = "55" ] ; then
346       if [[ "$_ZEND_OPCACHE_VERSION" =~ "master" ]] ; then
347         _OPCACHE_SRC=YES
348       else
349         _OPCACHE_SRC=NO
350       fi
351     fi
352     _OPCACHE_INI_TEST=$(grep "opcache.so" $_THIS_FILE)
353     if [[ "$_OPCACHE_INI_TEST" =~ "opcache.so" ]] ; then
354       _DO_NOTHING=YES
355     else
356       echo ";"                                   >> $_THIS_FILE
357       echo "; Zend OPcache"                      >> $_THIS_FILE
358       if [ "$1" = "55" ] && [ "$_OPCACHE_SRC" = "NO" ] ; then
359         echo "extension=opcache.so"              >> $_THIS_FILE
360       else
361         echo "zend_extension=\"$_OPCACHE_SO\""   >> $_THIS_FILE
362       fi
363       echo "opcache.enable=1"                    >> $_THIS_FILE
364       echo "opcache.memory_consumption=181"      >> $_THIS_FILE
365       echo "opcache.interned_strings_buffer=8"   >> $_THIS_FILE
366       echo "opcache.max_accelerated_files=64000" >> $_THIS_FILE
367       echo "opcache.revalidate_freq=60"          >> $_THIS_FILE
368       echo "opcache.enable_file_override=1"      >> $_THIS_FILE
369       echo "opcache.inherited_hack=1"            >> $_THIS_FILE
370       echo "opcache.dups_fix=1"                  >> $_THIS_FILE
371       echo "opcache.log_verbosity_level=0"       >> $_THIS_FILE
372       echo "opcache.fast_shutdown=1"             >> $_THIS_FILE
373       echo ";"                                   >> $_THIS_FILE
374     fi
375   fi
376 }
377 #
378 # Fix all FMP php.ini files to add Zend OPcache
379 fix_php_ini_opcache_all () {
380
381   _THIS_FILE=/opt/php52/etc/php52.ini
382   _OPCACHE_SO="/opt/php52/lib/php/extensions/no-debug-non-zts-$_PHP52_API/opcache.so"
383   fix_php_ini_opcache "52"
384
385   _THIS_FILE=/opt/php53/etc/php53.ini
386   _OPCACHE_SO="/opt/php53/lib/php/extensions/no-debug-non-zts-$_PHP53_API/opcache.so"
387   fix_php_ini_opcache "53"
388
389   _THIS_FILE=/opt/php54/etc/php54.ini
390   _OPCACHE_SO="/opt/php54/lib/php/extensions/no-debug-non-zts-$_PHP54_API/opcache.so"
391   fix_php_ini_opcache "54"
392
393   _THIS_FILE=/opt/php55/etc/php55.ini
394   _OPCACHE_SO="/opt/php55/lib/php/extensions/no-debug-non-zts-$_PHP55_API/opcache.so"
395   fix_php_ini_opcache "55"
396
397 }
398 #
399 # Fix php.ini file to add geos.so
400 fix_php_ini_geos () {
401   if [ -e "$_THIS_FILE" ] ; then
402     _GEOS_INI_TEST=$(grep "extension=geos.so" $_THIS_FILE)
403     if [[ "$_GEOS_INI_TEST" =~ "extension=geos.so" ]] ; then
404       _DO_NOTHING=YES
405     else
406       echo "extension=geos.so" >> $_THIS_FILE
407     fi
408   fi
409 }
410 #
411 # Fix 5.3 php.ini files to add geos.so
412 fix_php_ini_geos_all () {
413   if [ "$_PHP_GEOS" = "YES" ] || [[ "$_XTRAS_LIST" =~ "GEO" ]] ; then
414     _THIS_FILE=/opt/php53/etc/php53.ini
415     fix_php_ini_geos
416     _THIS_FILE=/opt/php53/lib/php.ini
417     fix_php_ini_geos
418   fi
419 }
420 #
421 # Fix php.ini file to add mongo.so
422 fix_php_ini_mongo () {
423   if [ -e "$_THIS_FILE" ] ; then
424     _MONGODB_INI_TEST=$(grep "extension=mongo.so" $_THIS_FILE)
425     if [[ "$_MONGODB_INI_TEST" =~ "extension=mongo.so" ]] ; then
426       _DO_NOTHING=YES
427     else
428       echo "extension=mongo.so" >> $_THIS_FILE
429     fi
430   fi
431 }
432 #
433 # Fix 5.3 php.ini files to add mongo.so
434 fix_php_ini_mongo_all () {
435   if [ "$_PHP_MONGODB" = "YES" ] || [[ "$_XTRAS_LIST" =~ "MNG" ]] ; then
436     _THIS_FILE=/opt/php53/etc/php53.ini
437     fix_php_ini_mongo
438     _THIS_FILE=/opt/php53/lib/php.ini
439     fix_php_ini_mongo
440   fi
441 }
442 #
443 # Update PHP Config.
444 update_php_conf () {
445   if [ -z "$_THISHTIP" ] ; then
446     _LOC_DOM="$_THISHOST"
447     find_correct_ip
448     _THISHTIP="$_LOC_IP"
449   fi
450   if [ ! -e "/opt/etc/fpm" ] || [ ! -e "/opt/etc/fpm/fpm-pool-common.conf" ] ; then
451     mkdir -p /opt/etc/fpm
452   fi
453   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm-pool-common.conf /opt/etc/fpm/fpm-pool-common.conf
454
455   if [ ! -e "/var/www/www55" ] ; then
456     adduser --system --group --home /var/www/www55 www55 &> /dev/null
457     usermod -aG www-data www55 &> /dev/null
458   fi
459   if [ ! -e "/opt/php55/etc/php55.ini" ] || [ ! -e "/opt/php55/etc/pool.d/www55.conf" ]  ; then
460     mkdir -p /opt/php55/etc/pool.d
461     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini
462   fi
463   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm55-pool-www.conf /opt/php55/etc/pool.d/www55.conf
464
465   if [ ! -e "/var/www/www54" ] ; then
466     adduser --system --group --home /var/www/www54 www54 &> /dev/null
467     usermod -aG www-data www54 &> /dev/null
468   fi
469   if [ ! -e "/opt/php54/etc/php54.ini" ] || [ ! -e "/opt/php54/etc/pool.d/www54.conf" ] ; then
470     mkdir -p /opt/php54/etc/pool.d
471     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini
472   fi
473   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm54-pool-www.conf /opt/php54/etc/pool.d/www54.conf
474
475   if [ ! -e "/var/www/www53" ] ; then
476     adduser --system --group --home /var/www/www53 www53 &> /dev/null
477     usermod -aG www-data www53 &> /dev/null
478   fi
479   if [ ! -e "/opt/php53/etc/php53.ini" ] || [ ! -e "/opt/php53/etc/pool.d/www53.conf" ] ; then
480     mkdir -p /opt/php53/etc/pool.d
481     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini
482   fi
483   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm53-pool-www.conf /opt/php53/etc/pool.d/www53.conf
484
485   if [ ! -e "/opt/php52/etc/php52.ini" ] ; then
486     mkdir -p /opt/php52/etc
487     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52.ini /opt/php52/etc/php52.ini
488   fi
489   if [ ! -e "/opt/php55/lib/php.ini" ] ; then
490     mkdir -p /opt/php55/lib
491     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini
492   fi
493   if [ ! -e "/opt/php54/lib/php.ini" ] ; then
494     mkdir -p /opt/php54/lib
495     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini
496   fi
497   if [ ! -e "/opt/php53/lib/php.ini" ] ; then
498     mkdir -p /opt/php53/lib
499     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini
500   fi
501   if [ ! -e "/opt/php52/lib/php.ini" ] ; then
502     mkdir -p /opt/php52/lib
503     cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-cli.ini /opt/php52/lib/php.ini
504   fi
505   if [ "$_CUSTOM_CONFIG_PHP55" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
506     if [[ "$_THISHOST" =~ "v182q.nyc." ]] && [ "$_CUSTOM_CONFIG_PHP55" = "YES" ] ; then
507       _DO_NOTHING=YES
508     else
509       cp -af /opt/php55/etc/php55.ini /var/backups/dragon/t/etc-php55.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
510       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini &> /dev/null
511       cp -af /opt/php55/lib/php.ini /var/backups/dragon/t/lib-php55.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
512       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini &> /dev/null
513     fi
514   fi
515   if [ "$_CUSTOM_CONFIG_PHP54" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
516     if [[ "$_THISHOST" =~ "v182q.nyc." ]] && [ "$_CUSTOM_CONFIG_PHP54" = "YES" ] ; then
517       _DO_NOTHING=YES
518     else
519       cp -af /opt/php54/etc/php54.ini /var/backups/dragon/t/etc-php54.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
520       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini &> /dev/null
521       cp -af /opt/php54/lib/php.ini /var/backups/dragon/t/lib-php54.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
522       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini &> /dev/null
523     fi
524   fi
525   if [ "$_CUSTOM_CONFIG_PHP53" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
526     if [[ "$_THISHOST" =~ "v182q.nyc." ]] && [ "$_CUSTOM_CONFIG_PHP53" = "YES" ] ; then
527       _DO_NOTHING=YES
528     else
529       cp -af /opt/php53/etc/php53.ini /var/backups/dragon/t/etc-php53.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
530       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini &> /dev/null
531       cp -af /opt/php53/lib/php.ini /var/backups/dragon/t/lib-php53.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
532       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini &> /dev/null
533     fi
534   fi
535   if [ "$_CUSTOM_CONFIG_PHP52" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
536     if [[ "$_THISHOST" =~ "v182q.nyc." ]] && [ "$_CUSTOM_CONFIG_PHP52" = "YES" ] ; then
537       _DO_NOTHING=YES
538     else
539       cp -af /opt/php52/etc/php52.ini /var/backups/dragon/t/etc-php52.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
540       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52.ini /opt/php52/etc/php52.ini &> /dev/null
541       cp -af /opt/php52/lib/php.ini /var/backups/dragon/t/lib-php52.ini-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
542       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-cli.ini /opt/php52/lib/php.ini &> /dev/null
543     fi
544   fi
545   ###
546   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-fpm.conf /opt/php55/etc/php55-fpm.conf &> /dev/null
547   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-fpm.conf /opt/php54/etc/php54-fpm.conf &> /dev/null
548   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-fpm.conf /opt/php53/etc/php53-fpm.conf &> /dev/null
549   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php52-fpm.conf /opt/php52/etc/php52-fpm.conf &> /dev/null
550   ###
551   sed -i "s/listen.allowed_clients =.*/listen.allowed_clients = 127.0.0.1,$_THISHTIP/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
552   sed -i "s/>127.0.0.1</\>127.0.0.1,$_THISHTIP\</g"  /opt/php52/etc/php52-fpm.conf &> /dev/null
553   ###
554
555   _THIS_FILE=/opt/php55/etc/php55.ini
556   fix_php_ini_redis
557   fix_php_ini_jsmin
558   fix_php_ini_suhosin
559   fix_php_ini_mailparse
560   _THIS_FILE=/opt/php55/lib/php.ini
561   fix_php_ini_redis
562   fix_php_ini_jsmin
563   fix_php_ini_suhosin
564   fix_php_ini_mailparse
565
566   _THIS_FILE=/opt/php54/etc/php54.ini
567   fix_php_ini_redis
568   fix_php_ini_jsmin
569   fix_php_ini_suhosin
570   fix_php_ini_mailparse
571   _THIS_FILE=/opt/php54/lib/php.ini
572   fix_php_ini_redis
573   fix_php_ini_jsmin
574   fix_php_ini_suhosin
575   fix_php_ini_mailparse
576
577   _THIS_FILE=/opt/php53/etc/php53.ini
578   fix_php_ini_redis
579   fix_php_ini_jsmin
580   fix_php_ini_suhosin
581   fix_php_ini_mailparse
582   _THIS_FILE=/opt/php53/lib/php.ini
583   fix_php_ini_redis
584   fix_php_ini_jsmin
585   fix_php_ini_suhosin
586   fix_php_ini_mailparse
587
588   _THIS_FILE=/opt/php52/etc/php52.ini
589   fix_php_ini_redis
590   fix_php_ini_jsmin
591   fix_php_ini_suhosin
592   fix_php_ini_mailparse
593   _THIS_FILE=/opt/php52/lib/php.ini
594   fix_php_ini_redis
595   fix_php_ini_jsmin
596   fix_php_ini_suhosin
597   fix_php_ini_mailparse
598
599   rm -f /etc/php5/conf.d/{opcache.ini,apc.ini,imagick.ini,memcached.ini,redis.ini,suhosin.ini,newrelic.ini} &> /dev/null
600   fix_php_ini_newrelic_all
601   fix_php_ini_geos_all
602   fix_php_ini_mongo_all
603   fix_php_ini_opcache_all
604
605   ###
606   sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php52/etc/php52.ini &> /dev/null
607   sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php52/lib/php.ini &> /dev/null
608   sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php53/etc/php53.ini &> /dev/null
609   sed -i "s/^zlib.output_compression.*/zlib.output_compression = Off/g" /opt/php53/lib/php.ini &> /dev/null
610   ###
611   sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php52/etc/php52.ini &> /dev/null
612   sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php52/lib/php.ini &> /dev/null
613   sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php53/etc/php53.ini &> /dev/null
614   sed -i "s/.*zlib.output_compression_level/;zlib.output_compression_level/g" /opt/php53/lib/php.ini &> /dev/null
615   ###
616 }
617 #
618 restore_default_php () {
619   msg "INFO: Restoring default PHP configuration"
620   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53-cli.ini /opt/php53/lib/php.ini &> /dev/null
621   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php53.ini /opt/php53/etc/php53.ini &> /dev/null
622   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54-cli.ini /opt/php54/lib/php.ini &> /dev/null
623   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php54.ini /opt/php54/etc/php54.ini &> /dev/null
624   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55-cli.ini /opt/php55/lib/php.ini &> /dev/null
625   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/php55.ini /opt/php55/etc/php55.ini &> /dev/null
626 }
627 #
628 tune_php () {
629   msg "INFO: Tuning PHP configuration"
630   if [ "$_TUNE_PHP_FPM_TIMEOUT" -lt "60" ] ; then
631     _TUNE_PHP_FPM_TIMEOUT=60
632   fi
633   sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/pool.d/*.conf &> /dev/null
634   sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/php*-fpm.conf &> /dev/null
635   sed -i "s/180/$_TUNE_PHP_FPM_TIMEOUT/g"     /opt/php*/etc/php*.ini      &> /dev/null
636   sed -i "s/^max_execution_time =.*/max_execution_time = $_TUNE_PHP_CLI_TIMEOUT/g" /opt/php*/lib/php.ini &> /dev/null
637   sed -i "s/^max_input_time =.*/max_input_time = $_TUNE_PHP_CLI_TIMEOUT/g"         /opt/php*/lib/php.ini &> /dev/null
638 }
639 #
640 restore_default_sql () {
641   msg "INFO: Restoring default SQL configuration"
642   sed -i "s/.*check_for_crashed_tables/#check_for_crashed_tables/g" /etc/mysql/debian-start &> /dev/null
643   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
644     if [ "$_CUSTOM_CONFIG_SQL" = "YES" ] ; then
645       _DO_NOTHING=YES
646     else
647       cp -af /etc/mysql/my.cnf /var/backups/dragon/t/my.cnf-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
648       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/my.cnf.txt /etc/mysql/my.cnf
649       sed -i "s/.*slow_query_log/#slow_query_log/g"           /etc/mysql/my.cnf &> /dev/null
650       sed -i "s/.*long_query_time/#long_query_time/g"         /etc/mysql/my.cnf &> /dev/null
651       sed -i "s/.*slow_query_log_file/#slow_query_log_file/g" /etc/mysql/my.cnf &> /dev/null
652       if [ ! -e "/etc/mysql/skip-name-resolve.txt" ] ; then
653         sed -i "s/.*skip-name-resolve/#skip-name-resolve/g"   /etc/mysql/my.cnf &> /dev/null
654       fi
655     fi
656   fi
657   mv -f /etc/mysql/my.cnf-pre* /var/backups/dragon/t/ &> /dev/null
658   sed -i "s/.*default-table-type/#default-table-type/g" /etc/mysql/my.cnf &> /dev/null
659   sed -i "s/.*language/#language/g" /etc/mysql/my.cnf &> /dev/null
660   _THIS_DB_SERVER_TEST=`mysql -V 2>&1`
661   if [[ "$_THIS_DB_SERVER_TEST" =~ "5.5." ]] || [[ "$_THIS_DB_SERVER_TEST" =~ "10.0" ]] ; then
662     _DO_NOTHING=YES
663   else
664     sed -i "s/.*lc_messages_dir /#lc_messages_dir /g" /etc/mysql/my.cnf &> /dev/null
665     sed -i "s/.*lc_messages /#lc_messages /g" /etc/mysql/my.cnf &> /dev/null
666   fi
667   if [[ "$_THIS_DB_SERVER_TEST" =~ "MariaDB" ]] ; then
668     sed -i "s/.*innodb_lazy_drop_table /#innodb_lazy_drop_table /g" /etc/mysql/my.cnf &> /dev/null
669   fi
670   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
671     if [ "$_DB_BINARY_LOG" = "NO" ] ; then
672       bash /var/xdrago/purge_binlogs.sh &> /dev/null
673       sed -i "s/.*purge_binlogs.*//g" /var/spool/cron/crontabs/root &> /dev/null
674       sed -i "/^$/d" /var/spool/cron/crontabs/root &> /dev/null
675       sed -i "s/^log_bin/#log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
676       sed -i "s/^expire_logs_days/#expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
677       sed -i "s/^max_binlog_size/#max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
678     elif [ "$_DB_BINARY_LOG" = "YES" ] ; then
679       sed -i "s/.*log_bin/log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
680       sed -i "s/.*expire_logs_days/expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
681       sed -i "s/.*max_binlog_size/max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
682     fi
683     if [ "$_DB_ENGINE" = "MyISAM" ] || [ "$_DB_ENGINE" = "InnoDB" ] ; then
684       sed -i "s/^default_storage_engine.*/default_storage_engine  = $_DB_ENGINE/g" /etc/mysql/my.cnf &> /dev/null
685     fi
686   fi
687 }
688 #
689 tune_sql () {
690   msg "INFO: Tuning SQL configuration"
691   sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /etc/mysql/my.cnf     &> /dev/null
692   sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /var/xdrago/minute.sh &> /dev/null
693 }
694 #
695 restore_default_nginx () {
696   msg "INFO: Restoring default Nginx configuration"
697   cd /var/xdrago/monitor/check
698   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/tools/system/monitor/check/scan_nginx ./
699   if [ -d "$_TUNE_HOSTMASTER" ] ; then
700     for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
701     do
702       sed -i "s/#limit_conn /limit_conn /g" $Files &> /dev/null
703     done
704   fi
705   su -s /bin/bash - aegir -c "drush @server_master provision-verify &> /dev/null"
706   mrun "sleep 8"
707 }
708 #
709 tune_nginx () {
710   msg "INFO: Tuning Nginx configuration"
711   sed -i "s/ = 300/ = $_TUNE_NGINX_FIREWALL/g" /var/xdrago/monitor/check/scan_nginx &> /dev/null
712   sed -i "s/300/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf &> /dev/null
713   sed -i "s/180/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf &> /dev/null
714   if [ "$_TUNE_NGINX_CONNECT" = "OFF" ] ; then
715     sed -i "s/limit_conn /#limit_conn /g" /var/aegir/config/server_master/nginx.conf &> /dev/null
716     if [ -d "$_TUNE_HOSTMASTER" ] ; then
717       for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
718       do
719         sed -i "s/limit_conn /#limit_conn /g" $Files &> /dev/null
720       done
721     fi
722   fi
723 }
724 #
725 restart_services () {
726   msg "INFO: Reloading services"
727   mrun "service php-fpm reload"     &> /dev/null
728   mrun "service php53-fpm reload"   &> /dev/null
729   mrun "service mysql restart"      &> /dev/null
730   mrun "service nginx reload"       &> /dev/null
731   if [ -e "/usr/sbin/csf" ] && [ -e "/etc/csf/csf.deny" ] ; then
732     mrun "csf -q"                   &> /dev/null
733   fi
734 }
735 #
736 tune_web_server_config () {
737   _LIM_FPM="$_L_PHP_FPM_WORKERS"
738   if [ "$_LIM_FPM" -lt "24" ] ; then
739     if [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
740       _LIM_FPM=24
741     fi
742   fi
743   let "_PROCESS_MAX_FPM = (($_LIM_FPM * 5))"
744   let "_CHILD_MAX_FPM = (($_LIM_FPM * 2))"
745   if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
746     _DO_NOTHING=YES
747   else
748     _PHP_FPM_WORKERS=${_PHP_FPM_WORKERS//[^0-9]/}
749     if [ ! -z "$_PHP_FPM_WORKERS" ] && [ "$_PHP_FPM_WORKERS" -gt "0" ] ; then
750       _CHILD_MAX_FPM="$_PHP_FPM_WORKERS"
751     fi
752   fi
753   let "_START_LEGACY_FPM = (($_LIM_FPM - 21))"
754   if [ "$_START_LEGACY_FPM" -lt "3" ] ; then
755     _START_LEGACY_FPM=3
756   fi
757   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php55/etc/php55-fpm.conf &> /dev/null
758   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php54/etc/php54-fpm.conf &> /dev/null
759   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php53/etc/php53-fpm.conf &> /dev/null
760   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
761   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
762   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
763   sed -i "s/max_children\">.*</max_children\">$_START_LEGACY_FPM</g" /opt/php52/etc/php52-fpm.conf &> /dev/null
764   if [ ! -z "$_PHP_FPM_DENY" ] ; then
765     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
766     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
767     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
768   fi
769   if [ ! -z "$_L_NGINX_WORKERS" ] ; then
770     sed -i "s/worker_processes.*/worker_processes  $_L_NGINX_WORKERS;/g" /etc/nginx/nginx.conf &> /dev/null
771   else
772     sed -i "s/worker_processes.*/worker_processes  4;/g" /etc/nginx/nginx.conf &> /dev/null
773   fi
774 }
775 #
776 tune_sql_memory_limits () {
777   if [ ! -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
778     msg "INFO: Running MySQLTuner check on all databases..."
779     msg "NOTE! This step may take a LONG time, please wait..."
780     rm -f /var/opt/mysqltuner*
781     ### curl -s --retry 10 --retry-delay 15 -A iCab "https://raw.github.com/major/MySQLTuner-perl/master/mysqltuner.pl" -o /var/opt/mysqltuner.pl
782     curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
783     if [ ! -e "/var/opt/mysqltuner.pl" ] ; then
784       mrun "sleep 60"
785       curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
786     fi
787     if [ -e "/var/opt/mysqltuner.pl" ] ; then
788       perl /var/opt/mysqltuner.pl > /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt 2>&1
789     fi
790   fi
791   if [ -e "/var/opt/mysqltuner.pl" ] && [ -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
792     _REC_MYISAM_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in MyISAM tables" | cut -d: -f2 | awk '{ print $1}'`
793     _REC_INNODB_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in InnoDB tables" | cut -d: -f2 | awk '{ print $1}'`
794     ###--------------------###
795     if [ ! -z "$_REC_MYISAM_MEM" ] ; then
796       _RAW_MYISAM_MEM=`echo $_REC_MYISAM_MEM | sed "s/[A-Z]//g"`
797       if [[ "$_REC_MYISAM_MEM" =~ "G" ]] ; then
798         let "_RAW_MYISAM_MEM = (($_RAW_MYISAM_MEM * 1024))"
799       fi
800       if [ "$_RAW_MYISAM_MEM" -gt "$_USE_SQL" ] ; then
801         _USE_MYISAM_MEM="$_USE_SQL"
802       else
803         _USE_MYISAM_MEM="$_RAW_MYISAM_MEM"
804       fi
805       _USE_MYISAM_MEM="${_USE_MYISAM_MEM}M"
806       sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
807     else
808       _USE_MYISAM_MEM="64M"
809       sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
810     fi
811     ###--------------------###
812     if [ ! -z "$_REC_INNODB_MEM" ] ; then
813       _RAW_INNODB_MEM=`echo $_REC_INNODB_MEM | sed "s/[A-Z]//g"`
814       if [[ "$_REC_INNODB_MEM" =~ "G" ]] ; then
815         let "_RAW_INNODB_MEM = (($_RAW_INNODB_MEM * 1024))"
816       fi
817       if [ "$_RAW_INNODB_MEM" -gt "$_USE_SQL" ] ; then
818         _USE_INNODB_MEM="$_USE_SQL"
819       else
820         _USE_INNODB_MEM="$_RAW_INNODB_MEM"
821       fi
822       _INNODB_BPI="0"
823       _INNODB_BPI=`echo "scale=0; $_USE_INNODB_MEM/1024" | bc`;
824       if [ "$_INNODB_BPI" -gt "1" ] ; then
825         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
826       fi
827       _USE_INNODB_MEM="${_USE_INNODB_MEM}M"
828       sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
829     else
830       _USE_INNODB_MEM="64M"
831       sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
832     fi
833   else
834     sed -i "s/= 181/= $_USE_SQL/g"  /etc/mysql/my.cnf &> /dev/null
835   fi
836 }
837 #
838 tune_memory_limits () {
839   msg "INFO: Default Memory Tuning"
840   _AWS_TEST_A=$(grep cloudimg /etc/fstab)
841   _AWS_TEST_B=$(grep cloudconfig /etc/fstab)
842   _ETH_TEST=`ifconfig 2>&1`
843   _VM_TEST=`uname -a 2>&1`
844   if [[ "$_ETH_TEST" =~ "venet0" ]] ; then
845     _VMFAMILY="VZ"
846   elif [ -e "/proc/bean_counters" ] ; then
847     _VMFAMILY="VZ"
848   elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/menu.lst" ] ; then
849     _VMFAMILY="TG"
850   elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/grub.cfg" ] ; then
851     _VMFAMILY="TG"
852   else
853     _VMFAMILY="XEN"
854   fi
855   if [[ "$_VM_TEST" =~ beng ]] ; then
856     _VMFAMILY="VS"
857   fi
858   if [[ "$_AWS_TEST_A" =~ "cloudimg" ]] || [[ "$_AWS_TEST_B" =~ "cloudconfig" ]] ; then
859     _VMFAMILY="AWS"
860   fi
861   _CPU_INFO=$(grep -c processor /proc/cpuinfo)
862   _CPU_INFO=${_CPU_INFO//[^0-9]/}
863   _NPROC_TEST=$(which nproc)
864   if [ -z "$_NPROC_TEST" ] ; then
865     _CPU_NR="$_CPU_INFO"
866   else
867     _CPU_NR=`nproc`
868   fi
869   _CPU_NR=${_CPU_NR//[^0-9]/}
870   if [ ! -z "$_CPU_NR" ] && [ ! -z "$_CPU_INFO" ] && [ "$_CPU_NR" -gt "$_CPU_INFO" ] && [ "$_CPU_INFO" -gt "0" ] ; then
871     _CPU_NR="$_CPU_INFO"
872   fi
873   if [ -z "$_CPU_NR" ] || [ "$_CPU_NR" -lt "1" ] ; then
874     _CPU_NR=1
875   fi
876   let "_CPU_MX = (($_CPU_NR * 2))"
877   if [ "$_CPU_MX" -lt "4" ] ; then
878     _CPU_MX=4
879   fi
880   let "_CPU_TG = (($_CPU_NR / 2))"
881   if [ "$_CPU_TG" -lt "4" ] ; then
882     _CPU_TG=4
883   fi
884   let "_CPU_VS = (($_CPU_NR / 12))"
885   if [ "$_CPU_VS" -lt "2" ] ; then
886     _CPU_VS=2
887   fi
888   _RAM=`free -mto | grep Mem: | awk '{ print $2 }'`
889   if [ "$_RESERVED_RAM" -gt "0" ] ; then
890     let "_RAM = (($_RAM - $_RESERVED_RAM))"
891   fi
892   let "_USE = (($_RAM / 4))"
893   if [ "$_VMFAMILY" = "VS" ] || [[ "$_THISHOST" =~ ".host8." ]] ; then
894     if [ "$_VMFAMILY" = "VS" ] ; then
895       if [ -e "/root/.tg.cnf" ] ; then
896         let "_USE_SQL = (($_RAM / 12))"
897       else
898         let "_USE_SQL = (($_RAM / 24))"
899       fi
900     else
901       let "_USE_SQL = (($_RAM / 8))"
902     fi
903   else
904     let "_USE_SQL = (($_RAM / 8))"
905   fi
906   _TMP_SQL="${_USE_SQL}M"
907   let "_SRT_SQL = (($_USE_SQL * 2))"
908   _SRT_SQL="${_SRT_SQL}K"
909   if [ "$_USE" -ge "512" ] && [ "$_USE" -lt "1024" ] ; then
910     _USE_PHP=512
911     _USE_OPC=512
912     _PRC_SQL=20
913     _QCE_SQL=128M
914     _RND_SQL=8M
915     _JBF_SQL=4M
916     if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
917       _L_PHP_FPM_WORKERS=12
918     else
919       _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
920     fi
921     if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
922       _L_NGINX_WORKERS=$_CPU_MX
923     else
924       _L_NGINX_WORKERS=$_NGINX_WORKERS
925     fi
926   elif [ "$_USE" -ge "1024" ] ; then
927     if [ "$_VMFAMILY" = "XEN" ] || [ "$_VMFAMILY" = "AWS" ] ; then
928       _USE_PHP=512
929       _USE_OPC=1024
930       _PRC_SQL=30
931       _QCE_SQL=128M
932       _RND_SQL=8M
933       _JBF_SQL=4M
934       if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
935         _L_PHP_FPM_WORKERS=24
936       else
937         _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
938       fi
939       if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
940         _L_NGINX_WORKERS=$_CPU_MX
941       else
942         _L_NGINX_WORKERS=$_NGINX_WORKERS
943       fi
944     elif [ "$_VMFAMILY" = "VS" ] || [ "$_VMFAMILY" = "TG" ] ; then
945       if [ -e "/boot/grub/grub.cfg" ] || [ -e "/boot/grub/menu.lst" ] || [ -e "/root/.tg.cnf" ] ; then
946         _USE_PHP=1024
947         _USE_OPC=1024
948         _PRC_SQL=50
949         _QCE_SQL=128M
950         _RND_SQL=8M
951         _JBF_SQL=4M
952         if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
953           _L_PHP_FPM_WORKERS=24
954         else
955           _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
956         fi
957         if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
958           _L_NGINX_WORKERS=$_CPU_TG
959         else
960           _L_NGINX_WORKERS=$_NGINX_WORKERS
961         fi
962         sed -i "s/64000/128000/g"  /opt/php53/etc/php55.ini &> /dev/null
963         sed -i "s/64000/128000/g"  /opt/php53/etc/php54.ini &> /dev/null
964         sed -i "s/64000/128000/g"  /opt/php53/etc/php53.ini &> /dev/null
965         sed -i "s/64000/128000/g"  /opt/php52/etc/php52.ini &> /dev/null
966       else
967         _USE_PHP=1024
968         _USE_OPC=1024
969         _PRC_SQL=25
970         _QCE_SQL=64M
971         _RND_SQL=2M
972         _JBF_SQL=2M
973         if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
974           _L_PHP_FPM_WORKERS=6
975         else
976           _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
977         fi
978         if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
979           _L_NGINX_WORKERS=$_CPU_VS
980         else
981           _L_NGINX_WORKERS=$_NGINX_WORKERS
982         fi
983       fi
984     else
985       _USE_PHP=256
986       _USE_OPC=256
987       _PRC_SQL=10
988       _QCE_SQL=32M
989       _RND_SQL=2M
990       _JBF_SQL=2M
991       if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
992         _L_PHP_FPM_WORKERS=12
993       else
994         _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
995       fi
996       if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
997         _L_NGINX_WORKERS=$_CPU_MX
998       else
999         _L_NGINX_WORKERS=$_NGINX_WORKERS
1000       fi
1001     fi
1002   else
1003     _USE_PHP="$_USE"
1004     _USE_OPC="$_USE"
1005     _PRC_SQL=10
1006     _QCE_SQL=32M
1007     _RND_SQL=1M
1008     _JBF_SQL=1M
1009     if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
1010       _L_PHP_FPM_WORKERS=6
1011     else
1012       _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
1013     fi
1014     if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
1015       _L_NGINX_WORKERS=$_CPU_MX
1016     else
1017       _L_NGINX_WORKERS=$_NGINX_WORKERS
1018     fi
1019   fi
1020   _USE_JETTY="-Xmx${_USE_OPC}m"
1021   if [ "$_VMFAMILY" = "VZ" ] ; then
1022     _USE_OPC=64
1023   fi
1024   let "_USE_FPM = (($_USE_PHP / 2))"
1025   if [ "$_USE_FPM" -lt "196" ] ; then
1026     _USE_FPM=196
1027   fi
1028   if [ "$_USE_PHP" -lt "196" ] ; then
1029     _USE_PHP=196
1030   fi
1031   if [ ! -e "/var/xdrago/conf/fpm-pool-foo.conf" ] ; then
1032     mkdir -p /var/xdrago/conf
1033   fi
1034   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm-pool-foo.conf /var/xdrago/conf/fpm-pool-foo.conf
1035   if [ -e "/opt/etc/fpm/fpm-pool-common.conf" ] ; then
1036     sed -i "s/256/$_USE_FPM/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
1037   fi
1038   if [ -e "/opt/php55/etc/php55.ini" ] ; then
1039     sed -i "s/256/$_USE_FPM/g" /opt/php55/etc/php55.ini &> /dev/null
1040     sed -i "s/181/$_USE_OPC/g" /opt/php55/etc/php55.ini &> /dev/null
1041     sed -i "s/256/$_USE_PHP/g" /opt/php55/lib/php.ini &> /dev/null
1042     sed -i "s/256/$_USE_PHP/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
1043   fi
1044   if [ -e "/opt/php54/etc/php54.ini" ] ; then
1045     sed -i "s/256/$_USE_FPM/g" /opt/php54/etc/php54.ini &> /dev/null
1046     sed -i "s/181/$_USE_OPC/g" /opt/php54/etc/php54.ini &> /dev/null
1047     sed -i "s/256/$_USE_PHP/g" /opt/php54/lib/php.ini &> /dev/null
1048     sed -i "s/256/$_USE_PHP/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
1049   fi
1050   if [ -e "/opt/php53/etc/php53.ini" ] ; then
1051     sed -i "s/256/$_USE_FPM/g" /opt/php53/etc/php53.ini &> /dev/null
1052     sed -i "s/181/$_USE_OPC/g" /opt/php53/etc/php53.ini &> /dev/null
1053     sed -i "s/256/$_USE_PHP/g" /opt/php53/lib/php.ini &> /dev/null
1054     sed -i "s/256/$_USE_PHP/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
1055   fi
1056   if [ -e "/opt/php52/etc/php52.ini" ] ; then
1057     sed -i "s/256/$_USE_FPM/g" /opt/php52/etc/php52.ini &> /dev/null
1058     sed -i "s/181/$_USE_OPC/g" /opt/php52/etc/php52.ini &> /dev/null
1059     sed -i "s/256/$_USE_PHP/g" /opt/php52/lib/php.ini &> /dev/null
1060   fi
1061   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
1062     tune_sql_memory_limits
1063     sed -i "s/= 191/= $_PRC_SQL/g"                                               /etc/mysql/my.cnf &> /dev/null
1064     sed -i "s/^tmp_table_size.*/tmp_table_size          = $_TMP_SQL/g"           /etc/mysql/my.cnf &> /dev/null
1065     sed -i "s/^max_heap_table_size.*/max_heap_table_size     = $_TMP_SQL/g"      /etc/mysql/my.cnf &> /dev/null
1066     sed -i "s/^myisam_sort_buffer_size.*/myisam_sort_buffer_size = $_SRT_SQL/g"  /etc/mysql/my.cnf &> /dev/null
1067     sed -i "s/^query_cache_size.*/query_cache_size        = $_QCE_SQL/g"         /etc/mysql/my.cnf &> /dev/null
1068     sed -i "s/^read_rnd_buffer_size.*/read_rnd_buffer_size    = $_RND_SQL/g"     /etc/mysql/my.cnf &> /dev/null
1069     sed -i "s/^join_buffer_size.*/join_buffer_size        = $_JBF_SQL/g"         /etc/mysql/my.cnf &> /dev/null
1070   fi
1071   _MAX_MEM_REDIS="${_USE_OPC}MB"
1072   sed -i "s/^maxmemory .*/maxmemory $_MAX_MEM_REDIS/g"  /etc/redis/redis.conf &> /dev/null
1073   if [ -e "/etc/default/jetty9" ] && [ -e "/opt/solr4" ] ; then
1074     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
1075   fi
1076   if [ -e "/etc/default/jetty8" ] && [ -e "/opt/solr3" ] ; then
1077     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
1078   fi
1079   if [ -e "/etc/default/jetty7" ] && [ -e "/opt/solr1" ] ; then
1080     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
1081   fi
1082   tune_web_server_config
1083 }
1084 #
1085 check_git_repos_status () {
1086   _GITHUB_WORKS=NO
1087   _DRUPAL_WORKS=NO
1088   _GITORIOUS_WORKS=NO
1089   if [ "$_FORCE_GIT_MIRROR" = "github" ] ; then
1090     msg "INFO: We will use forced GitHub repository without testing connection"
1091     _GITHUB_WORKS=YES
1092     _DRUPAL_WORKS=NO
1093     _GITORIOUS_WORKS=NO
1094     sleep 1
1095   elif [ "$_FORCE_GIT_MIRROR" = "drupal" ] ; then
1096     msg "INFO: We will use forced Drupal mirror without testing connection"
1097     _GITHUB_WORKS=NO
1098     _DRUPAL_WORKS=YES
1099     _GITORIOUS_WORKS=NO
1100     sleep 1
1101   elif [ "$_FORCE_GIT_MIRROR" = "gitorious" ] ; then
1102     msg "INFO: We will use forced Gitorious mirror without testing connection"
1103     _GITHUB_WORKS=NO
1104     _DRUPAL_WORKS=NO
1105     _GITORIOUS_WORKS=YES
1106     sleep 1
1107   else
1108     msg "INFO: Testing repository mirror servers availability..."
1109     sleep 1
1110     _GITHUB_WORKS=YES
1111     _DRUPAL_WORKS=YES
1112     _GITORIOUS_WORKS=YES
1113     if ! netcat -w 8 -z github.com 9418 ; then
1114       _GITHUB_WORKS=NO
1115       msg "WARN: The GitHub master repository server doesn't respond..."
1116     elif ! netcat -w 8 -z git.drupal.org 80 ; then
1117       _DRUPAL_WORKS=NO
1118       msg "WARN: The Drupal mirror repository server doesn't respond..."
1119     elif ! netcat -w 8 -z code.aegir.cc 9418 ; then
1120       _GITORIOUS_WORKS=NO
1121       msg "WARN: The Gitorious mirror repository server doesn't respond..."
1122     fi
1123   fi
1124   if [ "$_GITHUB_WORKS" = "YES" ] ; then
1125     _BOA_REPO_NAME="boa"
1126     _BOA_REPO_GIT_URL="git://github.com/omega8cc"
1127     msg "INFO: GitHub master repository will be used"
1128   elif [ "$_DRUPAL_WORKS" = "YES" ] ; then
1129     _BOA_REPO_NAME="barracuda"
1130     _BOA_REPO_GIT_URL="http://git.drupal.org/project"
1131     msg "INFO: Drupal mirror repository will be used"
1132   elif [ "$_GITORIOUS_WORKS" = "YES" ] ; then
1133     _BOA_REPO_NAME="barracuda-octopus"
1134     _BOA_REPO_GIT_URL="git://code.aegir.cc/aegir"
1135     msg "INFO: Gitorious mirror repository will be used"
1136   else
1137     cat <<EOF
1138
1139     None of repository servers responded in 8 seconds,
1140     so we can't continue this installation.
1141
1142     Please try again later or check if your firewall has port 9418 open.
1143
1144     Bye.
1145
1146 EOF
1147     exit 1
1148   fi
1149   _BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
1150 }
1151
1152
1153 ###---### init
1154 #
1155 touch /var/run/boa_run.pid
1156 #
1157 _BOA_REPO_NAME="boa"
1158 _BOA_REPO_GIT_URL="git://github.com/omega8cc"
1159 _BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
1160 if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
1161   check_git_repos_status
1162 fi
1163 #
1164 #
1165 if [ `whoami` = "root" ] ; then
1166   chmod a+w /dev/null
1167   if [ ! -e "/dev/fd" ] ; then
1168     if [ -e "/proc/self/fd" ] ; then
1169       rm -rf /dev/fd
1170       ln -s /proc/self/fd /dev/fd
1171     fi
1172   fi
1173   msg "INFO: This script is ran as a root user"
1174 else
1175   msg "ERROR: This script should be ran as a root user - please `sudo -i` first"
1176   msg "Bye"
1177   exit 1
1178 fi
1179 #
1180 #
1181 if [ ! -f "/var/log/barracuda_log.txt" ] ; then
1182   msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
1183   msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
1184   msg "Bye"
1185   exit 1
1186 else
1187   _VERSIONS_TEST=`cat /var/log/barracuda_log.txt`
1188   if [[ "$_VERSIONS_TEST" =~ "$_INSTALLER_VERSION" ]] ; then
1189     _VERSIONS_TEST_RESULT=OK
1190   else
1191     msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
1192     msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
1193     msg "Bye"
1194     exit 1
1195   fi
1196 fi
1197 #
1198 #
1199 rm -f /var/run/aegir_upgrade.pid
1200 rm -f /opt/tmp/testecho*
1201 _SRCDIR=/opt/tmp/files
1202 mkdir -p $_SRCDIR
1203 chmod -R 777 /opt/tmp &> /dev/null
1204 cd /opt/tmp
1205 rm -f -r /opt/tmp/$_BOA_REPO_NAME
1206 if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
1207   git clone $_BOA_REPO_GIT_URL/$_BOA_REPO_NAME.git &> /dev/null
1208   if [ "$_FORCE_GIT_MIRROR" = "test" ] ; then
1209     cd /opt/tmp/$_BOA_REPO_NAME
1210     git checkout 2.0.5-dev &> /dev/null
1211     cd /opt/tmp
1212   fi
1213 else
1214   curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/versions/stable/tar/boa-$_INSTALLER_VERSION.tar.gz" | tar -xzf -
1215   _BOA_REPO_NAME="boa"
1216 fi
1217 #
1218 # Get spinner
1219 cd $_SRCDIR
1220 rm -f spinner*
1221 cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/helpers/spinner ./
1222 chmod +x spinner &> /dev/null
1223 #
1224 # Create tmp stuff
1225 _LOG=/var/backups/bond-$_NOW.log
1226 _SILENT=/opt/tmp/silent.log
1227 #
1228 #
1229
1230
1231 ###---### Tune Your Aegir Hosting System
1232 #
1233 echo " "
1234 msg "TUNER START -> checkpoint: "
1235 cat <<EOF
1236
1237   * Aegir Satellite Instance to tune: $_TUNE_HOSTMASTER
1238   * Nginx server mod_evasive will be set to $_TUNE_NGINX_CONNECT
1239   * Nginx server fastcgi timeout will be set to $_TUNE_NGINX_TIMEOUT seconds
1240   * Nginx firewall limit of allowed requests will be set to $_TUNE_NGINX_FIREWALL/300
1241   * Database server timeout will be set to $_TUNE_SQL_TIMEOUT seconds
1242   * PHP-FPM server timeout will be set to $_TUNE_PHP_FPM_TIMEOUT seconds
1243   * PHP-CLI drush timeout will be set to $_TUNE_PHP_CLI_TIMEOUT seconds
1244
1245 EOF
1246 echo " "
1247 if prompt_yes_no "Are you ready to tune your Aegir Hosting System with above values?" ; then
1248   true
1249   if [ ! -e "/root/.upstart.cnf" ] ; then
1250     msg "INFO: We will stop cron and then wait 30 seconds..."
1251     service cron stop &> /dev/null
1252     mrun "sleep 30"
1253   fi
1254   msg "INFO: Tuning in progress, please wait..."
1255   if [ -e "/root/.barracuda.cnf" ] ; then
1256     source /root/.barracuda.cnf
1257   fi
1258   restore_default_php
1259   update_php_conf
1260   tune_php
1261   restore_default_sql
1262   tune_sql
1263   restore_default_nginx
1264   tune_nginx
1265   tune_memory_limits
1266   restart_services
1267   msg "INFO: Tuning completed"
1268 else
1269   if [ ! -e "/root/.upstart.cnf" ] ; then
1270     msg "INFO: We will stop cron and then wait 30 seconds..."
1271     service cron stop &> /dev/null
1272     mrun "sleep 30"
1273   fi
1274   if [ -e "/root/.barracuda.cnf" ] ; then
1275     source /root/.barracuda.cnf
1276   fi
1277   restore_default_php
1278   update_php_conf
1279   restore_default_sql
1280   restore_default_nginx
1281   tune_memory_limits
1282   restart_services
1283   msg "INFO: Tuning stopped and default settings restored"
1284 fi
1285 rm -f -r /var/opt/*
1286 rm -f -r /opt/tmp/*
1287 rm -f /var/run/boa_run.pid
1288 if [ ! -e "/root/.upstart.cnf" ] ; then
1289   service cron start &> /dev/null
1290 fi
1291 msg "INFO: Cron started again"
1292 msg "BYE!"
1293
1294 ###----------------------------------------###
1295 ###
1296 ###  Barracuda-Octopus-Nginx-Drupal Tuner
1297 ###  Copyright (C) 2010-2014 Omega8.cc
1298 ###  noc@omega8.cc www.omega8.cc
1299 ###
1300 ###----------------------------------------###
1301