# coding: utf-8 import time import os import sys sys.path.append("/usr/local/lib/python2.7/site-packages") import psutil 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 = {} 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 getInitdConf(): path = getPluginDir() + "/init.d/gogs.tpl" return path def getConf(): path = getServerDir() + "/custom/conf/app.ini" return path 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(): file_tpl = getConf() service_path = public.getServerDir() initD_path = getServerDir() + '/init.d' if not os.path.exists(initD_path): os.mkdir(initD_path) file_bin = initD_path + '/' + getPluginName() content = public.readFile(file_tpl) content = content.replace('{$SERVER_PATH}', service_path) public.writeFile(file_bin, content) public.execShell('chmod +x ' + file_bin) log_path = getServerDir() + '/log' if not os.path.exists(log_path): os.mkdir(log_path) return file_bin def start(): file = initDreplace() data = public.execShell(file + ' start') if data[1] == '': return 'ok' return 'fail' 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' def runLog(): log_path = getServerDir() + '/log/gogs.log' return log_path 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_log': print runLog() elif func == 'conf': print getConf() elif func == 'init_conf': print getInitdConf() else: print 'fail'