diff --git a/app/__init__.py b/app/__init__.py index 8c1dde41c0e0de6ec95c91eec3d9e0ca663ef54e..dbc9c41a5d6abeac2aa0d85cd92d66aaf1ebad07 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,6 @@ from app.db import db from app.log import log from app.oss import alg_oss from app.redis import redis -from app.sftp_client import sftp_client from app.setting import STATIC_ROOT, STATICFILES_DIRS, OUTLINE_PATH @@ -14,7 +13,6 @@ def init_app(app): log.init_app(app) db.init_app(app) redis.init_app(app) - sftp_client.init_app(app) create_outline_files() init_blueprints(app) diff --git a/app/sftp_client.py b/app/sftp_client.py deleted file mode 100644 index cbae59184b81c46d36a0b50608ac8e38ae366de1..0000000000000000000000000000000000000000 --- a/app/sftp_client.py +++ /dev/null @@ -1,71 +0,0 @@ -import logging - -import paramiko - -logger = logging.getLogger('sftp') - - -class SFTPClient(object): - - def init_app(self, app): - self.host = app.config['STORAGE_HOST'] - self.port = app.config['STORAGE_SFTP_PORT'] - self.user = app.config['STORAGE_USER'] - self.password = app.config['STORAGE_PASSWORD'] - - def upload_file(self, local_path, server_path, timeout=10): - """ - 上传文件,注意:不支持文件夹 - :param server_path: 远程路径,比如:/home/sdn/tmp.txt - :param local_path: 本地路径,比如:D:/text.txt - :param timeout: 超时时间(默认),必须是int类型 - :return: bool - """ - try: - t = paramiko.Transport((self.host, self.port)) - t.connect(username=self.user, password=self.password) - sftp = paramiko.SFTPClient.from_transport(t) - sftp.banner_timeout = timeout - self._mkdirs(server_path, sftp) - sftp.put(local_path, server_path) - t.close() - return True - except Exception as e: - logger.error(f'sftp upload file[{local_path}] failed! error:{e}') - return False - - def down_file(self, server_path, local_path, timeout=10): - """ - 下载文件,注意:不支持文件夹 - :param server_path: 远程路径,比如:/home/sdn/tmp.txt - :param local_path: 本地路径,比如:D:/text.txt - :param timeout: 超时时间(默认),必须是int类型 - :return: bool - """ - try: - t = paramiko.Transport((self.host, 22)) - t.banner_timeout = timeout - t.connect(username=self.user, password=self.password) - sftp = paramiko.SFTPClient.from_transport(t) - sftp.get(server_path, local_path, sftp) - t.close() - return True - except Exception as e: - logger.error(f'sftp download file[{local_path}] failed! error:{e}') - return False - - @staticmethod - def _mkdirs(server_path, sftp): - path_list = server_path.split("/")[0:-1] - for path_item in path_list: - if not path_item: - continue - try: - sftp.chdir(path_item) - except Exception as e: - logger.info(f'The directory does not exist, now create it.[{e}]') - sftp.mkdir(path_item) - sftp.chdir(path_item) - - -sftp_client = SFTPClient() diff --git a/requirements.txt b/requirements.txt index a19e3414e8f2a28713954886792c4654f2f60503..cae743a747e690c189baa5be352b0abaed9c39b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ openpyxl~=3.0.9 oss2~=2.15.0 PyJWT~=2.3.0 pyyaml -paramiko \ No newline at end of file +httplib2 \ No newline at end of file diff --git a/services/plan_service.py b/services/plan_service.py index b4826b2f4278a5f0c5074c166d50f9e1472b5cc4..bef1f24dc02222578d4b17724157e668f9f0b604 100644 --- a/services/plan_service.py +++ b/services/plan_service.py @@ -216,6 +216,8 @@ async def __start_auto_task(auto_tasks): if suite_name not in suite_dict: suite_dict[suite_name] = list() case_obj = {'test_case': case_name} + if task.device_ip == '-': + task.device_ip = '' if task.device_ip: case_obj['server'] = {'ip': task.device_ip, 'channel_type': 'toneagent'} if task.cluster: diff --git a/services/tone_job_service.py b/services/tone_job_service.py index 84f192817dd601ba6872647fecf250d0b408c78e..cf6c1effcdec5ab7b9a55601dac8fc8c56be81b1 100644 --- a/services/tone_job_service.py +++ b/services/tone_job_service.py @@ -2,6 +2,8 @@ import time import json import uuid +import requests + from app.conf import conf from app.log import log from common.enums import Tone_Job_State, Track_Result, Case_Result, Status_EN @@ -11,7 +13,6 @@ from models.plan_model import update_plan_status from services.const import ERROR_UN_EXISTED_TONE_JOB from common.tone.tone_request import get_res_from_tone from common.tone.api import TONE_JOB_QUERY, TONE_CREATE_JOB -from app.sftp_client import sftp_client async def create_job(data, task_id): @@ -51,7 +52,7 @@ async def save_job_result(data): tone_job = await ToneJob.query_obj_one(ToneJob.tone_job_id == data.get('job_id')) if not tone_job: return ERROR_UN_EXISTED_TONE_JOB, False - machine_info = await parse_result_file(data.get('machine_file'), data.get('job_id')) + machine_info = await parse_result_file(data.get('machine_file')) if machine_info: tone_job.server_info = machine_info else: @@ -80,6 +81,7 @@ async def save_func_detail(task_id, data): test_case_id = func_result.get('test_case_id') case = [task_case_result for task_case_result in run_result.get('case_infos') if int(task_case_result['tone_case_id']) == test_case_id] + statistic_json = await parse_result_file(func_result.get('statistic_file')) if len(case) == 1: case[0]['result'] = func_result.get('case_state') case[0]['statistics'] = func_result.get('result_statistics') @@ -95,7 +97,7 @@ async def save_func_detail(task_id, data): 'task_id': task_id }) continue - statistic_json = await parse_result_file(func_result.get('statistic_file'), tone_job_id) + statistic_json = await parse_result_file(func_result.get('statistic_file')) for case_result in func_result.get('case_result'): current, expect = [], [] if statistic_json: @@ -124,14 +126,11 @@ async def save_func_detail(task_id, data): await task.update() -async def parse_result_file(res_file, job_id): +async def parse_result_file(res_file): if res_file: - index = res_file.find('/' + str(job_id)) - oss_file = res_file[index + 1:] - filepath = 'common/static/' + str(uuid.uuid4()) + '.json' - sftp_client.down_file(oss_file, filepath) - with open(filepath, 'r') as f: - return json.load(f) + req = requests.get(res_file) + if req.status_code == 200: + return req.json() return None