forked from mtusi2026/mtusi2026lab2
ЛР 2 — БВТ2301 — Майорова — на 5
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local lab credentials once; never print values or overwrite an existing setup."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
directory = root / 'secrets'
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
password_path = directory / 'postgres_password'
|
||||
if not password_path.exists():
|
||||
password_path.write_text(secrets.token_hex(24))
|
||||
password_path.chmod(0o600)
|
||||
password = password_path.read_text().strip()
|
||||
for name, value in {
|
||||
'database_url': 'postgresql://messenger:' + password + '@postgres:5432/messenger',
|
||||
'cache_url': 'redis://redis:6379/0',
|
||||
}.items():
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(value)
|
||||
# Docker Compose bind-mounted secrets must be readable by UID 10001.
|
||||
# The containing host directory is accessible only to its owner.
|
||||
path.chmod(0o644)
|
||||
env = root / '.env'
|
||||
if not env.exists():
|
||||
env.write_text((root / '.env.example').read_text())
|
||||
env.chmod(0o600)
|
||||
print('Local configuration ready; secrets were not printed.')
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
python3 scripts/snapshot.py > evidence/restart-before.json
|
||||
docker compose restart postgres redis
|
||||
docker compose up -d --wait postgres redis
|
||||
docker compose restart api
|
||||
docker compose up -d --wait api
|
||||
python3 scripts/snapshot.py > evidence/restart-after.json
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Record restart evidence without env, credentials or machine-specific labels."""
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api', 'postgres', 'redis'], text=True).split()
|
||||
containers = json.loads(subprocess.check_output(['docker', 'inspect', *ids], text=True))
|
||||
result = []
|
||||
for container in containers:
|
||||
result.append(dict(service=container['Config']['Labels']['com.docker.compose.service'], container_id=container['Id'][:12], started_at=container['State']['StartedAt'], status=container['State']['Status'], volumes=[m['Name'] for m in container['Mounts'] if m['Type'] == 'volume']))
|
||||
print(json.dumps(sorted(result, key=lambda x: x['service']), indent=2))
|
||||
Reference in New Issue
Block a user