├── .gitignore ├── LICENSE ├── README.md ├── demo1.png ├── demo2.png ├── osutil.py ├── server.py └── settings.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonWk 2 | NutzWk 微服务开发框架应用守护进程 https://wizzer.cn 3 | 4 | * 基于 Python 3.7.3 开发 5 | * 支持 NutzWk 5.1.6 以上版本 6 | * 支持分布式部署 7 | * 可用性:试用,欢迎提交issue 8 | 9 | ## QQ交流群 10 | * 1群: 68428921 (已满) 11 | * 2群: 24457628 12 | 13 | 14 | ## 使用说明 15 | 16 | * 根据配置要求将jar包及配置文件置于指定文件夹 17 | * 服务器的主机名不可重复 18 | * `pip3 install psutil` 安装psutil模块,获取CPU、内存占用情况 19 | * `nohup python3 server.py >/dev/null 2>&1 &` 启动守护进程 20 | * wk-nb-web-vue/wk-nb-web-api 等web项目是需要占用端口的,在同台服务器上启动多个实例的话,需使用不同配置文件配置不同端口,可以用版本号区分,如 1.2-9001、1.2-9002 21 | * wk-nb-web-api 默认端口 9001 是为守护进程通信用的,需要启动才能获取最新任务 22 | 23 | ## 配置说明 24 | 25 | * settings.py 配置文件内容 26 | ```python 27 | #!/usr/bin/python 28 | # -*- coding: utf-8 -*- 29 | # 初始jar包及配置文件文件夹,jar文件及配置文件以实例名为文件名,文件名中不包含版本号 30 | APP_JARS = 'C://data/jars/' 31 | # 运行目录,不要手动创建,启动时判断目录是否存在来初始化项目 32 | APP_ROOT = 'C://data/nutzwk/' 33 | # jar文件名,名称要保持与配置文件里 nutz.application.name 值一致 34 | # 如果是单机 wk-nb-service-sys 一定为第一个启动项目 35 | APP_LIST = [ 36 | 'wk-nb-service-sys', 37 | 'wk-nb-service-cms' 38 | ] 39 | # jar包启动的jvm配置参数 40 | APP_OPTS = { 41 | 'wk-nb-service-sys': '-Xmx100m -Xms60m', 42 | 'wk-nb-service-cms': '-Xmx80m -Xms40m', 43 | 'wk-nb-service-wx': '-Xmx80m -Xms40m', 44 | 'wk-nb-task': '-Xmx80m -Xms40m', 45 | 'wk-nb-web-api': '-Xmx80m -Xms40m', 46 | 'wk-nb-web-vue': '-Xmx150m -Xms80m' 47 | } 48 | # 通信密钥,保持与 wk-nb-web-api 模块里配置内容一致,用于心跳通信 49 | HTTP_SECRET_ID = 'wizzer' 50 | HTTP_SECRET_KEY = 'nutzwk' 51 | # API路径 52 | HTTP_URL = 'http://127.0.0.1:9001/open/api/deploy' 53 | # 心跳周期(单位:秒) 54 | HTTP_HEARTBEAT = 10 55 | HTTP_TIMEOUT = 5 56 | 57 | CACHE_TASK_IDS = [] 58 | CACHE_HOST_NAME = '' 59 | ``` 60 | * 初始化时 APP_JARS 文件列表示例 61 | ```text 62 | wk-nb-service-cms.jar 63 | wk-nb-service-cms.properties 64 | ``` 65 | 66 | ## NutzWk 配置文件 67 | 68 | ```text 69 | #实例名称与打包成功后的jar文件名保持一致 70 | nutz.application.name=wk-nb-service-cms 71 | 72 | #动态修改日志等级,启用 73 | logback.exts.loglevel.enabled=true 74 | #心跳间隔(单位:秒) 75 | logback.exts.loglevel.heartbeat=10 76 | 77 | #为应用管理提供支持,启用获取部署版本号,结合守护进程使用 78 | logback.exts.deploy.enabled=true 79 | #为应用管理提供支持,部署根路径 80 | logback.exts.deploy.root=C://data/nutzwk 81 | ``` 82 | 83 | ## 管理后台界面 84 | 85 | ![demo1](demo1.png) 86 | 87 | 88 | ![demo2](demo2.png) 89 | -------------------------------------------------------------------------------- /demo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wizzercn/PythonWk/e18fae3288edee09e62e562945776c81082be4af/demo1.png -------------------------------------------------------------------------------- /demo2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wizzercn/PythonWk/e18fae3288edee09e62e562945776c81082be4af/demo2.png -------------------------------------------------------------------------------- /osutil.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import hashlib 4 | import os 5 | import psutil 6 | 7 | import settings 8 | 9 | 10 | def netstat(): 11 | status_temp = [] 12 | net_connections = psutil.net_connections() 13 | for key in net_connections: 14 | status_temp.append(key.status) 15 | return status_temp.count("ESTABLISHED") 16 | 17 | 18 | # 签名算法 19 | 20 | def createSign(appkey, params): 21 | sign = '' 22 | list = sorted(params.items(), key=lambda params: params[0], reverse=False) 23 | for k, v in list: 24 | sign = sign + k + '=' + v + '&' 25 | sign = sign + 'appkey=' + appkey 26 | m2 = hashlib.md5() 27 | m2.update(sign.encode('utf-8')) 28 | return m2.hexdigest() 29 | 30 | 31 | # 获取当前程序版本 32 | def getAppVersion(appname): 33 | path = settings.APP_ROOT + appname + '/app/' 34 | if os.path.exists(path): 35 | dirs = os.listdir(path) 36 | for dir in dirs: 37 | if os.path.exists(path + dir + '/version'): 38 | return str(dir) 39 | return '' 40 | 41 | 42 | # 获取当前配置文件版本 43 | def getConfVersion(appname): 44 | path = settings.APP_ROOT + appname + '/conf/' 45 | if os.path.exists(path): 46 | dirs = os.listdir(path) 47 | for dir in dirs: 48 | if os.path.exists(path + dir + '/version'): 49 | return str(dir) 50 | return '' 51 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import json 4 | import logging.handlers 5 | import math 6 | import os 7 | import platform 8 | import random 9 | import sched 10 | import shutil 11 | import signal 12 | import socket 13 | import subprocess 14 | import threading 15 | import time 16 | from urllib import request, parse 17 | 18 | import psutil 19 | 20 | import osutil 21 | import settings 22 | 23 | if not os.path.exists('/data/python'): 24 | os.makedirs('/data/python') 25 | log = logging.getLogger() 26 | log.setLevel(logging.DEBUG) 27 | fh = logging.handlers.RotatingFileHandler("/data/python/watchdog.log", maxBytes=16 * 1024 * 1024, 28 | backupCount=1, encoding="UTF-8") 29 | fh.setLevel(logging.DEBUG) 30 | formatter = logging.Formatter('$: %(asctime)s > %(levelname)-5s > %(filename)s:%(lineno)s > %(message)s') 31 | fh.setFormatter(formatter) 32 | log.addHandler(fh) 33 | 34 | hostname = socket.gethostname() 35 | settings.CACHE_HOST_NAME = hostname 36 | 37 | # 初始化sched模块的scheduler类 38 | # 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。 39 | schedule = sched.scheduler(time.time, time.sleep) 40 | 41 | 42 | # 被周期性调度触发的函数 43 | def heart(): 44 | h = startHeartThread() 45 | h.start() 46 | schedule.enter(settings.HTTP_HEARTBEAT, 0, heart) 47 | 48 | 49 | def start(): 50 | # 延迟几秒,让项目启动完全 51 | time.sleep(30) 52 | # enter四个参数分别为: 间隔时间、优先级、被调用触发的函数、传递参数,如果只有一个参数需加,号 (xxx,) 53 | schedule.enter(settings.HTTP_HEARTBEAT, 0, heart) 54 | schedule.run() 55 | 56 | 57 | # 心跳请求,定时获取最新任务 58 | class startHeartThread(threading.Thread): 59 | def __init__(self): 60 | threading.Thread.__init__(self) 61 | self.daemon = False 62 | 63 | def run(self): 64 | url = settings.HTTP_URL + "/task" 65 | now = int(time.time()) 66 | nonce = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) 67 | headers = { 68 | r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 69 | 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', 70 | r'Accept': 'application/json, text/javascript, */*; q=0.01', 71 | r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 72 | } 73 | mem = psutil.virtual_memory() 74 | cpu_percent = psutil.cpu_percent(0.5) 75 | net = psutil.net_io_counters(pernic=False, nowrap=True) 76 | hdd = psutil.disk_usage('/') 77 | data = { 78 | 'appid': settings.HTTP_SECRET_ID, 79 | 'nonce': nonce, 80 | 'timestamp': str(now), 81 | 'apps': ','.join(settings.APP_LIST), 82 | 'hostname': settings.CACHE_HOST_NAME, 83 | 'mem_total': str(mem.total), 84 | 'mem_used': str(mem.used), 85 | 'mem_free': str(mem.free), 86 | 'mem_percent': str(mem.percent), 87 | 'cpu_percent': str(cpu_percent), 88 | 'net_sent': str(net.bytes_sent), 89 | 'net_recv': str(net.bytes_recv), 90 | 'net_tcp': str(osutil.netstat()), 91 | 'hdd_total': str(hdd.total), 92 | 'hdd_used': str(hdd.used), 93 | 'hdd_free': str(hdd.free), 94 | 'hdd_percent': str(hdd.percent) 95 | } 96 | sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) 97 | data['sign'] = sign 98 | data = parse.urlencode(data).encode('utf-8') 99 | try: 100 | req = request.Request(url, data, headers, None, None, 'POST') 101 | page = request.urlopen(req, None, settings.HTTP_TIMEOUT).read() 102 | result = page.decode('utf-8') 103 | log.debug(result) 104 | res_data = json.loads(result) 105 | if res_data['code'] == 0: 106 | task_list = res_data['data'] 107 | for task in task_list: 108 | # 将任务ID放到缓存里,防止重复执行同一个任务 109 | if task['id'] in settings.CACHE_TASK_IDS: 110 | log.debug('任务已存在,任务ID:::' + task['id']) 111 | else: 112 | settings.CACHE_TASK_IDS.append(task['id']) 113 | t = execTaskThread(task['id'], task) 114 | t.start() 115 | log.debug('任务已添加,任务ID:::' + task['id']) 116 | except Exception as e: 117 | log.error(str(e) + ':::' + url) 118 | 119 | 120 | # 杀死进程 121 | def killProcess(pid): 122 | sysname = platform.system() 123 | try: 124 | if sysname == 'Linux': 125 | os.kill(int(pid), signal.SIGKILL) 126 | result = {'code': 0, 'msg': '已杀死PID为 %s 的进程' % (pid)} 127 | elif sysname == 'Windows': 128 | os.kill(int(pid), -1) 129 | result = {'code': 0, 'msg': '已杀死PID为 %s 的进程' % (pid)} 130 | else: 131 | result = {'code': -1, 'msg': '不支持的操作系统::' + platform.system()} 132 | except OSError as e: 133 | log.error('杀死进程出错::' + str(e)) 134 | result = {'code': -2, 'msg': '杀死进程出错::' + str(e)} 135 | return result 136 | 137 | 138 | # 启动jar程序 139 | class startJavaThread(threading.Thread): 140 | 141 | def __init__(self, appname): 142 | threading.Thread.__init__(self) 143 | self.appname = appname 144 | self.daemon = False 145 | 146 | def run(self): 147 | _env = os.environ.copy() 148 | _env["APPNAME"] = self.appname 149 | appversion = osutil.getAppVersion(self.appname) 150 | confversion = osutil.getConfVersion(self.appname) 151 | apppath = settings.APP_ROOT + self.appname + '/app/' + appversion + '/' + self.appname + '.jar' 152 | confpath = settings.APP_ROOT + self.appname + '/conf/' + confversion + '/' 153 | try: 154 | jvm = settings.APP_OPTS[self.appname] 155 | except KeyError as e: 156 | jvm = '' 157 | cmd = 'java ' + ' -Dnutz.boot.configure.properties.dir=' + confpath + ' ' + jvm + ' -jar ' + apppath 158 | log.info(cmd) 159 | subprocess.call(cmd,close_fds=True,shell=True,env=_env) 160 | #with open("/data/python/" + self.appname + ".log", "w") as f: 161 | # subprocess.call(cmd, close_fds=True, shell=True, env=_env, stdout=f, stderr=f) 162 | 163 | 164 | # 执行任务命令 165 | class execTaskThread(threading.Thread): 166 | 167 | def __init__(self, taskid, task): 168 | threading.Thread.__init__(self) 169 | self.taskid = taskid 170 | self.task = task 171 | self.daemon = False 172 | 173 | def run(self): 174 | action = self.task['action'] 175 | if 'stop' == action: 176 | result = killProcess(self.task['processId']) 177 | if result['code'] == 0: 178 | settings.CACHE_TASK_IDS.remove(self.taskid) 179 | r = execReportThread(self.taskid, 2, '执行成功') 180 | r.start() 181 | else: 182 | r = execReportThread(self.taskid, 3, result['msg']) 183 | r.start() 184 | if 'start' == action: 185 | appname = self.task['name'] 186 | appversion = self.task['appVersion'] 187 | confversion = self.task['confVersion'] 188 | ok = True 189 | # 判断程序版本是否存在,不存在则下载,并把version文件移到新文件夹 190 | if not os.path.exists(settings.APP_ROOT + appname + '/app/' + appversion + '/' + appname + '.jar'): 191 | try: 192 | os.makedirs(settings.APP_ROOT + appname + '/app/' + appversion) 193 | except Exception as e: 194 | log.error(str(e)) 195 | if not dowload('jar', appname, appversion): 196 | ok = False 197 | r = execReportThread(self.taskid, 3, 'Jar包下载失败') 198 | r.start() 199 | # 判断配置文件是否存在,存在则下载并覆盖,不存在则下载 200 | if not os.path.exists(settings.APP_ROOT + appname + '/conf/' + confversion + '/' + appname + '.properties'): 201 | try: 202 | os.makedirs(settings.APP_ROOT + appname + '/conf/' + confversion) 203 | except Exception as e: 204 | log.error(str(e)) 205 | if not dowload('conf', appname, confversion): 206 | ok = False 207 | r = execReportThread(self.taskid, 3, '配置文件下载失败') 208 | r.start() 209 | if ok: 210 | try: 211 | oldappversion = osutil.getAppVersion(appname) 212 | if oldappversion != appversion: 213 | shutil.move(settings.APP_ROOT + appname + '/app/' + oldappversion + '/version', 214 | settings.APP_ROOT + appname + '/app/' + appversion + '/version') 215 | oldconfversion = osutil.getConfVersion(appname) 216 | if oldconfversion != confversion: 217 | shutil.move(settings.APP_ROOT + appname + '/conf/' + oldconfversion + '/version', 218 | settings.APP_ROOT + appname + '/conf/' + confversion + '/version') 219 | except Exception as e: 220 | ok = False 221 | log.error(str(e)) 222 | r = execReportThread(self.taskid, 3, '切换版本出错') 223 | r.start() 224 | if ok: 225 | t = startJavaThread(appname) 226 | t.start() 227 | settings.CACHE_TASK_IDS.remove(self.taskid) 228 | r = execReportThread(self.taskid, 2, '执行成功') 229 | r.start() 230 | 231 | 232 | def dowload(type, name, version): 233 | if type == 'conf': 234 | url = settings.HTTP_URL + "/conf/download" 235 | else: 236 | url = settings.HTTP_URL + "/jar/download" 237 | now = int(time.time()) 238 | nonce = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) 239 | headers = { 240 | r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 241 | 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', 242 | r'Accept': 'application/json, text/javascript, */*; q=0.01', 243 | r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 244 | } 245 | data = { 246 | 'appid': settings.HTTP_SECRET_ID, 247 | 'nonce': nonce, 248 | 'timestamp': str(now), 249 | 'hosts': ','.join(settings.APP_LIST), 250 | 'hostname': settings.CACHE_HOST_NAME, 251 | 'name': name, 252 | 'version': version 253 | } 254 | sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) 255 | data['sign'] = sign 256 | data = parse.urlencode(data).encode('utf-8') 257 | try: 258 | req = request.Request(url, data, headers, None, None, 'POST') 259 | data = request.urlopen(req, None, settings.HTTP_TIMEOUT).read() 260 | if type == 'conf': 261 | filepath = settings.APP_ROOT + name + '/conf/' + version + "/" + name + '.properties' 262 | else: 263 | filepath = settings.APP_ROOT + name + '/app/' + version + "/" + name + '.jar' 264 | with open(filepath, "wb+") as file: 265 | file.write(data) 266 | return True 267 | except Exception as e: 268 | log.error(str(e) + ':::' + url) 269 | return False 270 | 271 | 272 | # 上报执行结果 status 0-待执行,1-执行中,2-执行成功,3-执行失败,4-撤销任务 273 | class execReportThread(threading.Thread): 274 | 275 | def __init__(self, taskid, status, msg): 276 | threading.Thread.__init__(self) 277 | self.taskid = taskid 278 | self.status = status 279 | self.msg = msg 280 | self.daemon = False 281 | 282 | def run(self): 283 | url = settings.HTTP_URL + "/report" 284 | now = int(time.time()) 285 | nonce = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) 286 | headers = { 287 | r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 288 | 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', 289 | r'Accept': 'application/json, text/javascript, */*; q=0.01', 290 | r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 291 | } 292 | data = { 293 | 'appid': settings.HTTP_SECRET_ID, 294 | 'nonce': nonce, 295 | 'timestamp': str(now), 296 | 'hostname': settings.CACHE_HOST_NAME, 297 | 'taskid': self.taskid, 298 | 'status': str(self.status), 299 | 'msg': self.msg 300 | } 301 | sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) 302 | data['sign'] = sign 303 | data = parse.urlencode(data).encode('utf-8') 304 | try: 305 | req = request.Request(url, data, headers, None, None, 'POST') 306 | request.urlopen(req, None, settings.HTTP_TIMEOUT).read() 307 | except Exception as e: 308 | log.error(str(e) + ':::' + url) 309 | 310 | 311 | def init(): 312 | # 初始化运行目录 313 | if not os.path.exists(settings.APP_ROOT): 314 | now = time.time() 315 | log.info('初始化运行目录: ' + settings.APP_ROOT) 316 | os.makedirs(settings.APP_ROOT) 317 | for appName in settings.APP_LIST: 318 | if not os.path.exists(settings.APP_ROOT + appName): 319 | try: 320 | os.makedirs(settings.APP_ROOT + appName + '/app/0/') 321 | os.makedirs(settings.APP_ROOT + appName + '/conf/0/') 322 | shutil.copyfile(settings.APP_JARS + appName + '.jar', 323 | settings.APP_ROOT + appName + '/app/0/' + appName + '.jar') 324 | shutil.copyfile(settings.APP_JARS + appName + '.properties', 325 | settings.APP_ROOT + appName + '/conf/0/' + appName + '.properties') 326 | file = open(settings.APP_ROOT + appName + '/app/0/version', 'w') 327 | file.write("") 328 | file.close() 329 | file = open(settings.APP_ROOT + appName + '/conf/0/version', 'w') 330 | file.write("") 331 | file.close() 332 | except Exception as e: 333 | log.error('运行目录初始化出错::' + str(e)) 334 | log.info('初始化运行目录完成,耗时: ' + str(math.ceil(1000 * (time.time() - now))) + "ms") 335 | else: 336 | log.info('运行目录已存在,开始启动项目...') 337 | 338 | for appName in settings.APP_LIST: 339 | try: 340 | t = startJavaThread(appName) 341 | t.start() 342 | time.sleep(5) 343 | except Exception as e: 344 | log.error(str(e)) 345 | start() 346 | 347 | 348 | if __name__ == '__main__': 349 | init() 350 | -------------------------------------------------------------------------------- /settings.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # 初始jar包及配置文件文件夹,jar文件及配置文件以实例名为文件名,文件名中不包含版本号 4 | APP_JARS = 'C://data/jars/' 5 | # 运行目录,不要手动创建,启动时判断目录是否存在来初始化项目 6 | APP_ROOT = 'C://data/nutzwk/' 7 | # jar文件名,名称要保持与配置文件里 nutz.application.name 值一致 8 | APP_LIST = [ 9 | 'wk-nb-service-sys', 10 | 'wk-nb-web-api', 11 | 'wk-nb-service-cms', 12 | 'wk-nb-service-wx', 13 | 'wk-nb-task', 14 | 'wk-nb-web-vue' 15 | ] 16 | # jar包启动的jvm配置参数 17 | APP_OPTS = { 18 | 'wk-nb-service-sys': '-Xmx100m -Xms60m', 19 | 'wk-nb-service-cms': '-Xmx80m -Xms40m', 20 | 'wk-nb-service-wx': '-Xmx80m -Xms40m', 21 | 'wk-nb-task': '-Xmx80m -Xms40m', 22 | 'wk-nb-web-api': '-Xmx80m -Xms40m', 23 | 'wk-nb-web-vue': '-Xmx150m -Xms80m' 24 | } 25 | # 通信密钥,保持与 wk-nb-web-api 模块里配置内容一致,用于心跳通信 26 | HTTP_SECRET_ID = 'wizzer' 27 | HTTP_SECRET_KEY = 'nutzwk' 28 | # API路径 29 | HTTP_URL = 'http://127.0.0.1:9001/open/api/deploy' 30 | # 心跳周期(单位:秒) 31 | HTTP_HEARTBEAT = 15 32 | HTTP_TIMEOUT = 120 33 | 34 | CACHE_TASK_IDS = [] 35 | CACHE_HOST_NAME = '' 36 | --------------------------------------------------------------------------------