forked from mtusi2026/mtusi2026lab3
ЛР 3 - БВТ2304 - Гончаревская.Е.А. - на 3
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Create local Docker Compose secret files.")
|
||||
parser.add_argument("--force", action="store_true", help="replace existing local secret files")
|
||||
args = parser.parse_args()
|
||||
|
||||
directory = ROOT / "secrets"
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
files = [directory / "postgres_password", directory / "database_url", directory / "redis_url"]
|
||||
if not args.force and any(path.exists() for path in files):
|
||||
raise SystemExit("Secret files already exist. Use --force only if you intend to rotate credentials.")
|
||||
|
||||
password = secrets.token_hex(24)
|
||||
values = {
|
||||
"postgres_password": password + "\n",
|
||||
"database_url": f"postgresql://messenger:{password}@postgres:5432/messenger?connect_timeout=5\n",
|
||||
"redis_url": "redis://valkey:6379/0\n",
|
||||
}
|
||||
for name, value in values.items():
|
||||
path = directory / name
|
||||
path.write_text(value, encoding="utf-8")
|
||||
try:
|
||||
os.chmod(path, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
print("Local secret files created. Values were not printed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
docker compose --env-file .env restart postgres valkey api
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
*[!a-fA-F0-9-]*|'') echo "Invalid API instance id" >&2; exit 2 ;;
|
||||
esac
|
||||
docker stop --time 5 "$TARGET_INSTANCE_ID" >/dev/null
|
||||
Reference in New Issue
Block a user