выполнение на 5

This commit is contained in:
kaktuz20000
2026-11-04 10:46:41 +03:00
parent 276dbdad8a
commit dd098ac0a3
47 changed files with 4967 additions and 42 deletions
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
import json
import os
import secrets
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SECRET_DIR = ROOT / "secrets"
def main():
SECRET_DIR.mkdir(mode=0o700, exist_ok=True)
names = (
"postgres_password",
"database_url",
"s3_access_key_id",
"s3_secret_access_key",
"s3_admin_access_key_id",
"s3_admin_secret_access_key",
"rabbitmq_password",
"rabbitmq.conf",
"broker_url",
"s3.json",
)
existing = [name for name in names if (SECRET_DIR / name).exists()]
if existing:
raise SystemExit(
"Secret files already exist; refusing to rotate credentials without a migration: "
+ ", ".join(existing)
)
db_password = secrets.token_hex(24)
broker_password = secrets.token_urlsafe(32)
values = {
"postgres_password": db_password,
"database_url": f"postgresql://messenger:{db_password}@postgres:5432/messenger",
"s3_access_key_id": "mngr" + secrets.token_hex(8),
"s3_secret_access_key": secrets.token_urlsafe(40),
"s3_admin_access_key_id": "admin" + secrets.token_hex(7),
"s3_admin_secret_access_key": secrets.token_urlsafe(40),
"rabbitmq_password": broker_password,
"broker_url": f"amqp://messenger:{broker_password}@rabbitmq:5672/%2f",
}
for name, value in values.items():
path = SECRET_DIR / name
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
output.write(value + "\n")
descriptor = os.open(
SECRET_DIR / "rabbitmq.conf",
os.O_WRONLY | os.O_CREAT | os.O_EXCL,
0o600,
)
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
output.write("default_user = messenger\n")
output.write(f"default_pass = {broker_password}\n")
s3_config = {
"identities": [
{
"name": "bootstrap-admin",
"credentials": [
{
"accessKey": values["s3_admin_access_key_id"],
"secretKey": values["s3_admin_secret_access_key"],
}
],
"actions": ["Admin"],
},
{
"name": "messenger-api",
"credentials": [
{
"accessKey": values["s3_access_key_id"],
"secretKey": values["s3_secret_access_key"],
}
],
"actions": [
"Read:messenger-attachments",
"Write:messenger-attachments",
"List:messenger-attachments",
],
},
]
}
descriptor = os.open(SECRET_DIR / "s3.json", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
json.dump(s3_config, output, separators=(",", ":"))
output.write("\n")
print("Local secrets generated with owner-only permissions; values were not printed.")
if __name__ == "__main__":
main()
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
exec python3 "$(dirname "$0")/bootstrap-secrets.py"
+33
View File
@@ -0,0 +1,33 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
interval=${RECONCILE_INTERVAL_SECONDS:-900}
grace=${RECONCILE_GRACE_SECONDS:-3600}
case "$interval" in
''|*[!0-9]*)
echo "RECONCILE_INTERVAL_SECONDS must be an integer" >&2
exit 2
;;
esac
case "$grace" in
''|*[!0-9]*)
echo "RECONCILE_GRACE_SECONDS must be an integer" >&2
exit 2
;;
esac
if [ "$interval" -lt 60 ]; then
echo "RECONCILE_INTERVAL_SECONDS must be at least 60" >&2
exit 2
fi
if [ "$grace" -lt 60 ]; then
echo "RECONCILE_GRACE_SECONDS must be at least 60" >&2
exit 2
fi
while :
do
python3 src/reconcile.py --grace-seconds "$grace" --apply
sleep "$interval"
done
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart postgres s3-core rabbitmq
docker compose up -d --wait postgres s3-core rabbitmq
docker compose restart s3
docker compose up -d --wait s3
attempt=0
until docker compose exec --index 1 -T api python3 -c 'from src.object_store import ObjectStore; store = ObjectStore(); key, _ = next(store.objects()); store.get(key, 10485760)' >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge 4 ]; then
echo "S3 object reads did not recover after restart" >&2
exit 1
fi
sleep 2
done
docker compose restart api
docker compose restart publisher worker
docker compose up -d --wait api publisher worker
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
found=
for container_id in $(docker compose ps --quiet --status running api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
found=$container_id
break
fi
done
if [ -z "$found" ]; then
printf '%s\n' 'No running API container matched the requested instance alias.' >&2
exit 1
fi
docker stop --time 5 "$found" >/dev/null