mdserver-web/plugins/gogs/index.py

705 lines
18 KiB
Python
Raw Normal View History

2018-12-13 05:07:44 -05:00
# coding: utf-8
import time
import os
import sys
2019-02-13 23:02:16 -05:00
import re
2018-12-14 03:06:59 -05:00
2022-04-10 10:55:52 -04:00
2022-04-10 10:57:23 -04:00
sys.path.append(os.getcwd() + "/class/core")
import mw
2022-04-10 10:55:52 -04:00
cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\''
info = mw.execShell(cmd)
p = "/usr/local/lib/" + info[0].strip() + "/site-packages"
sys.path.append(p)
2018-12-13 05:07:44 -05:00
import psutil
2018-12-14 03:06:59 -05:00
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-14 03:06:59 -05:00
app_debug = True
def getPluginName():
return 'gogs'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2018-12-14 03:06:59 -05:00
2019-02-14 04:50:32 -05:00
sys.path.append(getPluginDir() + "/class")
2021-02-03 13:49:43 -05:00
import mysqlDb
2019-02-14 04:50:32 -05:00
2018-12-14 03:06:59 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2018-12-14 03:06:59 -05:00
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
2018-12-14 04:39:47 -05:00
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
2019-03-11 07:22:18 -04:00
t = t.split(':', 1)
2018-12-14 03:06:59 -05:00
tmp[t[0]] = t[1]
2018-12-14 04:39:47 -05:00
elif args_len > 1:
for i in range(len(args)):
2019-03-11 07:22:18 -04:00
t = args[i].split(':', 1)
2018-12-14 04:39:47 -05:00
tmp[t[0]] = t[1]
2018-12-14 03:06:59 -05:00
return tmp
2019-01-07 00:48:54 -05:00
def getInitdConfTpl():
2018-12-14 04:39:47 -05:00
path = getPluginDir() + "/init.d/gogs.tpl"
return path
2019-01-07 00:48:54 -05:00
def getInitdConf():
path = getServerDir() + "/init.d/gogs"
return path
2018-12-14 05:29:53 -05:00
def getConf():
path = getServerDir() + "/custom/conf/app.ini"
return path
2019-02-13 06:49:02 -05:00
def getConfTpl():
path = getPluginDir() + "/conf/app.ini"
return path
2018-12-14 03:06:59 -05:00
def status():
2020-07-10 03:57:25 -04:00
data = mw.execShell(
2018-12-14 03:06:59 -05:00
"ps -ef|grep " + getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'")
if data[0] == '':
return 'stop'
return 'start'
2019-02-28 06:54:53 -05:00
def getHomeDir():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
user = mw.execShell(
2019-02-13 06:58:42 -05:00
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
2019-02-28 06:54:53 -05:00
return '/Users/' + user
2019-02-13 06:58:42 -05:00
else:
2019-03-12 01:11:00 -04:00
return '/root'
2019-02-28 06:54:53 -05:00
def getRunUser():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
user = mw.execShell(
2019-02-28 06:54:53 -05:00
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
return user
else:
2019-03-12 01:11:00 -04:00
return 'root'
2019-02-28 06:54:53 -05:00
__SR = '''#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export USER=%s
export HOME=%s && ''' % ( getRunUser(), getHomeDir())
def contentReplace(content):
2019-02-13 06:58:42 -05:00
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
2019-02-13 06:49:02 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2019-02-28 06:54:53 -05:00
content = content.replace('{$RUN_USER}', getRunUser())
content = content.replace('{$HOME_DIR}', getHomeDir())
2019-02-13 06:49:02 -05:00
return content
2018-12-14 03:06:59 -05:00
def initDreplace():
2019-01-07 00:48:54 -05:00
file_tpl = getInitdConfTpl()
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
2018-12-14 03:06:59 -05:00
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
2018-12-14 04:39:47 -05:00
file_bin = initD_path + '/' + getPluginName()
2018-12-14 03:06:59 -05:00
2019-02-13 06:49:02 -05:00
if not os.path.exists(file_bin):
2020-07-10 03:57:25 -04:00
content = mw.readFile(file_tpl)
2019-02-13 06:49:02 -05:00
content = contentReplace(content)
2020-07-10 03:57:25 -04:00
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
2019-02-13 06:49:02 -05:00
conf_bin = getConf()
2019-02-13 06:56:38 -05:00
if not os.path.exists(conf_bin):
2020-07-10 03:57:25 -04:00
mw.execShell('mkdir -p ' + getServerDir() + '/custom/conf')
2019-02-13 06:49:02 -05:00
conf_tpl = getConfTpl()
2020-07-10 03:57:25 -04:00
content = mw.readFile(conf_tpl)
2019-02-13 06:49:02 -05:00
content = contentReplace(content)
2020-07-10 03:57:25 -04:00
mw.writeFile(conf_bin, content)
2018-12-14 05:04:50 -05:00
log_path = getServerDir() + '/log'
if not os.path.exists(log_path):
os.mkdir(log_path)
2018-12-14 03:06:59 -05:00
return file_bin
2019-02-15 07:19:41 -05:00
def getRootUrl():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2019-02-15 07:19:41 -05:00
rep = 'ROOT_URL\s*=\s*(.*)'
tmp = re.search(rep, content)
if not tmp:
return ''
return tmp.groups()[0]
2019-03-05 03:00:31 -05:00
def getSshPort():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2019-03-05 03:00:31 -05:00
rep = 'SSH_PORT\s*=\s*(.*)'
tmp = re.search(rep, content)
if not tmp:
return ''
return tmp.groups()[0]
2021-11-11 05:38:47 -05:00
def getHttpPort():
content = mw.readFile(getConf())
rep = 'HTTP_PORT\s*=\s*(.*)'
tmp = re.search(rep, content)
if not tmp:
return ''
return tmp.groups()[0]
2019-02-15 07:19:41 -05:00
def getRootPath():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2019-02-15 07:19:41 -05:00
rep = 'ROOT\s*=\s*(.*)'
tmp = re.search(rep, content)
if not tmp:
return ''
return tmp.groups()[0]
2019-02-14 04:50:32 -05:00
def getDbConfValue():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2019-02-14 04:50:32 -05:00
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(':')
2021-02-03 13:49:43 -05:00
conn = mysqlDb.mysqlDb()
2019-02-14 04:50:32 -05:00
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:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, 'MySQLdb组件缺失! <br>进入SSH命令行输入 pip install mysql-python')
2019-02-14 04:50:32 -05:00
if "2002," in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, '数据库连接失败,请检查数据库服务是否启动!')
2019-02-14 04:50:32 -05:00
if "using password:" in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, '数据库管理密码错误!')
2019-02-14 04:50:32 -05:00
if "Connection refused" in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, '数据库连接失败,请检查数据库服务是否启动!')
2019-02-14 04:50:32 -05:00
if "1133," in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, '数据库用户不存在!')
2019-02-14 04:50:32 -05:00
if "1007," in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, '数据库已经存在!')
2019-02-14 04:50:32 -05:00
if "1044," in _mysqlMsg:
2020-07-10 03:57:25 -04:00
return mw.returnData(False, mysqlMsg[1])
2019-02-14 04:50:32 -05:00
if "2003," in _mysqlMsg:
2021-11-06 07:50:28 -04:00
return mw.returnData(False, "Can't connect to MySQL server on '127.0.0.1' (61)")
2020-07-10 03:57:25 -04:00
return mw.returnData(True, 'OK')
2019-02-14 04:50:32 -05:00
2018-12-14 03:06:59 -05:00
def start():
2019-02-14 04:50:32 -05:00
is_frist = True
conf_bin = getConf()
if os.path.exists(conf_bin):
is_frist = False
2018-12-14 03:06:59 -05:00
file = initDreplace()
2019-02-14 04:50:32 -05:00
if is_frist:
return "第一次启动Gogs,默认使用MySQL连接!<br>可以在配置文件中重新设置,再启动!"
conn = pMysqlDb()
list_table = conn.query('show tables')
data = isSqlError(list_table)
if not data['status']:
return data['msg']
2020-07-10 03:57:25 -04:00
data = mw.execShell(__SR + file + ' start')
2018-12-14 03:06:59 -05:00
if data[1] == '':
return 'ok'
2019-02-14 02:19:36 -05:00
return data[0]
2018-12-14 03:06:59 -05:00
def stop():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(__SR + file + ' stop')
2018-12-14 03:06:59 -05:00
if data[1] == '':
return 'ok'
2019-02-14 02:19:36 -05:00
return data[1]
2018-12-14 03:06:59 -05:00
def restart():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(__SR + file + ' restart')
2018-12-14 03:06:59 -05:00
if data[1] == '':
return 'ok'
2019-02-14 02:19:36 -05:00
return data[1]
2018-12-14 03:06:59 -05:00
def reload():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(__SR + file + ' reload')
2018-12-14 03:06:59 -05:00
if data[1] == '':
return 'ok'
2019-02-14 02:19:36 -05:00
return data[1]
2018-12-14 03:06:59 -05:00
def initdStatus():
if not app_debug:
2020-07-10 03:57:25 -04:00
os_name = mw.getOs()
2018-12-14 03:06:59 -05:00
if os_name == 'darwin':
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:
2020-07-10 03:57:25 -04:00
os_name = mw.getOs()
2018-12-14 03:06:59 -05:00
if os_name == 'darwin':
return "Apple Computer does not support"
mem_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mem_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
2018-12-14 03:06:59 -05:00
return 'ok'
def initdUinstall():
if not app_debug:
2020-07-10 03:57:25 -04:00
os_name = mw.getOs()
2018-12-14 03:06:59 -05:00
if os_name == 'darwin':
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chkconfig --del ' + getPluginName())
2018-12-14 03:06:59 -05:00
return 'ok'
2018-12-14 05:29:53 -05:00
def runLog():
log_path = getServerDir() + '/log/gogs.log'
return log_path
2019-02-13 23:02:16 -05:00
2019-02-15 07:19:41 -05:00
def postReceiveLog():
log_path = getServerDir() + '/log/hooks/post-receive.log'
return log_path
2019-02-13 23:02:16 -05:00
def getGogsConf():
gets = [
2019-02-14 01:28:11 -05:00
{'name': 'DOMAIN', 'type': -1, 'ps': '服务器域名'},
{'name': 'ROOT_URL', 'type': -1, 'ps': '公开的完整URL路径'},
{'name': 'HTTP_ADDR', 'type': -1, 'ps': '应用HTTP监听地址'},
{'name': 'HTTP_PORT', 'type': -1, 'ps': '应用 HTTP 监听端口号'},
{'name': 'START_SSH_SERVER', 'type': 2, 'ps': '启动内置SSH服务器'},
{'name': 'SSH_PORT', 'type': -1, 'ps': 'SSH 端口号'},
2019-02-13 23:02:16 -05:00
{'name': 'REQUIRE_SIGNIN_VIEW', 'type': 2, 'ps': '强制登录浏览'},
2019-02-14 01:28:11 -05:00
{'name': 'ENABLE_CAPTCHA', 'type': 2, 'ps': '启用验证码服务'},
{'name': 'DISABLE_REGISTRATION', 'type': 2, 'ps': '禁止注册,只能由管理员创建帐号'},
{'name': 'ENABLE_NOTIFY_MAIL', 'type': 2, 'ps': '是否开启邮件通知'},
{'name': 'FORCE_PRIVATE', 'type': 2, 'ps': '强制要求所有新建的仓库都是私有'},
{'name': 'SHOW_FOOTER_BRANDING', 'type': 2, 'ps': 'Gogs推广信息'},
{'name': 'SHOW_FOOTER_VERSION', 'type': 2, 'ps': 'Gogs版本信息'},
{'name': 'SHOW_FOOTER_TEMPLATE_LOAD_TIME', 'type': 2, 'ps': 'Gogs模板加载时间'},
2019-02-13 23:02:16 -05:00
]
2020-07-10 03:57:25 -04:00
conf = mw.readFile(getConf())
2019-02-13 23:02:16 -05:00
result = []
2019-02-14 01:28:11 -05:00
2019-02-13 23:02:16 -05:00
for g in gets:
rep = g['name'] + '\s*=\s*(.*)'
tmp = re.search(rep, conf)
if not tmp:
continue
g['value'] = tmp.groups()[0]
result.append(g)
2020-07-10 03:57:25 -04:00
return mw.getJson(result)
2019-02-13 23:02:16 -05:00
def submitGogsConf():
2019-02-15 07:19:41 -05:00
gets = ['DOMAIN',
'ROOT_URL',
'HTTP_ADDR',
'HTTP_PORT',
'START_SSH_SERVER',
'SSH_PORT',
'REQUIRE_SIGNIN_VIEW',
'FORCE_PRIVATE',
'ENABLE_CAPTCHA',
'DISABLE_REGISTRATION',
'ENABLE_NOTIFY_MAIL',
'SHOW_FOOTER_BRANDING',
'SHOW_FOOTER_VERSION',
2019-02-14 01:28:11 -05:00
'SHOW_FOOTER_TEMPLATE_LOAD_TIME']
2019-02-13 23:02:16 -05:00
args = getArgs()
filename = getConf()
2020-07-10 03:57:25 -04:00
conf = mw.readFile(filename)
2019-02-13 23:02:16 -05:00
for g in gets:
if g in args:
rep = g + '\s*=\s*(.*)'
2019-02-14 01:28:11 -05:00
val = g + ' = ' + args[g]
2019-02-13 23:02:16 -05:00
conf = re.sub(rep, val, conf)
2020-07-10 03:57:25 -04:00
mw.writeFile(filename, conf)
2019-02-14 01:28:11 -05:00
reload()
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '设置成功')
2019-02-13 23:02:16 -05:00
2019-02-14 11:21:56 -05:00
def userList():
2019-02-15 07:19:41 -05:00
import math
2019-02-14 11:21:56 -05:00
args = getArgs()
2019-02-15 07:19:41 -05:00
2019-02-14 11:21:56 -05:00
page = 1
page_size = 10
search = ''
if 'page' in args:
page = int(args['page'])
if 'page_size' in args:
page_size = int(args['page_size'])
if 'search' in args:
search = args['search']
2019-02-15 07:19:41 -05:00
data = {}
data['root_url'] = getRootUrl()
pm = pMysqlDb()
start = (page - 1) * page_size
list_count = pm.query('select count(id) as num from user')
count = list_count[0][0]
list_data = pm.query(
'select id,name,email from user order by id desc limit ' + str(start) + ',' + str(page_size))
page_info = {'count': count, 'p': page,
'row': page_size, 'tojs': 'gogsUserList'}
2020-07-10 03:57:25 -04:00
data['list'] = mw.getPage(page_info)
2019-02-15 07:19:41 -05:00
data['page'] = page
data['page_size'] = page_size
data['page_count'] = int(math.ceil(count / page_size))
data['data'] = list_data
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', data)
2019-02-15 07:19:41 -05:00
def getAllUserProject(user, search=''):
path = getRootPath() + '/' + user
dlist = []
if os.path.exists(path):
for filename in os.listdir(path):
tmp = {}
filePath = path + '/' + filename
if os.path.isdir(filePath):
if search == '':
tmp['name'] = filename.replace('.git', '')
dlist.append(tmp)
else:
if filename.find(search) != -1:
tmp['name'] = filename.replace('.git', '')
dlist.append(tmp)
return dlist
def checkProjectListIsHasScript(user, data):
path = getRootPath() + '/' + user
for x in range(len(data)):
name = data[x]['name'] + '.git'
path_tmp = path + '/' + name + '/custom_hooks/post-receive'
if os.path.exists(path_tmp):
data[x]['has_hook'] = True
else:
data[x]['has_hook'] = False
return data
def userProjectList():
import math
args = getArgs()
# print args
page = 1
page_size = 5
search = ''
if not 'name' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '缺少参数name')
2019-02-15 07:19:41 -05:00
if 'page' in args:
page = int(args['page'])
if 'page_size' in args:
page_size = int(args['page_size'])
if 'search' in args:
search = args['search']
data = {}
ulist = getAllUserProject(args['name'])
dlist_sum = len(ulist)
start = (page - 1) * page_size
ret_data = ulist[start:start + page_size]
ret_data = checkProjectListIsHasScript(args['name'], ret_data)
data['root_url'] = getRootUrl()
data['data'] = ret_data
data['args'] = args
2020-07-10 03:57:25 -04:00
data['list'] = mw.getPage(
2019-02-15 07:19:41 -05:00
{'count': dlist_sum, 'p': page, 'row': page_size, 'tojs': 'userProjectList'})
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', data)
2019-02-15 07:19:41 -05:00
def projectScriptEdit():
args = getArgs()
if not 'user' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'username missing')
2019-02-15 07:19:41 -05:00
if not 'name' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'project name missing')
2019-02-15 07:19:41 -05:00
user = args['user']
name = args['name'] + '.git'
post_receive = getRootPath() + '/' + user + '/' + name + \
'/custom_hooks/commit'
2019-02-15 07:19:41 -05:00
if os.path.exists(post_receive):
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', {'path': post_receive})
2019-02-15 07:19:41 -05:00
else:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, 'file does not exist')
2019-02-15 07:19:41 -05:00
def projectScriptLoad():
args = getArgs()
if not 'user' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'username missing')
2019-02-15 07:19:41 -05:00
if not 'name' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'project name missing')
2019-02-15 07:19:41 -05:00
user = args['user']
name = args['name'] + '.git'
path = getRootPath() + '/' + user + '/' + name
post_receive_tpl = getPluginDir() + '/hook/post-receive.tpl'
post_receive = path + '/custom_hooks/post-receive'
if not os.path.exists(path + '/custom_hooks'):
2020-07-10 03:57:25 -04:00
mw.execShell('mkdir -p ' + path + '/custom_hooks')
2019-02-15 07:19:41 -05:00
2020-07-10 03:57:25 -04:00
pct_content = mw.readFile(post_receive_tpl)
2019-02-15 07:19:41 -05:00
pct_content = pct_content.replace('{$PATH}', path + '/custom_hooks')
2020-07-10 03:57:25 -04:00
mw.writeFile(post_receive, pct_content)
mw.execShell('chmod 777 ' + post_receive)
2019-02-15 07:19:41 -05:00
commit_tpl = getPluginDir() + '/hook/commit.tpl'
commit = path + '/custom_hooks/commit'
2020-07-10 03:57:25 -04:00
codeDir = mw.getRootDir() + '/git'
2019-02-15 07:19:41 -05:00
2020-07-10 03:57:25 -04:00
cc_content = mw.readFile(commit_tpl)
2019-03-05 03:00:31 -05:00
2021-11-11 05:38:47 -05:00
sshUrl = 'http://127.0.0.1:' + getHttpPort()
2019-03-05 03:00:31 -05:00
cc_content = cc_content.replace('{$GITROOTURL}', sshUrl)
2019-02-15 07:19:41 -05:00
cc_content = cc_content.replace('{$CODE_DIR}', codeDir)
cc_content = cc_content.replace('{$USERNAME}', user)
cc_content = cc_content.replace('{$PROJECT}', args['name'])
2020-07-10 03:57:25 -04:00
cc_content = cc_content.replace('{$WEB_ROOT}', mw.getWwwDir())
mw.writeFile(commit, cc_content)
mw.execShell('chmod 777 ' + commit)
2019-02-15 07:19:41 -05:00
return 'ok'
def projectScriptUnload():
args = getArgs()
if not 'user' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'username missing')
2019-02-15 07:19:41 -05:00
if not 'name' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'project name missing')
2019-02-15 07:19:41 -05:00
user = args['user']
name = args['name'] + '.git'
post_receive = getRootPath() + '/' + user + '/' + name + \
'/custom_hooks/post-receive'
2020-07-10 03:57:25 -04:00
mw.execShell('rm -f ' + post_receive)
2019-02-15 07:19:41 -05:00
commit = getRootPath() + '/' + user + '/' + name + \
'/custom_hooks/commit'
2020-07-10 03:57:25 -04:00
mw.execShell('rm -f ' + commit)
2019-02-15 07:19:41 -05:00
return 'ok'
def projectScriptDebug():
args = getArgs()
if not 'user' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'username missing')
2019-02-15 07:19:41 -05:00
if not 'name' in args:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'project name missing')
2019-02-15 07:19:41 -05:00
user = args['user']
name = args['name'] + '.git'
commit_log = getRootPath() + '/' + user + '/' + name + \
'/custom_hooks/sh.log'
data = {}
if os.path.exists(commit_log):
data['status'] = True
data['path'] = commit_log
else:
data['status'] = False
data['msg'] = '没有日志文件'
2019-02-14 11:21:56 -05:00
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2019-02-14 11:21:56 -05:00
2019-02-15 12:20:56 -05:00
def gogsEdit():
data = {}
data['post_receive'] = getPluginDir() + '/hook/post-receive.tpl'
data['commit'] = getPluginDir() + '/hook/commit.tpl'
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2019-02-15 12:20:56 -05:00
2019-02-25 04:10:32 -05:00
2019-03-12 01:11:00 -04:00
def getRsaPublic():
path = getHomeDir()
path += '/.ssh/id_rsa.pub'
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2019-03-12 01:11:00 -04:00
data = {}
2020-07-10 03:57:25 -04:00
data['mw'] = content
return mw.getJson(data)
2019-03-12 01:11:00 -04:00
2019-02-25 04:10:32 -05:00
def getTotalStatistics():
st = status()
data = {}
2022-04-10 02:14:29 -04:00
if st.strip() == 'start':
2019-02-25 04:15:53 -05:00
pm = pMysqlDb()
list_count = pm.query('select count(id) as num from repository')
2022-04-10 02:14:29 -04:00
if list_count.find("error") > -1:
data['status'] = False
data['count'] = 0
return mw.returnJson(False, 'fail', data)
2019-02-25 04:15:53 -05:00
2019-02-25 04:10:32 -05:00
data['status'] = True
2019-02-25 04:15:53 -05:00
data['count'] = count
2020-07-10 03:57:25 -04:00
data['ver'] = mw.readFile(getServerDir() + '/version.pl').strip()
return mw.returnJson(True, 'ok', data)
2022-04-10 02:14:29 -04:00
data['status'] = False
data['count'] = 0
return mw.returnJson(False, 'fail', data)
2019-02-25 04:10:32 -05:00
2019-03-12 01:11:00 -04:00
2018-12-14 03:06:59 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-05-01 02:17:42 -04:00
print(status())
2018-12-14 03:06:59 -05:00
elif func == 'start':
2021-05-01 02:17:42 -04:00
print(start())
2018-12-14 03:06:59 -05:00
elif func == 'stop':
2021-05-01 02:17:42 -04:00
print(stop())
2018-12-14 03:06:59 -05:00
elif func == 'restart':
2021-05-01 02:17:42 -04:00
print(restart())
2018-12-14 03:06:59 -05:00
elif func == 'reload':
2021-05-01 02:17:42 -04:00
print(reload())
2018-12-14 03:06:59 -05:00
elif func == 'initd_status':
2021-05-01 02:17:42 -04:00
print(initdStatus())
2018-12-14 03:06:59 -05:00
elif func == 'initd_install':
2021-05-01 02:17:42 -04:00
print(initdInstall())
2018-12-14 03:06:59 -05:00
elif func == 'initd_uninstall':
2021-05-01 02:17:42 -04:00
print(initdUinstall())
2018-12-14 05:29:53 -05:00
elif func == 'run_log':
2021-05-01 02:17:42 -04:00
print(runLog())
2019-02-15 07:19:41 -05:00
elif func == 'post_receive_log':
2021-05-01 02:17:42 -04:00
print(postReceiveLog())
2018-12-14 03:06:59 -05:00
elif func == 'conf':
2021-05-01 02:17:42 -04:00
print(getConf())
2018-12-14 05:29:53 -05:00
elif func == 'init_conf':
2021-05-01 02:17:42 -04:00
print(getInitdConf())
2019-02-13 23:02:16 -05:00
elif func == 'get_gogs_conf':
2021-05-01 02:17:42 -04:00
print(getGogsConf())
2019-02-13 23:02:16 -05:00
elif func == 'submit_gogs_conf':
2021-05-01 02:17:42 -04:00
print(submitGogsConf())
2019-02-14 11:21:56 -05:00
elif func == 'user_list':
2021-05-01 02:17:42 -04:00
print(userList())
2019-02-15 07:19:41 -05:00
elif func == 'user_project_list':
2021-05-01 02:17:42 -04:00
print(userProjectList())
2019-02-15 07:19:41 -05:00
elif func == 'project_script_edit':
2021-05-01 02:17:42 -04:00
print(projectScriptEdit())
2019-02-15 07:19:41 -05:00
elif func == 'project_script_load':
2021-05-01 02:17:42 -04:00
print(projectScriptLoad())
2019-02-15 07:19:41 -05:00
elif func == 'project_script_unload':
2021-05-01 02:17:42 -04:00
print(projectScriptUnload())
2019-02-15 07:19:41 -05:00
elif func == 'project_script_debug':
2021-05-01 02:17:42 -04:00
print(projectScriptDebug())
2019-02-15 12:20:56 -05:00
elif func == 'gogs_edit':
2021-05-01 02:17:42 -04:00
print(gogsEdit())
2019-03-12 01:11:00 -04:00
elif func == 'get_rsa_public':
2021-05-01 02:17:42 -04:00
print(getRsaPublic())
2019-02-25 04:10:32 -05:00
elif func == 'get_total_statistics':
2021-05-01 02:17:42 -04:00
print(getTotalStatistics())
2018-12-14 03:06:59 -05:00
else:
2021-05-01 02:17:42 -04:00
print('fail')