forked from mtusi2026/mtusi2026lab6
solution
This commit is contained in:
+258
@@ -0,0 +1,258 @@
|
||||
"""Запускает проверки в одноразовом Docker-окружении и сохраняет evidence."""
|
||||
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
from uuid import uuid4
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EXCLUDED = {
|
||||
".git",
|
||||
".agents",
|
||||
".aws",
|
||||
".codex",
|
||||
".env",
|
||||
"secrets",
|
||||
"evidence",
|
||||
".venv",
|
||||
"venv",
|
||||
"__pycache__",
|
||||
".pytest_cache",
|
||||
".github",
|
||||
"docs",
|
||||
}
|
||||
TEXT_SUFFIXES = {".py", ".sh", ".conf", ".yaml", ".yml", ".json", ".txt", ".md", ".ini"}
|
||||
|
||||
|
||||
def docker(*args, **kwargs):
|
||||
kwargs.setdefault("check", True)
|
||||
return subprocess.run(["docker", *args], cwd=ROOT, **kwargs)
|
||||
|
||||
|
||||
def archive_source(destination):
|
||||
with tarfile.open(destination, "w") as archive:
|
||||
for path in sorted(ROOT.rglob("*")):
|
||||
relative = path.relative_to(ROOT)
|
||||
if (
|
||||
not path.is_file()
|
||||
or path.is_symlink()
|
||||
or any(part in EXCLUDED for part in relative.parts)
|
||||
or path.name.startswith(".env.")
|
||||
and path.name != ".env.example"
|
||||
or path.suffix in {".zip", ".log", ".pem", ".key"}
|
||||
):
|
||||
continue
|
||||
content = path.read_bytes()
|
||||
if path.suffix in TEXT_SUFFIXES:
|
||||
content = content.replace(b"\r\n", b"\n")
|
||||
entry = tarfile.TarInfo(relative.as_posix())
|
||||
entry.size = len(content)
|
||||
entry.mode = 0o755 if path.suffix == ".sh" else 0o644
|
||||
archive.addfile(entry, io.BytesIO(content))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
mode = parser.add_mutually_exclusive_group()
|
||||
mode.add_argument("--format", action="store_true", help="применить Black и выйти")
|
||||
mode.add_argument(
|
||||
"--integration-only",
|
||||
action="store_true",
|
||||
help="запустить проверки стенда и телеметрии без pytest",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
project = "messenger-check-" + uuid4().hex[:12]
|
||||
image = project + ":tools"
|
||||
image_env = {
|
||||
"API_IMAGE": project + ":api",
|
||||
"MINIO_IMAGE": project + ":server",
|
||||
"MINIO_BOOTSTRAP_IMAGE": project + ":bootstrap",
|
||||
}
|
||||
engine = project + "-engine"
|
||||
runner = project + "-runner"
|
||||
workspace = project + "-workspace"
|
||||
data = project + "-data"
|
||||
network = project + "-network"
|
||||
output = ROOT / "evidence" / "lab6"
|
||||
if args.integration_only:
|
||||
output /= "integration"
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
summary = {
|
||||
"status": "failed",
|
||||
"project": project,
|
||||
"profile": "integration" if args.integration_only else "full",
|
||||
"started_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
try:
|
||||
docker("build", "--tag", image, "--file", "infra/testing/Dockerfile", ".")
|
||||
if args.format:
|
||||
docker(
|
||||
"run",
|
||||
"--rm",
|
||||
"--volume",
|
||||
str(ROOT) + ":/workspace",
|
||||
image,
|
||||
"python3",
|
||||
"-m",
|
||||
"black",
|
||||
"app",
|
||||
"infra",
|
||||
"migrations",
|
||||
"scripts",
|
||||
"student-tests",
|
||||
"tests",
|
||||
)
|
||||
summary["status"] = "formatted"
|
||||
return
|
||||
compose = ("compose", "--env-file", ".env.example")
|
||||
env = {**os.environ, **image_env}
|
||||
config = json.loads(
|
||||
docker(
|
||||
*compose, "config", "--format", "json", env=env, capture_output=True
|
||||
).stdout
|
||||
)
|
||||
dependencies = sorted(
|
||||
{
|
||||
service["image"]
|
||||
for service in config["services"].values()
|
||||
if "build" not in service
|
||||
}
|
||||
)
|
||||
docker(
|
||||
*compose,
|
||||
"pull",
|
||||
"--ignore-buildable",
|
||||
"--policy",
|
||||
"missing",
|
||||
env=env,
|
||||
timeout=3600,
|
||||
)
|
||||
docker(
|
||||
*compose, "build", "api", "minio", "minio-bootstrap", env=env, timeout=3600
|
||||
)
|
||||
docker("network", "create", network, stdout=subprocess.DEVNULL)
|
||||
for volume in (workspace, data):
|
||||
docker("volume", "create", volume, stdout=subprocess.DEVNULL)
|
||||
docker(
|
||||
"run",
|
||||
"-d",
|
||||
"--privileged",
|
||||
"--name",
|
||||
engine,
|
||||
"--network",
|
||||
network,
|
||||
"--env",
|
||||
"DOCKER_TLS_CERTDIR=",
|
||||
"--volume",
|
||||
workspace + ":/workspace",
|
||||
"--volume",
|
||||
data + ":/var/lib/docker",
|
||||
"docker:29.1.3-dind",
|
||||
"dockerd",
|
||||
"--host=unix:///var/run/docker.sock",
|
||||
"--host=tcp://127.0.0.1:2375",
|
||||
"--tls=false",
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
docker(
|
||||
"create",
|
||||
"--name",
|
||||
runner,
|
||||
"--network",
|
||||
"container:" + engine,
|
||||
"--env",
|
||||
"DOCKER_HOST=tcp://127.0.0.1:2375",
|
||||
*[
|
||||
arg
|
||||
for key, value in image_env.items()
|
||||
for arg in ("--env", f"{key}={value}")
|
||||
],
|
||||
"--env",
|
||||
f"INTEGRATION_ONLY={int(args.integration_only)}",
|
||||
"--volume",
|
||||
workspace + ":/workspace",
|
||||
image,
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="messenger-check-") as directory:
|
||||
archive = Path(directory) / "source.tar"
|
||||
archive_source(archive)
|
||||
docker("cp", str(archive), runner + ":/tmp/source.tar")
|
||||
image_archive = Path(directory) / "images.tar"
|
||||
docker(
|
||||
"image",
|
||||
"save",
|
||||
"--output",
|
||||
str(image_archive),
|
||||
*image_env.values(),
|
||||
*dependencies,
|
||||
)
|
||||
docker("cp", str(image_archive), runner + ":/tmp/images.tar")
|
||||
docker("start", "--attach", runner, check=False, timeout=7200)
|
||||
result = subprocess.run(
|
||||
["docker", "wait", runner],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
capture_output=True,
|
||||
timeout=7200,
|
||||
)
|
||||
result.check_returncode()
|
||||
docker("cp", runner + ":/workspace/evidence/.", str(output))
|
||||
if int(result.stdout.strip()) != 0:
|
||||
raise RuntimeError(
|
||||
f"Проверки завершились ошибкой. См. {output.relative_to(ROOT)}."
|
||||
)
|
||||
summary["status"] = "passed"
|
||||
finally:
|
||||
cleanup = []
|
||||
for command in (
|
||||
["rm", "--force", runner, engine],
|
||||
["volume", "rm", workspace, data],
|
||||
["network", "rm", network],
|
||||
["image", "rm", image, *image_env.values()],
|
||||
):
|
||||
result = subprocess.run(
|
||||
["docker", *command], cwd=ROOT, encoding="utf-8", capture_output=True
|
||||
)
|
||||
cleanup.append(
|
||||
{
|
||||
"command": ["docker", *command],
|
||||
"returncode": result.returncode,
|
||||
"output": result.stdout.strip() + result.stderr.strip(),
|
||||
}
|
||||
)
|
||||
remaining = {}
|
||||
for kind in ("container", "volume", "network", "image"):
|
||||
result = subprocess.run(
|
||||
["docker", kind, "ls", "--format", "{{json .}}"],
|
||||
cwd=ROOT,
|
||||
encoding="utf-8",
|
||||
capture_output=True,
|
||||
)
|
||||
remaining[kind] = [
|
||||
line for line in result.stdout.splitlines() if project in line
|
||||
]
|
||||
if result.returncode:
|
||||
remaining[kind].append("resource audit failed")
|
||||
summary["cleanup"] = cleanup
|
||||
summary["remaining_resources"] = remaining
|
||||
summary["finished_at"] = datetime.now(timezone.utc).isoformat()
|
||||
if any(remaining.values()):
|
||||
summary["status"] = "failed"
|
||||
(output / "run.json").write_text(
|
||||
json.dumps(summary, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
|
||||
)
|
||||
if summary["status"] == "failed":
|
||||
raise SystemExit(1)
|
||||
print("PASS: проверки завершены, временное окружение удалено")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user