forked from mtusi2026/mtusi2026lab6
solution
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
"""Создаёт только отсутствующие локальные настройки и файловые секреты."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
|
||||
def main() -> None:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
destination = root / ".env"
|
||||
if not destination.exists():
|
||||
with destination.open("x", encoding="utf-8", newline="\n") as target:
|
||||
target.write((root / ".env.example").read_text(encoding="utf-8"))
|
||||
destination.chmod(0o600)
|
||||
directory = root / "secrets"
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
for name in (
|
||||
"db_password",
|
||||
"redis_password",
|
||||
"broker_password",
|
||||
"minio_root_user",
|
||||
"minio_root_password",
|
||||
"s3_access_key_id",
|
||||
"s3_secret_access_key",
|
||||
"grafana_admin_password",
|
||||
):
|
||||
path = directory / name
|
||||
try:
|
||||
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o444)
|
||||
except FileExistsError:
|
||||
continue
|
||||
with os.fdopen(descriptor, "w", encoding="utf-8") as target:
|
||||
target.write(secrets.token_urlsafe(24))
|
||||
print("Создан файл " + str(path.relative_to(root)))
|
||||
print("Настройки готовы; существующие значения сохранены")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user