ЛР6: наблюдаемость, сквозные traces и исследование sampling
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local credentials without printing or overwriting existing values."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
directory = root / 'secrets'
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
|
||||
def save(name, value):
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(value)
|
||||
path.chmod(0o644) # UID 10001 in Compose; parent is owner-only 0700.
|
||||
return path.read_text().strip()
|
||||
|
||||
password = save('postgres_password', secrets.token_hex(24))
|
||||
save('database_url', 'postgresql://messenger:' + password + '@postgres:5432/messenger')
|
||||
save('cache_url', 'redis://redis:6379/0')
|
||||
save('broker_url', 'redis://broker:6379/0')
|
||||
identities = []
|
||||
for role, actions in {
|
||||
'admin': ['Admin', 'Read', 'Write', 'List'],
|
||||
'api': ['Read:messenger-attachments', 'Write:messenger-attachments', 'List:messenger-attachments'],
|
||||
'worker': ['Read:messenger-attachments', 'Write:messenger-attachments', 'List:messenger-attachments'],
|
||||
}.items():
|
||||
key = save('s3_' + role + '_key', secrets.token_hex(12))
|
||||
secret = save('s3_' + role + '_secret', secrets.token_hex(24))
|
||||
identities.append({'name': role, 'credentials': [{'accessKey': key, 'secretKey': secret}], 'actions': actions})
|
||||
configuration = directory / 's3_config'
|
||||
configuration.write_text(json.dumps({'identities': identities}))
|
||||
configuration.chmod(0o600) # Only the root S3 gateway mounts the full identity configuration.
|
||||
env = root / '.env'
|
||||
if not env.exists():
|
||||
env.write_text((root / '.env.example').read_text())
|
||||
env.chmod(0o600)
|
||||
print('Local configuration ready; no secrets printed.')
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
python3 scripts/snapshot.py > evidence/restart-before.json
|
||||
docker compose stop api lb worker dispatcher
|
||||
docker compose restart postgres redis s3-store s3 broker otel-collector prometheus loki tempo grafana
|
||||
docker compose up -d --wait postgres redis s3-store s3 broker otel-collector prometheus loki tempo grafana
|
||||
docker compose up -d --wait api lb worker dispatcher
|
||||
python3 scripts/snapshot.py > evidence/restart-after.json
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Sanitized process and resource evidence; never dump container environments."""
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
ids = subprocess.check_output(['docker', 'compose', 'ps', '-aq'], text=True).split()
|
||||
records = []
|
||||
for container in ids:
|
||||
raw = subprocess.check_output(['docker', 'inspect', container], text=True)
|
||||
data = json.loads(raw)[0]
|
||||
records.append({'container': data['Id'], 'name': data['Name'], 'service': data['Config']['Labels']['com.docker.compose.service'], 'image': data['Config']['Image'], 'user': data['Config']['User'], 'running': data['State']['Running'], 'started_at': data['State']['StartedAt'], 'exit_code': data['State']['ExitCode'], 'oom_killed': data['State']['OOMKilled'], 'memory_limit': data['HostConfig']['Memory'], 'nano_cpus': data['HostConfig']['NanoCpus'], 'ports': data['NetworkSettings']['Ports'], 'mounts': [{'type': m['Type'], 'destination': m['Destination'], 'name': m.get('Name')} for m in data['Mounts']]})
|
||||
print(json.dumps(records, indent=2))
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/stop_one.py
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Resolve the requested process alias using each actual container, never a fake header."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.check_output(['docker', *args], text=True).strip()
|
||||
|
||||
|
||||
def instance(container):
|
||||
code = "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=1).headers['X-Instance-Id'])"
|
||||
return docker('exec', container, 'python', '-c', code)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
target = os.environ['TARGET_INSTANCE_ID']
|
||||
started = time.monotonic()
|
||||
for container in docker('compose', 'ps', '-q', 'api').splitlines():
|
||||
if instance(container) == target:
|
||||
docker('stop', '--time', '8', container)
|
||||
state = json.loads(docker('inspect', '--format', '{{json .State}}', container))
|
||||
assert not state['Running'], 'target must remain stopped'
|
||||
record = {'instance': target, 'container': container, 'running': state['Running'], 'exit_code': state['ExitCode'], 'seconds': round(time.monotonic() - started, 3)}
|
||||
with Path('evidence/stopped-instances.jsonl').open('a') as out:
|
||||
out.write(json.dumps(record) + '\n')
|
||||
print(json.dumps(record))
|
||||
break
|
||||
else:
|
||||
raise SystemExit('Requested live API instance not found')
|
||||
@@ -0,0 +1,16 @@
|
||||
import os
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
base = os.environ.get('BASE_URL', 'http://127.0.0.1:18087')
|
||||
for attempt in range(120):
|
||||
try:
|
||||
with urllib.request.urlopen(base + '/health/ready', timeout=2) as response:
|
||||
if response.status == 200:
|
||||
print('Ready through load balancer.')
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
raise SystemExit('Readiness timeout')
|
||||
Reference in New Issue
Block a user