172 lines
9.0 KiB
Python
172 lines
9.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Bounded Stage-only SEO operation; no generic Prisma migration execution."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shlex
|
|
import shutil
|
|
import subprocess
|
|
import time
|
|
import uuid
|
|
from catalog import CATALOG_SQL, validate
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SQL_PATH = 'backend/prisma/migrations/20261028120000_add_seo_tracking_contract/migration.sql'
|
|
SQL_DIGEST = 'de1386398026c6373ce8692fb165b904e189f830d7b20914d8a4b512a47f9d16'
|
|
IMAGE = 'sha256:de3a4eab8fdfa507ea92aac488b916b08089e515db49b055fe71dfa271ba3a28'
|
|
STAGE = 'canino_db_stage'
|
|
|
|
|
|
def check_binding(sha, source, mode, reviewed):
|
|
if not re.fullmatch('[0-9a-f]{40}', sha) or sha != source:
|
|
raise ValueError('Exact source/event SHA binding required')
|
|
if mode == 'apply' and reviewed != sha:
|
|
raise ValueError('Independent reviewed operation SHA required')
|
|
|
|
|
|
def check_database(name):
|
|
if name.strip() != 'caninodb_stage':
|
|
raise ValueError('Not Stage database')
|
|
|
|
|
|
class Docker:
|
|
def __init__(self, remote=False):
|
|
self.remote = remote
|
|
|
|
def run(self, args, data=None, timeout=180):
|
|
cmd = ['docker'] + args
|
|
if self.remote:
|
|
cmd = ['ssh', '-F', '/opt/data/.ssh/config', '-o', 'StrictHostKeyChecking=yes',
|
|
'-o', 'UserKnownHostsFile=/opt/data/.ssh/known_hosts', 'parsa-host', shlex.join(cmd)]
|
|
return subprocess.run(cmd, input=data, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
timeout=timeout, check=True).stdout
|
|
|
|
def sql(self, container, query):
|
|
# Live identity and credentials are never read into Python or printed.
|
|
if container == STAGE:
|
|
args = ['exec', '-i', STAGE, 'sh', '-c',
|
|
'exec psql -X -qAt -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"']
|
|
else:
|
|
args = ['exec', '-i', container, 'psql', '-h', '127.0.0.1', '-U', 'postgres',
|
|
'-d', 'rehearsal', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1']
|
|
return self.run(args, query.encode()).decode().strip()
|
|
|
|
|
|
def catalog(docker, name):
|
|
return json.loads(docker.sql(name, 'SET search_path=public;\n' + CATALOG_SQL))
|
|
|
|
|
|
def transaction(sql):
|
|
return ("BEGIN; SET LOCAL lock_timeout='5s'; SET LOCAL statement_timeout='60s'; "
|
|
"SELECT pg_advisory_xact_lock(731920260); SET LOCAL search_path=public;\n" + sql + '\nCOMMIT;')
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('mode', choices=['rehearse', 'apply'])
|
|
ap.add_argument('--sha', required=True)
|
|
ap.add_argument('--ssh-rehearsal', action='store_true')
|
|
ap.add_argument('--evidence', required=True)
|
|
a = ap.parse_args()
|
|
source = subprocess.check_output(['git', '-C', str(ROOT), 'rev-parse', 'HEAD'], text=True).strip()
|
|
check_binding(a.sha, source, a.mode, os.environ.get('STAGE_SCHEMA_REVIEWED_SHA', ''))
|
|
if a.mode == 'apply' or not a.ssh_rehearsal:
|
|
if os.environ.get('GITHUB_ACTIONS') != 'true' or os.environ.get('GITHUB_SHA') != a.sha:
|
|
raise ValueError('CI exact SHA required')
|
|
if os.environ.get('GITHUB_REF') != 'refs/heads/ops/seo-schema-stage-' + a.mode:
|
|
raise ValueError('Dedicated Stage operation ref required')
|
|
if a.ssh_rehearsal and a.mode != 'rehearse':
|
|
raise ValueError('SSH transport permits rehearsal only')
|
|
sql_bytes = (ROOT / SQL_PATH).read_bytes()
|
|
if hashlib.sha256(sql_bytes).hexdigest() != SQL_DIGEST:
|
|
raise ValueError('Reviewed SQL digest mismatch')
|
|
if subprocess.check_output(['git','-C',str(ROOT),'show',a.sha+':'+SQL_PATH]) != sql_bytes:
|
|
raise ValueError('Dirty SQL refused')
|
|
# A local read-only rehearsal may exercise pending tooling. CI must execute
|
|
# only committed operation and catalog sources from the event SHA.
|
|
if not a.ssh_rehearsal:
|
|
for rel in ('scripts/stage-schema/operation.py', 'scripts/stage-schema/catalog.py'):
|
|
if subprocess.check_output(['git','-C',str(ROOT),'show',a.sha+':'+rel]) != (ROOT/rel).read_bytes():
|
|
raise ValueError('Dirty operation source refused')
|
|
if a.mode == 'apply' and a.ssh_rehearsal:
|
|
raise ValueError('Live apply cannot use SSH rehearsal transport')
|
|
evidence = Path(a.evidence)
|
|
evidence.mkdir(mode=0o700, parents=True, exist_ok=False)
|
|
os.chmod(evidence, 0o700)
|
|
d = Docker(a.ssh_rehearsal)
|
|
owned = []
|
|
try:
|
|
# Share application deployment lock; never chmod operational lock/config.
|
|
runner = d.run(['inspect','-f','{{.Config.Image}}','gitea_runner']).decode().strip()
|
|
lock = d.run(['create','--name','canina-stage-deploy-lock','--pull=never',
|
|
'--network','none','--cap-drop=ALL','--security-opt=no-new-privileges',
|
|
'--mount','type=bind,src=/opt/services/canina,dst=/host-canina,readonly',
|
|
'--entrypoint','/bin/sh',runner,'-c',
|
|
'exec 9</host-canina/stage-backend-deploy.lock; flock -n 9 || exit 1; '
|
|
'printf "LOCKED\\n"; while :; do sleep 60; done']).decode().strip()
|
|
owned.append(lock)
|
|
d.run(['start',lock])
|
|
for _ in range(10):
|
|
if d.run(['logs',lock]).strip() == b'LOCKED': break
|
|
time.sleep(1)
|
|
else: raise ValueError('Host lock not acquired')
|
|
check_database(d.sql(STAGE, 'BEGIN READ ONLY; SELECT current_database(); COMMIT;'))
|
|
size = int(d.sql(STAGE, 'SELECT pg_database_size(current_database());'))
|
|
free = int(d.run(['exec',STAGE,'sh','-c','df -Pk /var/lib/postgresql/data | tail -1']).split()[3])*1024
|
|
if free < max(size*5, 256*1024*1024): raise ValueError('Insufficient backup/restore capacity')
|
|
if shutil.disk_usage(evidence).free < max(size*2, 128*1024*1024):
|
|
raise ValueError('Insufficient private backup capacity')
|
|
ledger_before = d.sql(STAGE, 'SELECT coalesce(jsonb_agg(to_jsonb(m) ORDER BY id),\'[]\'::jsonb) FROM _prisma_migrations m;')
|
|
def dump(schema):
|
|
args = ['exec', STAGE, 'sh','-c', 'exec pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" --no-owner --no-acl -Fc' + (' --schema-only' if schema else '')]
|
|
return d.run(args,timeout=180)
|
|
backup = dump(a.mode == 'rehearse')
|
|
backup_path = evidence / ('stage-schema.dump' if a.mode == 'rehearse' else 'stage-full.dump')
|
|
fd = os.open(backup_path,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600)
|
|
with os.fdopen(fd,'wb') as f: f.write(backup)
|
|
def disposable():
|
|
name = 'seo-schema-rehearsal-' + uuid.uuid4().hex[:16]
|
|
cid = d.run(['run','-d','--name',name,'--pull=never','--network','none',
|
|
'-e','POSTGRES_HOST_AUTH_METHOD=trust','-e','POSTGRES_DB=rehearsal',IMAGE]).decode().strip()
|
|
owned.append(cid)
|
|
for _ in range(60):
|
|
try:
|
|
d.run(['exec',cid,'pg_isready','-h','127.0.0.1','-U','postgres','-d','rehearsal'],timeout=10)
|
|
return cid
|
|
except subprocess.CalledProcessError: time.sleep(1)
|
|
raise ValueError('Final TCP server readiness timed out')
|
|
reference = disposable()
|
|
d.sql(reference, 'CREATE TABLE public.blogs(id UUID PRIMARY KEY);')
|
|
d.sql(reference, transaction(sql_bytes.decode()))
|
|
expected = catalog(d, reference)
|
|
clone = disposable()
|
|
d.run(['exec','-i',clone,'pg_restore','-h','127.0.0.1','-U','postgres','-d','rehearsal',
|
|
'--no-owner','--no-acl','--exit-on-error'],data=backup)
|
|
d.sql(clone, transaction(sql_bytes.decode()))
|
|
validate(catalog(d,clone),expected)
|
|
# Prove repeat is safe and no catalog drift from idempotent migration.
|
|
d.sql(clone, transaction(sql_bytes.decode()))
|
|
validate(catalog(d,clone),expected)
|
|
if a.mode == 'apply':
|
|
d.sql(STAGE, transaction(sql_bytes.decode()))
|
|
validate(catalog(d,STAGE),expected)
|
|
ledger_after = d.sql(STAGE, 'SELECT coalesce(jsonb_agg(to_jsonb(m) ORDER BY id),\'[]\'::jsonb) FROM _prisma_migrations m;')
|
|
if ledger_before != ledger_after: raise ValueError('Historical Prisma ledger changed')
|
|
receipt = dict(sha=a.sha,sql_sha256=SQL_DIGEST,mode=a.mode,database='caninodb_stage',
|
|
backup_sha256=hashlib.sha256(backup).hexdigest(), backup_bytes=len(backup),
|
|
restore_scope='schema-only sanitized clone' if a.mode=='rehearse' else 'full data without ACL/owner',
|
|
catalog_sha256=hashlib.sha256(json.dumps(expected,sort_keys=True).encode()).hexdigest(),
|
|
ledger_policy='Prisma ledger untouched; separate operation receipt', ledger_unchanged=True)
|
|
(evidence/'receipt.json').write_text(json.dumps(receipt,indent=2)+'\n')
|
|
(evidence/'catalog.json').write_text(json.dumps(expected,indent=2)+'\n')
|
|
print(json.dumps(receipt))
|
|
finally:
|
|
for cid in reversed(owned):
|
|
try: d.run(['rm','-f',cid],timeout=30)
|
|
except Exception: print('Owned container cleanup failed: '+cid, file=__import__('sys').stderr)
|
|
|
|
if __name__ == '__main__': main()
|