"""
Helpers of the settings modules, which are not settings themselves.
The settings diagnostics that every environment's module logs, after its overrides.
"""
import os
import sys
import time
from collections.abc import Mapping
from smarter.__version__ import __version__ as smarter_version
from smarter.common.conf import smarter_settings
from smarter.common.helpers.resources import log_resource_limits
from smarter.lib import logging
logger = logging.getSmarterLogger(__name__)
[docs]
def default_storage_backend(settings: Mapping[str, object]) -> str:
"""
Return the backend of the default storage in ``STORAGES``.
:param settings: A settings module's namespace.
:returns: Its dotted path, or ``unknown`` when ``STORAGES`` has no default.
"""
storages = settings.get("STORAGES")
if not isinstance(storages, dict):
return "unknown"
default_storage = storages.get("default")
if not isinstance(default_storage, dict):
return "unknown"
backend = str(default_storage.get("BACKEND", "unknown"))
return backend
[docs]
def log_smtp_configuration(settings: Mapping[str, object]) -> None:
"""
Log the SMTP server that email is sent through, or warn that there is none.
The tool worker (``database_unavailable``) sends no email, and is given no SMTP credentials, by
design (ADR-038), so it doesn't warn: it confirms, at INFO, that email is disabled there. Called
by :func:`log_settings_diagnostics`, its log entries are named after the environment's module,
as that function's are.
:param settings: The environment's settings module's namespace.
"""
if smarter_settings.smtp_is_configured:
logger.debug(
"SMTP server configured: %s:%s (SSL=%s, TLS=%s)",
settings["SMTP_HOST"],
settings["SMTP_PORT"],
settings["SMTP_USE_SSL"],
settings["SMTP_USE_TLS"],
stacklevel=3,
)
return
if smarter_settings.database_unavailable:
logger.info("SMTP is disabled in the tool worker, by design: it sends no email.", stacklevel=3)
return
logger.warning("SMTP server not configured", stacklevel=3)
[docs]
def log_settings_diagnostics(settings: Mapping[str, object]) -> None:
"""
Log the platform's diagnostics, and the settings that every environment shares, outside of manage.py.
Each environment's module calls it with its ``globals()``, after
:func:`smarter.settings.utils.overrides.apply_overrides`, so that the log shows the values that Django
runs with, the deployment's overrides included, rather than this module's. The log entries are named
after the caller, the environment's module, e.g. ``smarter.settings.prod``.
:param settings: The environment's settings module's namespace.
"""
if not (smarter_settings.platform_settings_output or "manage.py" not in sys.argv[0]):
return
logger.debug("=" * 80, stacklevel=2)
logger.debug("%s Settings diagnostics information %s", "=" * (40 - 17), "=" * (40 - 18), stacklevel=2)
logger.debug("=" * 80, stacklevel=2)
logger.debug("Django settings module: %s", os.environ.get("DJANGO_SETTINGS_MODULE"), stacklevel=2)
try:
with open("/proc/uptime", encoding="utf-8") as f:
uptime_seconds = float(f.readline().split()[0])
uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
logger.debug("Container uptime: %s", uptime_str, stacklevel=2)
except FileNotFoundError:
logger.warning("Container uptime not available", stacklevel=2)
except OSError as e:
logger.error("Error reading container uptime: %s", e, stacklevel=2)
log_resource_limits(logger)
try:
debian_version = "not found"
with open("/etc/debian_version", encoding="utf-8") as f:
debian_version = f.read().strip()
logger.debug("Debian v%s %s", debian_version, os.uname().version, stacklevel=2)
except FileNotFoundError:
logger.error("Debian version file not found", stacklevel=2)
except OSError as e:
logger.error("Error reading Debian version: %s", e, stacklevel=2)
logger.debug("Python v%s", sys.version, stacklevel=2)
from django import get_version
logger.debug("Django v%s", get_version(), stacklevel=2)
logger.debug("Smarter v%s", smarter_version, stacklevel=2)
logger.debug("Storages backend: %s", default_storage_backend(settings), stacklevel=2)
log_smtp_configuration(settings)
# the settings have already warned, once, if AWS is not configured.
logger.debug("AWS is configured: %s", smarter_settings.aws_is_configured, stacklevel=2)
logger.debug("=" * 80, stacklevel=2)