forked from mtusi2026/mtusi2026lab5
готво 5
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
"""Управление только API-контейнерами текущего Compose-проекта."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.check_output(["docker", *args], cwd=ROOT, text=True).strip()
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return docker("compose", "--env-file", os.environ.get("ENV_FILE", ".env"), *args)
|
||||
|
||||
|
||||
def containers():
|
||||
return compose("ps", "--all", "-q", "api").split()
|
||||
|
||||
|
||||
def describe(container):
|
||||
# Никогда не выводим Config.Env: там credentials.
|
||||
return json.loads(docker("inspect", "--format", "{{json .State}}", container))
|
||||
|
||||
|
||||
def instance_id(container):
|
||||
code = "import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2); print(r.headers['X-Instance-Id'])"
|
||||
return docker("exec", container, "python", "-c", code)
|
||||
|
||||
|
||||
def wait_ready(container, timeout=60):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
state = describe(container)
|
||||
if state.get("Health", {}).get("Status") == "healthy":
|
||||
return instance_id(container)
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError("API container did not become healthy: " + container[:12])
|
||||
|
||||
|
||||
def wait_visible(instance, timeout=30):
|
||||
base = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/")
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
with urllib.request.urlopen(base + "/health/ready", timeout=2) as response:
|
||||
if response.headers.get("X-Instance-Id") == instance:
|
||||
return
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
raise RuntimeError("New replica was not discovered by HAProxy")
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# TODO: copy to restart.sh and implement using YOUR compose/stack services.
|
||||
# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable).
|
||||
# Preserve persistent volumes and schema/data; never down -v / flush / re-seed.
|
||||
# Allow operator unseal if required in lab7; do not print credentials.
|
||||
# Exit 0 only after the intended action; test waits for API readiness separately.
|
||||
printf '%s
|
||||
' 'TODO: implement restart.sh for your infrastructure' >&2
|
||||
exit 2
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
# Полный restart работающих ролей; volumes не удаляются.
|
||||
docker compose --env-file "${ENV_FILE:-.env}" restart postgres redis minio broker
|
||||
docker compose --env-file "${ENV_FILE:-.env}" up -d --wait --force-recreate api
|
||||
docker compose --env-file "${ENV_FILE:-.env}" restart reconciler dispatcher worker lb
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Create local synthetic credentials without overwriting an existing .env."""
|
||||
import argparse
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
root=Path(__file__).resolve().parents[1]
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--output',type=Path,default=root/'.env')
|
||||
args=parser.parse_args()
|
||||
values={
|
||||
'POSTGRES_PASSWORD':secrets.token_hex(24),
|
||||
'ADMIN_PASSWORD':secrets.token_hex(24),
|
||||
'MINIO_ROOT_PASSWORD':secrets.token_hex(24),
|
||||
'S3_SECRET_ACCESS_KEY':secrets.token_hex(24),
|
||||
'S3_RECONCILE_SECRET_ACCESS_KEY':secrets.token_hex(24),
|
||||
'S3_WORKER_SECRET_ACCESS_KEY':secrets.token_hex(24),
|
||||
}
|
||||
lines=(root/'.env.example').read_text().splitlines()
|
||||
content='\n'.join(key+'='+values[key] if '=' in line and (key:=line.split('=',1)[0]) in values else line for line in lines)+'\n'
|
||||
try:
|
||||
descriptor=os.open(args.output,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600)
|
||||
except FileExistsError:
|
||||
parser.exit(1,'Файл уже существует; его настройки и пароли сохранены.\n')
|
||||
with os.fdopen(descriptor,'w') as stream:
|
||||
stream.write(content)
|
||||
print('Создан файл локальных настроек с новыми паролями; значения не выводятся.')
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# TODO: copy to stop-one.sh, map TARGET_INSTANCE_ID to ONE API container/task.
|
||||
# Stop that instance and leave it stopped; no stop of DB/LB/volumes.
|
||||
# Return quickly (within 10 s); the test sends requests during this script.
|
||||
# Restore the replica explicitly after the test. Never print credentials.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
printf '%s
|
||||
' 'TODO: implement stop-one.sh for your infrastructure' >&2
|
||||
exit 2
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Сопоставить X-Instance-Id реальному контейнеру и оставить его остановленным."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
from replicas import containers, describe, docker, instance_id
|
||||
|
||||
target = os.environ["TARGET_INSTANCE_ID"]
|
||||
running = [c for c in containers() if describe(c)["Running"]]
|
||||
if len(running) < 2:
|
||||
raise SystemExit("Need at least two running API containers")
|
||||
for container in running:
|
||||
if instance_id(container) == target:
|
||||
before = describe(container)
|
||||
started = time.monotonic()
|
||||
docker("stop", "--time", "10", container)
|
||||
after = describe(container)
|
||||
assert not after["Running"]
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"container": container[:12],
|
||||
"instance_id": target,
|
||||
"started_at": before["StartedAt"],
|
||||
"finished_at": after["FinishedAt"],
|
||||
"stop_seconds": round(time.monotonic() - started, 3),
|
||||
"exit_code": after["ExitCode"],
|
||||
}
|
||||
)
|
||||
)
|
||||
break
|
||||
else:
|
||||
raise SystemExit("Requested instance not found in this Compose project")
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec "${PYTHON:-python3}" scripts/stop-one.py
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Исходный persistence-сценарий плюс доказательство рестарта API и всех хранилищ."""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def states():
|
||||
result = {}
|
||||
for service in ('api', 'postgres', 'redis', 'minio', 'broker', 'worker', 'dispatcher', 'reconciler', 'lb'):
|
||||
ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', service], cwd=ROOT, text=True).split()
|
||||
if not ids:
|
||||
raise RuntimeError('Не запущен сервис ' + service)
|
||||
result[service] = {}
|
||||
for container in ids:
|
||||
state = json.loads(subprocess.check_output(['docker', 'inspect', '--format', '{{json .State}}', container], cwd=ROOT, text=True))
|
||||
result[service][container] = {'StartedAt': state['StartedAt'], 'Pid': state['Pid']}
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--action-script', required=True)
|
||||
parser.add_argument('--ca-file')
|
||||
args = parser.parse_args()
|
||||
before = states()
|
||||
print('ДО: ' + json.dumps(before), flush=True)
|
||||
command = [sys.executable, str(ROOT / 'tests/resilience.py'), '--mode', 'persistence',
|
||||
'--action-script', args.action_script, '--base-url', args.base_url]
|
||||
if args.ca_file:
|
||||
command += ['--ca-file', args.ca_file]
|
||||
subprocess.run(command, cwd=ROOT, check=True)
|
||||
after = states()
|
||||
print('ПОСЛЕ: ' + json.dumps(after), flush=True)
|
||||
for service in before:
|
||||
if before[service] == after[service]:
|
||||
raise AssertionError('Сервис не перезапущен: ' + service)
|
||||
print('PASS: StartedAt изменился у API, PostgreSQL, Redis, MinIO, broker, worker, dispatcher, reconciler и LB; прежний токен и данные сохранены')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Reproducible lab5 acceptance on a fresh isolated Compose project."""
|
||||
import json
|
||||
import hashlib
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main():
|
||||
evidence = ROOT / 'evidence'
|
||||
evidence.mkdir(exist_ok=True)
|
||||
project = 'mtusilab5-' + uuid.uuid4().hex[:10]
|
||||
with socket.socket() as listener:
|
||||
listener.bind(('127.0.0.1', 0))
|
||||
port = listener.getsockname()[1]
|
||||
results = []
|
||||
code_manifest = {}
|
||||
for directory in ('src', 'migrations', 'infra'):
|
||||
for path in sorted((ROOT/directory).rglob('*')):
|
||||
if path.is_file() and '__pycache__' not in path.parts:
|
||||
code_manifest[str(path.relative_to(ROOT))] = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
for name in ('Dockerfile','compose.yaml','requirements.txt','lab.json'):
|
||||
code_manifest[name] = hashlib.sha256((ROOT/name).read_bytes()).hexdigest()
|
||||
with tempfile.TemporaryDirectory(prefix='mtusi-lab5-') as temporary:
|
||||
envfile = Path(temporary) / '.env'
|
||||
subprocess.run([sys.executable, 'scripts/setup-env.py', '--output', str(envfile)], cwd=ROOT, check=True)
|
||||
env = dict(os.environ, ENV_FILE=str(envfile), COMPOSE_ENV_FILES=str(envfile),
|
||||
COMPOSE_PROJECT_NAME=project, APP_PORT=str(port),
|
||||
BASE_URL=f'http://127.0.0.1:{port}')
|
||||
compose = ['docker', 'compose', '--env-file', str(envfile), '-p', project]
|
||||
def step(name, command):
|
||||
started = time.monotonic()
|
||||
path = evidence / ('lab5-' + name + '.txt')
|
||||
with path.open('w') as output:
|
||||
process = subprocess.run(command, cwd=ROOT, env=env, stdout=output, stderr=subprocess.STDOUT)
|
||||
result = dict(step=name, result='PASS' if process.returncode == 0 else 'FAIL',
|
||||
seconds=round(time.monotonic() - started, 2), evidence=path.name)
|
||||
results.append(result)
|
||||
print(json.dumps(result), flush=True)
|
||||
if process.returncode:
|
||||
raise RuntimeError(name + ' failed; see ' + str(path))
|
||||
try:
|
||||
step('quality', ['make', 'lint', 'check', 'validate', 'PYTHON=' + sys.executable])
|
||||
step('clean-start', compose + ['up', '-d', '--build', '--wait'])
|
||||
step('public-smoke', [sys.executable, 'course-tests/smoke.py', '--base-url', env['BASE_URL']])
|
||||
step('regression', ['make','test-attachments','test-idempotency','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
||||
step('migration', compose + ['exec','-T','worker','python','-c', (ROOT/'tests/lab5_migration.py').read_text()])
|
||||
step('limits', compose + ['exec','-T','worker','python','-c', (ROOT/'tests/lab5_limits.py').read_text()])
|
||||
step('faults', [sys.executable,'tests/lab5.py','--faults','--project',project,'--base-url',env['BASE_URL']])
|
||||
step('persistence', ['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
||||
step('failover', ['make','test-attachment-failover','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
||||
step('review', ['make','test-api-regressions','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
||||
step('final-smoke', [sys.executable,'course-tests/smoke.py','--base-url',env['BASE_URL']])
|
||||
finally:
|
||||
with (evidence/'lab5-container-status.txt').open('w') as output:
|
||||
subprocess.run(compose+['ps','-a'],cwd=ROOT,env=env,stdout=output,stderr=subprocess.STDOUT)
|
||||
subprocess.run(compose+['down','--volumes','--remove-orphans'],cwd=ROOT,env=env,
|
||||
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,check=True)
|
||||
(evidence/'lab5-summary.json').write_text(json.dumps(dict(project=project,
|
||||
fresh_volumes=True, code_manifest=code_manifest, results=results),ensure_ascii=False,indent=2)+'\n')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user