|
| 1 | +import importlib.metadata |
| 2 | +import platform |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import docker |
| 7 | +import requests |
| 8 | +from setuptools_scm import get_version |
| 9 | + |
| 10 | +from rdagent.log import rdagent_logger as logger |
| 11 | + |
| 12 | + |
| 13 | +def sys_info(): |
| 14 | + """collect system related info""" |
| 15 | + method_list = [ |
| 16 | + ["Name of current operating system: ", "system"], |
| 17 | + ["Processor architecture: ", "machine"], |
| 18 | + ["System, version, and hardware information: ", "platform"], |
| 19 | + ["Version number of the system: ", "version"], |
| 20 | + ] |
| 21 | + for method in method_list: |
| 22 | + logger.info(f"{method[0]}{getattr(platform, method[1])()}") |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +def python_info(): |
| 27 | + """collect Python related info""" |
| 28 | + python_version = sys.version.replace("\n", " ") |
| 29 | + logger.info(f"Python version: {python_version}") |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +def docker_info(): |
| 34 | + client = docker.from_env() |
| 35 | + containers = client.containers.list(all=True) |
| 36 | + if containers: |
| 37 | + containers.sort(key=lambda c: c.attrs["Created"]) |
| 38 | + last_container = containers[-1] |
| 39 | + logger.info(f"Container ID: {last_container.id}") |
| 40 | + logger.info(f"Container Name: {last_container.name}") |
| 41 | + logger.info(f"Container Status: {last_container.status}") |
| 42 | + logger.info(f"Image ID used by the container: {last_container.image.id}") |
| 43 | + logger.info(f"Image tag used by the container: {last_container.image.tags}") |
| 44 | + logger.info(f"Container port mapping: {last_container.ports}") |
| 45 | + logger.info(f"Container Label: {last_container.labels}") |
| 46 | + logger.info(f"Startup Commands: {' '.join(client.containers.get(last_container.id).attrs['Config']['Cmd'])}") |
| 47 | + else: |
| 48 | + logger.info(f"No run containers.") |
| 49 | + |
| 50 | + |
| 51 | +def rdagent_info(): |
| 52 | + """collect rdagent related info""" |
| 53 | + current_version = importlib.metadata.version("rdagent") |
| 54 | + logger.info(f"RD-Agent version: {current_version}") |
| 55 | + api_url = f"https://api.github.com/repos/microsoft/RD-Agent/contents/requirements.txt?ref=main" |
| 56 | + response = requests.get(api_url) |
| 57 | + if response.status_code == 200: |
| 58 | + files = response.json() |
| 59 | + file_url = files["download_url"] |
| 60 | + file_response = requests.get(file_url) |
| 61 | + if file_response.status_code == 200: |
| 62 | + all_file_contents = file_response.text.split("\n") |
| 63 | + else: |
| 64 | + logger.warning(f"Failed to retrieve {files['name']}, status code: {file_response.status_code}") |
| 65 | + else: |
| 66 | + logger.warning(f"Failed to retrieve files in folder, status code: {response.status_code}") |
| 67 | + package_list = [ |
| 68 | + item.split("#")[0].strip() for item in all_file_contents if item.strip() and not item.startswith("#") |
| 69 | + ] |
| 70 | + package_version_list = [] |
| 71 | + for package in package_list: |
| 72 | + if package == "typer[all]": |
| 73 | + package = "typer" |
| 74 | + version = importlib.metadata.version(package) |
| 75 | + package_version_list.append(f"{package}=={version}") |
| 76 | + logger.info(f"Package version: {package_version_list}") |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +def collect_info(): |
| 81 | + """Prints information about the system and the installed packages.""" |
| 82 | + sys_info() |
| 83 | + python_info() |
| 84 | + docker_info() |
| 85 | + rdagent_info() |
| 86 | + return None |
0 commit comments