"""Build the complete source snapshot and boot a separate stack with empty volumes. The active project and its data are untouched. Credentials and source copies are kept in a temporary directory; only our UUID-named test project is removed. """ import hashlib import json import os import secrets import shutil import socket import subprocess import sys import time from pathlib import Path from tempfile import TemporaryDirectory ROOT=Path(__file__).resolve().parents[1] project='lab4-clean-'+secrets.token_hex(4) with socket.socket() as sock: sock.bind(('127.0.0.1',0));port=sock.getsockname()[1] inventory=subprocess.run(['git','ls-files','--cached','--others','--exclude-standard','-z'],cwd=ROOT,capture_output=True) if inventory.returncode==0: files=inventory.stdout.split(b'\0') else: manifest_path=ROOT/'SOURCE_MANIFEST.json' if not manifest_path.is_file(): raise SystemExit('Нужен Git clone или архив с SOURCE_MANIFEST.json') files=[os.fsencode(name) for name in json.loads(manifest_path.read_text())['files']] proof={'project':project,'empty_volumes':True,'source':'complete working snapshot including new files', 'build_cache':'API rebuilt with --no-cache; pinned MinIO build may use BuildKit cache'} with TemporaryDirectory(prefix='lab4-clean-') as temporary: source=Path(temporary)/'source';source.mkdir() manifest={} for raw in files: if not raw:continue relative=Path(os.fsdecode(raw)) original=ROOT/relative if not original.is_file():continue target=source/relative;target.parent.mkdir(parents=True,exist_ok=True) shutil.copy2(original,target) if relative.parts[0] in ('src','migrations','infra') or str(relative) in ('Dockerfile','Dockerfile.dockerignore','requirements.txt','requirements-student.txt', 'student.env.example','student.mk','compose.yaml','alembic.ini'): manifest[str(relative)]=hashlib.sha256(original.read_bytes()).hexdigest() subprocess.run([sys.executable,str(source/'scripts/setup-env.py')],cwd=source,check=True,capture_output=True) env_file=source/'.env' text=env_file.read_text().replace('APP_PORT=8080','APP_PORT='+str(port)) env_file.write_text(text) environment={**os.environ,'COMPOSE_PROJECT_NAME':project,'ENV_FILE':str(env_file), 'APP_PORT':str(port),'APP_IMAGE':'mtusi-messenger:'+project, 'MINIO_IMAGE':'mtusi-minio:'+project} # A clean launch must not inherit deployment overrides from the current shell. for name in ('POSTGRES_PASSWORD','POSTGRES_USER','POSTGRES_DB','MINIO_ROOT_USER', 'MINIO_ROOT_PASSWORD','S3_ACCESS_KEY_ID','S3_SECRET_ACCESS_KEY','S3_RECONCILE_ACCESS_KEY_ID', 'S3_RECONCILE_SECRET_ACCESS_KEY','UPLOAD_CONCURRENCY','API_REPLICAS','API_MEMORY'): environment.pop(name,None) command=['docker','compose','--env-file',str(env_file)] def run(*args,timeout=300): return subprocess.run(args,cwd=source,env=environment,text=True,check=True, capture_output=True,timeout=timeout) def run_build(*args): started=time.perf_counter() result=run(*command,'build',*args,timeout=900) return {'seconds':round(time.perf_counter()-started,3),'exit_code':result.returncode} try: proof['api_build']=run_build('--no-cache','api') proof['minio_build']=run_build('minio') run(*command,'up','-d','--no-build','--wait',timeout=180) version=run(*command,'exec','-T','postgres','sh','-c', 'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Atc "SELECT version_num FROM alembic_version"').stdout.strip() assert version=='008',version proof['schema_revision']=version proof['public_smoke']=run(sys.executable,'tests/smoke.py','--base-url',f'http://127.0.0.1:{port}').stdout.strip() proof['attachments']=run(sys.executable,'tests/attachments.py','--base-url',f'http://127.0.0.1:{port}').stdout.strip() proof['reconciler']=json.loads(run(*command,'exec','-T','reconciler','python','-m','src.maintenance.reconcile').stdout) assert proof['reconciler']['result']=='ok' rows=run(*command,'ps','--format','json').stdout proof['services']=[{'service':r['Service'],'state':r['State'],'health':r.get('Health','')} for r in (json.loads(line) for line in rows.splitlines() if line.strip())] proof['code_manifest']=manifest proof['result']='PASS' except subprocess.CalledProcessError as error: # Never dump compose config/environment; build/application outputs exclude credentials. print((error.stdout+error.stderr)[-4000:]) raise finally: run(*command,'down','--volumes','--remove-orphans',timeout=90) proof['temporary_project_and_volumes_removed']=True run('docker','image','rm',environment['APP_IMAGE'],environment['MINIO_IMAGE'],timeout=60) proof['temporary_image_tags_removed']=True (ROOT/'evidence/lab4-clean-start.json').write_text(json.dumps(proof,ensure_ascii=False,indent=2)+'\n') print(json.dumps({k:v for k,v in proof.items() if k!='code_manifest'},ensure_ascii=False,indent=2))