ЛР6: наблюдаемость, SLO и диагностика отказов
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import hashlib
|
||||
import json
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPOSE = ["docker", "compose", "-f", "docker-compose.yml", "-f", "compose.local.yaml"]
|
||||
|
||||
|
||||
def cmd(*args):
|
||||
return subprocess.check_output(args, cwd=ROOT, text=True, encoding="utf-8").strip()
|
||||
|
||||
|
||||
def save(name, value):
|
||||
(ROOT / "evidence" / name).write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
def containers():
|
||||
ids = cmd(*COMPOSE, "ps", "-q").splitlines()
|
||||
data = json.loads(cmd("docker", "inspect", *ids))
|
||||
return [
|
||||
{
|
||||
"service": row["Config"]["Labels"]["com.docker.compose.service"],
|
||||
"image": row["Config"]["Image"],
|
||||
"image_id": row["Image"],
|
||||
"started_at": row["State"]["StartedAt"],
|
||||
"health": row["State"].get("Health", {}).get("Status", row["State"]["Status"]),
|
||||
"memory_limit_bytes": row["HostConfig"]["Memory"],
|
||||
"nano_cpus": row["HostConfig"]["NanoCpus"],
|
||||
"volumes": [item["Name"] for item in row["Mounts"] if item["Type"] == "volume"],
|
||||
}
|
||||
for row in data
|
||||
]
|
||||
|
||||
|
||||
def verify_sources():
|
||||
protected = ["README.md", "COURSE.md", "contracts", "tests", "lab.json"]
|
||||
subprocess.run(
|
||||
["git", "diff", "--exit-code", "470d9d6", "--", *protected], cwd=ROOT, check=True
|
||||
)
|
||||
names = cmd(
|
||||
"git", "-c", "core.quotePath=false", "ls-files", "-c", "-o", "--exclude-standard"
|
||||
).splitlines()
|
||||
root_secrets = ROOT / "secrets"
|
||||
credentials = [
|
||||
path.read_bytes().strip()
|
||||
for path in root_secrets.glob("*.txt")
|
||||
if "password" in path.stem or "secret_access_key" in path.stem
|
||||
]
|
||||
assert all(len(value) > 12 for value in credentials), "expected generated secrets"
|
||||
source = {}
|
||||
for name in names:
|
||||
if not (ROOT / name).is_file():
|
||||
continue
|
||||
content = (ROOT / name).read_bytes()
|
||||
assert not any(
|
||||
secret in content for secret in credentials
|
||||
), "generated credential in Git-visible file"
|
||||
if name.startswith(
|
||||
("src/", "worker/", "infra/otel/", "student-tests/", "scripts/")
|
||||
) or name in (
|
||||
"docker-compose.yml",
|
||||
"Dockerfile",
|
||||
"compose.test.yaml",
|
||||
"compose.local.yaml",
|
||||
"build.gradle",
|
||||
):
|
||||
source[name] = hashlib.sha256(content).hexdigest()
|
||||
save("source-sha256.json", source)
|
||||
print("PASS public starter/contract intact, generated secrets absent from Git-visible files")
|
||||
|
||||
|
||||
def main():
|
||||
service_state = containers()
|
||||
assert (
|
||||
len(
|
||||
[row for row in service_state if row["service"] == "api" and row["health"] == "healthy"]
|
||||
)
|
||||
== 2
|
||||
)
|
||||
assert len([row for row in service_state if row["service"] == "worker"]) == 2
|
||||
save(
|
||||
"environment.json",
|
||||
{
|
||||
"timestamp": datetime.now().astimezone().isoformat(),
|
||||
"skeleton": "470d9d6",
|
||||
"transfer_source": "ef7e8c3",
|
||||
"engine": cmd(
|
||||
"docker",
|
||||
"info",
|
||||
"--format",
|
||||
"version={{.ServerVersion}} cpus={{.NCPU}} memory_bytes={{.MemTotal}}",
|
||||
),
|
||||
"services": service_state,
|
||||
},
|
||||
)
|
||||
save(
|
||||
"resources.json",
|
||||
[
|
||||
json.loads(line)
|
||||
for line in cmd(
|
||||
"docker",
|
||||
"stats",
|
||||
"--no-stream",
|
||||
"--format",
|
||||
"{{json .}}",
|
||||
*cmd(*COMPOSE, "ps", "-q").splitlines(),
|
||||
).splitlines()
|
||||
],
|
||||
)
|
||||
verify_sources()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user