forked from mtusi2026/mtusi2026lab6
Lab 6: track - 1
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local-only secret files once; do not print their values."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
d = root / 'secrets'
|
||||
d.mkdir(mode=0o700, exist_ok=True)
|
||||
d.chmod(0o700)
|
||||
|
||||
def safe_token(n=32):
|
||||
first = secrets.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
||||
return first + secrets.token_urlsafe(n)
|
||||
|
||||
def ensure(name, value):
|
||||
p = d / name
|
||||
if not p.exists():
|
||||
p.write_text(value, encoding='utf-8', newline='\n')
|
||||
p.chmod(0o600)
|
||||
|
||||
ensure('db_password', safe_token(32))
|
||||
ensure('rustfs_root_user', 'RUSTFSROOT')
|
||||
ensure('rustfs_root_password', safe_token(32))
|
||||
ensure('s3_access_key_id', 'messengerapi')
|
||||
ensure('s3_secret_access_key', safe_token(40))
|
||||
ensure('grafana_admin_password', safe_token(24))
|
||||
ensure(
|
||||
'database_url',
|
||||
'postgresql://messenger:' + (d / 'db_password').read_text(encoding='utf-8').strip() + '@postgres:5432/messenger',
|
||||
)
|
||||
ensure('cache_url', 'redis://redis:6379/0')
|
||||
ensure('broker_url', 'redis://broker:6379/0')
|
||||
|
||||
env = root / '.env'
|
||||
if not env.exists():
|
||||
env.write_text(
|
||||
'API_REPLICAS=2\n'
|
||||
'APP_PORT=8081\n'
|
||||
'GRAFANA_PORT=3000\n'
|
||||
'SESSION_TTL_SECONDS=3600\n'
|
||||
'WORKER_CONCURRENCY=2\n'
|
||||
'MAX_QUEUED_JOBS=500\n'
|
||||
'JOB_MAX_ATTEMPTS=5\n'
|
||||
'JOB_TIMEOUT_SECONDS=60\n'
|
||||
'OTEL_SDK_DISABLED=false\n'
|
||||
'OTEL_TRACES_SAMPLER=always_on\n'
|
||||
'OTEL_TRACES_SAMPLER_ARG=1.0\n'
|
||||
'OTEL_COLLECTOR_CONFIG=./infra/otel/collector.yaml\n',
|
||||
encoding='utf-8',
|
||||
newline='\n',
|
||||
)
|
||||
env.chmod(0o600)
|
||||
print('Local secrets created or preserved; values hidden.')
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Explicitly replay one terminal failed attachment job, never a ready job."""
|
||||
import sys, uuid
|
||||
from pathlib import Path
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
from app.migrate import setting
|
||||
import psycopg
|
||||
|
||||
if len(sys.argv)!=2:
|
||||
raise SystemExit('usage: python scripts/replay_failed.py ATTACHMENT_UUID')
|
||||
aid=str(uuid.UUID(sys.argv[1]))
|
||||
with psycopg.connect(setting('DATABASE_URL')) as c:
|
||||
row=c.execute("SELECT j.job_id,j.state,a.status FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=%s FOR UPDATE OF j,a",(aid,)).fetchone()
|
||||
if not row or row[1]!='failed' or row[2]!='failed':
|
||||
raise SystemExit('replay refused: attachment must be terminal failed')
|
||||
c.execute("UPDATE attachment_jobs SET state='pending',attempt=0,available_at=clock_timestamp(),lease_until=NULL,last_error=NULL WHERE job_id=%s",(row[0],))
|
||||
c.execute("UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=%s",(aid,))
|
||||
c.execute("INSERT INTO attachment_job_audit(job_id,event,detail) VALUES (%s,'operator_replay','explicit replay command')",(row[0],))
|
||||
print('failed job queued for explicit replay')
|
||||
@@ -0,0 +1 @@
|
||||
bash scripts/restart.sh
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose down
|
||||
sleep 2
|
||||
docker compose up -d --no-build --pull never --wait --wait-timeout 90
|
||||
@@ -0,0 +1 @@
|
||||
bash scripts/stop-one.sh
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID from a real API response}"
|
||||
for id in $(docker compose ps -q api); do
|
||||
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
"$host"-*)
|
||||
printf 'Stopping API container %s (hostname %s)\n' "$id" "$host"
|
||||
docker stop --time 5 "$id"
|
||||
docker inspect -f '{{.Id}} {{.State.Status}}' "$id"
|
||||
exit 0;;
|
||||
esac
|
||||
done
|
||||
printf 'No running API matches target\n' >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user