forked from mtusi2026/mtusi2026lab3
Implement lab 3 replica failover and shared idempotency for grade 4
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
"""Generate local-only synthetic credentials once; never overwrite them."""
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
os.umask(0o077)
|
||||
(root / "secrets").mkdir(exist_ok=True)
|
||||
p = root / "secrets/postgres_password"
|
||||
if not p.exists():
|
||||
p.write_text(secrets.token_hex(32))
|
||||
values = {"database_url": "postgresql://messenger:" + p.read_text().strip() + "@postgres:5432/messenger", "cache_url": "redis://valkey:6379/0"}
|
||||
for name, value in values.items():
|
||||
dest = root / "secrets" / name
|
||||
if not dest.exists():
|
||||
dest.write_text(value)
|
||||
# Docker Desktop bind-mounted secrets need read permission for container UID 10001.
|
||||
for name in ["postgres_password", "database_url", "cache_url"]:
|
||||
(root / "secrets" / name).chmod(0o644)
|
||||
env = root / ".env"
|
||||
if not env.exists():
|
||||
env.write_text("SESSION_TTL_SECONDS=3600\nHOST_PORT=8080\n")
|
||||
print("Local configuration ready (secrets not printed)")
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose restart postgres valkey api lb
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Replace each original API container, keeping its peer available."""
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
import os
|
||||
os.chdir(Path(__file__).resolve().parents[1])
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(args, text=True)
|
||||
|
||||
def ready():
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
ids = command('docker','compose','ps','-q','api').split()
|
||||
states = json.loads(command('docker','inspect',*ids)) if ids else []
|
||||
if len(states) == 2 and all(x['State'].get('Health',{}).get('Status')=='healthy' for x in states):
|
||||
time.sleep(3) # allow HAProxy readiness/DNS to converge
|
||||
return
|
||||
time.sleep(.5)
|
||||
raise RuntimeError('API did not become healthy')
|
||||
|
||||
if __name__ == '__main__':
|
||||
ready()
|
||||
old = command('docker','compose','ps','-q','api').split()
|
||||
for container in old:
|
||||
command('docker','stop','-t','8',container)
|
||||
command('docker','rm',container)
|
||||
command('docker','compose','up','-d','--no-deps','--no-build','--scale','api=2','api')
|
||||
ready()
|
||||
new = command('docker','compose','ps','-q','api').split()
|
||||
assert not set(old) & set(new)
|
||||
print(json.dumps({'old_containers':old,'new_containers':new}))
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID to an API hostname}"
|
||||
for id in $(docker compose ps -q api); do
|
||||
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
docker stop -t 8 "$id"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
echo 'Target is not a running API replica in this project' >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user