59 lines
2.9 KiB
Python
59 lines
2.9 KiB
Python
"""Чистый Git-экспорт, новые секреты и отдельный временный Compose-проект."""
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import uuid
|
|
import zipfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
name = 'lyakhonov-lab5-repro-' + uuid.uuid4().hex[:8]
|
|
directory = Path(tempfile.mkdtemp(prefix=name + '-'))
|
|
archive = subprocess.check_output(['git', 'archive', '--format=zip', 'HEAD'], cwd=ROOT)
|
|
with zipfile.ZipFile(io.BytesIO(archive)) as contents:
|
|
contents.extractall(directory)
|
|
with socket.socket() as listener:
|
|
listener.bind(('127.0.0.1', 0))
|
|
port = listener.getsockname()[1]
|
|
env = dict(os.environ, LAB5_PORT=str(port), LAB5_FAULT_INJECTION='0', JOB_TIMEOUT_SECONDS='60', JOB_LEASE_SECONDS='90')
|
|
services = ('api', 'migrate', 'worker', 'dispatcher', 'staging-cleaner')
|
|
images = [name + ':app', name + ':s3']
|
|
override = {'services': {service: {'image': images[0]} for service in services}}
|
|
override['services'].update({service: {'image': images[1]} for service in ('s3', 'bucket-init')})
|
|
(directory / 'repro-override.json').write_text(json.dumps(override), encoding='utf-8')
|
|
compose = ['docker', 'compose', '-p', name, '-f', 'compose.yaml', '-f', 'repro-override.json']
|
|
events = []
|
|
def run(args):
|
|
result = subprocess.run(args, cwd=directory, env=env, text=True, encoding='utf-8', capture_output=True)
|
|
# Только безопасные последние строки: ни inspect .Config.Env, ни secret files.
|
|
events.append(dict(command=args, exit_code=result.returncode, output=(result.stdout + result.stderr)[-5000:]))
|
|
if result.returncode:
|
|
raise RuntimeError('reproduction failed: ' + ' '.join(args))
|
|
try:
|
|
run([sys.executable, '-X', 'utf8', 'scripts/bootstrap.py'])
|
|
run([sys.executable, '-X', 'utf8', 'scripts/check_contract.py'])
|
|
run(['docker', 'build', '-t', images[0], '.'])
|
|
run(['docker', 'build', '-t', images[1], '-f', 'Dockerfile.s3', '.'])
|
|
run([*compose, 'up', '-d', '--wait'])
|
|
run([sys.executable, '-X', 'utf8', 'tests/smoke.py', '--base-url', f'http://127.0.0.1:{port}'])
|
|
run([*compose, 'ps'])
|
|
finally:
|
|
run([*compose, 'down', '--volumes', '--remove-orphans'])
|
|
for tag in images:
|
|
run(['docker', 'image', 'rm', tag])
|
|
result = dict(project=name, directory=str(directory), port=port,
|
|
commit=subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip(), events=events)
|
|
(ROOT / 'evidence/reproduction.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
print('PASS: clean export, fresh secrets, isolated project; temporary Docker resources removed')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|