From cbb864f5f21ec28625433c00cde291503b1e9665 Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Wed, 19 Jun 2024 09:37:15 +0800 Subject: [PATCH 1/6] =?UTF-8?q?k8s=E5=9C=BA=E6=99=AF=E4=B8=8B=E7=9A=84dock?= =?UTF-8?q?er=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/devkit_utils/docker_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/common/devkit_utils/docker_utils.py b/common/devkit_utils/docker_utils.py index 2c217c7..92f7f29 100644 --- a/common/devkit_utils/docker_utils.py +++ b/common/devkit_utils/docker_utils.py @@ -6,8 +6,9 @@ from devkit_utils import shell_tools def get_docker_id(docker_path: str): paths_name = docker_path.strip().split("/") - if len(paths_name) >= 3: - return paths_name[2] + length = len(paths_name) + if length >= 3: + return paths_name[length - 1] else: raise Exception("can not found docker id") @@ -22,6 +23,8 @@ def is_docker_process(pid): fields = line.split(":") if len(fields) >= 3 and str(fields[1]) == "devices" and str(fields[2]).startswith("/docker"): return True, get_docker_id(fields[2]) + if len(fields) >= 3 and str(fields[1]) == "devices" and str(fields[2]).startswith("/kubepods"): + return True, get_docker_id(fields[2]) return False, None -- Gitee From 8a0d6fd2a02e8833db7784aa4ea6497e05718a10 Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Thu, 20 Jun 2024 16:44:33 +0800 Subject: [PATCH 2/6] =?UTF-8?q?k8s=E5=9C=BA=E6=99=AF=E4=B8=8Bdocker?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8Ccrictl=E5=91=BD=E4=BB=A4=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/container/__init__.py | 0 common/container/container.py | 46 ++++++++++ common/container/crictl.py | 85 +++++++++++++++++++ common/container/docker.py | 58 +++++++++++++ common/devkit_utils/container_utils.py | 18 ++++ common/devkit_utils/docker_utils.py | 43 ---------- common/devkit_utils/proc_utils.py | 51 +++++++++++ .../bin/flight_records_sample.py | 49 +++++------ 8 files changed, 280 insertions(+), 70 deletions(-) create mode 100644 common/container/__init__.py create mode 100644 common/container/container.py create mode 100644 common/container/crictl.py create mode 100644 common/container/docker.py create mode 100644 common/devkit_utils/container_utils.py delete mode 100644 common/devkit_utils/docker_utils.py create mode 100644 common/devkit_utils/proc_utils.py diff --git a/common/container/__init__.py b/common/container/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/container/container.py b/common/container/container.py new file mode 100644 index 0000000..e4181dc --- /dev/null +++ b/common/container/container.py @@ -0,0 +1,46 @@ +import abc +import typing + + +class Container(abc.ABC): + def __init__(self, container_id): + self.container_id = container_id + + @abc.abstractmethod + def create_dir_in_container(self, target_dir, mode=755): + """ + 在docker中创建目录 + """ + + @abc.abstractmethod + def copy_to_container(self, origin_file, target_dir, mode=755): + """ + 复制宿主机文件到容器内 + """ + + @abc.abstractmethod + def copy_to_host(self, origin_file, target_dir, mode=755): + """ + 复制容器中的文件到宿主机 + """ + + @abc.abstractmethod + def delete_in_container(self, target_dir): + """ + 删除文件夹 + """ + + +class ContainerFactory(abc.ABC): + + @abc.abstractmethod + def check_in_machine(self): + """ + 检测当前服务器中是否存在该类型容器 + """ + + @abc.abstractmethod + def get_container(self) -> typing.Dict[str, Container]: + """ + 获取当前运行的容器 + """ diff --git a/common/container/crictl.py b/common/container/crictl.py new file mode 100644 index 0000000..59c89c3 --- /dev/null +++ b/common/container/crictl.py @@ -0,0 +1,85 @@ +import logging +import os.path +import typing + +from container.container import Container, ContainerFactory +from devkit_utils import shell_tools + + +class CrictlContainer(Container): + + def __init__(self, container_id, name, pod_id, pod): + super().__init__(container_id) + self.name = name + self.pod_id = pod_id + self.pod = pod + + def create_dir_in_container(self, target_dir, mode=755): + outcome = shell_tools.exec_shell(f"kubectl exec {self.pod} -c {self.name} -- mkdir -p {target_dir}", + is_shell=True) + logging.info(outcome) + outcome = shell_tools.exec_shell(f"kubectl exec {self.pod} -c {self.name} -- chmod {mode} {target_dir}", + is_shell=True) + logging.info(outcome) + + def copy_to_container(self, origin_file, target_dir, mode=755): + file_name = os.path.basename(origin_file) + outcome = shell_tools.exec_shell(f"kubectl cp -c {self.name} {origin_file} {self.pod}:{target_dir}", + is_shell=True) + logging.info(outcome) + outcome = shell_tools.exec_shell( + f"kubectl exec {self.pod} -c {self.name} -- chmod {mode} {target_dir}/{file_name}", + is_shell=True) + logging.info(outcome) + + def copy_to_host(self, origin_file, target_dir, mode=755): + outcome = shell_tools.exec_shell(f"kubectl cp -c {self.name} {self.pod}:{origin_file} {target_dir}", + is_shell=True) + logging.info(outcome) + + def delete_in_container(self, target_dir): + outcome = shell_tools.exec_shell(f"kubectl exec {self.pod} -c {self.name} -- rm -rf {target_dir}", + is_shell=True) + logging.info(outcome) + + +class CrictlContainerFactory(ContainerFactory): + + def __init__(self): + self.container_id_index = 0 + self.name_index = 0 + self.pod_id_index = 0 + self.pod_index = 0 + + def check_in_machine(self): + outcome = shell_tools.exec_shell("crictl ps", is_shell=True) + logging.info(outcome) + if outcome.return_code == 0: + outcome = shell_tools.exec_shell("kubectl get pods", is_shell=True) + if outcome.return_code == 0: + return True + return False + + def get_container(self) -> typing.List[Container]: + containers = list() + outcome = shell_tools.exec_shell("crictl ps", is_shell=True) + lines = outcome.out.split("\n") + self.__parse(lines[0]) + for line in lines[1:]: + fields = line.split() + containers.append(CrictlContainer(fields[self.container_id_index], fields[self.name_index], + fields[self.pod_id_index], fields[self.pod_index])) + return containers + + def __parse(self, header: str): + fields = header.split() + for i in range(0, len(fields)): + field = header[i].lower() + if field == "container": + self.container_id_index = i + if field == "name": + self.name_index = i + if field == "pod_id": + self.pod_id_index = i + if field == "pod": + self.pod_index = i diff --git a/common/container/docker.py b/common/container/docker.py new file mode 100644 index 0000000..ba5b053 --- /dev/null +++ b/common/container/docker.py @@ -0,0 +1,58 @@ +import logging +import os.path +import typing + +from container.container import Container, ContainerFactory +from devkit_utils import shell_tools + + +class DockerContainer(Container): + + def __init__(self, container_id): + super().__init__(container_id) + + def create_dir_in_container(self, target_dir, mode=755): + outcome = shell_tools.exec_shell(f"docker exec {self.container_id} mkdir -p {target_dir}", is_shell=True) + logging.info(outcome) + outcome = shell_tools.exec_shell(f"docker exec {self.container_id} chmod {mode} {target_dir}", is_shell=True) + logging.info(outcome) + + def copy_to_container(self, origin_file, target_dir, mode=755): + file_name = os.path.basename(origin_file) + outcome = shell_tools.exec_shell(f"docker cp {origin_file} {self.container_id}:{target_dir}", is_shell=True) + logging.info(outcome) + outcome = shell_tools.exec_shell(f"docker exec {self.container_id} chmod {mode} {target_dir}/{file_name}", + is_shell=True) + logging.info(outcome) + + def copy_to_host(self, origin_file, target_dir, mode=755): + outcome = shell_tools.exec_shell(f"docker cp {self.container_id}:{origin_file} {target_dir}", + is_shell=True) + logging.info(outcome) + + def delete_in_container(self, target_dir): + outcome = shell_tools.exec_shell(f"docker exec {self.container_id} rm -rf {target_dir}", + is_shell=True) + logging.info(outcome) + + +class DockerContainerFactory(ContainerFactory): + def __init__(self): + self.container_id_index = 0 + + def check_in_machine(self): + outcome = shell_tools.exec_shell("docker ps", is_shell=True) + logging.info(outcome) + if outcome.return_code == 0: + return True + else: + return False + + def get_container(self) -> typing.List[Container]: + containers = list() + outcome = shell_tools.exec_shell("docker ps", is_shell=True) + lines = outcome.out.split("\n") + for line in lines[1:]: + fields = line.split() + containers.append(DockerContainer(fields[self.container_id_index])) + return containers diff --git a/common/devkit_utils/container_utils.py b/common/devkit_utils/container_utils.py new file mode 100644 index 0000000..f4b917d --- /dev/null +++ b/common/devkit_utils/container_utils.py @@ -0,0 +1,18 @@ +import typing + +from container.container import Container +from container.crictl import CrictlContainerFactory +from container.docker import DockerContainerFactory + + +def get_containers() -> typing.List[Container]: + """ + 获取容器信息,当存在docker命令时,使用docker命令获取,不存在docker命令时,尝试使用crictl(必须存在kubectl命令)获取 + """ + docker_factory = DockerContainerFactory() + if docker_factory.check_in_machine(): + return docker_factory.get_container() + crictl_factory = CrictlContainerFactory() + if crictl_factory.check_in_machine(): + return crictl_factory.get_container() + return list() diff --git a/common/devkit_utils/docker_utils.py b/common/devkit_utils/docker_utils.py deleted file mode 100644 index 92f7f29..0000000 --- a/common/devkit_utils/docker_utils.py +++ /dev/null @@ -1,43 +0,0 @@ -import logging -import os.path - -from devkit_utils import shell_tools - - -def get_docker_id(docker_path: str): - paths_name = docker_path.strip().split("/") - length = len(paths_name) - if length >= 3: - return paths_name[length - 1] - else: - raise Exception("can not found docker id") - - -def is_docker_process(pid): - cgroup_file = f'/proc/{pid}/cgroup' - if not os.path.exists(cgroup_file): - return False, None - with open(cgroup_file, "r", encoding="utf-8") as file: - cgroup_infos = file.readlines() - for line in cgroup_infos: - fields = line.split(":") - if len(fields) >= 3 and str(fields[1]) == "devices" and str(fields[2]).startswith("/docker"): - return True, get_docker_id(fields[2]) - if len(fields) >= 3 and str(fields[1]) == "devices" and str(fields[2]).startswith("/kubepods"): - return True, get_docker_id(fields[2]) - return False, None - - -def create_dir_in_docker(docker_id, target_dir, mode=755): - outcome = shell_tools.exec_shell(f"docker exec {docker_id} mkdir -p {target_dir}", is_shell=True) - logging.info(outcome) - outcome = shell_tools.exec_shell(f"docker exec {docker_id} chmod {mode} {target_dir}", is_shell=True) - logging.info(outcome) - - -def copy_to_docker(docker_id, origin_file, target_dir): - file_name = os.path.basename(origin_file) - outcome = shell_tools.exec_shell(f"docker cp {origin_file} {docker_id}:{target_dir}", is_shell=True) - logging.info(outcome) - outcome = shell_tools.exec_shell(f"docker exec {docker_id} chmod 755 {target_dir}/{file_name}", is_shell=True) - logging.info(outcome) diff --git a/common/devkit_utils/proc_utils.py b/common/devkit_utils/proc_utils.py new file mode 100644 index 0000000..19392fc --- /dev/null +++ b/common/devkit_utils/proc_utils.py @@ -0,0 +1,51 @@ +import os +import typing + +from container.container import Container + + +def is_java_process(pid) -> bool: + """ + 判断是否是一个java进程 + """ + comm_file = f'/proc/{pid}/comm' + if not os.path.exists(comm_file): + return False + with open(comm_file, "r", encoding="utf-8") as file: + comm = file.readline() + if "java" in comm: + return True + return False + + +def is_container_process(pid, containers: typing.List[Container]) -> (bool, Container): + """ + 判断是否是一个容器进程 + """ + cgroup_file = f'/proc/{pid}/cgroup' + if not os.path.exists(cgroup_file): + return False, None + with open(cgroup_file, "r", encoding="utf-8") as file: + cgroup_infos = file.readlines() + for line in cgroup_infos: + # 三层结构不支持 宿主机-containerd-containerd + # 当前只支持两层结构 宿主机-containerd,并且宿主机 有 docker 或者(crictl和kubectl)命令 + if "devices" in line and "/docker/" in line: + return True, __get_container(line, containers, True) + if "devices" in line and "/kubepods/" in line: + return True, __get_container(line, containers, False) + return False, None + + +def __get_container(device_line: str, containers: typing.List[Container], is_docker: bool): + paths_name = device_line.strip().split("/") + length = len(paths_name) + if length >= 3 and is_docker: + for container in containers: + if container.container_id in paths_name[2]: + return container + elif length >= 3: + for container in containers: + if container.container_id in paths_name[length - 1]: + return container + raise Exception("can not found docker id") diff --git a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py index 40ee1d7..2891a94 100644 --- a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py +++ b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py @@ -7,7 +7,7 @@ import time import psutil -from devkit_utils import shell_tools, file_utils, docker_utils +from devkit_utils import shell_tools, file_utils, container_utils, proc_utils from devkit_utils.error_coce import ErrorCodeEnum from devkit_utils.log_config import config_log_ini from devkit_utils.pyinstaller_utils import PyInstallerUtils @@ -16,12 +16,11 @@ ROOT_PATH = os.path.dirname(os.path.dirname(__file__)) class TargetProcess: - def __init__(self, pid, name, is_docker=False, docker_id=None): + def __init__(self, pid, name, container=None): self.pid = pid self.name = name self.jfr_name = None - self.is_docker = is_docker - self.docker_id = docker_id + self.container = container self.jfr_path = None @@ -50,7 +49,6 @@ class FlightRecordsFactory: file_utils.create_dir(self.dir_to_storage_jfr) self.jcmd_path = None self.user_is_root = False - self.is_docker = False def start_sample(self): try: @@ -126,15 +124,18 @@ class FlightRecordsFactory: os.chmod(self.temporary_settings_path, mode=0o644) def __init_pids(self): + containers = container_utils.get_containers() for app in self.apps.split(","): commander_to_view_pid = "ps -ef|grep java|grep -v grep|grep {}|awk '{{print $2}}'".format(app) outcome = shell_tools.exec_shell(commander_to_view_pid, is_shell=True) logging.info("app:%s to pid %s", app, outcome) pids = outcome.out.split() for pid in pids: - is_docker, docker_id = docker_utils.is_docker_process(pid) - if is_docker: - self.pids.append(TargetProcess(pid, app, is_docker, docker_id)) + if not proc_utils.is_java_process(pid): + continue + is_in_container, container = proc_utils.is_container_process(pid, containers) + if is_in_container: + self.pids.append(TargetProcess(pid, app, container)) else: self.pids.append(TargetProcess(pid, app)) @@ -157,12 +158,11 @@ class FlightRecordsFactory: def __start_recorder_by_root(self): logging.info(PyInstallerUtils.get_env()) for target in self.pids: - if target.is_docker: - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_dir, mode=777) - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_config_dir, mode=777) - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_data_dir, mode=777) - docker_utils.copy_to_docker(target.docker_id, self.temporary_settings_path, - self.tmp_config_dir) + if target.container: + target.container.create_dir_in_container(self.tmp_dir, mode=777) + target.container.create_dir_in_container(self.tmp_config_dir, mode=777) + target.container.create_dir_in_container(self.tmp_data_dir, mode=777) + target.container.copy_to_container(self.temporary_settings_path, self.tmp_config_dir) jfr_path = self.__jfr_name(target.name, target.pid) username = psutil.Process(int(target.pid)).username() command = (f"su - {username} -c '" @@ -181,12 +181,11 @@ class FlightRecordsFactory: def __start_recorder(self): for target in self.pids: - if target.is_docker: - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_dir, mode=777) - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_config_dir, mode=777) - docker_utils.create_dir_in_docker(target.docker_id, self.tmp_data_dir, mode=777) - docker_utils.copy_to_docker(target.docker_id, self.temporary_settings_path, - self.tmp_config_dir) + if target.container: + target.container.create_dir_in_container(self.tmp_dir, mode=777) + target.container.create_dir_in_container(self.tmp_config_dir, mode=777) + target.container.create_dir_in_container(self.tmp_data_dir, mode=777) + target.container.copy_to_container(self.temporary_settings_path, self.tmp_config_dir) jfr_path = self.__jfr_name(target.name, target.pid) command = (f"jcmd {target.pid} JFR.start settings={self.temporary_settings_path} duration={self.duration}s" f" name={self.RECORD_NAME} filename={jfr_path}") @@ -243,13 +242,9 @@ class FlightRecordsFactory: def __copy_to_host(self): for target in self.pids_to_start_recording: - if target.is_docker: - outcome = shell_tools.exec_shell(f"docker cp {target.docker_id}:{target.jfr_path} {target.jfr_path}", - is_shell=True) - logging.info(outcome) - outcome = shell_tools.exec_shell(f"docker exec {target.docker_id} rm -rf {self.tmp_dir}", - is_shell=True) - logging.info(outcome) + if target.container: + target.container.copy_to_host(target.jfr_path, target.jfr_path) + target.container.delete_in_container(self.tmp_dir) def __jfr_name(self, app, pid): return os.path.join(self.tmp_data_dir, f"{app}_PID_{pid}_Time_{self.now_date}.jfr") -- Gitee From 927250b9089f2d5226f8a0d2d053878286ca52a7 Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Thu, 20 Jun 2024 19:47:18 +0800 Subject: [PATCH 3/6] =?UTF-8?q?k8s=E5=9C=BA=E6=99=AF=E4=B8=8Bdocker?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8Ccrictl=E5=91=BD=E4=BB=A4=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/container/crictl.py | 9 +++++---- common/container/docker.py | 2 +- common/devkit_utils/proc_utils.py | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/container/crictl.py b/common/container/crictl.py index 59c89c3..3e0fa09 100644 --- a/common/container/crictl.py +++ b/common/container/crictl.py @@ -1,5 +1,6 @@ import logging import os.path +import re import typing from container.container import Container, ContainerFactory @@ -63,18 +64,18 @@ class CrictlContainerFactory(ContainerFactory): def get_container(self) -> typing.List[Container]: containers = list() outcome = shell_tools.exec_shell("crictl ps", is_shell=True) - lines = outcome.out.split("\n") + lines = outcome.out.strip().split("\n") self.__parse(lines[0]) for line in lines[1:]: - fields = line.split() + fields = re.split(r"\s{2,}", line) containers.append(CrictlContainer(fields[self.container_id_index], fields[self.name_index], fields[self.pod_id_index], fields[self.pod_index])) return containers def __parse(self, header: str): - fields = header.split() + fields = re.split(r"\s{2,}", header) for i in range(0, len(fields)): - field = header[i].lower() + field = fields[i].lower() if field == "container": self.container_id_index = i if field == "name": diff --git a/common/container/docker.py b/common/container/docker.py index ba5b053..42cb7aa 100644 --- a/common/container/docker.py +++ b/common/container/docker.py @@ -51,7 +51,7 @@ class DockerContainerFactory(ContainerFactory): def get_container(self) -> typing.List[Container]: containers = list() outcome = shell_tools.exec_shell("docker ps", is_shell=True) - lines = outcome.out.split("\n") + lines = outcome.out.strip().split("\n") for line in lines[1:]: fields = line.split() containers.append(DockerContainer(fields[self.container_id_index])) diff --git a/common/devkit_utils/proc_utils.py b/common/devkit_utils/proc_utils.py index 19392fc..ac21ab1 100644 --- a/common/devkit_utils/proc_utils.py +++ b/common/devkit_utils/proc_utils.py @@ -18,7 +18,7 @@ def is_java_process(pid) -> bool: return False -def is_container_process(pid, containers: typing.List[Container]) -> (bool, Container): +def is_container_process(pid, containers: typing.List[Container]) -> tuple[bool, typing.Optional[Container]]: """ 判断是否是一个容器进程 """ @@ -30,10 +30,10 @@ def is_container_process(pid, containers: typing.List[Container]) -> (bool, Cont for line in cgroup_infos: # 三层结构不支持 宿主机-containerd-containerd # 当前只支持两层结构 宿主机-containerd,并且宿主机 有 docker 或者(crictl和kubectl)命令 - if "devices" in line and "/docker/" in line: - return True, __get_container(line, containers, True) if "devices" in line and "/kubepods/" in line: return True, __get_container(line, containers, False) + if "devices" in line and "/docker/" in line: + return True, __get_container(line, containers, True) return False, None -- Gitee From c0657434915a496873cadea6da98af3fa8d213ec Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Thu, 20 Jun 2024 20:00:57 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devkit_tester_agent/bin/flight_records_sample.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py index 2891a94..bdc73ae 100644 --- a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py +++ b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py @@ -4,9 +4,11 @@ import logging.config import os.path import shutil import time +import typing import psutil +from container.container import Container from devkit_utils import shell_tools, file_utils, container_utils, proc_utils from devkit_utils.error_coce import ErrorCodeEnum from devkit_utils.log_config import config_log_ini @@ -16,11 +18,11 @@ ROOT_PATH = os.path.dirname(os.path.dirname(__file__)) class TargetProcess: - def __init__(self, pid, name, container=None): + def __init__(self, pid, name, container: typing.Optional[Container] = None): self.pid = pid self.name = name self.jfr_name = None - self.container = container + self.container: typing.Optional[Container] = container self.jfr_path = None @@ -34,7 +36,7 @@ class FlightRecordsFactory: self.root_path = root_path self.wait_for_jmeter_stop = wait_for_jmeter_stop self.pids: list[TargetProcess] = list() - self.pids_to_start_recording = list() + self.pids_to_start_recording: list[TargetProcess] = list() self.pids_to_stop_recording = list() self.jfr_paths: list[str] = [] self.return_code = ErrorCodeEnum.FINISHED -- Gitee From 524c17f65620f9f46703495080aefd2882225447 Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Thu, 20 Jun 2024 20:06:04 +0800 Subject: [PATCH 5/6] =?UTF-8?q?k8s=E5=9C=BA=E6=99=AF=E4=B8=8Bdocker?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8Ccrictl=E5=91=BD=E4=BB=A4=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/container/crictl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/container/crictl.py b/common/container/crictl.py index 3e0fa09..15fe92d 100644 --- a/common/container/crictl.py +++ b/common/container/crictl.py @@ -80,7 +80,7 @@ class CrictlContainerFactory(ContainerFactory): self.container_id_index = i if field == "name": self.name_index = i - if field == "pod_id": + if field == "pod id": self.pod_id_index = i if field == "pod": self.pod_index = i -- Gitee From 3b1d3ceedcf3a45f0f2421a9bc9a550a1daa4758 Mon Sep 17 00:00:00 2001 From: zoupanpan Date: Fri, 21 Jun 2024 11:35:47 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E5=BE=88=E5=BF=AB?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/container/crictl.py | 4 +++ .../devkit_tester/bin/entrance.py | 8 +++--- .../bin/flight_records_sample.py | 26 +++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/common/container/crictl.py b/common/container/crictl.py index 15fe92d..3e53d5f 100644 --- a/common/container/crictl.py +++ b/common/container/crictl.py @@ -16,6 +16,7 @@ class CrictlContainer(Container): self.pod = pod def create_dir_in_container(self, target_dir, mode=755): + logging.info("create dir:%s", target_dir) outcome = shell_tools.exec_shell(f"kubectl exec {self.pod} -c {self.name} -- mkdir -p {target_dir}", is_shell=True) logging.info(outcome) @@ -24,6 +25,7 @@ class CrictlContainer(Container): logging.info(outcome) def copy_to_container(self, origin_file, target_dir, mode=755): + logging.info("origin:%s target:%s", origin_file, target_dir) file_name = os.path.basename(origin_file) outcome = shell_tools.exec_shell(f"kubectl cp -c {self.name} {origin_file} {self.pod}:{target_dir}", is_shell=True) @@ -34,11 +36,13 @@ class CrictlContainer(Container): logging.info(outcome) def copy_to_host(self, origin_file, target_dir, mode=755): + logging.info("origin:%s target:%s", origin_file, target_dir) outcome = shell_tools.exec_shell(f"kubectl cp -c {self.name} {self.pod}:{origin_file} {target_dir}", is_shell=True) logging.info(outcome) def delete_in_container(self, target_dir): + logging.info("target:%s", target_dir) outcome = shell_tools.exec_shell(f"kubectl exec {self.pod} -c {self.name} -- rm -rf {target_dir}", is_shell=True) logging.info(outcome) diff --git a/component/DevKitTester/devkit_tester/bin/entrance.py b/component/DevKitTester/devkit_tester/bin/entrance.py index c2f997f..dad4dfa 100644 --- a/component/DevKitTester/devkit_tester/bin/entrance.py +++ b/component/DevKitTester/devkit_tester/bin/entrance.py @@ -343,12 +343,12 @@ class Distributor: logging.info("ip:%s start devkit pipeline agent", ip) if self.enable_jmeter_command: start_command = ( - f"bash {task_id}/devkit_tester_agent/bin/devkit_agent_start.sh -a {self.apps} " - f"-d {self.duration} -t {task_id} -w") + f"bash --login -c 'bash {task_id}/devkit_tester_agent/bin/devkit_agent_start.sh -a {self.apps} " + f"-d {self.duration} -t {task_id} -w'") else: start_command = ( - f"bash {task_id}/devkit_tester_agent/bin/devkit_agent_start.sh -a {self.apps} " - f"-d {self.duration} -t {task_id}") + f"bash --login -c 'bash {task_id}/devkit_tester_agent/bin/devkit_agent_start.sh -a {self.apps} " + f"-d {self.duration} -t {task_id}'") stdin, stdout, stderr = ssh_client.exec_command(start_command) logging.info("start the sampling process on server %s:%s", ip, stderr.readlines()) self.__close_pipeline(stdin, stdout, stderr) diff --git a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py index bdc73ae..5f451ea 100644 --- a/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py +++ b/component/DevKitTester/devkit_tester_agent/bin/flight_records_sample.py @@ -24,6 +24,7 @@ class TargetProcess: self.jfr_name = None self.container: typing.Optional[Container] = container self.jfr_path = None + self.username = None class FlightRecordsFactory: @@ -158,7 +159,6 @@ class FlightRecordsFactory: self.user_is_root = False def __start_recorder_by_root(self): - logging.info(PyInstallerUtils.get_env()) for target in self.pids: if target.container: target.container.create_dir_in_container(self.tmp_dir, mode=777) @@ -167,6 +167,7 @@ class FlightRecordsFactory: target.container.copy_to_container(self.temporary_settings_path, self.tmp_config_dir) jfr_path = self.__jfr_name(target.name, target.pid) username = psutil.Process(int(target.pid)).username() + target.username = username command = (f"su - {username} -c '" f"{self.jcmd_path} {target.pid} JFR.start settings={self.temporary_settings_path} " f"duration={self.duration}s name={self.RECORD_NAME} filename={jfr_path}'") @@ -177,6 +178,9 @@ class FlightRecordsFactory: target.jfr_path = jfr_path self.jfr_paths.append(jfr_path) self.pids_to_start_recording.append(target) + else: + if target.container: + target.container.delete_in_container(self.tmp_dir) # 移动到data目录下 with open(file=os.path.join(self.root_path, "config/upload_sample.ini"), mode="w", encoding="utf-8") as file: file.write(os.linesep.join(self.jfr_paths)) @@ -198,27 +202,35 @@ class FlightRecordsFactory: target.jfr_path = jfr_path self.jfr_paths.append(jfr_path) self.pids_to_start_recording.append(target) + else: + if target.container: + target.container.delete_in_container(self.tmp_dir) # 移动到data目录下 with open(file=os.path.join(self.root_path, "config/upload_sample.ini"), mode="w", encoding="utf-8") as file: file.write(os.linesep.join(self.jfr_paths)) def __stop_recorder_by_root(self): for target in self.pids_to_start_recording: - username = psutil.Process(int(target.pid)).username() - command = f"su - {username} -c '{self.jcmd_path} {target.pid} JFR.stop name={self.RECORD_NAME}'" + if not os.path.exists(os.path.join("/proc", target.pid)): + continue + command = f"su - {target.username} -c '{self.jcmd_path} {target.pid} JFR.stop name={self.RECORD_NAME}'" outcome = shell_tools.exec_shell(command, is_shell=True) logging.info(outcome) def __stop_recorder(self): for target in self.pids_to_start_recording: + if not os.path.exists(os.path.join("/proc", target.pid)): + continue outcome = shell_tools.exec_shell("jcmd {} JFR.stop name={}".format(target.pid, self.RECORD_NAME), is_shell=True) logging.info(outcome) def __check_has_stopped_recorder_by_root(self): for target in self.pids_to_start_recording: - username = psutil.Process(int(target.pid)).username() - command = f"su - {username} -c '{self.jcmd_path} {target.pid} JFR.check name={self.RECORD_NAME}'" + if not os.path.exists(os.path.join("/proc", target.pid)): + self.pids_to_stop_recording.append(target) + continue + command = f"su - {target.username} -c '{self.jcmd_path} {target.pid} JFR.check name={self.RECORD_NAME}'" outcome = shell_tools.exec_shell(command, is_shell=True) logging.info(outcome) if outcome.out.find("Could not find"): @@ -231,6 +243,9 @@ class FlightRecordsFactory: def __check_has_stopped_recorder(self): for target in self.pids_to_start_recording: + if not os.path.exists(os.path.join("/proc", target.pid)): + self.pids_to_stop_recording.append(target) + continue outcome = shell_tools.exec_shell("jcmd {} JFR.check name={}".format(target.pid, self.RECORD_NAME), is_shell=True) logging.info(outcome) @@ -280,6 +295,7 @@ def main(): parser.set_defaults(root_path=PyInstallerUtils.obtain_root_path(ROOT_PATH)) args = parser.parse_args() config_log_ini(args.root_path, "devkit_tester_agent") + logging.info(PyInstallerUtils.get_env()) logging.info("start") factory = FlightRecordsFactory(args.applications, args.duration, args.root_path, args.waiting, args.task_id) factory.start_sample() -- Gitee