Implement lab 5 durable image processing worker
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
"""Explicit local Compose failure hooks. Never deletes volumes."""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
os.chdir(ROOT)
|
||||
mode = sys.argv[1]
|
||||
if mode == 'restart':
|
||||
services = subprocess.check_output(['docker', 'compose', 'ps', '--services', '--status', 'running'], text=True).split()
|
||||
subprocess.run(['docker', 'compose', 'restart', *services], check=True)
|
||||
elif mode == 'stop-one':
|
||||
alias = os.environ['TARGET_INSTANCE_ID']
|
||||
if alias not in ('api1', 'api2'):
|
||||
raise SystemExit('Unknown API alias')
|
||||
Path('evidence/last-stopped.json').write_text(json.dumps({'service': alias}), encoding='utf-8')
|
||||
subprocess.run(['docker', 'compose', 'stop', '-t', '2', alias], check=True)
|
||||
else:
|
||||
raise SystemExit('Unknown action')
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Create local synthetic secrets once; never overwrite existing credentials."""
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
directory = root / 'secrets'
|
||||
directory.mkdir(exist_ok=True)
|
||||
password_file = directory / 'db_password'
|
||||
if not password_file.exists():
|
||||
with password_file.open('x', encoding='utf-8') as file:
|
||||
file.write(secrets.token_urlsafe(32))
|
||||
password = password_file.read_text(encoding='utf-8').strip()
|
||||
url_file = directory / 'database_url'
|
||||
if not url_file.exists():
|
||||
with url_file.open('x', encoding='utf-8') as file:
|
||||
file.write('postgresql://messenger:' + password + '@db:5432/messenger')
|
||||
env = root / '.env'
|
||||
if not env.exists():
|
||||
with env.open('x', encoding='utf-8') as file:
|
||||
file.write('SESSION_TTL_SECONDS=3600\n')
|
||||
print('Local configuration ready; credentials are not printed.')
|
||||
for name in ('s3_root_user','s3_root_password','s3_access_key','s3_secret_key'):
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
with path.open('x', encoding='utf-8') as file:
|
||||
file.write(secrets.token_hex(16))
|
||||
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
python scripts/action.py restart
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/action.py restart
|
||||
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
python scripts/action.py stop-one
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/action.py stop-one
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Run public and student acceptance, preserving sanitized evidence."""
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
os.chdir(ROOT)
|
||||
EVIDENCE = ROOT / 'evidence'
|
||||
ENV = {**os.environ, 'PYTHONUTF8': '1', 'BASE_URL': os.getenv('BASE_URL', 'http://127.0.0.1:8080')}
|
||||
|
||||
|
||||
def run(args, artifact):
|
||||
result = subprocess.run(args, cwd=ROOT, capture_output=True, text=True,
|
||||
encoding='utf-8', errors='replace', env=ENV)
|
||||
output = result.stdout + result.stderr
|
||||
(EVIDENCE / artifact).write_text(output, encoding='utf-8')
|
||||
print(output, end='', flush=True)
|
||||
if result.returncode:
|
||||
raise SystemExit(result.returncode)
|
||||
return output
|
||||
|
||||
|
||||
def containers():
|
||||
ids = subprocess.check_output(['docker', 'compose', 'ps', '-q'], text=True).split()
|
||||
raw = json.loads(subprocess.check_output(['docker', 'inspect', *ids], text=True))
|
||||
return [{'name': x['Name'], 'id': x['Id'], 'started_at': x['State']['StartedAt'],
|
||||
'image': x['Config']['Image']} for x in raw]
|
||||
|
||||
|
||||
def main():
|
||||
run([sys.executable, 'scripts/check_contract.py'], 'contract.txt')
|
||||
run([sys.executable, 'tests/smoke.py'], 'smoke.txt')
|
||||
before = containers()
|
||||
suffix = '.cmd' if os.name == 'nt' else '.sh'
|
||||
run([sys.executable, 'tests/resilience.py', '--mode', 'persistence',
|
||||
'--action-script', 'scripts/restart' + suffix], 'persistence.txt')
|
||||
after = containers()
|
||||
old = {x['id']: x for x in before}
|
||||
assert all(x['id'] in old and x['started_at'] != old[x['id']]['started_at'] for x in after)
|
||||
(EVIDENCE / 'restart.json').write_text(json.dumps({'before':before,'after':after},indent=2),encoding='utf-8')
|
||||
try:
|
||||
run([sys.executable, 'tests/resilience.py', '--mode', 'failover',
|
||||
'--action-script', 'scripts/stop-one' + suffix], 'failover.txt')
|
||||
finally:
|
||||
subprocess.run(['docker','compose','up','-d','--no-deps','--wait','api1','api2'], check=True)
|
||||
time.sleep(2)
|
||||
run([sys.executable, 'student-tests/replication.py'], 'replication.txt')
|
||||
if (ROOT / 'student-tests/attachments.py').exists():
|
||||
run([sys.executable, 'student-tests/attachments.py'], 'attachments.txt')
|
||||
if (ROOT / 'student-tests/pipeline.py').exists():
|
||||
run([sys.executable, 'student-tests/pipeline.py'], 'pipeline.txt')
|
||||
if (ROOT / 'student-tests/observability.py').exists():
|
||||
run([sys.executable, 'student-tests/observability.py'], 'observability.txt')
|
||||
process = subprocess.Popen([sys.executable,'tests/load.py','--duration','30','--concurrency','4',
|
||||
'--output','evidence/load.json'], stdout=subprocess.PIPE,stderr=subprocess.STDOUT,
|
||||
text=True,encoding='utf-8',env=ENV)
|
||||
ids = [x['id'] for x in containers()]
|
||||
samples=[]
|
||||
while process.poll() is None:
|
||||
stats=subprocess.check_output(['docker','stats','--no-stream','--format',
|
||||
'{{.Name}} CPU={{.CPUPerc}} RAM={{.MemUsage}}',*ids],text=True)
|
||||
samples.append({'at':datetime.now(timezone.utc).isoformat(),'stats':stats.strip()})
|
||||
time.sleep(2)
|
||||
output=process.communicate()[0]
|
||||
(EVIDENCE/'load.txt').write_text(output,encoding='utf-8')
|
||||
(EVIDENCE/'resources.json').write_text(json.dumps(samples,indent=2),encoding='utf-8')
|
||||
assert process.returncode==0, output
|
||||
(EVIDENCE/'environment.json').write_text(json.dumps({'platform':platform.platform(),
|
||||
'cpu':platform.processor(),'logical_cpus':os.cpu_count(),'observed_at':datetime.now(timezone.utc).isoformat()},indent=2),encoding='utf-8')
|
||||
print(output,flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user