diff --git a/.gitignore b/.gitignore index 2974224039a0bf740a22b0ed85e9b25bb13d0ac6..3774bbbdde2d0837ee7736d9305e05289cfe45fa 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,7 @@ logs .pytest_cache rpmbuild +build -app.ini \ No newline at end of file +app.ini +/tests/data.py diff --git a/README.en.md b/README.en.md deleted file mode 100644 index 1cd7cc9559fb49d38a6dbb8edf783eca3603829a..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# python-tone-agent - -#### Description -ToneAgent的Python版SDK - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md index 42b5adb79117c6d60830dec555909ff97449b409..00a9c605b583da1e82e107c750e8cdf8294d6775 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,15 @@ -`pip3 install setuptools` +`推荐 Python 版本 >= 3.7.8` -The usual way to create an RPM of your module distribution is to run the bdist_rpm command: -`python setup.py bdist_rpm` +#### 本地启动: -or the bdist command with the --format option: -`python setup.py bdist --formats=rpm` +##### 增加 app.ini 配置文件 +``` +[DEFAULT] +env = dev +``` +##### 安装依赖 +`pip3 install -r requirements.txt` - -`journalctl -u toneagent.service` - -本地启动: -`export TONE_AGENT_ENV=dev` +##### 启动项目 `python3 main.py` -`python setup.py develop` - -wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz -yum install -y gcc patch libffi-devel python3-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel -tar -zxvf Python-3.7.8.tgz -sudo mkdir /usr/local/python378 -cd Python-3.7.8/ -sudo ./configure --prefix=/usr/local/python378 -sudo make -sudo make install -rm -rf /usr/bin/python3 -rm -rf /usr/bin/pip3 -sudo ln -s /usr/local/python378/bin/python3.7 /usr/bin/python3 -sudo ln -s /usr/local/python378/bin/pip3.7 /usr/bin/pip3 diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 57d631c3f004f9f6e24c31388e4df58801b8aba1..0000000000000000000000000000000000000000 --- a/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# coding: utf-8 diff --git a/core/task.py b/core/task.py index 194876afbb96c11b8056f15254a75de57c961b7d..ba08263a07664645e11bb4052934e9cbed2cb88a 100644 --- a/core/task.py +++ b/core/task.py @@ -1,12 +1,12 @@ # coding: utf-8 import base64 -import os -import signal import subprocess from threading import Timer from typing import Optional +import psutil + from conf import SERVICE_CONFIG, CONFIG from core import BaseObject from core.common.enum import TaskStatus @@ -86,7 +86,7 @@ class Task(BaseObject): error_msg: str = p.stderr.read() error_code: str = '' p.kill() - # os.killpg(int(p.pid), signal.SIGKILL) + # 将 result, end_time 等信息写入 result 文件 Result( tid=self.tid, @@ -105,20 +105,18 @@ class Task(BaseObject): """ (超时后)终止命令 """ - logger.info(f'task(tid:{self.tid} | pid:{p.pid}) ' - f'killed by timeout timer.timeout({self.timeout})') - p.kill() - # todo 进程kill后 更新结果 - # Result( - # tid=self.tid, - # pid=p.pid, - # status=TaskStatus.COMPLETED, - # result='', - # error_code=error_code, - # error_msg=error_msg, - # exit_code=str(p.returncode), - # finish_time=get_present_time() - # ).update() + if psutil.pid_exists(p.pid): + logger.info(f'task(tid:{self.tid} | pid:{p.pid}) ' + f'killed by timer. timeout: {self.timeout}') + p.kill() + Result( + tid=self.tid, + pid=p.pid, + status=TaskStatus.COMPLETED, + error_msg=f'killed by timer. timeout({self.timeout})', + exit_code=str(p.returncode), + finish_time=get_present_time() + ).update() @staticmethod def conv_env(env: str) -> Optional[dict]: diff --git a/requirements.txt b/requirements.txt index c4f60d9f98170cc5917ebf03cb16c5af9311db74..fe52a21e546f28d9a6f62f4bb975ad7e2bf23ace 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ sanic schedule pyyaml requests +psutil \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py index fbc4148e4ce680ba0b042f53a449e9292e140639..6d04e348a3db7dc5cf143b1f796d776e8b227865 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -5,12 +5,13 @@ import time import requests +from tests.data import agent_proxy, access_key, secret_key + class ToneAgentRequest(object): def __init__(self, access_key, secret_key): - # self._domain = 'http://127.0.0.1:9999' - self._domain = 'http://121.196.236.71:8081' + self._domain = agent_proxy self._access_key = access_key self._secret_key = secret_key self._data = { @@ -103,15 +104,15 @@ class SendTaskRequest(ToneAgentRequest): if __name__ == '__main__': - access_key = '3d2560ed395942058ca8151ab76847a7' - secret_key = '1d4cf71c-a96e-4174-9849-c24025c328b4' - + script = """ + echo test + """ request = SendTaskRequest(access_key, secret_key) request.set_ip('127.0.0.250') - request.set_script('cat /etc/os-release | grep -i id=') - request.set_script_type('cmd') + request.set_script(script) + request.set_script_type('shell') request.set_sync('true') - request.set_timeout(60) + request.set_timeout(5) # request.set_cwd('/tmp') # request.set_env("a='1,2,3'") res = request.send_request()