1 Star 0 Fork 22

c2c_jialuo/DeepinOceanCms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sailfish.py 9.66 KB
一键复制 编辑 原始数据 按行查看 历史
一名无聊的程序员 提交于 2020-04-07 17:42 +08:00 . 更新0.20.04
# -*- coding: utf-8 -*-
import tornado.escape
import os
import time
import psutil
import sys
from prettytable import PrettyTable
# if os.path.exists('conf.json') == True:
# with open('conf.json', 'r') as f:
# c2c_backend_conf = tornado.escape.json_decode(f.read())
# else:
# c2c_backend_conf = {}
# print("未找到配置文件")
# exit()
logs = []
processItems = []
def getBackendConf( confName ):
confFileName = "%s.json" % confName
if os.path.exists( confFileName ) == True:
with open(confFileName, 'r') as f:
c2c_backend_conf = tornado.escape.json_decode(f.read())
return c2c_backend_conf
else:
c2c_backend_conf = {}
print("未找到配置文件%s" % confFileName)
exit()
def debugPrint(msg):
print( msg )
return True
def addProcessItems(processName , processPid):
processItems.append( {
'name' : processName,
'pid' : processPid
} )
return True
def addLog( log , isSuccess = True):
logs.append( {
'isSuccess' : isSuccess,
'log' : log
} )
return True
def startProcess( module ):
scriptFile = "%s/%s" % ( module['dir'] , module['script'] )
if os.path.exists( scriptFile ) == False:
log = "文件%s路径错误" % scriptFile
addLog( log , False )
addProcessItems( module['script'] , 0 )
return False
command = "cd %s && %s %s 1>/dev/null 2>/dev/null &" % ( module['dir'] , module['pyenv'] , module['script'] )
result = os.popen( command )
newPid = getProcessPid( module['script'] )
addProcessItems( module['script'] , newPid )
if newPid > 0 :
debugPrint( "进程%s启动成功" % module['script'] )
return True
else:
debugPrint( "进程%s启动失败" % module['script'] )
return False
def killProcessByPid( pid , processName ):
command = "kill -9 %d" % pid
result = os.popen( command )
debugPrint( command )
newPid = getProcessPid( processName )
if newPid > 0 :
debugPrint( "进程%s关闭失败" % processName )
return False
else:
return True
def getProcessPid(processName):
command = "ps aux|grep %s|grep -v grep|grep -v PPID|awk '{ print $2}'" % processName
result = os.popen( command )
res = result.read().splitlines()
if len( res ) > 0:
debugPrint( processName + '启动成功ID' + str( res[0] ) )
return int( res[0] )
else:
debugPrint( processName + '未启动' )
return 0
def printLogTable():
x = PrettyTable(["Log", "Result"])
x.align["Log"] = "l"
x.padding_width = 1
for log in logs:
x.add_row( [ log['log'] , log['isSuccess'] ] )
print ( x )
def printProcessTable():
x = PrettyTable(["ProcessName", "Result"])
x.align["ProcessName"] = "l"
x.padding_width = 1
for log in processItems:
x.add_row( [ log['name'] , log['pid'] ] )
print ( x )
def stopServer( confName ):
c2c_backend_conf = getBackendConf( confName )
for key in c2c_backend_conf['modules'].keys():
if c2c_backend_conf['modules'][ key ]['is_run'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['run_script']
pid = getProcessPid( module[ 'script' ] )
log = '进程%s的ID:%d' % ( module['script'] , pid )
addLog( log )
if pid > 0:
status = killProcessByPid( pid , module[ 'script' ] )
if status == True:
log = '进程%s关闭成功' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s关闭成功' % module[ 'script' ] )
else:
log = '进程%s关闭失败' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s关闭成功' % module[ 'script' ] )
pid = getProcessPid( module[ 'script' ] )
addProcessItems( module['script'] , pid )
if c2c_backend_conf['modules'][ key ]['is_run_mq'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['mq_script']
pid = getProcessPid( module[ 'script' ] )
if pid > 0:
status = killProcessByPid( pid , module[ 'script' ] )
if status == True:
log = '进程%s关闭成功' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s关闭成功' % module[ 'script' ] )
else:
log = '进程%s关闭成功' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s关闭成功' % module[ 'script' ] )
pid = getProcessPid( module[ 'script' ] )
addProcessItems( module['script'] , pid )
#
# 打印日志
printLogTable()
# 打印进程
printProcessTable()
# 检测
def checkServer( confName ):
c2c_backend_conf = getBackendConf( confName )
for key in c2c_backend_conf['modules'].keys():
if c2c_backend_conf['modules'][ key ]['is_run'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['run_script']
pid = getProcessPid( module[ 'script' ] )
addProcessItems( module['script'],pid )
if c2c_backend_conf['modules'][ key ]['is_run_mq'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['mq_script']
pid = getProcessPid( module[ 'script' ] )
addProcessItems( module['script'],pid )
debugPrint( key )
# 打印日志
printLogTable()
# 打印进程
printProcessTable()
# 开启和重启
def restartServer( confName ):
c2c_backend_conf = getBackendConf( confName )
for key in c2c_backend_conf['modules'].keys():
if c2c_backend_conf['modules'][ key ]['is_run'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['run_script']
pid = getProcessPid( module[ 'script' ] )
log = '进程%s的ID:%d' % ( module['script'] , pid )
addLog( log )
if pid > 0:
status = killProcessByPid( pid , module[ 'script' ] )
if status == True:
log = '进程%s关闭成功,正在等待重启' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s关闭成功,正在等待重启' % module[ 'script' ] )
else:
log = '进程%s关闭失败,正在等待重启' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s关闭成功,正在等待重启' % module[ 'script' ] )
debugPrint( "正在启动%s" % key )
status = startProcess( module )
if status == True:
log = '进程%s启动成功' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s启动成功' % module[ 'script' ] )
else:
log = '进程%s启动失败' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s启动失败' % module[ 'script' ] )
if c2c_backend_conf['modules'][ key ]['is_run_mq'] == 1:
module = c2c_backend_conf['modules'][ key ]
module[ 'pyenv' ] = c2c_backend_conf['common']['pyenv']
module[ 'script' ] = c2c_backend_conf['modules'][ key ]['mq_script']
pid = getProcessPid( module[ 'script' ] )
if pid > 0:
status = killProcessByPid( pid , module[ 'script' ] )
if status == True:
log = '进程%s关闭成功,正在等待重启' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s关闭成功,正在等待重启' % module[ 'script' ] )
else:
log = '进程%s关闭成功,正在等待重启' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s关闭成功,正在等待重启' % module[ 'script' ] )
debugPrint( "正在启动%s" % key )
status = startProcess( module )
if status == True:
log = '进程%s启动成功' % module[ 'script' ]
addLog( log )
debugPrint( '进程%s启动成功' % module[ 'script' ] )
else:
log = '进程%s启动失败' % module[ 'script' ]
addLog( log , False )
debugPrint( '进程%s启动失败' % module[ 'script' ] )
debugPrint( key )
# 打印日志
printLogTable()
# 打印进程
printProcessTable()
if __name__ == '__main__':
if len( sys.argv ) < 2:
print( '选择操作类型start|stop|restart|check' )
elif len( sys.argv ) < 3:
print( '选择要调用的配置文件名称' )
elif sys.argv[1] == 'start' or sys.argv[1] == 'restart':
restartServer( sys.argv[2] )
elif sys.argv[1] == 'stop':
stopServer( sys.argv[2] )
elif sys.argv[1] == 'check':
checkServer( sys.argv[2] )
print( sys.argv )
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/c2c_jialuo/DeepinOceanCms.git
git@gitee.com:c2c_jialuo/DeepinOceanCms.git
c2c_jialuo
DeepinOceanCms
DeepinOceanCms
master

搜索帮助