forked from mtusi2026/mtusi2026lab3
Лабораторная 3: реализован стенд с балансировщиком и идемпотентностью
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Scale the lab API topology and apply a fixed aggregate CPU/RAM budget."""
|
||||
import argparse
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SERVICES = ("api-1", "api-2", "api-3", "api-4")
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.run(
|
||||
["docker", "compose", *args],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
).stdout
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("count", choices=(1, 2, 4), type=int)
|
||||
parser.add_argument("--total-cpus", type=float, default=2.0)
|
||||
parser.add_argument("--total-memory-mb", type=int, default=1024)
|
||||
args = parser.parse_args()
|
||||
|
||||
if shutil.which("docker") is None:
|
||||
print("Docker CLI is unavailable", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
active = set(SERVICES[: args.count])
|
||||
for service in active:
|
||||
compose("start", service)
|
||||
for service in set(SERVICES) - active:
|
||||
compose("stop", "--timeout", "5", service)
|
||||
|
||||
for service in active:
|
||||
container_id = compose("ps", "-q", service).strip()
|
||||
if not container_id:
|
||||
print(f"{service} is not running", file=sys.stderr)
|
||||
return 1
|
||||
cpus = args.total_cpus / args.count
|
||||
memory_mb = max(128, args.total_memory_mb // args.count)
|
||||
subprocess.run(
|
||||
[
|
||||
"docker", "update",
|
||||
"--cpus", str(cpus),
|
||||
"--memory", f"{memory_mb}m",
|
||||
"--memory-swap", f"{memory_mb}m",
|
||||
container_id,
|
||||
],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
print(f"active replicas={args.count}; total CPU={args.total_cpus}; RAM={args.total_memory_mb}MB")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user