forked from mtusi2026/mtusi2026lab4
all for lab4
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from urllib.parse import unquote, urlsplit
|
||||
import uuid
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def output(args):
|
||||
result = subprocess.run(
|
||||
args,
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
if result.returncode:
|
||||
raise RuntimeError(
|
||||
"Docker command failed; "
|
||||
"check Docker and running services"
|
||||
)
|
||||
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def service(name):
|
||||
ids = output([
|
||||
"docker", "compose", "ps", "-q", name,
|
||||
]).splitlines()
|
||||
|
||||
if not ids:
|
||||
raise RuntimeError(
|
||||
"Service is not running: " + name
|
||||
)
|
||||
|
||||
return json.loads(output([
|
||||
"docker", "inspect", ids[0],
|
||||
]))[0]
|
||||
|
||||
|
||||
def environment(container):
|
||||
return dict(
|
||||
item.split("=", 1)
|
||||
for item in container["Config"]["Env"]
|
||||
if "=" in item
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Reconcile PostgreSQL attachment metadata and S3"
|
||||
)
|
||||
|
||||
modes = parser.add_mutually_exclusive_group()
|
||||
modes.add_argument("--apply", action="store_true")
|
||||
modes.add_argument("--dry-run", action="store_true")
|
||||
|
||||
parser.add_argument(
|
||||
"--chat-id",
|
||||
type=lambda value: str(uuid.UUID(value)),
|
||||
default=None,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--grace-seconds",
|
||||
type=int,
|
||||
default=3600,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--max-seconds",
|
||||
type=int,
|
||||
default=600,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--backup-dir",
|
||||
default="backups/attachments",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
default="results/reconcile.json",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.grace_seconds < 60 or args.max_seconds < 1:
|
||||
parser.error(
|
||||
"grace-seconds >= 60; max-seconds >= 1"
|
||||
)
|
||||
|
||||
api = service("api")
|
||||
postgres = service("postgres")
|
||||
minio = service("minio")
|
||||
|
||||
env = environment(api)
|
||||
pg = environment(postgres)
|
||||
|
||||
common = (
|
||||
set(api["NetworkSettings"]["Networks"])
|
||||
& set(postgres["NetworkSettings"]["Networks"])
|
||||
& set(minio["NetworkSettings"]["Networks"])
|
||||
)
|
||||
|
||||
if len(common) != 1:
|
||||
raise RuntimeError(
|
||||
"Expected exactly one common Docker network "
|
||||
"for API, PostgreSQL and MinIO"
|
||||
)
|
||||
|
||||
network = next(iter(common))
|
||||
|
||||
database_url = (
|
||||
env.get("SPRING_DATASOURCE_URL")
|
||||
or env.get("DB_URL")
|
||||
or env.get("DATABASE_URL")
|
||||
or ""
|
||||
)
|
||||
|
||||
if database_url:
|
||||
address = urlsplit(
|
||||
database_url.removeprefix("jdbc:")
|
||||
)
|
||||
|
||||
if address.scheme not in ("postgresql", "postgres"):
|
||||
raise RuntimeError(
|
||||
"Unsupported database URL scheme"
|
||||
)
|
||||
|
||||
db_host = address.hostname
|
||||
db_port = address.port or 5432
|
||||
db_name = unquote(address.path.lstrip("/"))
|
||||
|
||||
url_user = (
|
||||
unquote(address.username)
|
||||
if address.username else None
|
||||
)
|
||||
|
||||
url_password = (
|
||||
unquote(address.password)
|
||||
if address.password else None
|
||||
)
|
||||
|
||||
else:
|
||||
db_host = env.get("DB_HOST") or "postgres"
|
||||
db_port = int(env.get("DB_PORT") or "5432")
|
||||
|
||||
db_name = (
|
||||
env.get("DB_NAME")
|
||||
or pg.get("POSTGRES_DB")
|
||||
or pg.get("POSTGRES_USER")
|
||||
or "postgres"
|
||||
)
|
||||
|
||||
url_user = None
|
||||
url_password = None
|
||||
|
||||
db_user = (
|
||||
env.get("SPRING_DATASOURCE_USERNAME")
|
||||
or env.get("DB_USERNAME")
|
||||
or env.get("DB_USER")
|
||||
or url_user
|
||||
or pg.get("POSTGRES_USER")
|
||||
)
|
||||
|
||||
db_password = (
|
||||
env.get("SPRING_DATASOURCE_PASSWORD")
|
||||
or env.get("DB_PASSWORD")
|
||||
or url_password
|
||||
or pg.get("POSTGRES_PASSWORD")
|
||||
)
|
||||
|
||||
if not db_host or not db_name:
|
||||
raise RuntimeError(
|
||||
"Cannot determine PostgreSQL host or database"
|
||||
)
|
||||
|
||||
if not db_user or not db_password:
|
||||
raise RuntimeError(
|
||||
"Database credentials are not available "
|
||||
"in container environment"
|
||||
)
|
||||
|
||||
required = [
|
||||
"S3_ENDPOINT",
|
||||
"S3_ACCESS_KEY",
|
||||
"S3_SECRET_KEY",
|
||||
"S3_BUCKET",
|
||||
]
|
||||
|
||||
if any(not env.get(key) for key in required):
|
||||
raise RuntimeError(
|
||||
"Missing S3 settings in API container environment"
|
||||
)
|
||||
|
||||
config = {
|
||||
"apply": args.apply,
|
||||
"chat_id": args.chat_id,
|
||||
"grace_seconds": args.grace_seconds,
|
||||
"max_seconds": args.max_seconds,
|
||||
"bucket": env["S3_BUCKET"],
|
||||
"db": {
|
||||
"host": db_host,
|
||||
"port": db_port,
|
||||
"dbname": db_name,
|
||||
"user": db_user,
|
||||
"password": db_password,
|
||||
},
|
||||
"s3": {
|
||||
"endpoint_url": env["S3_ENDPOINT"],
|
||||
"aws_access_key_id": env["S3_ACCESS_KEY"],
|
||||
"aws_secret_access_key": env["S3_SECRET_KEY"],
|
||||
"region_name": "us-east-1",
|
||||
},
|
||||
}
|
||||
|
||||
backups = (ROOT / args.backup_dir).resolve()
|
||||
backups.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
command = [
|
||||
"docker", "run",
|
||||
"--rm",
|
||||
"-i",
|
||||
"--network", network,
|
||||
"--read-only",
|
||||
"--tmpfs", "/tmp:rw,noexec,nosuid,size=16m",
|
||||
"--memory", "256m",
|
||||
"--cpus", "1",
|
||||
"--cap-drop", "ALL",
|
||||
"--security-opt", "no-new-privileges",
|
||||
"--mount",
|
||||
(
|
||||
f"type=bind,source={backups},"
|
||||
"target=/backups,readonly"
|
||||
),
|
||||
"mtusi4-reconciler:local",
|
||||
]
|
||||
|
||||
result = subprocess.run(
|
||||
command,
|
||||
cwd=ROOT,
|
||||
input=json.dumps(config),
|
||||
text=True,
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
try:
|
||||
report = json.loads(result.stdout)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
raise RuntimeError(
|
||||
"Worker did not return JSON; "
|
||||
"check image build and backup directory permissions"
|
||||
) from None
|
||||
|
||||
target = ROOT / args.output
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
rendered = json.dumps(
|
||||
report,
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
|
||||
target.write_text(
|
||||
rendered + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
print(rendered)
|
||||
print("Report saved:", target, file=sys.stderr)
|
||||
|
||||
return result.returncode
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
|
||||
except (
|
||||
RuntimeError,
|
||||
ValueError,
|
||||
OSError,
|
||||
subprocess.TimeoutExpired,
|
||||
) as error:
|
||||
print("FAIL:", str(error), file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
+40
-5
@@ -3,11 +3,46 @@ set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
docker compose stop -t 20 api lb
|
||||
mkdir -p .local-logs
|
||||
exec > .local-logs/restart.log 2>&1
|
||||
|
||||
docker compose restart postgres redis
|
||||
set -x
|
||||
|
||||
docker compose up -d --no-deps --no-recreate \
|
||||
--wait --wait-timeout 30 postgres redis
|
||||
date -u
|
||||
|
||||
docker compose start api lb
|
||||
echo "Stopping API and load balancer"
|
||||
docker compose stop -t 10 api lb
|
||||
|
||||
echo "Restarting persistent services"
|
||||
docker compose restart -t 10 postgres redis minio
|
||||
|
||||
echo "Waiting for persistent services"
|
||||
docker compose up -d \
|
||||
--no-build \
|
||||
--pull never \
|
||||
--no-deps \
|
||||
--no-recreate \
|
||||
--wait \
|
||||
--wait-timeout 25 \
|
||||
postgres redis minio
|
||||
|
||||
echo "Starting API"
|
||||
docker compose up -d \
|
||||
--no-build \
|
||||
--pull never \
|
||||
--no-deps \
|
||||
--no-recreate \
|
||||
--wait \
|
||||
--wait-timeout 45 \
|
||||
api
|
||||
|
||||
echo "Starting load balancer without dependencies"
|
||||
docker compose up -d \
|
||||
--no-build \
|
||||
--pull never \
|
||||
--no-deps \
|
||||
--no-recreate \
|
||||
lb
|
||||
|
||||
echo "Restart completed"
|
||||
date -u
|
||||
Reference in New Issue
Block a user