From ee773fb05c8c2ba5aa64059bb455db36184bfb7e Mon Sep 17 00:00:00 2001 From: midoks Date: Sat, 5 Jan 2019 14:26:19 +0800 Subject: [PATCH] update --- plugins/mysql/conf/my.cnf | 2 +- plugins/mysql/index.html | 3 +- plugins/mysql/index.py | 233 +++++++++++++-------------------- plugins/mysql/init.d/mysql.tpl | 152 +++++++++++++++++++++ 4 files changed, 249 insertions(+), 141 deletions(-) create mode 100644 plugins/mysql/init.d/mysql.tpl diff --git a/plugins/mysql/conf/my.cnf b/plugins/mysql/conf/my.cnf index d04822eed..5f0d241b9 100644 --- a/plugins/mysql/conf/my.cnf +++ b/plugins/mysql/conf/my.cnf @@ -20,7 +20,7 @@ port = 3306 socket = /tmp/mysql.sock # Here follows entries for some specific programs - +# {$ROOT_PATH} # The MySQL server [mysqld] log-error = mysql.log diff --git a/plugins/mysql/index.html b/plugins/mysql/index.html index a1af452f9..7da5b38b2 100755 --- a/plugins/mysql/index.html +++ b/plugins/mysql/index.html @@ -2,7 +2,8 @@

服务

-

自启动

+

自启动

+

配置文件

diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index 69687c67b..76f639978 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -8,6 +8,10 @@ import time sys.path.append(os.getcwd() + "/class/core") import public +app_debug = False +if public.isAppleSystem(): + app_debug = True + def getPluginName(): return 'mysql' @@ -44,137 +48,49 @@ def getArgs(): return tmp -def checkMyCnf(): - # 处理MySQL配置文件 - import os - confFile = '/etc/my.cnf' - if os.path.exists(confFile): - conf = readFile(confFile) - if len(conf) > 100: - return True - versionFile = '/www/server/mysql/version.pl' - if not os.path.exists(versionFile): - return False - - versions = ['5.1', '5.5', '5.6', '5.7', 'AliSQL'] - version = readFile(versionFile) - for key in versions: - if key in version: - version = key - break - - shellStr = ''' -#!/bin/bash -PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin -export PATH - -CN='125.88.182.172' -HK='download.bt.cn' -HK2='103.224.251.67' -US='174.139.221.74' -sleep 0.5; -CN_PING=`ping -c 1 -w 1 $CN|grep time=|awk '{print $7}'|sed "s/time=//"` -HK_PING=`ping -c 1 -w 1 $HK|grep time=|awk '{print $7}'|sed "s/time=//"` -HK2_PING=`ping -c 1 -w 1 $HK2|grep time=|awk '{print $7}'|sed "s/time=//"` -US_PING=`ping -c 1 -w 1 $US|grep time=|awk '{print $7}'|sed "s/time=//"` - -echo "$HK_PING $HK" > ping.pl -echo "$HK2_PING $HK2" >> ping.pl -echo "$US_PING $US" >> ping.pl -echo "$CN_PING $CN" >> ping.pl -nodeAddr=`sort -V ping.pl|sed -n '1p'|awk '{print $2}'` -if [ "$nodeAddr" == "" ];then - nodeAddr=$HK -fi - -Download_Url=http://$nodeAddr:5880 - - -MySQL_Opt() -{ - MemTotal=`free -m | grep Mem | awk '{print $2}'` - if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf - elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf - elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf - elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf - elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf - elif [[ ${MemTotal} -ge 32768 ]]; then - sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf - sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf - sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf - sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf - sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf - sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf - sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf - sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf - sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf - sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf - fi -} - -wget -O /etc/my.cnf $Download_Url/install/conf/mysql-%s.conf -T 5 -MySQL_Opt -''' % (version,) - # 判断是否迁移目录 - if os.path.exists('data/datadir.pl'): - newPath = public.readFile('data/datadir.pl') - mycnf = public.readFile('/etc/my.cnf') - mycnf = mycnf.replace('/www/server/data', newPath) - public.writeFile('/etc/my.cnf', mycnf) - - os.system(shellStr) - public.writeLog('TYPE_SOFE', 'MYSQL_CHECK_ERR') - return True +def getConf(): + path = getServerDir() + '/conf/my.cnf' + return path + + +def getInitdTpl(): + path = getPluginDir() + '/init.d/mysql.tpl' + return path + + +def contentReplace(content): + service_path = public.getServerDir() + content = content.replace('{$ROOT_PATH}', public.getRootDir()) + content = content.replace('{$SERVER_PATH}', service_path) + return content + + +def initDreplace(): + initd_tpl = getInitdTpl() + + initD_path = getServerDir() + '/init.d' + if not os.path.exists(initD_path): + os.mkdir(initD_path) + + file_bin = initD_path + '/' + getPluginName() + if not os.path.exists(file_bin): + content = public.readFile(initd_tpl) + content = contentReplace(content) + public.writeFile(file_bin, content) + public.execShell('chmod +x ' + file_bin) + + mysql_conf_dir = getServerDir() + '/conf' + if not os.path.exists(mysql_conf_dir): + os.mkdir(mysql_conf_dir) + + mysql_conf = mysql_conf_dir + '/my.cnf' + if not os.path.exists(mysql_conf): + mysql_conf_tpl = getPluginDir() + '/conf/my.cnf' + content = public.readFile(mysql_conf_tpl) + content = contentReplace(content) + public.writeFile(mysql_conf, content) + + return file_bin def status(): @@ -186,6 +102,8 @@ def status(): def start(): + + init_file = initDreplace() initMysql = getServerDir() + '/scripts/mysql_install_db ' + '--basedir=' + \ getServerDir() + ' --datadir=' + getServerDir() + '/bin/mysql/data' return initMysql @@ -215,17 +133,42 @@ def reload(): return 'ok' -def getConf(): - path = os.path.dirname(os.getcwd()) + "/redis/redis.conf" - return path +def initdStatus(): + if not app_debug: + if public.isAppleSystem(): + return "Apple Computer does not support" + + initd_bin = getInitDFile() + if os.path.exists(initd_bin): + return 'ok' + return 'fail' + + +def initdInstall(): + import shutil + if not app_debug: + if public.isAppleSystem(): + return "Apple Computer does not support" + + mysql_bin = initDreplace() + initd_bin = getInitDFile() + shutil.copyfile(mysql_bin, initd_bin) + public.execShell('chmod +x ' + initd_bin) + return 'ok' + + +def initdUinstall(): + if not app_debug: + if public.isAppleSystem(): + return "Apple Computer does not support" + initd_bin = getInitDFile() + os.remove(initd_bin) + return 'ok' + if __name__ == "__main__": func = sys.argv[1] - if func == 'run_info': - print runInfo() - elif func == 'conf': - print getConf() - elif func == 'status': + if func == 'status': print status() elif func == 'start': print start() @@ -235,3 +178,15 @@ if __name__ == "__main__": print restart() elif func == 'reload': print reload() + elif func == 'initd_status': + print initdStatus() + elif func == 'initd_install': + print initdInstall() + elif func == 'initd_uninstall': + print initdUinstall() + elif func == 'run_info': + print runInfo() + elif func == 'conf': + print getConf() + else: + print 'error' diff --git a/plugins/mysql/init.d/mysql.tpl b/plugins/mysql/init.d/mysql.tpl new file mode 100644 index 000000000..90b82b840 --- /dev/null +++ b/plugins/mysql/init.d/mysql.tpl @@ -0,0 +1,152 @@ +#! /bin/sh + +### BEGIN INIT INFO +# Provides: php-fpm +# Required-Start: $remote_fs $network +# Required-Stop: $remote_fs $network +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts php-fpm +# Description: starts the PHP FastCGI Process Manager daemon +### END INIT INFO + +prefix={$SERVER_PATH}/php/{$PHP_VERSION} +exec_prefix=${prefix} + +php_fpm_BIN=${exec_prefix}/sbin/php-fpm +php_fpm_CONF=${prefix}/etc/php-fpm.conf +php_fpm_PID=${prefix}/var/run/php-fpm.pid + + +php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID" + + +wait_for_pid () { + try=0 + + while test $try -lt 35 ; do + + case "$1" in + 'created') + if [ -f "$2" ] ; then + try='' + break + fi + ;; + + 'removed') + if [ ! -f "$2" ] ; then + try='' + break + fi + ;; + esac + + echo -n . + try=`expr $try + 1` + sleep 1 + + done + +} + +case "$1" in + start) + echo -n "Starting php-fpm " + + $php_fpm_BIN --daemonize $php_opts + + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + fi + + wait_for_pid created $php_fpm_PID + + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + + stop) + echo -n "Gracefully shutting down php-fpm " + + if [ ! -r $php_fpm_PID ] ; then + echo "warning, no pid file found - php-fpm is not running ?" + exit 1 + fi + + kill -QUIT `cat $php_fpm_PID` + + wait_for_pid removed $php_fpm_PID + + if [ -n "$try" ] ; then + echo " failed. Use force-quit" + exit 1 + else + echo " done" + fi + ;; + + status) + if [ ! -r $php_fpm_PID ] ; then + echo "php-fpm is stopped" + exit 0 + fi + + PID=`cat $php_fpm_PID` + if ps -p $PID | grep -q $PID; then + echo "php-fpm (pid $PID) is running..." + else + echo "php-fpm dead but pid file exists" + fi + ;; + + force-quit) + echo -n "Terminating php-fpm " + + if [ ! -r $php_fpm_PID ] ; then + echo "warning, no pid file found - php-fpm is not running ?" + exit 1 + fi + + kill -TERM `cat $php_fpm_PID` + + wait_for_pid removed $php_fpm_PID + + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + + restart) + $0 stop + $0 start + ;; + + reload) + + echo -n "Reload service php-fpm " + + if [ ! -r $php_fpm_PID ] ; then + echo "warning, no pid file found - php-fpm is not running ?" + exit 1 + fi + + kill -USR2 `cat $php_fpm_PID` + + echo " done" + ;; + + *) + echo "Usage: $0 {start|stop|force-quit|restart|reload|status}" + exit 1 + ;; + +esac