forked from mtusi2026/mtusi2026lab3
готово
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"""Compose rolling replacement с одной временной репликой и ожиданием LB."""
|
||||
|
||||
import json
|
||||
import time
|
||||
|
||||
from haproxy import drain, release
|
||||
from replicas import compose, containers, describe, docker, wait_ready, wait_visible
|
||||
|
||||
old = [c for c in containers() if describe(c)["Running"]]
|
||||
if len(old) < 2 or len(old) != len(containers()):
|
||||
raise SystemExit("Restore all API replicas first; need >=2")
|
||||
count = len(old)
|
||||
events = []
|
||||
# Build separately before starting this script. --no-recreate keeps old instances
|
||||
# alive even when the image has changed. Each new container uses the current image.
|
||||
for index, previous in enumerate(old):
|
||||
before = set(containers())
|
||||
compose("up", "-d", "--no-deps", "--no-recreate", "--scale", f"api={count + 1}", "api")
|
||||
added = set(containers()) - before
|
||||
assert len(added) == 1, "Expected one surge replica"
|
||||
replacement = added.pop()
|
||||
new_instance = wait_ready(replacement)
|
||||
wait_visible(new_instance)
|
||||
drained_slots = drain(previous)
|
||||
started = time.monotonic()
|
||||
docker("stop", "--time", "10", previous)
|
||||
state = describe(previous)
|
||||
assert state["ExitCode"] == 0, "API did not exit gracefully"
|
||||
docker("rm", previous)
|
||||
release(drained_slots)
|
||||
event = {
|
||||
"old_container": previous[:12],
|
||||
"new_container": replacement[:12],
|
||||
"new_instance": new_instance,
|
||||
"old_exit_code": state["ExitCode"],
|
||||
"drained_slots": drained_slots,
|
||||
"stop_seconds": round(time.monotonic() - started, 3),
|
||||
}
|
||||
events.append(event)
|
||||
assert len(containers()) == count
|
||||
print(
|
||||
json.dumps({"mode": "rolling replacement, one surge, same schema", "events": events}, indent=2)
|
||||
)
|
||||
Reference in New Issue
Block a user