From cff2f4f8afb1c86fe16ce1c5c0f977d3797ea7d8 Mon Sep 17 00:00:00 2001 From: midoks Date: Fri, 17 Jun 2022 14:03:05 +0800 Subject: [PATCH] #31 --- class/core/common.py | 1 + plugins/supervisor/conf/supervisor.conf | 2 +- plugins/supervisor/index.html | 1 + plugins/supervisor/index.py | 124 +++++++++++-------- plugins/supervisor/init.d/supervisor.service | 14 +++ plugins/supervisor/install.sh | 9 +- 6 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 plugins/supervisor/init.d/supervisor.service diff --git a/class/core/common.py b/class/core/common.py index 111e7e5f9..f98aacfd8 100755 --- a/class/core/common.py +++ b/class/core/common.py @@ -73,6 +73,7 @@ def initInitD(): # 加入自启动 mw.execShell('chkconfig --add mw') mw.execShell('systemctl enable mw') + mw.execShell('systemctl daemon-reload') def initUserInfo(): diff --git a/plugins/supervisor/conf/supervisor.conf b/plugins/supervisor/conf/supervisor.conf index 373c0be09..d8df8fdd2 100644 --- a/plugins/supervisor/conf/supervisor.conf +++ b/plugins/supervisor/conf/supervisor.conf @@ -52,7 +52,7 @@ silent=false ; no logs to stdout if true; default false minfds=1024 ; min. avail startup file descriptors; default 1024 minprocs=200 ; min. avail process descriptors;default 200 ;umask=022 ; process file creation umask; default 022 -;user=supervisord ; setuid to this UNIX account at startup; recommended if root +user={$OS_USER} ; setuid to this UNIX account at startup; recommended if root ;identifier=supervisor ; supervisord identifier, default is 'supervisor' ;directory=/tmp ; default is not to cd during start ;nocleanup=true ; don't clean up tempfiles at start; default false diff --git a/plugins/supervisor/index.html b/plugins/supervisor/index.html index bd7b2d98f..798116a54 100755 --- a/plugins/supervisor/index.html +++ b/plugins/supervisor/index.html @@ -3,6 +3,7 @@

服务

+

自启动

进程管理

主配置

主日志

diff --git a/plugins/supervisor/index.py b/plugins/supervisor/index.py index aa0858e20..baf92b737 100755 --- a/plugins/supervisor/index.py +++ b/plugins/supervisor/index.py @@ -26,12 +26,6 @@ def getServerDir(): return mw.getServerDir() + '/' + getPluginName() -def getInitDFile(): - if app_debug: - return '/tmp/' + getPluginName() - return '/etc/init.d/' + getPluginName() - - def getConf(): path = getServerDir() + "/supervisor.conf" return path @@ -76,7 +70,6 @@ def checkArgs(data, ck=[]): def status(): - data = mw.execShell( "ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'") if data[0] == '': @@ -86,33 +79,49 @@ def status(): def initDreplace(): - # initD_path = getServerDir() + '/init.d' - # if not os.path.exists(initD_path): - # os.mkdir(initD_path) - # file_bin = initD_path + '/' + getPluginName() + confD = etServerDir() + "/conf.d" + conf = etServerDir() + "//supervisor.conf" + systemDir = '/lib/systemd/system' + systemService = systemDir + '/supervisor.service' + systemServiceTpl = getPluginDir() + '/init.d/supervisor.service' - # file_tpl = getInitDTpl() - # initd replace - # content = mw.readFile(file_tpl) - # content = content.replace('{$SERVER_PATH}', service_path) - # mw.writeFile(file_bin, content) - # mw.execShell('chmod +x ' + file_bin) + service_path = os.path.dirname(os.getcwd()) - if not os.path.exists(getServerDir() + "/conf.d"): - os.mkdir(getServerDir() + "/conf.d") + if not os.path.exists(confD): + os.mkdir(confD) - if not os.path.exists(getServerDir() + '/supervisor.conf'): + if not os.path.exists(conf): # config replace - service_path = os.path.dirname(os.getcwd()) + user = 'root' + if mw.isAppleSystem(): + cmd = "who | sed -n '2, 1p' |awk '{print $1}'" + user = mw.execShell(cmd)[0].strip() + conf_content = mw.readFile(getConfTpl()) conf_content = conf_content.replace('{$SERVER_PATH}', service_path) - mw.writeFile(getServerDir() + '/supervisor.conf', conf_content) + conf_content = conf_content.replace('{$OS_USER}', user) + mw.writeFile(conf, conf_content) + + if os.path.exists(systemDir) and not os.path.exists(systemService): + se_content = mw.readFile(systemServiceTpl) + se_content = conf_content.replace('{$SERVER_PATH}', service_path) + mw.writeFile(systemService, se_content) + mw.execShell('systemctl daemon-reload') return True def start(): initDreplace() + + systemDir = '/lib/systemd/system' + systemService = systemDir + '/supervisor.service' + if os.path.exists(systemService): + data = mw.execShell('systemctl start supervisor') + if data[1] == '': + return 'ok' + return 'fail' + cmd = 'supervisord -c ' + getServerDir() + '/supervisor.conf' # print(cmd) data = mw.execShell(cmd) @@ -124,7 +133,15 @@ def start(): def stop(): initDreplace() - data = mw.execShell('supervisorctl shutdown') + + systemDir = '/lib/systemd/system' + systemService = systemDir + '/supervisor.service' + if os.path.exists(systemService): + data = mw.execShell('systemctl stop supervisor') + if data[1] == '': + return 'ok' + return 'fail' + mw.execShell( "ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill") if data[1] == '': @@ -133,47 +150,51 @@ def stop(): def restart(): + systemDir = '/lib/systemd/system' + systemService = systemDir + '/supervisor.service' + if os.path.exists(systemService): + data = mw.execShell('systemctl restart supervisor') + if data[1] == '': + return 'ok' + return 'fail' mw.execShell( "ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill") - return start() - initDreplace() - data = mw.execShell('supervisorctl reload') - if data[1] == '': - return 'ok' - return 'fail' def reload(): - initDreplace() - data = mw.execShell('supervisorctl reload') - if data[1] == '': - return 'ok' - return 'fail' + + systemDir = '/lib/systemd/system' + systemService = systemDir + '/supervisor.service' + if os.path.exists(systemService): + data = mw.execShell('systemctl reload supervisor') + if data[1] == '': + return 'ok' + return 'fail' + + mw.execShell( + "ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill") + return start() def initdStatus(): if not app_debug: if mw.isAppleSystem(): return "Apple Computer does not support" - initd_bin = getInitDFile() - if os.path.exists(initd_bin): - return 'ok' - return 'fail' + + shell_cmd = 'systemctl status supervisor | grep loaded | grep "enabled;"' + data = mw.execShell(shell_cmd) + if data[0] == '': + return 'fail' + return 'ok' def initdInstall(): - import shutil - if not app_debug: - if mw.isAppleSystem(): - return "Apple Computer does not support" + if mw.isAppleSystem(): + return "Apple Computer does not support" - source_bin = initDreplace() - initd_bin = getInitDFile() - shutil.copyfile(source_bin, initd_bin) - mw.execShell('chmod +x ' + initd_bin) - mw.execShell('chkconfig --add ' + getPluginName()) + mw.execShell('systemctl enable supervisor') return 'ok' @@ -182,9 +203,7 @@ def initdUinstall(): if mw.isAppleSystem(): return "Apple Computer does not support" - mw.execShell('chkconfig --del ' + getPluginName()) - initd_bin = getInitDFile() - os.remove(initd_bin) + mw.execShell('systemctl diable supervisor') return 'ok' @@ -259,6 +278,11 @@ def getUserListData(): if user in special: continue user_list.append(user) + + if mw.isAppleSystem(): + cmd = "who | sed -n '2, 1p' |awk '{print $1}'" + user = mw.execShell(cmd)[0].strip() + user_list.append(user) return user_list diff --git a/plugins/supervisor/init.d/supervisor.service b/plugins/supervisor/init.d/supervisor.service new file mode 100644 index 000000000..41f4fe569 --- /dev/null +++ b/plugins/supervisor/init.d/supervisor.service @@ -0,0 +1,14 @@ +[Unit] +Description=supervisor server daemon +After=network.target + +[Service] +Type=forking +ExecStart=supervisord -c {$SERVER_PATH}/supervisor/supervisor.conf +ExecStop=supervisorctl -c {$SERVER_PATH}/supervisor/supervisor.conf $OPTIONS shutdown +ExecReload=supervisorctl -c {$SERVER_PATH}/supervisor/supervisor.conf $OPTIONS reload +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/plugins/supervisor/install.sh b/plugins/supervisor/install.sh index 5d1cdd567..056e19445 100755 --- a/plugins/supervisor/install.sh +++ b/plugins/supervisor/install.sh @@ -51,13 +51,18 @@ Install_app() fi echo "${VERSION}" > $serverPath/supervisor/version.pl - echo '安装完成' > $install_tmp + echo '安装完成[supervisor]' > $install_tmp } Uninstall_app() { rm -rf $serverPath/supervisor - echo "Uninstall_redis" > $install_tmp + + if [ -f /lib/systemd/system/supervisor.service ];then + rm -rf /lib/systemd/system/supervisor.service + fi + + echo "卸载完成[supervisor]" > $install_tmp } action=$1