Drupal 7.27.1 and Pressflow 6.31.1
[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.2
141 ###
142 _AEGIR_VERSION=HEAD
143
144
145 ###----------------------------------------###
146 ### DON'T EDIT ANYTHING BELOW THIS LINE    ###
147 ###----------------------------------------###
148
149 _INSTALLER_VERSION=BOA-2.2.3-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:/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   sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/pool.d/*.conf &> /dev/null
631   sed -i "s/180s/${_TUNE_PHP_FPM_TIMEOUT}s/g" /opt/php*/etc/php*-fpm.conf &> /dev/null
632   sed -i "s/180/$_TUNE_PHP_FPM_TIMEOUT/g"     /opt/php*/etc/php*.ini      &> /dev/null
633   sed -i "s/^max_execution_time =.*/max_execution_time = $_TUNE_PHP_CLI_TIMEOUT/g" /opt/php*/lib/php.ini &> /dev/null
634   sed -i "s/^max_input_time =.*/max_input_time = $_TUNE_PHP_CLI_TIMEOUT/g"         /opt/php*/lib/php.ini &> /dev/null
635 }
636 #
637 restore_default_sql () {
638   msg "INFO: Restoring default SQL configuration"
639   sed -i "s/.*check_for_crashed_tables/#check_for_crashed_tables/g" /etc/mysql/debian-start &> /dev/null
640   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] || [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
641     if [ "$_CUSTOM_CONFIG_SQL" = "YES" ] ; then
642       _DO_NOTHING=YES
643     else
644       cp -af /etc/mysql/my.cnf /var/backups/dragon/t/my.cnf-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
645       cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/my.cnf.txt /etc/mysql/my.cnf
646       sed -i "s/.*slow_query_log/#slow_query_log/g"           /etc/mysql/my.cnf &> /dev/null
647       sed -i "s/.*long_query_time/#long_query_time/g"         /etc/mysql/my.cnf &> /dev/null
648       sed -i "s/.*slow_query_log_file/#slow_query_log_file/g" /etc/mysql/my.cnf &> /dev/null
649       if [ ! -e "/etc/mysql/skip-name-resolve.txt" ] ; then
650         sed -i "s/.*skip-name-resolve/#skip-name-resolve/g"   /etc/mysql/my.cnf &> /dev/null
651       fi
652     fi
653   fi
654   mv -f /etc/mysql/my.cnf-pre* /var/backups/dragon/t/ &> /dev/null
655   sed -i "s/.*default-table-type/#default-table-type/g" /etc/mysql/my.cnf &> /dev/null
656   sed -i "s/.*language/#language/g" /etc/mysql/my.cnf &> /dev/null
657   _THIS_DB_SERVER_TEST=`mysql -V 2>&1`
658   if [[ "$_THIS_DB_SERVER_TEST" =~ "5.5." ]] ; then
659     _DO_NOTHING=YES
660   else
661     sed -i "s/.*lc_messages_dir /#lc_messages_dir /g" /etc/mysql/my.cnf &> /dev/null
662     sed -i "s/.*lc_messages /#lc_messages /g" /etc/mysql/my.cnf &> /dev/null
663   fi
664   if [[ "$_THIS_DB_SERVER_TEST" =~ "MariaDB" ]] ; then
665     sed -i "s/.*innodb_lazy_drop_table /#innodb_lazy_drop_table /g" /etc/mysql/my.cnf &> /dev/null
666   fi
667   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
668     if [ "$_DB_BINARY_LOG" = "NO" ] ; then
669       bash /var/xdrago/purge_binlogs.sh &> /dev/null
670       sed -i "s/.*purge_binlogs.*//g" /var/spool/cron/crontabs/root &> /dev/null
671       sed -i "/^$/d" /var/spool/cron/crontabs/root &> /dev/null
672       sed -i "s/^log_bin/#log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
673       sed -i "s/^expire_logs_days/#expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
674       sed -i "s/^max_binlog_size/#max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
675     elif [ "$_DB_BINARY_LOG" = "YES" ] ; then
676       sed -i "s/.*log_bin/log_bin/g"                   /etc/mysql/my.cnf &> /dev/null
677       sed -i "s/.*expire_logs_days/expire_logs_days/g" /etc/mysql/my.cnf &> /dev/null
678       sed -i "s/.*max_binlog_size/max_binlog_size/g"   /etc/mysql/my.cnf &> /dev/null
679     fi
680     if [ "$_DB_ENGINE" = "MyISAM" ] || [ "$_DB_ENGINE" = "InnoDB" ] ; then
681       sed -i "s/^default_storage_engine.*/default_storage_engine  = $_DB_ENGINE/g" /etc/mysql/my.cnf &> /dev/null
682     fi
683   fi
684 }
685 #
686 tune_sql () {
687   msg "INFO: Tuning SQL configuration"
688   sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /etc/mysql/my.cnf     &> /dev/null
689   sed -i "s/3600/$_TUNE_SQL_TIMEOUT/g" /var/xdrago/minute.sh &> /dev/null
690 }
691 #
692 restore_default_nginx () {
693   msg "INFO: Restoring default Nginx configuration"
694   cd /var/xdrago/monitor/check
695   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/tools/system/monitor/check/scan_nginx ./
696   if [ -d "$_TUNE_HOSTMASTER" ] ; then
697     for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
698     do
699       sed -i "s/#limit_conn /limit_conn /g" $Files &> /dev/null
700     done
701   fi
702   su -s /bin/bash - aegir -c "drush @server_master provision-verify &> /dev/null"
703   mrun "sleep 8"
704 }
705 #
706 tune_nginx () {
707   msg "INFO: Tuning Nginx configuration"
708   sed -i "s/ = 300/ = $_TUNE_NGINX_FIREWALL/g" /var/xdrago/monitor/check/scan_nginx &> /dev/null
709   sed -i "s/300/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf &> /dev/null
710   sed -i "s/180/$_TUNE_NGINX_TIMEOUT/g" /var/aegir/config/server_master/nginx.conf &> /dev/null
711   if [ "$_TUNE_NGINX_CONNECT" = "OFF" ] ; then
712     sed -i "s/limit_conn /#limit_conn /g" /var/aegir/config/server_master/nginx.conf &> /dev/null
713     if [ -d "$_TUNE_HOSTMASTER" ] ; then
714       for Files in `find $_TUNE_HOSTMASTER/config/server_master/nginx/vhost.d -type f`
715       do
716         sed -i "s/limit_conn /#limit_conn /g" $Files &> /dev/null
717       done
718     fi
719   fi
720 }
721 #
722 restart_services () {
723   msg "INFO: Reloading services"
724   mrun "service php-fpm reload"     &> /dev/null
725   mrun "service php53-fpm reload"   &> /dev/null
726   mrun "service mysql restart"      &> /dev/null
727   mrun "service nginx reload"       &> /dev/null
728   if [ -e "/usr/sbin/csf" ] && [ -e "/etc/csf/csf.deny" ] ; then
729     mrun "csf -q"                   &> /dev/null
730   fi
731 }
732 #
733 tune_web_server_config () {
734   _LIM_FPM="$_L_PHP_FPM_WORKERS"
735   if [ "$_LIM_FPM" -lt "24" ] ; then
736     if [[ "$_THISHOST" =~ ".host8." ]] || [ "$_VMFAMILY" = "VS" ] ; then
737       _LIM_FPM=24
738     fi
739   fi
740   let "_PROCESS_MAX_FPM = (($_LIM_FPM * 5))"
741   let "_CHILD_MAX_FPM = (($_LIM_FPM * 2))"
742   let "_START_LEGACY_FPM = (($_LIM_FPM - 21))"
743   if [ "$_START_LEGACY_FPM" -lt "3" ] ; then
744     _START_LEGACY_FPM=3
745   fi
746   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php55/etc/php55-fpm.conf &> /dev/null
747   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php54/etc/php54-fpm.conf &> /dev/null
748   sed -i "s/process.max =.*/process.max = $_PROCESS_MAX_FPM/g"       /opt/php53/etc/php53-fpm.conf &> /dev/null
749   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
750   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
751   sed -i "s/pm.max_children =.*/pm.max_children = $_CHILD_MAX_FPM/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
752   sed -i "s/max_children\">.*</max_children\">$_START_LEGACY_FPM</g" /opt/php52/etc/php52-fpm.conf &> /dev/null
753   if [ ! -z "$_PHP_FPM_DENY" ] ; then
754     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
755     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
756     sed -i "s/passthru,/$_PHP_FPM_DENY,/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
757   fi
758   if [ ! -z "$_L_NGINX_WORKERS" ] ; then
759     sed -i "s/worker_processes.*/worker_processes  $_L_NGINX_WORKERS;/g" /etc/nginx/nginx.conf &> /dev/null
760   else
761     sed -i "s/worker_processes.*/worker_processes  4;/g" /etc/nginx/nginx.conf &> /dev/null
762   fi
763 }
764 #
765 tune_sql_memory_limits () {
766   if [ ! -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
767     msg "INFO: Running MySQLTuner check on all databases..."
768     msg "NOTE! This step may take a LONG time, please wait..."
769     rm -f /var/opt/mysqltuner*
770     ### curl -s --retry 10 --retry-delay 15 -A iCab "https://raw.github.com/major/MySQLTuner-perl/master/mysqltuner.pl" -o /var/opt/mysqltuner.pl
771     curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
772     if [ ! -e "/var/opt/mysqltuner.pl" ] ; then
773       mrun "sleep 60"
774       curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/mysqltuner.pl" -o /var/opt/mysqltuner.pl
775     fi
776     if [ -e "/var/opt/mysqltuner.pl" ] ; then
777       perl /var/opt/mysqltuner.pl > /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt
778     fi
779   fi
780   if [ -e "/var/opt/mysqltuner.pl" ] && [ -e "/var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt" ] ; then
781     _REC_MYISAM_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in MyISAM tables" | cut -d: -f2 | awk '{ print $1}'`
782     _REC_INNODB_MEM=`cat /var/opt/mysqltuner-$_INSTALLER_VERSION-$_NOW.txt | grep "Data in InnoDB tables" | cut -d: -f2 | awk '{ print $1}'`
783     ###--------------------###
784     if [ ! -z "$_REC_MYISAM_MEM" ] ; then
785       _RAW_MYISAM_MEM=`echo $_REC_MYISAM_MEM | sed "s/[A-Z]//g"`
786       if [[ "$_REC_MYISAM_MEM" =~ "G" ]] ; then
787         let "_RAW_MYISAM_MEM = (($_RAW_MYISAM_MEM * 1024))"
788       fi
789       if [ "$_RAW_MYISAM_MEM" -gt "$_USE_SQL" ] ; then
790         _USE_MYISAM_MEM="$_USE_SQL"
791       else
792         _USE_MYISAM_MEM="$_RAW_MYISAM_MEM"
793       fi
794       _USE_MYISAM_MEM="${_USE_MYISAM_MEM}M"
795       sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
796     else
797       _USE_MYISAM_MEM="64M"
798       sed -i "s/^key_buffer_size.*/key_buffer_size         = $_USE_MYISAM_MEM/g"  /etc/mysql/my.cnf &> /dev/null
799     fi
800     ###--------------------###
801     if [ ! -z "$_REC_INNODB_MEM" ] ; then
802       _RAW_INNODB_MEM=`echo $_REC_INNODB_MEM | sed "s/[A-Z]//g"`
803       if [[ "$_REC_INNODB_MEM" =~ "G" ]] ; then
804         let "_RAW_INNODB_MEM = (($_RAW_INNODB_MEM * 1024))"
805       fi
806       if [ "$_RAW_INNODB_MEM" -gt "$_USE_SQL" ] ; then
807         _USE_INNODB_MEM="$_USE_SQL"
808       else
809         _USE_INNODB_MEM="$_RAW_INNODB_MEM"
810       fi
811       _INNODB_BPI="0"
812       _INNODB_BPI=`echo "scale=0; $_USE_INNODB_MEM/1024" | bc`;
813       if [ "$_INNODB_BPI" -gt "1" ] ; then
814         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
815       fi
816       _USE_INNODB_MEM="${_USE_INNODB_MEM}M"
817       sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
818     else
819       _USE_INNODB_MEM="64M"
820       sed -i "s/^innodb_buffer_pool_size.*/innodb_buffer_pool_size = $_USE_INNODB_MEM/g"  /etc/mysql/my.cnf &> /dev/null
821     fi
822   else
823     sed -i "s/= 181/= $_USE_SQL/g"  /etc/mysql/my.cnf &> /dev/null
824   fi
825 }
826 #
827 tune_memory_limits () {
828   msg "INFO: Default Memory Tuning"
829   _AWS_TEST_A=$(grep cloudimg /etc/fstab)
830   _AWS_TEST_B=$(grep cloudconfig /etc/fstab)
831   _ETH_TEST=`ifconfig 2>&1`
832   _VM_TEST=`uname -a 2>&1`
833   if [[ "$_ETH_TEST" =~ "venet0" ]] ; then
834     _VMFAMILY="VZ"
835   elif [ -e "/proc/bean_counters" ] ; then
836     _VMFAMILY="VZ"
837   elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/menu.lst" ] ; then
838     _VMFAMILY="TG"
839   elif [[ "$_THISHOST" =~ ".host8." ]] && [ -e "/boot/grub/grub.cfg" ] ; then
840     _VMFAMILY="TG"
841   else
842     _VMFAMILY="XEN"
843   fi
844   if [[ "$_VM_TEST" =~ beng ]] ; then
845     _VMFAMILY="VS"
846   fi
847   if [[ "$_AWS_TEST_A" =~ "cloudimg" ]] || [[ "$_AWS_TEST_B" =~ "cloudconfig" ]] ; then
848     _VMFAMILY="AWS"
849   fi
850   _CPU_INFO=$(grep -c processor /proc/cpuinfo)
851   _CPU_INFO=${_CPU_INFO//[^0-9]/}
852   _NPROC_TEST=$(which nproc)
853   if [ -z "$_NPROC_TEST" ] ; then
854     _CPU_NR="$_CPU_INFO"
855   else
856     _CPU_NR=`nproc`
857   fi
858   _CPU_NR=${_CPU_NR//[^0-9]/}
859   if [ ! -z "$_CPU_NR" ] && [ ! -z "$_CPU_INFO" ] && [ "$_CPU_NR" -gt "$_CPU_INFO" ] && [ "$_CPU_INFO" -gt "0" ] ; then
860     _CPU_NR="$_CPU_INFO"
861   fi
862   if [ -z "$_CPU_NR" ] || [ "$_CPU_NR" -lt "1" ] ; then
863     _CPU_NR=1
864   fi
865   let "_CPU_MX = (($_CPU_NR * 2))"
866   if [ "$_CPU_MX" -lt "4" ] ; then
867     _CPU_MX=4
868   fi
869   let "_CPU_TG = (($_CPU_NR / 2))"
870   if [ "$_CPU_TG" -lt "4" ] ; then
871     _CPU_TG=4
872   fi
873   let "_CPU_VS = (($_CPU_NR / 12))"
874   if [ "$_CPU_VS" -lt "2" ] ; then
875     _CPU_VS=2
876   fi
877   _RAM=`free -mto | grep Mem: | awk '{ print $2 }'`
878   if [ "$_RESERVED_RAM" -gt "0" ] ; then
879     let "_RAM = (($_RAM - $_RESERVED_RAM))"
880   fi
881   let "_USE = (($_RAM / 4))"
882   if [ "$_VMFAMILY" = "VS" ] || [[ "$_THISHOST" =~ ".host8." ]] ; then
883     if [ "$_VMFAMILY" = "VS" ] ; then
884       if [ -e "/root/.tg.cnf" ] ; then
885         let "_USE_SQL = (($_RAM / 12))"
886       else
887         let "_USE_SQL = (($_RAM / 24))"
888       fi
889     else
890       let "_USE_SQL = (($_RAM / 8))"
891     fi
892   else
893     let "_USE_SQL = (($_RAM / 8))"
894   fi
895   _TMP_SQL="${_USE_SQL}M"
896   let "_SRT_SQL = (($_USE_SQL * 2))"
897   _SRT_SQL="${_SRT_SQL}K"
898   if [ "$_USE" -ge "512" ] && [ "$_USE" -lt "1024" ] ; then
899     _USE_PHP=512
900     _USE_OPC=512
901     _PRC_SQL=20
902     _QCE_SQL=128M
903     _RND_SQL=8M
904     _JBF_SQL=4M
905     if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
906       _L_PHP_FPM_WORKERS=12
907     else
908       _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
909     fi
910     if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
911       _L_NGINX_WORKERS=$_CPU_MX
912     else
913       _L_NGINX_WORKERS=$_NGINX_WORKERS
914     fi
915   elif [ "$_USE" -ge "1024" ] ; then
916     if [ "$_VMFAMILY" = "XEN" ] || [ "$_VMFAMILY" = "AWS" ] ; then
917       _USE_PHP=512
918       _USE_OPC=1024
919       _PRC_SQL=30
920       _QCE_SQL=128M
921       _RND_SQL=8M
922       _JBF_SQL=4M
923       if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
924         _L_PHP_FPM_WORKERS=24
925       else
926         _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
927       fi
928       if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
929         _L_NGINX_WORKERS=$_CPU_MX
930       else
931         _L_NGINX_WORKERS=$_NGINX_WORKERS
932       fi
933     elif [ "$_VMFAMILY" = "VS" ] || [ "$_VMFAMILY" = "TG" ] ; then
934       if [ -e "/boot/grub/grub.cfg" ] || [ -e "/boot/grub/menu.lst" ] || [ -e "/root/.tg.cnf" ] ; then
935         _USE_PHP=1024
936         _USE_OPC=1024
937         _PRC_SQL=50
938         _QCE_SQL=128M
939         _RND_SQL=8M
940         _JBF_SQL=4M
941         if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
942           _L_PHP_FPM_WORKERS=24
943         else
944           _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
945         fi
946         if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
947           _L_NGINX_WORKERS=$_CPU_TG
948         else
949           _L_NGINX_WORKERS=$_NGINX_WORKERS
950         fi
951         sed -i "s/64000/128000/g"  /opt/php53/etc/php55.ini &> /dev/null
952         sed -i "s/64000/128000/g"  /opt/php53/etc/php54.ini &> /dev/null
953         sed -i "s/64000/128000/g"  /opt/php53/etc/php53.ini &> /dev/null
954         sed -i "s/64000/128000/g"  /opt/php52/etc/php52.ini &> /dev/null
955       else
956         _USE_PHP=1024
957         _USE_OPC=1024
958         _PRC_SQL=25
959         _QCE_SQL=64M
960         _RND_SQL=2M
961         _JBF_SQL=2M
962         if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
963           _L_PHP_FPM_WORKERS=6
964         else
965           _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
966         fi
967         if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
968           _L_NGINX_WORKERS=$_CPU_VS
969         else
970           _L_NGINX_WORKERS=$_NGINX_WORKERS
971         fi
972       fi
973     else
974       _USE_PHP=256
975       _USE_OPC=256
976       _PRC_SQL=10
977       _QCE_SQL=32M
978       _RND_SQL=2M
979       _JBF_SQL=2M
980       if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
981         _L_PHP_FPM_WORKERS=12
982       else
983         _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
984       fi
985       if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
986         _L_NGINX_WORKERS=$_CPU_MX
987       else
988         _L_NGINX_WORKERS=$_NGINX_WORKERS
989       fi
990     fi
991   else
992     _USE_PHP="$_USE"
993     _USE_OPC="$_USE"
994     _PRC_SQL=10
995     _QCE_SQL=32M
996     _RND_SQL=1M
997     _JBF_SQL=1M
998     if [ "$_PHP_FPM_WORKERS" = "AUTO" ] ; then
999       _L_PHP_FPM_WORKERS=6
1000     else
1001       _L_PHP_FPM_WORKERS=$_PHP_FPM_WORKERS
1002     fi
1003     if [ "$_NGINX_WORKERS" = "AUTO" ] ; then
1004       _L_NGINX_WORKERS=$_CPU_MX
1005     else
1006       _L_NGINX_WORKERS=$_NGINX_WORKERS
1007     fi
1008   fi
1009   _USE_JETTY="-Xmx${_USE_OPC}m"
1010   if [ "$_VMFAMILY" = "VZ" ] ; then
1011     _USE_OPC=64
1012   fi
1013   let "_USE_FPM = (($_USE_PHP / 2))"
1014   if [ "$_USE_FPM" -lt "196" ] ; then
1015     _USE_FPM=196
1016   fi
1017   if [ "$_USE_PHP" -lt "196" ] ; then
1018     _USE_PHP=196
1019   fi
1020   if [ ! -e "/var/xdrago/conf/fpm-pool-foo.conf" ] ; then
1021     mkdir -p /var/xdrago/conf
1022   fi
1023   cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/fpm-pool-foo.conf /var/xdrago/conf/fpm-pool-foo.conf
1024   if [ -e "/opt/etc/fpm/fpm-pool-common.conf" ] ; then
1025     sed -i "s/256/$_USE_FPM/g" /opt/etc/fpm/fpm-pool-common.conf &> /dev/null
1026   fi
1027   if [ -e "/opt/php55/etc/php55.ini" ] ; then
1028     sed -i "s/256/$_USE_FPM/g" /opt/php55/etc/php55.ini &> /dev/null
1029     sed -i "s/181/$_USE_OPC/g" /opt/php55/etc/php55.ini &> /dev/null
1030     sed -i "s/256/$_USE_PHP/g" /opt/php55/lib/php.ini &> /dev/null
1031     sed -i "s/256/$_USE_PHP/g" /opt/php55/etc/pool.d/www55.conf &> /dev/null
1032   fi
1033   if [ -e "/opt/php54/etc/php54.ini" ] ; then
1034     sed -i "s/256/$_USE_FPM/g" /opt/php54/etc/php54.ini &> /dev/null
1035     sed -i "s/181/$_USE_OPC/g" /opt/php54/etc/php54.ini &> /dev/null
1036     sed -i "s/256/$_USE_PHP/g" /opt/php54/lib/php.ini &> /dev/null
1037     sed -i "s/256/$_USE_PHP/g" /opt/php54/etc/pool.d/www54.conf &> /dev/null
1038   fi
1039   if [ -e "/opt/php53/etc/php53.ini" ] ; then
1040     sed -i "s/256/$_USE_FPM/g" /opt/php53/etc/php53.ini &> /dev/null
1041     sed -i "s/181/$_USE_OPC/g" /opt/php53/etc/php53.ini &> /dev/null
1042     sed -i "s/256/$_USE_PHP/g" /opt/php53/lib/php.ini &> /dev/null
1043     sed -i "s/256/$_USE_PHP/g" /opt/php53/etc/pool.d/www53.conf &> /dev/null
1044   fi
1045   if [ -e "/opt/php52/etc/php52.ini" ] ; then
1046     sed -i "s/256/$_USE_FPM/g" /opt/php52/etc/php52.ini &> /dev/null
1047     sed -i "s/181/$_USE_OPC/g" /opt/php52/etc/php52.ini &> /dev/null
1048     sed -i "s/256/$_USE_PHP/g" /opt/php52/lib/php.ini &> /dev/null
1049   fi
1050   if [ "$_CUSTOM_CONFIG_SQL" = "NO" ] ; then
1051     tune_sql_memory_limits
1052     sed -i "s/= 191/= $_PRC_SQL/g"                                               /etc/mysql/my.cnf &> /dev/null
1053     sed -i "s/^tmp_table_size.*/tmp_table_size          = $_TMP_SQL/g"           /etc/mysql/my.cnf &> /dev/null
1054     sed -i "s/^max_heap_table_size.*/max_heap_table_size     = $_TMP_SQL/g"      /etc/mysql/my.cnf &> /dev/null
1055     sed -i "s/^myisam_sort_buffer_size.*/myisam_sort_buffer_size = $_SRT_SQL/g"  /etc/mysql/my.cnf &> /dev/null
1056     sed -i "s/^query_cache_size.*/query_cache_size        = $_QCE_SQL/g"         /etc/mysql/my.cnf &> /dev/null
1057     sed -i "s/^read_rnd_buffer_size.*/read_rnd_buffer_size    = $_RND_SQL/g"     /etc/mysql/my.cnf &> /dev/null
1058     sed -i "s/^join_buffer_size.*/join_buffer_size        = $_JBF_SQL/g"         /etc/mysql/my.cnf &> /dev/null
1059   fi
1060   _MAX_MEM_REDIS="${_USE_OPC}MB"
1061   sed -i "s/^maxmemory .*/maxmemory $_MAX_MEM_REDIS/g"  /etc/redis/redis.conf &> /dev/null
1062   if [ -e "/etc/default/jetty9" ] && [ -e "/opt/solr4" ] ; then
1063     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
1064   fi
1065   if [ -e "/etc/default/jetty8" ] && [ -e "/opt/solr3" ] ; then
1066     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
1067   fi
1068   if [ -e "/etc/default/jetty7" ] && [ -e "/opt/solr1" ] ; then
1069     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
1070   fi
1071   tune_web_server_config
1072 }
1073 #
1074 check_git_repos_status () {
1075   _GITHUB_WORKS=NO
1076   _DRUPAL_WORKS=NO
1077   _GITORIOUS_WORKS=NO
1078   if [ "$_FORCE_GIT_MIRROR" = "github" ] ; then
1079     msg "INFO: We will use forced GitHub repository without testing connection"
1080     _GITHUB_WORKS=YES
1081     _DRUPAL_WORKS=NO
1082     _GITORIOUS_WORKS=NO
1083     sleep 1
1084   elif [ "$_FORCE_GIT_MIRROR" = "drupal" ] ; then
1085     msg "INFO: We will use forced Drupal mirror without testing connection"
1086     _GITHUB_WORKS=NO
1087     _DRUPAL_WORKS=YES
1088     _GITORIOUS_WORKS=NO
1089     sleep 1
1090   elif [ "$_FORCE_GIT_MIRROR" = "gitorious" ] ; then
1091     msg "INFO: We will use forced Gitorious mirror without testing connection"
1092     _GITHUB_WORKS=NO
1093     _DRUPAL_WORKS=NO
1094     _GITORIOUS_WORKS=YES
1095     sleep 1
1096   else
1097     msg "INFO: Testing repository mirror servers availability..."
1098     sleep 1
1099     _GITHUB_WORKS=YES
1100     _DRUPAL_WORKS=YES
1101     _GITORIOUS_WORKS=YES
1102     if ! netcat -w 8 -z github.com 9418 ; then
1103       _GITHUB_WORKS=NO
1104       msg "WARN: The GitHub master repository server doesn't respond..."
1105     elif ! netcat -w 8 -z git.drupal.org 80 ; then
1106       _DRUPAL_WORKS=NO
1107       msg "WARN: The Drupal mirror repository server doesn't respond..."
1108     elif ! netcat -w 8 -z code.aegir.cc 9418 ; then
1109       _GITORIOUS_WORKS=NO
1110       msg "WARN: The Gitorious mirror repository server doesn't respond..."
1111     fi
1112   fi
1113   if [ "$_GITHUB_WORKS" = "YES" ] ; then
1114     _BOA_REPO_NAME="boa"
1115     _BOA_REPO_GIT_URL="git://github.com/omega8cc"
1116     msg "INFO: GitHub master repository will be used"
1117   elif [ "$_DRUPAL_WORKS" = "YES" ] ; then
1118     _BOA_REPO_NAME="barracuda"
1119     _BOA_REPO_GIT_URL="http://git.drupal.org/project"
1120     msg "INFO: Drupal mirror repository will be used"
1121   elif [ "$_GITORIOUS_WORKS" = "YES" ] ; then
1122     _BOA_REPO_NAME="barracuda-octopus"
1123     _BOA_REPO_GIT_URL="git://code.aegir.cc/aegir"
1124     msg "INFO: Gitorious mirror repository will be used"
1125   else
1126     cat <<EOF
1127
1128     None of repository servers responded in 8 seconds,
1129     so we can't continue this installation.
1130
1131     Please try again later or check if your firewall has port 9418 open.
1132
1133     Bye.
1134
1135 EOF
1136     exit 1
1137   fi
1138   _BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
1139 }
1140
1141
1142 ###---### init
1143 #
1144 touch /var/run/boa_run.pid
1145 #
1146 _BOA_REPO_NAME="boa"
1147 _BOA_REPO_GIT_URL="git://github.com/omega8cc"
1148 _BOA_REPO_GIT_URLX=${_BOA_REPO_GIT_URL//\//\\\/}
1149 if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
1150   check_git_repos_status
1151 fi
1152 #
1153 #
1154 if [ `whoami` = "root" ] ; then
1155   chmod a+w /dev/null
1156   if [ ! -e "/dev/fd" ] ; then
1157     if [ -e "/proc/self/fd" ] ; then
1158       rm -rf /dev/fd
1159       ln -s /proc/self/fd /dev/fd
1160     fi
1161   fi
1162   msg "INFO: This script is ran as a root user"
1163 else
1164   msg "ERROR: This script should be ran as a root user - please `sudo -i` first"
1165   msg "Bye"
1166   exit 1
1167 fi
1168 #
1169 #
1170 if [ ! -f "/var/log/barracuda_log.txt" ] ; then
1171   msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
1172   msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
1173   msg "Bye"
1174   exit 1
1175 else
1176   _VERSIONS_TEST=`cat /var/log/barracuda_log.txt`
1177   if [[ "$_VERSIONS_TEST" =~ "$_INSTALLER_VERSION" ]] ; then
1178     _VERSIONS_TEST_RESULT=OK
1179   else
1180     msg "ERROR: This script should be used only when the same version of BARRACUDA was used before"
1181     msg "Your system has to be configured/upgraded by BARRACUDA version $_INSTALLER_VERSION first"
1182     msg "Bye"
1183     exit 1
1184   fi
1185 fi
1186 #
1187 #
1188 rm -f /var/run/aegir_upgrade.pid
1189 rm -f /opt/tmp/testecho*
1190 _SRCDIR=/opt/tmp/files
1191 mkdir -p $_SRCDIR
1192 chmod -R 777 /opt/tmp &> /dev/null
1193 cd /opt/tmp
1194 rm -f -r /opt/tmp/$_BOA_REPO_NAME
1195 if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
1196   git clone $_BOA_REPO_GIT_URL/$_BOA_REPO_NAME.git &> /dev/null
1197   if [ "$_FORCE_GIT_MIRROR" = "test" ] ; then
1198     cd /opt/tmp/$_BOA_REPO_NAME
1199     git checkout 2.0.5-dev &> /dev/null
1200     cd /opt/tmp
1201   fi
1202 else
1203   curl -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/versions/stable/tar/boa-$_INSTALLER_VERSION.tar.gz" | tar -xzf -
1204   _BOA_REPO_NAME="boa"
1205 fi
1206 #
1207 # Get spinner
1208 cd $_SRCDIR
1209 rm -f spinner*
1210 cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/helpers/spinner ./
1211 chmod +x spinner &> /dev/null
1212 #
1213 # Create tmp stuff
1214 _LOG=/var/backups/bond-$_NOW.log
1215 _SILENT=/opt/tmp/silent.log
1216 #
1217 #
1218
1219
1220 ###---### Tune Your Aegir Hosting System
1221 #
1222 echo " "
1223 msg "TUNER START -> checkpoint: "
1224 cat <<EOF
1225
1226   * Aegir Satellite Instance to tune: $_TUNE_HOSTMASTER
1227   * Nginx server mod_evasive will be set to $_TUNE_NGINX_CONNECT
1228   * Nginx server fastcgi timeout will be set to $_TUNE_NGINX_TIMEOUT seconds
1229   * Nginx firewall limit of allowed requests will be set to $_TUNE_NGINX_FIREWALL/300
1230   * Database server timeout will be set to $_TUNE_SQL_TIMEOUT seconds
1231   * PHP-FPM server timeout will be set to $_TUNE_PHP_FPM_TIMEOUT seconds
1232   * PHP-CLI drush timeout will be set to $_TUNE_PHP_CLI_TIMEOUT seconds
1233
1234 EOF
1235 echo " "
1236 if prompt_yes_no "Are you ready to tune your Aegir Hosting System with above values?" ; then
1237   true
1238   if [ ! -e "/root/.upstart.cnf" ] ; then
1239     msg "INFO: We will stop cron and then wait 30 seconds..."
1240     service cron stop &> /dev/null
1241     mrun "sleep 30"
1242   fi
1243   msg "INFO: Tuning in progress, please wait..."
1244   if [ -e "/root/.barracuda.cnf" ] ; then
1245     source /root/.barracuda.cnf
1246   fi
1247   restore_default_php
1248   update_php_conf
1249   tune_php
1250   restore_default_sql
1251   tune_sql
1252   restore_default_nginx
1253   tune_nginx
1254   tune_memory_limits
1255   restart_services
1256   msg "INFO: Tuning completed"
1257 else
1258   if [ ! -e "/root/.upstart.cnf" ] ; then
1259     msg "INFO: We will stop cron and then wait 30 seconds..."
1260     service cron stop &> /dev/null
1261     mrun "sleep 30"
1262   fi
1263   if [ -e "/root/.barracuda.cnf" ] ; then
1264     source /root/.barracuda.cnf
1265   fi
1266   restore_default_php
1267   update_php_conf
1268   restore_default_sql
1269   restore_default_nginx
1270   tune_memory_limits
1271   restart_services
1272   msg "INFO: Tuning stopped and default settings restored"
1273 fi
1274 rm -f -r /var/opt/*
1275 rm -f -r /opt/tmp/*
1276 rm -f /var/run/boa_run.pid
1277 if [ ! -e "/root/.upstart.cnf" ] ; then
1278   service cron start &> /dev/null
1279 fi
1280 msg "INFO: Cron started again"
1281 msg "BYE!"
1282
1283 ###----------------------------------------###
1284 ###
1285 ###  Barracuda-Octopus-Nginx-Drupal Tuner
1286 ###  Copyright (C) 2010-2014 Omega8.cc
1287 ###  noc@omega8.cc www.omega8.cc
1288 ###
1289 ###----------------------------------------###
1290