diff options
| author | 李玺康 <[email protected]> | 2022-05-18 16:37:32 +0800 |
|---|---|---|
| committer | 李玺康 <[email protected]> | 2022-05-18 16:37:32 +0800 |
| commit | cd4c356eef319fb5faf1b3ce27ecf918a2199498 (patch) | |
| tree | b4438d1994265d019b3b31908fcee822f597391b | |
| parent | d046870366dfe93f663de79e3f37a1dd41358834 (diff) | |
控制层中增加删除历史报告数据逻辑
| -rw-r--r-- | control/business.py | 53 | ||||
| -rw-r--r-- | control/control.py | 6 | ||||
| -rw-r--r-- | control/control_config.ini | 6 | ||||
| -rw-r--r-- | control/remote_command.py | 52 |
4 files changed, 113 insertions, 4 deletions
diff --git a/control/business.py b/control/business.py index 459f282..ca62978 100644 --- a/control/business.py +++ b/control/business.py @@ -6,6 +6,7 @@ import os import random import re import configparser +import shutil import threading import time import sys @@ -19,6 +20,7 @@ from queue import Queue import concurrent import socket from automation.control.tsg_log import TsgLogger +from automation.control.remote_command import Linux try: import xml.etree.cElementTree as ET except ImportError: @@ -96,6 +98,43 @@ def kill_all(instructNo_setup_json_abs_path, tsglogger): tsglogger.info(f"父进程的结果:{kill_parent_process.communicate()}") exit(0) +def clear_historical_reort(instructNo_setup_json_abs_path): + """ + 清楚控制层中report的历史文件,清楚agent server层中report的历史文件 + :param instructNo_setup_json_abs_path: #/opt/test/automation/tasks/2022051813000002800100859508 + :return: + """ + control_config = configparser.ConfigParser() + global config_abs_path + control_config.read(config_abs_path) + control_layer_retention_time = int(control_config.get("report_retention_time", "control_layer_retention_time")) + agent_server_layer_retention_time = int(control_config.get("report_retention_time", "agent_server_layer_retention_time")) + #确认删除时间之前的文件 + now_time = time.time() + last_control_layer_retention_time = now_time - 3600*24*control_layer_retention_time + + #删除控制层中report的历史文件 + clear_path = os.path.dirname(os.path.dirname(instructNo_setup_json_abs_path)) + print(clear_path) + clear_dir_list = os.listdir(clear_path) + for path in clear_dir_list: + dir_abs_path = os.path.join(clear_path, path) + dir_ctime = os.path.getctime(dir_abs_path) + print(dir_abs_path) + if dir_ctime <= last_control_layer_retention_time: + if os.path.isdir(dir_abs_path): + shutil.rmtree(dir_abs_path) + else: + os.remove(dir_abs_path) + + #删除agent server上的report历史文件: + agent_server_host = Linux(ip="192.168.42.4", username="root", password="Xxg-LAB<>90") + agent_server_host.connect() + clear_cmd = 'find /root/work/oss -ctime +{} -type d -name "20*" -exec rm -rf {{}} \\;'.format(agent_server_layer_retention_time) + clear_result = agent_server_host.send(clear_cmd) + agent_server_host.close() + + def get_setup_abs_path(instructNo, tsglogger): tsglogger.info("开始函数:{}".format(get_setup_abs_path.__name__)) control_config = configparser.ConfigParser() @@ -338,6 +377,16 @@ def execute_combine_report_of_robot(instructNo_setup_json_abs_path, tsglogger): report_path = re.search("/.*html", t_list[2]).group(0) os.rename(report_json_path1 + "/log.html", log_tetail_abs_path) os.rename(report_json_path1 + "/report.html", report_tetail_abs_path) + + #更改report.html文件中log.html文件路径 + report_html_abs_path = report_tetail_abs_path + log_html_name = log_detail_name + with open(report_html_abs_path, "r", encoding="utf-8") as f1: + f_old = f1.read() + f_new = f_old.replace("log.html", log_html_name) + with open(report_html_abs_path, "w", encoding="utf-8") as f2: + f2.write(f_new) + tsglogger.debug("返回参数:{}".format([report_path, log_path, xml_path])) tsglogger.info(f"结束函数:{execute_combine_report_of_robot.__name__}") return [report_path, log_path, xml_path] @@ -539,8 +588,8 @@ def callbaock_upload_result_file(file_path, instructNo, tsglogger): :return: """ tsglogger.info(f"开始函数:{callbaock_upload_result_file.__name__}") - print(file_path) - print(instructNo) + tsglogger.info(file_path) + tsglogger.info(instructNo) config = configparser.ConfigParser() #从配置文件中拿到路径ip global config_abs_path config.read(config_abs_path) diff --git a/control/control.py b/control/control.py index 80ed66f..4b47b43 100644 --- a/control/control.py +++ b/control/control.py @@ -59,7 +59,7 @@ def executeAutomation(instructNo, runMode, onlyDryrun, kill): shutil.copyfile("/opt/test/automation/control/setup_taskid_11223344.json", setup_json_name_path) instructNo = taskid_timestamp + "_taskid_" + "1" #instructNo = "2022032919164900060000313240" - instructNo = "2022042616220800600077608560" + instructNo = "2022051721000002800100775113" runMode = "fusion" else: tsglogger.info('fusion模式执行!') @@ -77,6 +77,10 @@ def executeAutomation(instructNo, runMode, onlyDryrun, kill): tsglogger.info(f"控制脚本父进程:{ppid}") with open(os.path.dirname(instructNo_setup_json_abs_path) + "/pid.txt", "w") as f: f.write(f"{ppid}") + + #清空历史垃圾数据 + business.clear_historical_reort(instructNo_setup_json_abs_path) + #2、重组保存dryrun、report的目录和使用部分文件名 instructNo_setup_json_abs_path_list = instructNo_setup_json_abs_path.split("/") dryrun_dir_path = "/".join(instructNo_setup_json_abs_path_list[:-1]) + "/dryrun" #dryrun目录路径信息 diff --git a/control/control_config.ini b/control/control_config.ini index 3456590..3d01764 100644 --- a/control/control_config.ini +++ b/control/control_config.ini @@ -26,4 +26,8 @@ mobile_info_dict = { "taobao", "telegram", "tor", "twitter", "wechat", "whatsapp", "wireguard", "youtube"], "xiaomi":["freegate", "psiphon3", "tachyon", "tor"], "iphonexr":["freegate", "psiphon3", "tachyon", "youtube"] - }
\ No newline at end of file + } + +[report_retention_time] +control_layer_retention_time = 7 +agent_server_layer_retention_time = 10
\ No newline at end of file diff --git a/control/remote_command.py b/control/remote_command.py new file mode 100644 index 0000000..04d53ca --- /dev/null +++ b/control/remote_command.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +import time +import paramiko + + +class Linux(): + def __init__(self, ip, username, password, timeout=30): + self.ip = ip + self.username = username + self.password = password + self.timeout = timeout + self.t = "" + self.chan = "" + self.try_times = 3 + + def connect(self): + while True: + try: + self.t = paramiko.Transport(sock=(self.ip, 22)) + self.t.connect(username=self.username, password=self.password) + self.chan = self.t.open_session() + self.chan.get_pty() + self.chan.invoke_shell() + print("连接{}成功".format(self.ip)) + self.revc_data = self.chan.recv(65535).decode("utf-8") + print("接收数据:{}".format(self.revc_data)) + return + except Exception as e: + if self.try_times != 0: + print(e) + print("重试") + self.try_times -= 1 + else: + print("{}连接失败".format(self.ip)) + exit(1) + + def close(self): + self.chan.close() + self.t.close() + + def send(self, cmd): + cmd += "\r" + result = "" + self.chan.send(cmd) + while True: + time.sleep(0.5) + ret = self.chan.recv(65535) + ret = ret.decode("utf-8") + result += ret + print(result) + return result |
