feat(lab3): HAProxy LB, stateless API replicas, X-Instance-Id, Idempotency-Key, Grade 5 Track 1
This commit is contained in:
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
docker compose --env-file .env restart postgres redis api lb >/dev/null
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Helper script to populate synthetic demo data for Lab 3 defense in Swagger UI."""
|
||||
import base64
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
||||
|
||||
|
||||
def req(method, path, body=None, headers=None):
|
||||
h = {"Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
h["Content-Type"] = "application/json"
|
||||
if headers:
|
||||
h.update(headers)
|
||||
r = urllib.request.Request(BASE_URL + path, data=data, headers=h, method=method)
|
||||
with urllib.request.urlopen(r, timeout=10) as resp:
|
||||
raw = resp.read()
|
||||
inst = resp.headers.get("X-Instance-Id", "")
|
||||
return (json.loads(raw.decode("utf-8")) if raw else None), inst
|
||||
|
||||
|
||||
def main():
|
||||
suffix = uuid.uuid4().hex[:6]
|
||||
alice_username = f"alice_{suffix}"
|
||||
bob_username = f"bob_{suffix}"
|
||||
password = "DemoPassword-2026!"
|
||||
|
||||
alice, inst1 = req("POST", "/api/v1/users", {
|
||||
"username": alice_username,
|
||||
"display_name": "Alice Zamorskaya",
|
||||
"password": password,
|
||||
})
|
||||
bob, inst2 = req("POST", "/api/v1/users", {
|
||||
"username": bob_username,
|
||||
"display_name": "Bob Bobrov",
|
||||
"password": password,
|
||||
})
|
||||
|
||||
alice_basic = base64.b64encode(f"{alice_username}:{password}".encode("utf-8")).decode("ascii")
|
||||
bob_basic = base64.b64encode(f"{bob_username}:{password}".encode("utf-8")).decode("ascii")
|
||||
|
||||
alice_sess, _ = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {alice_basic}"})
|
||||
bob_sess, _ = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {bob_basic}"})
|
||||
|
||||
alice_bearer = {"Authorization": f"Bearer {alice_sess['token']}"}
|
||||
bob_bearer = {"Authorization": f"Bearer {bob_sess['token']}"}
|
||||
|
||||
chat, _ = req("POST", "/api/v1/chats", {
|
||||
"title": "Lab 3 Defense Chat",
|
||||
"member_ids": [alice["id"], bob["id"]],
|
||||
}, headers=alice_bearer)
|
||||
|
||||
_, inst3 = req("POST", f"/api/v1/chats/{chat['id']}/messages", {
|
||||
"text": "Hello Bob! Sent with Idempotency-Key through HAProxy.",
|
||||
}, headers={**alice_bearer, "Idempotency-Key": f"seed-msg-1-{suffix}"})
|
||||
|
||||
_, inst4 = req("POST", f"/api/v1/chats/{chat['id']}/messages", {
|
||||
"text": "Hi Alice! Served across balanced API replicas.",
|
||||
}, headers={**bob_bearer, "Idempotency-Key": f"seed-msg-2-{suffix}"})
|
||||
|
||||
print("=== LAB 3 DEMO DATA CREATED ===")
|
||||
print(f"Swagger UI: {BASE_URL}/docs")
|
||||
print(f"Observed IDs: {sorted({x for x in [inst1, inst2, inst3, inst4] if x})}")
|
||||
print(f"Password: {password}")
|
||||
print(f"Alice Username: {alice_username}")
|
||||
print(f"Alice User ID: {alice['id']}")
|
||||
print(f"Alice Token: {alice_sess['token']}")
|
||||
print(f"Bob Username: {bob_username}")
|
||||
print(f"Bob User ID: {bob['id']}")
|
||||
print(f"Bob Token: {bob_sess['token']}")
|
||||
print(f"Chat ID: {chat['id']}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
TARGET_CID=""
|
||||
for cid in $(docker compose --env-file .env ps -q api); do
|
||||
short_id=$(printf '%s' "$cid" | cut -c1-12)
|
||||
hostname_val=$(docker inspect -f '{{.Config.Hostname}}' "$cid" 2>/dev/null || true)
|
||||
if [ "$short_id" = "$TARGET_INSTANCE_ID" ] || [ "$cid" = "$TARGET_INSTANCE_ID" ] || [ "$hostname_val" = "$TARGET_INSTANCE_ID" ]; then
|
||||
TARGET_CID="$cid"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$TARGET_CID" ]; then
|
||||
printf 'API container matching X-Instance-Id=%s not found\n' "$TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker stop -t 5 "$TARGET_CID" >/dev/null
|
||||
Reference in New Issue
Block a user