Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/gogs/index.py

288 lines
7.3 KiB

6 years ago
# coding: utf-8
import time
import os
import sys
6 years ago
import re
6 years ago
6 years ago
sys.path.append("/usr/local/lib/python2.7/site-packages")
import psutil
6 years ago
sys.path.append(os.getcwd() + "/class/core")
import public
app_debug = False
if public.getOs() == 'darwin':
app_debug = True
def getPluginName():
return 'gogs'
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 = {}
6 years ago
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
6 years ago
tmp[t[0]] = t[1]
6 years ago
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
6 years ago
return tmp
6 years ago
def getInitdConfTpl():
6 years ago
path = getPluginDir() + "/init.d/gogs.tpl"
return path
6 years ago
def getInitdConf():
path = getServerDir() + "/init.d/gogs"
return path
6 years ago
def getConf():
path = getServerDir() + "/custom/conf/app.ini"
return path
6 years ago
def getConfTpl():
path = getPluginDir() + "/conf/app.ini"
return path
6 years ago
def status():
data = public.execShell(
"ps -ef|grep " + getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'")
if data[0] == '':
return 'stop'
return 'start'
6 years ago
def contentReplace(content):
6 years ago
user = 'root'
if public.isAppleSystem():
user = public.execShell(
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
content = content.replace('{$HOME_DIR}', '/Users/' + user)
else:
content = content.replace('{$HOME_DIR}', '/root')
6 years ago
service_path = public.getServerDir()
content = content.replace('{$ROOT_PATH}', public.getRootDir())
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$RUN_USER}', user)
return content
6 years ago
def initDreplace():
6 years ago
file_tpl = getInitdConfTpl()
6 years ago
service_path = public.getServerDir()
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
6 years ago
file_bin = initD_path + '/' + getPluginName()
6 years ago
6 years ago
if not os.path.exists(file_bin):
content = public.readFile(file_tpl)
content = contentReplace(content)
public.writeFile(file_bin, content)
public.execShell('chmod +x ' + file_bin)
conf_bin = getConf()
6 years ago
if not os.path.exists(conf_bin):
public.execShell('mkdir -p ' + getServerDir() + '/custom/conf')
6 years ago
conf_tpl = getConfTpl()
content = public.readFile(conf_tpl)
content = contentReplace(content)
public.writeFile(conf_bin, content)
6 years ago
log_path = getServerDir() + '/log'
if not os.path.exists(log_path):
os.mkdir(log_path)
6 years ago
return file_bin
def start():
file = initDreplace()
data = public.execShell(file + ' start')
6 years ago
# print data
6 years ago
if data[1] == '':
return 'ok'
6 years ago
return data[1]
6 years ago
def stop():
file = initDreplace()
data = public.execShell(file + ' stop')
if data[1] == '':
return 'ok'
return 'fail'
def restart():
file = initDreplace()
data = public.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
def reload():
file = initDreplace()
data = public.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
def initdStatus():
if not app_debug:
os_name = public.getOs()
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:
os_name = public.getOs()
if os_name == 'darwin':
return "Apple Computer does not support"
mem_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mem_bin, initd_bin)
public.execShell('chmod +x ' + initd_bin)
return 'ok'
def initdUinstall():
if not app_debug:
os_name = public.getOs()
if os_name == 'darwin':
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
6 years ago
def runLog():
log_path = getServerDir() + '/log/gogs.log'
return log_path
6 years ago
def getGogsConf():
gets = [
6 years ago
{'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 端口号'},
6 years ago
{'name': 'REQUIRE_SIGNIN_VIEW', 'type': 2, 'ps': '强制登录浏览'},
6 years ago
{'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模板加载时间'},
6 years ago
]
conf = public.readFile(getConf())
result = []
6 years ago
6 years ago
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)
return public.getJson(result)
def submitGogsConf():
6 years ago
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',
'SHOW_FOOTER_TEMPLATE_LOAD_TIME']
6 years ago
args = getArgs()
filename = getConf()
conf = public.readFile(filename)
for g in gets:
if g in args:
rep = g + '\s*=\s*(.*)'
6 years ago
val = g + ' = ' + args[g]
6 years ago
conf = re.sub(rep, val, conf)
public.writeFile(filename, conf)
6 years ago
reload()
6 years ago
return public.returnJson(True, '设置成功')
6 years ago
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()
6 years ago
elif func == 'run_log':
print runLog()
6 years ago
elif func == 'conf':
print getConf()
6 years ago
elif func == 'init_conf':
print getInitdConf()
6 years ago
elif func == 'get_gogs_conf':
print getGogsConf()
elif func == 'submit_gogs_conf':
print submitGogsConf()
6 years ago
else:
print 'fail'