forked from mtusi2026/mtusi2026lab4
131 lines
5.8 KiB
Python
131 lines
5.8 KiB
Python
"""Lab4 grade-5 track 2: repeatable DB/S3 reconciliation.
|
|
|
|
Dry-run is default. Apply never drops metadata when BOTH object copies are gone:
|
|
without a trustworthy copy or external backup, deletion would destroy evidence.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import sys
|
|
import time
|
|
from datetime import datetime, timezone
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from botocore.exceptions import BotoCoreError, ClientError
|
|
|
|
from .attachments import BUCKET, s3_client
|
|
|
|
|
|
def inventory(client):
|
|
result = {}
|
|
for page in client.get_paginator("list_objects_v2").paginate(Bucket=BUCKET):
|
|
for obj in page.get("Contents", []):
|
|
# Only reconciliation-owned prefixes; never mutate arbitrary S3 keys.
|
|
key = obj["Key"]
|
|
if key.startswith(("attachments/", "recovery/")):
|
|
result[key] = obj["LastModified"]
|
|
return result
|
|
|
|
|
|
def get_db_rows(conn):
|
|
return conn.execute("SELECT id, object_key, backup_key FROM attachments").fetchall()
|
|
|
|
|
|
def current_reference(conn, key):
|
|
return conn.execute(
|
|
"SELECT 1 FROM attachments WHERE object_key=%s OR backup_key=%s", (key, key)
|
|
).fetchone() is not None
|
|
|
|
|
|
def reconcile(*, apply=False, grace_seconds=300, now=None, client=None, connect=None):
|
|
if grace_seconds < 300 and apply:
|
|
raise ValueError("Apply requires >=300 s grace period to protect in-flight uploads")
|
|
started = time.monotonic()
|
|
now = now or datetime.now(timezone.utc)
|
|
client = client or s3_client()
|
|
connect = connect or (lambda: psycopg.connect(__import__('os').environ['DATABASE_URL'], row_factory=dict_row))
|
|
objects = inventory(client)
|
|
stats = {
|
|
"mode": "apply" if apply else "dry-run", "grace_seconds": grace_seconds,
|
|
"s3_keys_scanned": len(objects), "db_rows_scanned": 0,
|
|
"orphan_keys": [], "orphan_keys_young": [],
|
|
"missing_primary_repairable": [], "missing_backup_repairable": [],
|
|
"missing_both_unrecoverable": [], "actions": [], "errors": [],
|
|
}
|
|
with connect() as conn:
|
|
rows = get_db_rows(conn)
|
|
stats["db_rows_scanned"] = len(rows)
|
|
references = {key for row in rows for key in (row["object_key"], row["backup_key"])}
|
|
for row in rows:
|
|
primary, backup = row["object_key"], row["backup_key"]
|
|
have_primary, have_backup = primary in objects, backup in objects
|
|
rid = str(row["id"])
|
|
if have_primary and have_backup:
|
|
continue
|
|
if not have_primary and not have_backup:
|
|
stats["missing_both_unrecoverable"].append(rid)
|
|
continue
|
|
dest, source, typ = (primary, backup, "missing_primary_repairable") if not have_primary else (backup, primary, "missing_backup_repairable")
|
|
stats[typ].append(rid)
|
|
if apply:
|
|
# Defensive recheck. Object may have reappeared after inventory.
|
|
try:
|
|
client.head_object(Bucket=BUCKET, Key=dest)
|
|
stats["actions"].append({"action": "already-restored", "key": dest})
|
|
continue
|
|
except ClientError as exc:
|
|
code = exc.response.get("Error", {}).get("Code")
|
|
if code not in ("404", "NotFound", "NoSuchKey"):
|
|
raise
|
|
try:
|
|
client.copy_object(Bucket=BUCKET, Key=dest,
|
|
CopySource={"Bucket": BUCKET, "Key": source})
|
|
stats["actions"].append({"action": "restore", "key": dest, "from": source})
|
|
except (BotoCoreError, ClientError) as exc:
|
|
stats["errors"].append({"action": "restore", "key": dest, "error_type": type(exc).__name__})
|
|
for key, modified in objects.items():
|
|
if key in references:
|
|
continue
|
|
age = (now - modified).total_seconds()
|
|
if age < grace_seconds:
|
|
stats["orphan_keys_young"].append(key)
|
|
continue
|
|
stats["orphan_keys"].append(key)
|
|
if apply:
|
|
# Query again immediately before destructive action; never delete
|
|
# an object now referenced by a committed metadata row.
|
|
if current_reference(conn, key):
|
|
stats["actions"].append({"action": "skip-now-referenced", "key": key})
|
|
continue
|
|
try:
|
|
client.delete_object(Bucket=BUCKET, Key=key)
|
|
stats["actions"].append({"action": "delete-old-orphan", "key": key})
|
|
except (BotoCoreError, ClientError) as exc:
|
|
stats["errors"].append({"action": "delete-old-orphan", "key": key, "error_type": type(exc).__name__})
|
|
stats["scan_elapsed_seconds"] = round(time.monotonic() - started, 3)
|
|
return stats
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Reconcile S3 attachments vs PostgreSQL (dry-run default)")
|
|
parser.add_argument("--apply", action="store_true", help="Make safe changes; orphans older than 300 seconds only")
|
|
parser.add_argument("--grace-seconds", type=int, default=300)
|
|
parser.add_argument("--output", help="Write JSON summary (no credentials)")
|
|
args = parser.parse_args()
|
|
try:
|
|
result = reconcile(apply=args.apply, grace_seconds=args.grace_seconds)
|
|
except Exception as exc:
|
|
# No exception string: botocore errors may contain signed credentials/URLs.
|
|
print(json.dumps({"status":"failed", "error_type":type(exc).__name__}), file=sys.stderr)
|
|
raise SystemExit(2) from None
|
|
rendered = json.dumps(result, ensure_ascii=False, indent=2)
|
|
print(rendered)
|
|
if args.output:
|
|
with open(args.output, "w", encoding="utf-8") as fh:
|
|
fh.write(rendered + "\n")
|
|
if result["errors"] or result["missing_both_unrecoverable"]:
|
|
raise SystemExit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|