Implement lab4 grade 3: private S3 attachments and persistence checks
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local synthetic credentials once; never print them or replace existing files."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
folder = root / 'secrets'
|
||||
folder.mkdir(exist_ok=True)
|
||||
password_file = folder / 'db_password'
|
||||
if not password_file.exists():
|
||||
password_file.write_text(secrets.token_hex(24), encoding='utf-8')
|
||||
password = password_file.read_text(encoding='utf-8').strip()
|
||||
values = {
|
||||
'database_url': 'postgresql://messenger:' + password + '@db:5432/messenger',
|
||||
'cache_url': 'redis://redis:6379/0',
|
||||
'cursor_key': secrets.token_hex(32),
|
||||
's3_access_key_id': 'lab4-' + secrets.token_hex(8),
|
||||
's3_secret_access_key': secrets.token_hex(24),
|
||||
}
|
||||
for name, value in values.items():
|
||||
target = folder / name
|
||||
if not target.exists():
|
||||
target.write_text(value, encoding='utf-8')
|
||||
if not (root / '.env').exists():
|
||||
(root / '.env').write_text('APP_HOST=0.0.0.0\nAPP_PORT=8084\nAPP_ENV=lab\nSESSION_TTL_SECONDS=3600\nSESSION_STORE=redis\nAPI_REPLICAS=2\n', encoding='utf-8')
|
||||
print('Local .env and secrets ready (existing values preserved).')
|
||||
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Operate only the lab4 Compose project; restart hooks never delete volumes."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(list(args), cwd=ROOT, text=True, encoding='utf-8').strip()
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return command('docker', 'compose', '--env-file', '.env', *args)
|
||||
|
||||
|
||||
def containers(service=None, all_states=False):
|
||||
ids = compose('ps', '-q', *(['--all'] if all_states else []), *([service] if service else [])).split()
|
||||
if not ids:
|
||||
return []
|
||||
result = []
|
||||
for item in json.loads(command('docker', 'inspect', *ids)):
|
||||
result.append(dict(id=item['Id'], name=item['Name'].lstrip('/'),
|
||||
service=item['Config']['Labels']['com.docker.compose.service'],
|
||||
hostname=item['Config']['Hostname'], pid=item['State']['Pid'],
|
||||
started_at=item['State']['StartedAt'], status=item['State']['Status'],
|
||||
health=item['State'].get('Health', {}).get('Status'), image=item['Image'],
|
||||
memory_limit=item['HostConfig']['Memory'],
|
||||
cpu_limit=item['HostConfig']['NanoCpus'] / 1e9,
|
||||
published_ports=item['NetworkSettings']['Ports'],
|
||||
mounts=[dict(name=m.get('Name'), destination=m['Destination'])
|
||||
for m in item['Mounts'] if m['Type'] == 'volume']))
|
||||
return sorted(result, key=lambda x: x['name'])
|
||||
|
||||
|
||||
def save(name, data):
|
||||
folder = ROOT / 'evidence'
|
||||
folder.mkdir(exist_ok=True)
|
||||
(folder / name).write_text(json.dumps(data, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
|
||||
|
||||
|
||||
def restore():
|
||||
compose('up', '-d', '--no-deps', '--no-recreate', 'api')
|
||||
deadline = time.monotonic() + 45
|
||||
while time.monotonic() < deadline:
|
||||
items = containers('api')
|
||||
if len(items) == 2 and all(x['health'] == 'healthy' for x in items):
|
||||
time.sleep(3)
|
||||
return
|
||||
time.sleep(.5)
|
||||
raise RuntimeError('Two API replicas did not become healthy')
|
||||
|
||||
|
||||
def stop_one():
|
||||
target = os.environ.get('TARGET_INSTANCE_ID')
|
||||
before = containers('api')
|
||||
assert len(before) >= 2, 'Need two live replicas'
|
||||
selected = [x for x in before if target and target.startswith(x['hostname'] + '-')]
|
||||
assert len(selected) == 1, 'TARGET_INSTANCE_ID must identify one live API process'
|
||||
started = time.monotonic()
|
||||
command('docker', 'stop', '--time', '8', selected[0]['id'])
|
||||
after = containers('api', True)
|
||||
assert next(x for x in after if x['id'] == selected[0]['id'])['status'] == 'exited'
|
||||
save('stop-one.json', dict(target_instance=target, before=before, after=after,
|
||||
hook_seconds=time.monotonic() - started))
|
||||
print('Selected API stopped; another replica remains running.')
|
||||
|
||||
|
||||
def restart():
|
||||
before = containers()
|
||||
compose('restart', 'api', 'db', 'redis', 's3', 'lb')
|
||||
after = containers()
|
||||
for old in before:
|
||||
new = next(x for x in after if x['id'] == old['id'])
|
||||
assert old['started_at'] != new['started_at'], old['name'] + ' did not restart'
|
||||
assert old['mounts'] == new['mounts'], 'Persistent volumes changed'
|
||||
save('restart.json', dict(before=before, after=after))
|
||||
print('API, PostgreSQL, Redis, MinIO and LB restarted; same volumes preserved.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('action', choices=['restart', 'stop-one', 'restore', 'state'])
|
||||
action = parser.parse_args().action
|
||||
if action == 'state':
|
||||
print(json.dumps(containers(all_states=True), indent=2))
|
||||
else:
|
||||
{'restart': restart, 'stop-one': stop_one, 'restore': restore}[action]()
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
cd /d "%~dp0\.."
|
||||
python -X utf8 scripts\labctl.py restart
|
||||
exit /b %errorlevel%
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/labctl.py restart
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
cd /d "%~dp0\.."
|
||||
python -X utf8 scripts\labctl.py stop-one
|
||||
exit /b %errorlevel%
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/labctl.py stop-one
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the grade 3 checks and keep small sanitized, reproducible evidence files."""
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EVIDENCE = ROOT / 'evidence'
|
||||
BASE = os.getenv('BASE_URL', 'http://127.0.0.1:8084')
|
||||
ENV = dict(os.environ, BASE_URL=BASE, PYTHONUTF8='1', PYTHONIOENCODING='utf-8')
|
||||
|
||||
|
||||
def run(name, args):
|
||||
print('Running', name, flush=True)
|
||||
proc = subprocess.run(args, cwd=ROOT, env=ENV, text=True, encoding='utf-8',
|
||||
errors='replace', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
output = '$ ' + ' '.join(args).replace(sys.executable, 'python') + '\n' + proc.stdout
|
||||
output = '\n'.join(line.rstrip() for line in output.splitlines()).rstrip() + '\n'
|
||||
(EVIDENCE / (name + '.txt')).write_text(output, encoding='utf-8')
|
||||
print(proc.stdout, end='', flush=True)
|
||||
if proc.returncode:
|
||||
raise SystemExit('Check failed: ' + name)
|
||||
|
||||
|
||||
def main():
|
||||
EVIDENCE.mkdir(exist_ok=True)
|
||||
run('check', [sys.executable, 'scripts/check_contract.py'])
|
||||
run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet'])
|
||||
run('smoke', [sys.executable, 'tests/smoke.py', '--base-url', BASE])
|
||||
run('idempotency', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_idempotency.py'])
|
||||
run('limits', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_limits.py'])
|
||||
run('attachments-grade3', [sys.executable, 'student-tests/grade3.py'])
|
||||
run('persistence', [sys.executable, 'tests/resilience.py', '--mode', 'persistence', '--base-url', BASE,
|
||||
'--action-script', 'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh'])
|
||||
run('failover', [sys.executable, 'tests/resilience.py', '--mode', 'failover', '--base-url', BASE,
|
||||
'--action-script', 'scripts/stop-one.cmd' if os.name == 'nt' else 'scripts/stop-one.sh'])
|
||||
run('restore', [sys.executable, 'scripts/labctl.py', 'restore'])
|
||||
run('smoke-after', [sys.executable, 'tests/smoke.py', '--base-url', BASE])
|
||||
run('processes', [sys.executable, 'scripts/labctl.py', 'state'])
|
||||
info = json.loads(subprocess.check_output(['docker', 'info', '--format', '{{json .}}'], env=ENV, text=True))
|
||||
environment = dict(verified_at_utc=datetime.now(timezone.utc).isoformat(), host_os=platform.platform(),
|
||||
client_python=platform.python_version(),
|
||||
docker={k: info[k] for k in ('ServerVersion', 'OperatingSystem', 'KernelVersion',
|
||||
'NCPU', 'MemTotal', 'Architecture')}, images={})
|
||||
environment['app_python'] = subprocess.check_output(
|
||||
['docker', 'compose', 'exec', '-T', 'api', 'python', '--version'], text=True, env=ENV).strip()
|
||||
for name in ('postgres:17.6-bookworm', 'redis:7.4.5-bookworm', 'haproxy:3.0.16-alpine',
|
||||
'minio/minio:RELEASE.2025-04-22T22-12-26Z'):
|
||||
environment['images'][name] = json.loads(subprocess.check_output(
|
||||
['docker', 'image', 'inspect', name, '--format', '{{json .RepoDigests}}'], text=True, env=ENV))
|
||||
(EVIDENCE / 'environment.json').write_text(json.dumps(environment, indent=2) + '\n', encoding='utf-8')
|
||||
print('PASS: all required grade 3 scenarios completed.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,19 @@
|
||||
"""Wait for the public entrypoint after starting the Compose stack."""
|
||||
import os
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
base = os.getenv('BASE_URL', 'http://127.0.0.1:8084').rstrip('/')
|
||||
deadline = time.monotonic() + 90
|
||||
while True:
|
||||
try:
|
||||
with urllib.request.urlopen(base + '/health/ready', timeout=3) as response:
|
||||
if response.status == 200:
|
||||
print('READY:', base)
|
||||
break
|
||||
except (urllib.error.URLError, OSError):
|
||||
pass
|
||||
if time.monotonic() >= deadline:
|
||||
raise SystemExit('Public API did not become ready within 90 seconds')
|
||||
time.sleep(.5)
|
||||
Reference in New Issue
Block a user