mdserver-web/plugins/gogs/index.py

198 lines
4.1 KiB
Python
Raw Normal View History

2018-12-13 05:07:44 -05:00
# coding: utf-8
import time
import os
import sys
2018-12-14 03:06:59 -05:00
2018-12-13 05:07:44 -05:00
sys.path.append("/usr/local/lib/python2.7/site-packages")
import psutil
2018-12-14 03:06:59 -05:00
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 = {}
2018-12-14 04:39:47 -05:00
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
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)):
t = args[i].split(':')
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
2018-12-14 03:06:59 -05:00
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'
def initDreplace():
2019-01-07 00:48:54 -05:00
file_tpl = getInitdConfTpl()
2018-12-14 03:06:59 -05:00
service_path = public.getServerDir()
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
content = public.readFile(file_tpl)
content = content.replace('{$SERVER_PATH}', service_path)
public.writeFile(file_bin, content)
public.execShell('chmod +x ' + file_bin)
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
def start():
file = initDreplace()
data = public.execShell(file + ' start')
2019-01-07 01:05:24 -05:00
# print data
2018-12-14 03:06:59 -05:00
if data[1] == '':
return 'ok'
2018-12-14 05:38:27 -05:00
return data[1]
2018-12-14 03:06:59 -05:00
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'
2018-12-14 05:29:53 -05:00
def runLog():
log_path = getServerDir() + '/log/gogs.log'
return log_path
2018-12-14 03:06:59 -05:00
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()
2018-12-14 05:29:53 -05:00
elif func == 'run_log':
print runLog()
2018-12-14 03:06:59 -05:00
elif func == 'conf':
print getConf()
2018-12-14 05:29:53 -05:00
elif func == 'init_conf':
print getInitdConf()
2018-12-14 03:06:59 -05:00
else:
print 'fail'