53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import docker
|
|
|
|
client = docker.from_env()
|
|
running_containers = []
|
|
# sleeping_containers = []
|
|
for c in client.containers.list(all=True):
|
|
info = c.attrs # словарь со всеми данными
|
|
image = info["Config"]["Image"] # образ
|
|
created = info["Created"] # время создания
|
|
labels = info["Config"].get("Labels", {}) # лейблы
|
|
ports = info["NetworkSettings"]["Ports"] # проброшенные порты ЪЪЪ
|
|
mounts = info.get("Mounts", []) # маунты
|
|
status = c.status # статус контейнера (вкл-выкл)
|
|
if tags := c.image.tags:
|
|
ver = c.image.tags[0].split(':')[-1] # версия
|
|
else: ver = None
|
|
workdir = info["Config"].get("WorkingDir") or '/' # рабочая директория
|
|
find_cmd = (
|
|
f"sh -c \"cd {workdir} 2>/dev/null && "
|
|
"find . -maxdepth 3 \\( "
|
|
"-name 'requirements*.txt' -o "
|
|
"-name 'Pipfile' -o "
|
|
"-name 'pyproject.toml' -o "
|
|
"-name 'package.json' "
|
|
"\\) 2>/dev/null | head -n 5\""
|
|
)
|
|
req = c.exec_run(find_cmd)[1].decode() # путь до файла с зависимостями
|
|
req_paths = [line.strip() for line in req.splitlines() if line.strip()]
|
|
|
|
language = None
|
|
for p in req_paths:
|
|
if "requirements" in p.lower() or p.endswith("Pipfile") or p.endswith("pyproject.toml"):
|
|
language = "python"
|
|
break
|
|
if p.endswith("package.json"):
|
|
language = "nodejs"
|
|
break
|
|
|
|
|
|
container = {
|
|
"name": c.name,
|
|
"version": ver,
|
|
"image": image,
|
|
"id": c.id[:12],
|
|
"create_time": created,
|
|
"mounted_data": mounts,
|
|
"labels": labels
|
|
}
|
|
if status == "running":
|
|
running_containers.append(container)
|
|
# else:
|
|
# sleeping_containers.append(container)
|