|
|
|
# coding:utf-8
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
sys.path.append(os.getcwd() + "/class/core")
|
|
|
|
import public
|
|
|
|
|
|
|
|
app_debug = False
|
|
|
|
if public.isAppleSystem():
|
|
|
|
app_debug = True
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginName():
|
|
|
|
return 'mysql'
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginDir():
|
|
|
|
return public.getPluginDir() + '/' + getPluginName()
|
|
|
|
|
|
|
|
|
|
|
|
def getServerDir():
|
|
|
|
return public.getServerDir() + '/' + getPluginName()
|
|
|
|
|
|
|
|
|
|
|
|
def getInitDFile():
|
|
|
|
if app_debug:
|
|
|
|
return '/tmp/' + getPluginName()
|
|
|
|
return '/etc/init.d/' + getPluginName()
|
|
|
|
|
|
|
|
|
|
|
|
def getArgs():
|
|
|
|
args = sys.argv[2:]
|
|
|
|
tmp = {}
|
|
|
|
args_len = len(args)
|
|
|
|
|
|
|
|
if args_len == 1:
|
|
|
|
t = args[0].strip('{').strip('}')
|
|
|
|
t = t.split(':')
|
|
|
|
tmp[t[0]] = t[1]
|
|
|
|
elif args_len > 1:
|
|
|
|
for i in range(len(args)):
|
|
|
|
t = args[i].split(':')
|
|
|
|
tmp[t[0]] = t[1]
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
|
|
|
|
|
|
|
|
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():
|
|
|
|
data = public.execShell(
|
|
|
|
"ps -ef|grep mysqld |grep -v grep | grep -v python | awk '{print $2}'")
|
|
|
|
if data[0] == '':
|
|
|
|
return 'stop'
|
|
|
|
return 'start'
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
|
|
|
|
init_file = initDreplace()
|
|
|
|
initMysql = getServerDir() + '/scripts/mysql_install_db ' + '--basedir=' + \
|
|
|
|
getServerDir() + ' --datadir=' + getServerDir() + '/bin/mysql/data'
|
|
|
|
return initMysql
|
|
|
|
|
|
|
|
cmd = getServerDir() + '/bin/mysqld_safe ' + getServerDir() + \
|
|
|
|
'/conf/my.cnf ' + '--user=mysql&'
|
|
|
|
# return cmd
|
|
|
|
data = public.execShell(cmd)
|
|
|
|
if data[0] == '':
|
|
|
|
return 'stop'
|
|
|
|
return data[0]
|
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
data = public.execShell(
|
|
|
|
"ps -ef|grep mysql |grep -v grep |grep -v python |awk '{print $2}' | xargs kill -9")
|
|
|
|
if data[0] == '':
|
|
|
|
return 'ok'
|
|
|
|
return 'fail'
|
|
|
|
|
|
|
|
|
|
|
|
def restart():
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
def reload():
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
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 == 'status':
|
|
|
|
print status()
|
|
|
|
elif func == 'start':
|
|
|
|
print start()
|
|
|
|
elif func == 'stop':
|
|
|
|
print stop()
|
|
|
|
elif func == 'restart':
|
|
|
|
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'
|