Lab2
This commit is contained in:
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
# Creates local Docker secrets for a synthetic lab stand.
|
||||
# Existing files are never overwritten; values are never printed.
|
||||
set -eu
|
||||
|
||||
dir="$(cd "$(dirname "$0")/.." && pwd)/infra/secrets"
|
||||
mkdir -p "$dir"
|
||||
|
||||
random() {
|
||||
od -An -tx1 -N24 /dev/urandom | tr -d ' \n'
|
||||
}
|
||||
|
||||
write_new() {
|
||||
if [ -f "$dir/$1" ]; then
|
||||
echo "keep infra/secrets/$1"
|
||||
else
|
||||
printf '%s' "$2" > "$dir/$1"
|
||||
chmod 600 "$dir/$1" 2>/dev/null || true
|
||||
echo "create infra/secrets/$1"
|
||||
fi
|
||||
}
|
||||
|
||||
if [ ! -f "$dir/postgres_password.txt" ]; then
|
||||
write_new postgres_password.txt "$(random)"
|
||||
fi
|
||||
pg_password="$(cat "$dir/postgres_password.txt")"
|
||||
write_new database_url.txt "postgresql://messenger:${pg_password}@db:5432/messenger"
|
||||
|
||||
if [ ! -f "$dir/valkey_password.txt" ]; then
|
||||
write_new valkey_password.txt "$(random)"
|
||||
fi
|
||||
valkey_password="$(cat "$dir/valkey_password.txt")"
|
||||
write_new cache_url.txt "redis://:${valkey_password}@valkey:6379/0"
|
||||
@@ -0,0 +1,79 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import asyncpg
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
MIGRATIONS_DIR = ROOT / "migrations"
|
||||
|
||||
|
||||
def read_database_url() -> str:
|
||||
file_name = os.getenv("DATABASE_URL_FILE")
|
||||
|
||||
if file_name:
|
||||
return Path(file_name).read_text(encoding="utf-8").strip()
|
||||
|
||||
value = os.getenv("DATABASE_URL")
|
||||
|
||||
if not value:
|
||||
raise RuntimeError("DATABASE_URL or DATABASE_URL_FILE is required.")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def plain_postgres_url(url: str) -> str:
|
||||
if url.startswith("postgresql+asyncpg://"):
|
||||
return url.replace("postgresql+asyncpg://", "postgresql://", 1)
|
||||
|
||||
return url
|
||||
|
||||
|
||||
async def migrate() -> None:
|
||||
connection = await asyncpg.connect(
|
||||
plain_postgres_url(read_database_url())
|
||||
)
|
||||
|
||||
try:
|
||||
await connection.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version TEXT PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
applied_rows = await connection.fetch(
|
||||
"SELECT version FROM schema_migrations"
|
||||
)
|
||||
applied = {row["version"] for row in applied_rows}
|
||||
|
||||
for migration_file in sorted(MIGRATIONS_DIR.glob("*.sql")):
|
||||
version = migration_file.name
|
||||
|
||||
if version in applied:
|
||||
print(f"SKIP {version}")
|
||||
continue
|
||||
|
||||
sql = migration_file.read_text(encoding="utf-8")
|
||||
|
||||
print(f"APPLY {version}")
|
||||
|
||||
async with connection.transaction():
|
||||
await connection.execute(sql)
|
||||
await connection.execute(
|
||||
"INSERT INTO schema_migrations (version) VALUES ($1)",
|
||||
version,
|
||||
)
|
||||
|
||||
print("PASS: migrations applied")
|
||||
finally:
|
||||
await connection.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(migrate())
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# TODO: copy to restart.sh and implement using YOUR compose/stack services.
|
||||
# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable).
|
||||
# Preserve persistent volumes and schema/data; never down -v / flush / re-seed.
|
||||
# Allow operator unseal if required in lab7; do not print credentials.
|
||||
# Exit 0 only after the intended action; test waits for API readiness separately.
|
||||
printf '%s
|
||||
' 'TODO: implement restart.sh for your infrastructure' >&2
|
||||
exit 2
|
||||
Regular → Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
# Restarts the API and every storage it uses; volumes are kept.
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose restart db valkey api
|
||||
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/sh
|
||||
# Collects lab2 evidence from a running stand:
|
||||
# docker compose --profile tests up -d --build api api_short_ttl
|
||||
# sh scripts/run_evidence.sh
|
||||
# Output contains only synthetic data and aggregates, never tokens/secrets.
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
|
||||
PYTHON="${PYTHON:-python3}"
|
||||
if [ -x .venv-wsl/bin/python3 ]; then PYTHON=.venv-wsl/bin/python3; fi
|
||||
if [ -x .venv/bin/python3 ]; then PYTHON=.venv/bin/python3; fi
|
||||
|
||||
containers() {
|
||||
docker compose ps -a --format '{{.Service}}\t{{.State}}\t{{.Status}}\t{{.CreatedAt}}'
|
||||
}
|
||||
|
||||
"$PYTHON" scripts/check_contract.py | tee evidence/check_contract.txt
|
||||
"$PYTHON" scripts/validate_openapi.py | tee evidence/validate_openapi.txt
|
||||
"$PYTHON" tests/smoke.py --base-url http://localhost:8080 | tee evidence/smoke.txt
|
||||
|
||||
{
|
||||
echo "## containers before restart hook"
|
||||
containers
|
||||
"$PYTHON" tests/resilience.py \
|
||||
--base-url http://localhost:8080 \
|
||||
--action-script scripts/restart.sh \
|
||||
--mode persistence
|
||||
echo "## containers after restart hook (Status shows new uptime)"
|
||||
containers
|
||||
} 2>&1 | tee evidence/persistence.txt
|
||||
|
||||
docker compose run --rm migrate 2>/dev/null | tee evidence/migrations_repeat.txt
|
||||
|
||||
"$PYTHON" student-tests/level4.py | tee evidence/level4.txt
|
||||
docker compose --profile tools run --rm db_checks 2>/dev/null \
|
||||
| tee evidence/explain.txt
|
||||
|
||||
"$PYTHON" tests/load.py \
|
||||
--base-url http://localhost:8080 \
|
||||
--duration 15 \
|
||||
--concurrency 4 \
|
||||
--output evidence/load.json | tee evidence/load.txt
|
||||
Reference in New Issue
Block a user