Implement lab4 S3 attachments and reliability checks
This commit is contained in:
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
compose() {
|
||||
docker compose \
|
||||
-f compose.yaml \
|
||||
-f compose.s3.yaml \
|
||||
--env-file .env \
|
||||
"$@"
|
||||
}
|
||||
|
||||
echo "Stopping LB and API..."
|
||||
compose stop --timeout 5 lb api
|
||||
|
||||
echo "Restarting PostgreSQL, Redis and S3..."
|
||||
compose restart --timeout 10 postgres redis s3
|
||||
|
||||
echo "Waiting for PostgreSQL..."
|
||||
attempt=0
|
||||
until compose exec -T postgres \
|
||||
pg_isready -U messenger -d messenger >/dev/null 2>&1
|
||||
do
|
||||
attempt=$((attempt + 1))
|
||||
if [ "$attempt" -ge 20 ]; then
|
||||
echo "PostgreSQL did not become ready" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "Starting API and LB..."
|
||||
compose start api lb
|
||||
|
||||
echo "Restart hook completed."
|
||||
@@ -0,0 +1,126 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
DIRECTORY = ROOT / "secrets"
|
||||
BUCKET = "messenger-attachments"
|
||||
|
||||
KEY_FILES = (
|
||||
"s3_admin_access_key",
|
||||
"s3_admin_secret_key",
|
||||
"s3_access_key_id",
|
||||
"s3_secret_access_key",
|
||||
)
|
||||
|
||||
|
||||
def configuration(values):
|
||||
return {
|
||||
"identities": [
|
||||
{
|
||||
"name": "bootstrap-admin",
|
||||
"credentials": [{
|
||||
"accessKey": values["s3_admin_access_key"],
|
||||
"secretKey": values["s3_admin_secret_key"],
|
||||
}],
|
||||
"actions": ["Admin", "Read", "Write", "List", "Tagging"],
|
||||
},
|
||||
{
|
||||
"name": "messenger-api",
|
||||
"credentials": [{
|
||||
"accessKey": values["s3_access_key_id"],
|
||||
"secretKey": values["s3_secret_access_key"],
|
||||
}],
|
||||
"actions": [
|
||||
f"Read:{BUCKET}",
|
||||
f"Write:{BUCKET}",
|
||||
f"List:{BUCKET}",
|
||||
],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def ensure_ignores():
|
||||
patterns = (
|
||||
".env",
|
||||
"/secrets/",
|
||||
"*.bak",
|
||||
"__pycache__/",
|
||||
"*.py[cod]",
|
||||
)
|
||||
for filename in (".gitignore", ".dockerignore"):
|
||||
path = ROOT / filename
|
||||
text = path.read_text() if path.exists() else ""
|
||||
existing = set(text.splitlines())
|
||||
missing = [item for item in patterns if item not in existing]
|
||||
if missing:
|
||||
path.write_text(
|
||||
text.rstrip("\n") + "\n" + "\n".join(missing) + "\n"
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
if DIRECTORY.is_symlink():
|
||||
raise SystemExit("Refusing a symlink at secrets/")
|
||||
|
||||
paths = [DIRECTORY / name for name in KEY_FILES]
|
||||
paths.append(DIRECTORY / "s3_config.json")
|
||||
present = [path.exists() for path in paths]
|
||||
|
||||
if any(present):
|
||||
if not all(path.is_file() and not path.is_symlink() for path in paths):
|
||||
raise SystemExit(
|
||||
"Incomplete or unsafe secret files; nothing regenerated"
|
||||
)
|
||||
|
||||
values = {
|
||||
name: (DIRECTORY / name).read_text().rstrip("\r\n")
|
||||
for name in KEY_FILES
|
||||
}
|
||||
if not all(values.values()):
|
||||
raise SystemExit("Empty credential file; nothing regenerated")
|
||||
|
||||
actual = json.loads(
|
||||
(DIRECTORY / "s3_config.json").read_text()
|
||||
)
|
||||
if actual != configuration(values):
|
||||
raise SystemExit(
|
||||
"Credentials and S3 configuration differ; nothing regenerated"
|
||||
)
|
||||
message = "Existing credentials verified; keys unchanged."
|
||||
else:
|
||||
DIRECTORY.mkdir(mode=0o700, exist_ok=True)
|
||||
DIRECTORY.chmod(0o700)
|
||||
|
||||
values = {
|
||||
"s3_admin_access_key": "admin-" + secrets.token_hex(12),
|
||||
"s3_admin_secret_key": secrets.token_hex(32),
|
||||
"s3_access_key_id": "api-" + secrets.token_hex(12),
|
||||
"s3_secret_access_key": secrets.token_hex(32),
|
||||
}
|
||||
|
||||
for name, value in values.items():
|
||||
with (DIRECTORY / name).open("x") as stream:
|
||||
stream.write(value + "\n")
|
||||
|
||||
with (DIRECTORY / "s3_config.json").open("x") as stream:
|
||||
json.dump(configuration(values), stream, indent=2)
|
||||
stream.write("\n")
|
||||
|
||||
message = "New credentials created; values are not printed."
|
||||
|
||||
DIRECTORY.chmod(0o700)
|
||||
for path in paths:
|
||||
path.chmod(0o644)
|
||||
|
||||
ensure_ignores()
|
||||
print(message)
|
||||
print("Secrets, .env, Python cache and .bak files are excluded.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
exec python3 - <<'PY'
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
target = os.environ.get("TARGET_INSTANCE_ID")
|
||||
if not target:
|
||||
raise SystemExit("TARGET_INSTANCE_ID is required")
|
||||
|
||||
compose = [
|
||||
"docker", "compose",
|
||||
"-f", "compose.yaml",
|
||||
"-f", "compose.s3.yaml",
|
||||
"--env-file", ".env",
|
||||
]
|
||||
|
||||
containers = subprocess.check_output(
|
||||
compose + ["ps", "-q", "api"],
|
||||
text=True,
|
||||
timeout=10,
|
||||
).split()
|
||||
|
||||
if len(containers) < 2:
|
||||
raise SystemExit("At least two running API containers are required")
|
||||
|
||||
probe = (
|
||||
"import urllib.request; "
|
||||
"r=urllib.request.urlopen("
|
||||
"'http://127.0.0.1:8080/health/ready', timeout=2); "
|
||||
"print(r.headers['X-Instance-Id'])"
|
||||
)
|
||||
|
||||
matches = []
|
||||
for container in containers:
|
||||
instance = subprocess.check_output(
|
||||
["docker", "exec", container, "python", "-c", probe],
|
||||
text=True,
|
||||
timeout=4,
|
||||
).strip()
|
||||
|
||||
if instance == target:
|
||||
matches.append(container)
|
||||
|
||||
if len(matches) != 1:
|
||||
raise SystemExit("Target instance was not resolved uniquely")
|
||||
|
||||
container = matches[0]
|
||||
print("Stopping target API container:", container[:12], flush=True)
|
||||
|
||||
subprocess.run(
|
||||
["docker", "stop", "--time", "5", container],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
timeout=15,
|
||||
)
|
||||
|
||||
running = subprocess.check_output(
|
||||
["docker", "inspect", "-f", "{{.State.Running}}", container],
|
||||
text=True,
|
||||
timeout=5,
|
||||
).strip()
|
||||
|
||||
if running != "false":
|
||||
raise SystemExit("Target API container is still running")
|
||||
|
||||
print("Target API container is stopped", flush=True)
|
||||
PY
|
||||
Reference in New Issue
Block a user