gonets/SRV/config/init_config.py

38 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import configparser
import os
import secrets
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def init_config():
"""Создаёт конфигурационный файл с SECRET_KEY"""
config_path = "/etc/gonets/config.ini"
config_dir = os.path.dirname(config_path)
if os.path.exists(config_path):
logger.info(
f"Конфигурационный файл {config_path} уже существует, пропускаем инициализацию"
)
return
try:
secret_key = secrets.token_urlsafe(32)
os.makedirs(config_dir, exist_ok=True)
config = configparser.ConfigParser()
config["JWT"] = {"secret_key": secret_key}
with open(config_path, "w") as config_file:
config.write(config_file)
os.chmod(config_path, 0o600)
logger.info(f"Создан конфигурационный файл {config_path}")
except Exception as e:
logger.error(
f"Ошибка при создании конфигурационного файла: {type(e).__name__} - {str(e)}"
)
raise
# Нативный запуск для тестов
if __name__ == "__main__":
init_config()