118 lines
5.5 KiB
Python
118 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Собирает текстовый evidence об инфраструктуре без секретов.
|
|
|
|
Все команды выполняются локальным Docker CLI; stdout/stderr сохраняются как
|
|
есть. Секреты в вывод не попадают: печатаются только имена Docker secrets и
|
|
пути внутри контейнеров, но не их значения.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OUT = ROOT / "evidence" / "12-infrastructure.txt"
|
|
STACK = os.environ.get("STACK_NAME", "lab4")
|
|
IMAGE = os.environ.get("API_IMAGE", "mtusi-messenger-lab4:4.0.0")
|
|
MINIO = "minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e"
|
|
|
|
|
|
def run(*args: str) -> tuple[int, str]:
|
|
result = subprocess.run(["docker", *args], capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
text = result.stdout
|
|
if result.stderr:
|
|
text += "$ stderr\n" + result.stderr
|
|
return result.returncode, text
|
|
|
|
|
|
def main() -> int:
|
|
lines: list[str] = []
|
|
lines.append("# Infrastructure and image pinning")
|
|
lines.append("host=" + platform.node() + " platform=" + platform.platform())
|
|
lines.append("python=" + sys.version.split()[0])
|
|
lines.append("")
|
|
for label, args in [
|
|
("docker version", ("version", "--format", "client={{.Client.Version}} server={{.Server.Version}}")),
|
|
("docker info", ("info", "--format", "swarm={{.Swarm.LocalNodeState}} cpus={{.NCPU}} mem={{.MemTotal}}")),
|
|
("stack services", ("stack", "services", STACK)),
|
|
(
|
|
"api service",
|
|
(
|
|
"service", "inspect", STACK + "_api", "--format",
|
|
"image={{.Spec.TaskTemplate.ContainerSpec.Image}} ports={{json .Endpoint.Ports}} "
|
|
"limits={{json .Spec.TaskTemplate.Resources.Limits}} "
|
|
"reservations={{json .Spec.TaskTemplate.Resources.Reservations}} "
|
|
"update={{json .Spec.UpdateConfig}}",
|
|
),
|
|
),
|
|
(
|
|
"minio service",
|
|
(
|
|
"service", "inspect", STACK + "_minio", "--format",
|
|
"image={{.Spec.TaskTemplate.ContainerSpec.Image}} ports={{json .Endpoint.Ports}} "
|
|
"limits={{json .Spec.TaskTemplate.Resources.Limits}} "
|
|
"mounts={{json .Spec.TaskTemplate.ContainerSpec.Mounts}}",
|
|
),
|
|
),
|
|
("traefik ports", ("service", "inspect", STACK + "_traefik", "--format", "ports={{json .Endpoint.Ports}}")),
|
|
("volumes", ("volume", "ls", "--filter", "name=" + STACK, "--format", "{{.Name}} {{.Driver}}")),
|
|
("secrets (names only)", ("secret", "ls", "--filter", "name=" + STACK, "--format", "{{.Name}}")),
|
|
("configs (names only)", ("config", "ls", "--filter", "name=" + STACK, "--format", "{{.Name}}")),
|
|
("api secret mounts", ("service", "inspect", STACK + "_api", "--format", "{{json .Spec.TaskTemplate.ContainerSpec.Secrets}}")),
|
|
("minio-init secret mounts", ("service", "inspect", STACK + "_minio-init", "--format", "{{json .Spec.TaskTemplate.ContainerSpec.Secrets}}")),
|
|
("migrate log", ("service", "logs", STACK + "_migrate", "--tail", "20")),
|
|
("minio-init log", ("service", "logs", STACK + "_minio-init", "--tail", "20")),
|
|
]:
|
|
lines.append("## " + label)
|
|
code, text = run(*args)
|
|
lines.append("exit=" + str(code))
|
|
lines.append(text.rstrip())
|
|
lines.append("")
|
|
|
|
lines.append("## other service ports (must be null)")
|
|
for name in ("postgres", "valkey", "minio-init", "migrate"):
|
|
code, text = run("service", "inspect", STACK + "_" + name, "--format", "{{.Spec.Name}} ports={{json .Endpoint.Ports}}")
|
|
lines.append("exit=" + str(code) + " " + text.strip())
|
|
lines.append("")
|
|
|
|
lines.append("## image digests")
|
|
for image in (
|
|
IMAGE,
|
|
MINIO,
|
|
"postgres@sha256:0ea6700a3b4f0ae6ce746519073558aed4d88a79d8d07622a9a644946c7319c4",
|
|
"valkey/valkey@sha256:4436c94fc34ce4af0354b9379d433a1f258998fb955f995c89822f98c14a8cab",
|
|
"traefik:v3.5.4",
|
|
"python:3.11.9-slim-bookworm",
|
|
):
|
|
code, text = run("image", "inspect", image, "--format", "{{.RepoTags}} id={{.Id}} digests={{json .RepoDigests}}")
|
|
lines.append("exit=" + str(code) + " " + text.strip())
|
|
lines.append("")
|
|
|
|
lines.append("## alembic current / check")
|
|
secrets = str(ROOT / "secrets")
|
|
for name, args in (
|
|
("current", ("-d", "--name", "alembic-current", "--network", STACK + "_backend", "--user", "0",
|
|
"-v", secrets + ":/run/secrets:ro", "-e", "DATABASE_URL_FILE=/run/secrets/database_url",
|
|
"--entrypoint", "alembic", IMAGE, "current")),
|
|
("check", ("-d", "--name", "alembic-check", "--network", STACK + "_backend", "--user", "0",
|
|
"-v", secrets + ":/run/secrets:ro", "-e", "DATABASE_URL_FILE=/run/secrets/database_url",
|
|
"--entrypoint", "alembic", IMAGE, "check")),
|
|
):
|
|
run("run", *args)
|
|
run("wait", "alembic-" + name)
|
|
code, text = run("logs", "alembic-" + name)
|
|
lines.append("-- " + name + " exit=" + str(code))
|
|
lines.append(text.rstrip())
|
|
run("rm", "-f", "alembic-" + name)
|
|
|
|
OUT.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
print("wrote", OUT)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|