| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import socket |
| import select |
| import json |
|
|
| import public |
| from btdockerModel.dockerSock.sockBase import base |
|
|
|
|
| class dockerContainer(base): |
| def __init__(self): |
| super(dockerContainer, self).__init__() |
|
|
| |
| def get_container(self): |
| ''' |
| @name 获取所有容器列表 |
| @author wzz <2024/3/13 上午 10:54> |
| @param "data":{"参数名":""} <数据类型> 参数描述 |
| @return dict{"status":True/False,"msg":"提示信息"} |
| ''' |
| try: |
| return json.loads(public.ExecShell("curl -s --unix-socket {} http:/{}/containers/json?all=1".format(self._sock, self.get_api_version()))[0]) |
| except Exception as e: |
| try: |
| c_list = public.ExecShell("whereis curl | awk 'print {$1}'")[0].split(" ") |
| for c in c_list: |
| if not c.endswith("/curl"): continue |
| res, err = public.ExecShell("{} -s --unix-socket {} http:/{}/containers/json?all=1".format(c, self._sock, self.get_api_version())) |
| if not err: return json.loads(res) |
| return [] |
| except: |
| return [] |
|
|
| |
| def get_container_inspect(self, container_id: str): |
| ''' |
| @name 获取指定容器的inspect |
| @param container_id: 容器id |
| @return dict{"status":True/False,"msg":"提示信息"} |
| ''' |
| try: |
| return json.loads(public.ExecShell("curl -s --unix-socket {} http:/{}/containers/{}/json".format(self._sock, self.get_api_version(), container_id))[0]) |
| except Exception as e: |
| try: |
| c_list = public.ExecShell("whereis curl | awk 'print {$1}'")[0].split(" ") |
| for c in c_list: |
| if not c.endswith("/curl"): continue |
| res, err = public.ExecShell("{} -s --unix-socket {} http:/{}/containers/{}/json".format(c, self._sock, self.get_api_version(), container_id)) |
| if not err: return json.loads(res) |
| return [] |
| except: |
| return [] |
|
|
| |
| def structure_docker_logs(self, log_data: str) -> list: |
| """ |
| @name: 清理 Docker 日志并转换为 JSON 格式 |
| @param: raw_logs: 原始日志字符串 |
| @return: 日志对象列表 |
| """ |
| formatted_logs = [] |
|
|
| |
| for line in log_data.split('\n'): |
| line = line.strip() |
|
|
| if line: |
| |
| parts = line.split(' ', 1) |
| if len(parts) > 1: |
| timestamp = parts[0] |
| message = parts[1] |
| formatted_logs.append(message) |
|
|
| |
| return "\n".join(formatted_logs) |
|
|
| |
| def get_container_logs(self, container_id: str, options: dict) -> list: |
| ''' |
| @name 获取容器日志 |
| @param container_id: 容器id |
| @return dict{"status":True/False,"msg":"提示信息"} |
| ''' |
| |
| url = f"/containers/{container_id}/logs?stdout=true&stderr=true×tamps=true" |
|
|
| if options: |
| if "since" in options and options["since"] != "": |
| url += "&since={}".format(options['since']) |
| if "until" in options and options["until"] != "": |
| url += "&until={}".format(options['until']) |
| if "tail" in options and options["tail"] != "": |
| url += "&tail={}".format(options['tail']) |
|
|
| |
| unix_socket = "/var/run/docker.sock" |
|
|
| |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket: |
| client_socket.settimeout(1) |
| client_socket.connect(unix_socket) |
|
|
| |
| http_request = f"GET {url} HTTP/1.1\r\n" |
| http_request += "Host: localhost\r\n" |
| http_request += "Accept: application/json\r\n" |
| http_request += "Content-Type: application/json\r\n" |
| http_request += "Connection: close\r\n\r\n" |
|
|
| |
| client_socket.sendall(http_request.encode()) |
|
|
| |
| response = b"" |
| while True: |
| data = client_socket.recv(4096) |
| if not data: |
| break |
| response += data |
|
|
| |
| headers, body = response.split(b"\r\n\r\n", 1) |
|
|
| |
| log_data = body.decode('utf-8', errors='replace') |
|
|
| result = self.structure_docker_logs(log_data) |
|
|
| |
| return result |
|
|