forked from mtusi2026/mtusi2026lab3
57 lines
2.3 KiB
Python
57 lines
2.3 KiB
Python
import json
|
|
|
|
from rolling_update import COMPOSE, ROOT, command, configured_replicas
|
|
|
|
|
|
def main():
|
|
records = []
|
|
for service in ("db", "redis", "migrate", "api", "haproxy", "frontend"):
|
|
ids = command(COMPOSE + ["ps", "--all", "-q", service]).split()
|
|
assert ids, "missing service: " + service
|
|
for container in ids:
|
|
info = json.loads(
|
|
command(
|
|
[
|
|
"docker",
|
|
"inspect",
|
|
"--format",
|
|
'{"hostname":{{json .Config.Hostname}},"cpu":{{.HostConfig.NanoCpus}},"memory":{{.HostConfig.Memory}},"ports":{{json .HostConfig.PortBindings}},"networks":{{json .NetworkSettings.Networks}},"mounts":{{json .Mounts}}}',
|
|
container,
|
|
]
|
|
)
|
|
)
|
|
assert info["cpu"] > 0 and info["memory"] > 0, "missing effective limits"
|
|
networks = set(info["networks"])
|
|
if service in ("api", "db", "redis"):
|
|
assert not info["ports"], "unexpected public storage/API port"
|
|
if service == "frontend":
|
|
assert networks == {"highload-lab3_edge"}
|
|
if service == "haproxy":
|
|
assert "highload-lab3_data" not in networks
|
|
if service == "api":
|
|
assert all(not mount["RW"] and mount["Type"] == "bind"
|
|
for mount in info["mounts"]), "API mounts must be read-only configuration"
|
|
records.append(
|
|
{
|
|
"service": service,
|
|
"instance": info["hostname"],
|
|
"cpu_limit": info["cpu"] / 1e9,
|
|
"memory_mib": info["memory"] // (1024 * 1024),
|
|
"networks": sorted(networks),
|
|
"public_ports": info["ports"],
|
|
}
|
|
)
|
|
assert len([row for row in records if row["service"] == "api"]) == configured_replicas()
|
|
ROOT.joinpath(".memory/grade4").mkdir(parents=True, exist_ok=True)
|
|
ROOT.joinpath(".memory/grade4/resources.json").write_text(
|
|
json.dumps(records, indent=2) + "\n"
|
|
)
|
|
print(json.dumps(records, indent=2))
|
|
print(
|
|
"PASS: effective CPU/RAM limits, configured API replicas, private storage/API, isolated frontend"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|