Files
canina/scripts/production-release.py
T
2026-10-26 10:15:06 +03:30

219 lines
13 KiB
Python

#!/usr/bin/env python3
"""Private production acceptance and runtime snapshot helpers. Never log config."""
import json
import os
from pathlib import Path
import re
import stat
import subprocess
import sys
ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore',
'.gitea/workflows/deploy.yml', 'scripts/deploy.sh', 'scripts/ci-host-deploy.sh',
'scripts/rollout-production.sh', 'scripts/production-release.py', 'scripts/test_production_release.py',
'scripts/tests/test_release_schema_guard.py',
# Exact CEO-approved schema CI repair; no migration/workflow wildcard.
'.gitea/workflows/auth-schema-reconcile.yml',
'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json',
'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql',
'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py',
'scripts/test_auth_schema_push.py',
'scripts/compose.stage.yml', 'scripts/rollout-stage.sh', 'scripts/revalidation-runtime.py',
'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py',
'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py',
'scripts/tests/test_stage_rollout.py', 'scripts/tests/test_secure_build_install.py',
# CI1285 verified-payload upgrade repair; no model, cron or payload wildcard.
'infra/hermes-editorial/installer.py',
'infra/hermes-editorial/tests/test_installer.py',
'backend/src/common/revalidation/revalidation.service.ts',
'backend/src/common/revalidation/revalidation.service.spec.ts',
'backend/src/support-chat/support-chat.http.spec.ts',
# Exact baseline gate repairs reviewed in 1bb6274; never a frontend wildcard.
'frontend/admin-panel/src/test/setup.ts',
'frontend/admin-panel/src/test/storage.test.ts',
'frontend/application/components/AnalyticsDeferred.tsx',
'frontend/application/components/BlogPage.tsx',
'frontend/application/components/__tests__/BlogPage.sorting.test.tsx',
'frontend/application/components/BlogPostClient.tsx',
'frontend/application/components/__tests__/BlogPostClient.test.tsx',
'frontend/application/lib/extractFaqs.ts',
'frontend/application/lib/extractFaqs.test.ts',
# Exact authorized publisher technical SEO repair; no frontend wildcard.
'frontend/application/app/blog/[slug]/page.tsx',
'frontend/application/app/blog/[slug]/publisher-seo.test.tsx',
'frontend/application/lib/blogAuthor.ts',
'frontend/application/lib/blogAuthor.test.ts'}
def validate_receipt(r, sha, baseline, paths):
if not (r.get('accepted') is True and r.get('scope') == 'reliability-only'
and r.get('sha') == sha and r.get('baseline') == baseline
and re.fullmatch(r'https://git\.parsaaghayi\.ir/parsa/canina/actions/runs/[0-9]+', r.get('stage_run_url',''))
and isinstance(r.get('reviewer'), str) and r['reviewer'].strip()
and sorted(r.get('paths',[])) == sorted(paths)
and paths and set(paths) <= ALLOWED):
raise ValueError('Exact SHA, audited Stage acceptance and narrow reviewed diff required')
# The runtime probe below checks missing columns only. Independent reviewed
# catalog evidence must cover native types and constraints; it is not inferred
# from Prisma DMMF scalar names or from this probe passing.
typed = r.get('typed_catalog_acceptance')
if not (isinstance(typed, dict) and typed.get('accepted') is True
and typed.get('sha') == sha
and typed.get('scope') == 'native-types-and-constraints'
and isinstance(typed.get('reviewer'), str) and typed['reviewer'].strip()
and re.fullmatch(r'[0-9a-f]{64}', typed.get('evidence_sha256', ''))):
raise ValueError('Independent reviewed typed catalog acceptance required')
def private_json(path):
p=Path(path); s=p.lstat()
if not stat.S_ISREG(s.st_mode) or s.st_uid != 0 or s.st_mode & 0o077 or s.st_nlink != 1:
raise ValueError('Root-owned private regular acceptance receipt required')
return json.loads(p.read_text())
def snapshot(items):
c={'services':{},'volumes':{},'networks':{'traefik_public':{'external':True,'name':'traefik_public'}}}
for x in items:
cfg=x['Config']; labels=cfg.get('Labels') or {}; name=labels.get('com.docker.compose.service')
if name not in {'backend_prod','frontend_prod'} or labels.get('com.docker.compose.project')!='prod' or x['Name']!='/canino_'+name:
raise ValueError('Unexpected production identity')
if set(x['NetworkSettings']['Networks']) != {'traefik_public'} or x['HostConfig'].get('Privileged') or x['HostConfig'].get('PortBindings'):
raise ValueError('Unexpected production isolation')
s={'image':x['Image'],'container_name':x['Name'][1:],'restart':x['HostConfig']['RestartPolicy']['Name'],
'environment':cfg['Env'],'labels':{k:v for k,v in labels.items() if not k.startswith('com.docker.compose.')},
'networks':['traefik_public'],'volumes':[]}
for key, source in [('user','User'),('working_dir','WorkingDir'),('entrypoint','Entrypoint'),('command','Cmd')]:
if cfg.get(source): s[key]=cfg[source]
for m in x['Mounts']:
if m['Type']=='volume' and m.get('Name')=='prod_canina_prod_uploads' and m['Destination']=='/app/uploads':
c['volumes'][m['Name']]={'external':True,'name':m['Name']}
source=m['Name']
elif m['Type']=='bind' and m['Source']=='/opt/services/proxy/certs/canina' and m['Destination']=='/app/ssl_certs': source=m['Source']
else: raise ValueError('Unreviewed production mount')
s['volumes'].append({'type':m['Type'],'source':source,'target':m['Destination'],'read_only':not m['RW']})
if name=='backend_prod' and not any(v['target']=='/app/uploads' for v in s['volumes']): raise ValueError('Missing uploads')
c['services'][name]=s
return c
def validate_catalog(models, catalog):
"""Missing-column guard only; does not validate native types or constraints.
A String field backed by int4 still passes here. Independent typed catalog
acceptance is mandatory in validate_receipt before production replacement.
"""
if not models or not catalog:
raise ValueError('Empty generated model or production catalog evidence')
actual = {(r['table'], r['column']): r['type'] for r in catalog}
missing = []
for model in models:
table = model.get('dbName') or model['name']
for field in model['fields']:
if field['kind'] == 'object':
continue
column = field.get('dbName') or field['name']
if (table, column) not in actual:
missing.append(table + '.' + column)
if missing:
raise ValueError('Missing production catalog columns: ' + ', '.join(missing))
def schema_gate(target):
"""Read-only missing-column guard, not a full schema compatibility check.
No checkout/schema diff can substitute for this check. A newly generated
client may require columns which were already in both source revisions.
"""
def read(args):
return json.loads(subprocess.check_output(args, text=True))
before = read(['docker', 'inspect', 'canino_backend_prod'])[0]
if not before['State']['Running'] or not re.fullmatch(r'sha256:[0-9a-f]{64}', before['Image']):
raise ValueError('Running immutable production backend required')
image = read(['docker', 'image', 'inspect', target])[0]['Id']
if not re.fullmatch(r'sha256:[0-9a-f]{64}', image):
raise ValueError('Immutable target image required')
# DMMF comes from the generated client in the built image, NOT schema.prisma.
models = read(['docker', 'run', '--rm', '--network', 'none', '--read-only',
'--cap-drop=ALL', '--security-opt=no-new-privileges', '--entrypoint',
'node', image, '-e',
'console.log(JSON.stringify(require("@prisma/client").Prisma.dmmf.datamodel.models))'])
# Use only the running backend's private connection; never print credentials.
# The transaction itself enforces READ ONLY even if runtime DB role can write.
probe = r'''(async()=>{const {PrismaClient}=require('@prisma/client');const p=new PrismaClient();
try {const rows=await p.$transaction(async tx=>{
await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY');
return await tx.$queryRawUnsafe(`SELECT c.relname AS "table", a.attname AS "column", t.typname AS "type"
FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid
JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
JOIN pg_catalog.pg_type t ON t.oid=a.atttypid
WHERE n.nspname='public' AND c.relkind IN ('r','p','v','m') AND a.attnum>0 AND NOT a.attisdropped`);
});console.log(JSON.stringify(rows));} finally {await p.$disconnect();}
})().catch(()=>{console.error('Read-only production catalog inspection failed');process.exitCode=1});'''
catalog = read(['docker', 'exec', before['Id'], 'node', '-e', probe])
validate_catalog(models, catalog)
after = read(['docker', 'inspect', 'canino_backend_prod'])[0]
if after['Id'] != before['Id'] or after['Image'] != before['Image'] or not after['State']['Running']:
raise ValueError('Production runtime changed during missing-column check')
return {'target_image': image, 'runtime_image': before['Image'],
'runtime_container': before['Id'], 'catalog_columns': len(catalog),
'scope': 'missing-columns-only', 'native_types_checked': False,
'constraints_checked': False}
def main():
action=sys.argv[1]
if action=='receipt':
sha=os.environ['DEPLOY_SHA']
r=private_json('/opt/services/canina/production-acceptance/'+sha+'.json')
# The reviewer binds the source baseline to actual immutable runtime images.
# A shared checkout is not evidence of what the running client contains.
base=r.get('baseline', '')
if not re.fullmatch(r'[0-9a-f]{40}', base):
raise ValueError('Reviewed immutable source baseline required')
prod=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod']))
actual_prod={x['Name'][1:]:x['Image'] for x in prod}
if (set(actual_prod) != {'canino_backend_prod','canino_frontend_prod'}
or r.get('production_images') != actual_prod
or not all(x['State']['Running'] and re.fullmatch(r'sha256:[0-9a-f]{64}',x['Image']) for x in prod)):
raise ValueError('Reviewed production immutable image provenance required')
paths=subprocess.check_output(['git','diff','--name-only',base,sha],text=True).splitlines()
validate_receipt(r,sha,base,paths)
# Bind audited evidence to the two currently running Stage artifacts.
stage=json.loads(subprocess.check_output(['docker','inspect','canino_backend_stage','canino_frontend_stage']))
actual={x['Name'][1:]:x['Image'] for x in stage}
if r.get('stage_images') != actual or not all(x['State']['Running'] for x in stage):
raise ValueError('Audited Stage artifacts no longer running')
for x in stage:
wd=x['Config']['Labels'].get('com.docker.compose.project.working_dir')
if wd != '/opt/services/canina/releases/'+sha:
raise ValueError('Stage source SHA mismatch')
marker=subprocess.check_output(['docker','exec','canino_backend_stage','cat','/app/STAGE_TEST_ARTIFACT_SHA'],text=True)
if marker != sha: raise ValueError('Stage backend artifact SHA mismatch')
elif action=='schema-check':
path=Path('.prod-compose.json')
c=json.loads(path.read_text())
evidence=schema_gate(c['services']['backend_prod']['image'])
# Prevent a mutable tag moving between compatibility check and replacement.
c['services']['backend_prod']['image']=evidence['target_image']
path.write_text(json.dumps(c))
Path('.prod-schema-evidence.json').write_text(json.dumps(evidence))
elif action=='snapshot':
items=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod']))
c=snapshot(items)
Path('.prod-rollback.json').write_text(json.dumps(c))
sha=os.environ['DEPLOY_SHA']
for name,s in c['services'].items():
# Keep operational environment, labels, mounts and networks, not dirty checkout compose.
s['image']='canina-production-'+name+':'+sha
s.pop('entrypoint',None); s.pop('command',None)
s.setdefault('labels',{})['ir.canina.release.sha']=sha
s['env_file']=[{'path':str(Path.cwd()/'.revalidation.env'),'required':True}]
s['environment']=[v for v in s['environment'] if not v.startswith('REVALIDATION_SECRET=')]
if name=='backend_prod':
s['environment']=[v for v in s['environment'] if not v.startswith('FRONTEND_INTERNAL_URL=')]
s['environment'].append('FRONTEND_INTERNAL_URL=http://frontend_prod:3000')
s['build']={'context':str(Path.cwd() / ('backend' if name=='backend_prod' else '.')),'dockerfile':'Dockerfile',
'args':{'NPM_REGISTRY':'https://registry.parsaaghayi.ir/repository/npm/'}}
if name=='frontend_prod': s['build']['args'].update({'NEXT_PUBLIC_API_URL':'https://api.canina.ir/api','VITE_API_URL':'https://api.canina.ir'})
Path('.prod-compose.json').write_text(json.dumps(c))
else: raise ValueError('Unknown action')
if __name__=='__main__': main()