forked from mtusi2026/mtusi2026lab3
перенос 2 лабы
This commit is contained in:
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
python3 scripts/init_local.py
|
||||
docker compose build "$@" api
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
if [[ ! -x .venv/bin/python ]]; then
|
||||
echo "Создай .venv и установи requirements.txt по REPORT.md" >&2
|
||||
exit 1
|
||||
fi
|
||||
./scripts/build.sh
|
||||
mkdir -p evidence
|
||||
.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | tee evidence/docker-checks.txt
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
|
||||
if [[ ! -x .venv/bin/python ]]; then
|
||||
echo "Не хватает .venv" >&2
|
||||
echo "python3 -m venv .venv" >&2
|
||||
echo ".venv/bin/python -m pip install -r requirements.txt" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
export BASE_URL="${BASE_URL:-http://localhost:8080}"
|
||||
echo "Проверяем сервер: $BASE_URL"
|
||||
make check test PYTHON="$project_dir/.venv/bin/python" BASE_URL="$BASE_URL"
|
||||
.venv/bin/python -m unittest discover -s student-tests -v
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
base_url="${BASE_URL:-http://localhost:8080}"
|
||||
suffix="$(date +%s)_$RANDOM"
|
||||
# Только синтетические пароли; токены остаются в памяти shell и не выводятся.
|
||||
password="Demo-password-$suffix"
|
||||
register() {
|
||||
local username="$1" display_name="$2"
|
||||
jq -nc --arg u "$username" --arg d "$display_name" --arg p "$password" \
|
||||
'{username:$u, display_name:$d, password:$p}' |
|
||||
curl -fsS -H 'Content-Type: application/json' --data-binary @- "$base_url/api/v1/users" | jq -er '.id'
|
||||
}
|
||||
login() {
|
||||
curl -fsS -X POST -u "$1:$password" "$base_url/api/v1/auth/sessions" | jq -er '.token'
|
||||
}
|
||||
alice_id="$(register "alice_$suffix" Алиса)"
|
||||
bob_id="$(register "bob_$suffix" Боб)"
|
||||
alice_token="$(login "alice_$suffix")"
|
||||
bob_token="$(login "bob_$suffix")"
|
||||
chat_id="$(jq -nc --arg a "$alice_id" --arg b "$bob_id" '{title:"Учебный чат",member_ids:[$a,$b]}' |
|
||||
curl -fsS -H "Authorization: Bearer $alice_token" -H 'Content-Type: application/json' \
|
||||
--data-binary @- "$base_url/api/v1/chats" | jq -er '.id')"
|
||||
for token in "$alice_token" "$bob_token"; do
|
||||
curl -fsS -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \
|
||||
-d '{"text":"Привет из lab2! 👋"}' "$base_url/api/v1/chats/$chat_id/messages" | jq .
|
||||
done
|
||||
curl -fsS -H "Authorization: Bearer $bob_token" "$base_url/api/v1/chats/$chat_id/messages" | jq .
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Создаёт локальное окружение и secrets; существующие значения сохраняет."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import shutil
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
if not (root / ".env").exists():
|
||||
shutil.copyfile(root / ".env.example", root / ".env")
|
||||
directory = root / "secrets"
|
||||
directory.mkdir(exist_ok=True, mode=0o700)
|
||||
directory.chmod(0o700)
|
||||
password_file = directory / "postgres_password"
|
||||
if not password_file.exists():
|
||||
password_file.write_text(secrets.token_hex(24))
|
||||
password = password_file.read_text().strip()
|
||||
values = {
|
||||
"database_url": f"postgresql://messenger:{quote(password, safe='')}@postgres:5432/messenger",
|
||||
"cache_url": "redis://valkey:6379/0",
|
||||
}
|
||||
for name, value in values.items():
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(value)
|
||||
# Каталог 0700 защищает файлы на хосте; 0444 позволяет USER 10001 читать bind-mounted secret.
|
||||
for name in ("postgres_password", "database_url", "cache_url"):
|
||||
(directory / name).chmod(0o444)
|
||||
print("Local .env and secrets are ready; values are not printed")
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
docker compose exec -T api python -m src.migrate
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
./scripts/build.sh
|
||||
mkdir -p evidence
|
||||
python3 -u research/run.py 2>&1 | tee evidence/research.txt
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
docker compose restart --timeout 10 postgres valkey api
|
||||
./scripts/wait-ready.sh
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
# В lab2 локальный запуск включает API, PostgreSQL и Valkey.
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
exec "$project_dir/scripts/start.sh"
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
python3 scripts/init_local.py
|
||||
docker compose up -d --build api
|
||||
./scripts/wait-ready.sh
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
docker compose down --timeout 10
|
||||
echo "Контейнеры удалены; именованные volumes сохранены"
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$project_dir"
|
||||
for ((attempt = 0; attempt < 60; attempt++)); do
|
||||
if docker compose exec -T api python -m src.healthcheck >/dev/null 2>&1; then
|
||||
echo "API ready"
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "API не готов за 60 секунд. Проверь docker compose logs api" >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user