"""Clean HEAD export, new secrets/images/volumes/ports; cleanup only this experiment.""" import json import os from pathlib import Path import shutil import socket import subprocess import sys import tempfile import time import uuid import zipfile ROOT=Path(__file__).resolve().parents[1] def port(): with socket.socket() as sock: sock.bind(('127.0.0.1',0)) return sock.getsockname()[1] def main(): tag=uuid.uuid4().hex[:10];project='lyakhonov-lab6-repro-'+tag directory=Path(tempfile.mkdtemp(prefix='lab6-repro-')).resolve() assert directory.parent==Path(tempfile.gettempdir()).resolve() and directory.name.startswith('lab6-repro-') api_port,grafana_port=port(),port() while grafana_port==api_port:grafana_port=port() app='lyakhonov-messenger:lab6-repro-'+tag;s3='lyakhonov-storage:lab6-repro-'+tag env=dict(os.environ,COMPOSE_PROJECT_NAME=project,LAB6_PORT=str(api_port),GRAFANA_PORT=str(grafana_port), BASE_URL=f'http://127.0.0.1:{api_port}',GRAFANA_URL=f'http://127.0.0.1:{grafana_port}', APP_IMAGE=app,S3_IMAGE=s3,PYTHONUTF8='1') result={'project':project,'directory':str(directory),'ports':[api_port,grafana_port], 'images':[app,s3],'commit':subprocess.check_output(['git','rev-parse','HEAD'],cwd=ROOT,text=True).strip(),'steps':[]} archive=directory/'source.zip';checkout=directory/'checkout';checkout.mkdir() def run(*args,timeout=600): started=time.time() proc=subprocess.run(args,cwd=checkout,env=env,capture_output=True,text=True,timeout=timeout) result['steps'].append({'command':list(args),'exit_code':proc.returncode,'duration':time.time()-started, 'output':(proc.stdout+proc.stderr)[-12000:]}) (ROOT/'evidence/reproduction.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8') if proc.returncode:raise RuntimeError('reproduction step failed: '+args[0]) try: subprocess.check_call(['git','archive','--format=zip','--output',str(archive),'HEAD'],cwd=ROOT) with zipfile.ZipFile(archive) as zipped:zipped.extractall(checkout) run(sys.executable,'scripts/bootstrap.py') run('docker','compose','-p',project,'config','--quiet') run('docker','compose','-p',project,'build','api','s3',timeout=1200) run('docker','compose','-p',project,'up','-d','--no-build') run(sys.executable,'scripts/wait_ready.py','--project',project,'--base-url',env['BASE_URL'],'--telemetry') run(sys.executable,'tests/smoke.py','--base-url',env['BASE_URL']) run(sys.executable,'student-tests/signals.py') ids=subprocess.check_output(['docker','compose','-p',project,'ps','-q'],cwd=checkout,env=env,text=True).split() inspected=json.loads(subprocess.check_output(['docker','inspect',*ids],text=True)) result['containers']=[{'id':r['Id'],'service':r['Config']['Labels']['com.docker.compose.service'], 'started_at':r['State']['StartedAt'],'mounts':[m.get('Name') for m in r['Mounts'] if m['Type']=='volume']} for r in inspected] result['checks']='PASS' finally: cleanup=subprocess.run(['docker','compose','-p',project,'down','-v','--remove-orphans'],cwd=checkout,env=env,capture_output=True,text=True) result['cleanup']={'exit_code':cleanup.returncode,'output':cleanup.stdout+cleanup.stderr} if cleanup.returncode==0: subprocess.run(['docker','image','rm',app,s3],capture_output=True,text=True) resolved=directory.resolve() assert resolved.parent==Path(tempfile.gettempdir()).resolve() and resolved.name=='lab6-repro-'+directory.name.removeprefix('lab6-repro-') shutil.rmtree(resolved) (ROOT/'evidence/reproduction.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8') print('PASS: clean Git export, new secrets and isolated stack; temporary resources removed') if __name__=='__main__':main()