From ffff3b71b244f167e13c46c677ad29a2b54517bf Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Thu, 14 Feb 2019 17:50:32 +0800 Subject: [PATCH] update --- plugins/gogs/class/mysql.py | 91 +++++++++++++++++++++++++++++++++++++ plugins/gogs/index.py | 73 ++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 1 deletion(-) create mode 100755 plugins/gogs/class/mysql.py diff --git a/plugins/gogs/class/mysql.py b/plugins/gogs/class/mysql.py new file mode 100755 index 000000000..c3a68a4fb --- /dev/null +++ b/plugins/gogs/class/mysql.py @@ -0,0 +1,91 @@ +# coding: utf-8 + +import re +import os +import sys + +sys.path.append("/usr/local/lib/python2.7/site-packages") + + +class mysql: + __DB_PASS = None + __DB_USER = 'root' + __DB_PORT = 3306 + __DB_HOST = 'localhost' + __DB_NAME = 'test' + __DB_CONN = None + __DB_CUR = None + __DB_ERR = None + __DB_CNF = '/etc/my.cnf' + + def __Conn(self): + '''连接MYSQL数据库''' + try: + socket = '/tmp/mysql.sock' + try: + import MySQLdb + except Exception, ex: + self.__DB_ERR = ex + return False + try: + self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, db=self.__DB_NAME, charset="utf8", connect_timeout=10, unix_socket=socket) + except MySQLdb.Error, e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, db=self.__DB_NAME, charset="utf8", connect_timeout=10, unix_socket=socket) + self.__DB_CUR = self.__DB_CONN.cursor() + return True + + except MySQLdb.Error, e: + self.__DB_ERR = e + return False + + def setHost(self, host): + self.__DB_HOST = host + + def setPwd(self, pwd): + self.__DB_PASS = pwd + + def setUser(self, user): + self.__DB_USER = user + + def setPort(self, port): + self.__DB_PORT = port + + def setDb(self, name): + self.__DB_NAME = name + + def getPwd(self): + return self.__DB_PASS + + def execute(self, sql): + # 执行SQL语句返回受影响行 + if not self.__Conn(): + return self.__DB_ERR + try: + result = self.__DB_CUR.execute(sql) + self.__DB_CONN.commit() + self.__Close() + return result + except Exception, ex: + return ex + + def query(self, sql): + # 执行SQL语句返回数据集 + if not self.__Conn(): + return self.__DB_ERR + try: + self.__DB_CUR.execute(sql) + result = self.__DB_CUR.fetchall() + # 将元组转换成列表 + data = map(list, result) + self.__Close() + return data + except Exception, ex: + return ex + + # 关闭连接 + def __Close(self): + self.__DB_CUR.close() + self.__DB_CONN.close() diff --git a/plugins/gogs/index.py b/plugins/gogs/index.py index 66a575e61..15d387138 100755 --- a/plugins/gogs/index.py +++ b/plugins/gogs/index.py @@ -25,6 +25,9 @@ def getPluginName(): def getPluginDir(): return public.getPluginDir() + '/' + getPluginName() +sys.path.append(getPluginDir() + "/class") +import mysql + def getServerDir(): return public.getServerDir() + '/' + getPluginName() @@ -129,10 +132,78 @@ def initDreplace(): return file_bin +def getDbConfValue(): + content = public.readFile(getConf()) + + rep_scope = "\[database\](.*?)\[" + tmp = re.findall(rep_scope, content, re.S) + + rep = '(\w*)\s*=\s*(.*)' + tmp = re.findall(rep, tmp[0]) + r = {} + for x in range(len(tmp)): + k = tmp[x][0] + v = tmp[x][1] + r[k] = v + return r + + +def pMysqlDb(): + conf = getDbConfValue() + + host = conf['HOST'].split(':') + conn = mysql.mysql() + + conn.setHost(host[0]) + conn.setUser(conf['USER']) + conn.setPwd(conf['PASSWD']) + conn.setPort(int(host[1])) + conn.setDb(conf['NAME']) + return conn + + +def isSqlError(mysqlMsg): + # 检测数据库执行错误 + _mysqlMsg = str(mysqlMsg) + # print _mysqlMsg + if "MySQLdb" in _mysqlMsg: + return public.returnData(False, 'MySQLdb组件缺失!
进入SSH命令行输入: pip install mysql-python') + if "2002," in _mysqlMsg: + return public.returnData(False, '数据库连接失败,请检查数据库服务是否启动!') + if "using password:" in _mysqlMsg: + return public.returnData(False, '数据库管理密码错误!') + if "Connection refused" in _mysqlMsg: + return public.returnData(False, '数据库连接失败,请检查数据库服务是否启动!') + if "1133," in _mysqlMsg: + return public.returnData(False, '数据库用户不存在!') + if "1007," in _mysqlMsg: + return public.returnData(False, '数据库已经存在!') + if "1044," in _mysqlMsg: + return public.returnData(False, mysqlMsg[1]) + if "2003," in _mysqlMsg: + return public.returnData(False, mysqlMsg[1]) + return public.returnData(True, 'OK') + + def start(): + + is_frist = True + conf_bin = getConf() + if os.path.exists(conf_bin): + is_frist = False + file = initDreplace() + + if is_frist: + return "第一次启动Gogs,默认使用MySQL连接!
可以在配置文件中重新设置,再启动!" + + conn = pMysqlDb() + list_table = conn.query('show tables') + data = isSqlError(list_table) + if not data['status']: + return data['msg'] + data = public.execShell(file + ' start') - # print data if data[1] == '': return 'ok' return data[0]