Overrides

Django settings that the deployment passes to the process, which overwrite Smarter’s in every environment: DJANGO_* environment variables from the Helm chart’s env on Kubernetes, and locally from .env and the overrides/ file that Docker Compose mounts. How to write them, and where they go, is in Overrides.

Overrides: Django settings that the deployment passes to the process, which overwrite Smarter’s.

Smarter’s settings modules compute most Django settings from smarter_settings. The deployment can overwrite any of them at startup, without a change to Smarter’s code. Nobody who uses the platform can: an override exists only in the process’s environment, or in a file mounted into the container, both of which the deployment controls.

How execution gets here on Kubernetes, where the Helm chart deploys Smarter:

  1. The chart’s smarter.env (templates/_helpers.tpl) builds the platform’s environment from env in its values. It always adds DJANGO_CELERY_BROKER_URL, the chart’s own Redis, unless the values set one. The values’ env sets DJANGO_AUTHENTICATION_BACKENDS by default. So every pod that the chart deploys overrides at least those two settings, with no change by anyone.

  2. templates/secret-env.yaml writes that environment into the Secret <appName>-env, and every Deployment and Job reads it with envFrom. Each DJANGO_* variable in it is in the process’s environment, os.environ.

  3. The container starts its process, and Django imports the settings module smarter.settings.<environment>, e.g. smarter.settings.prod. The app server’s uvicorn loads smarter.asgi, and manage.py runs, each of which sets DJANGO_SETTINGS_MODULE from smarter_settings.environment. A Celery worker or beat takes the DJANGO_SETTINGS_MODULE that smarter.env put in the Secret.

  4. That module imports smarter.settings.base (and smarter.settings.base_aws), assigns its own settings, then, on its last lines, calls apply_overrides() with its globals(), which overwrites each setting named by a DJANGO_* variable.

Locally, Docker Compose passes .env to every container (env_file), so a DJANGO_* line in .env reaches step 4 the same way. Docker Compose also mounts overrides/ from the repository’s root, which holds the only other source of overrides: the overrides file, a YAML mapping of setting names to values, at SMARTER_OVERRIDES_FILE (default DEFAULT_OVERRIDES_FILE). Nested settings, such as LOGGING or CACHES, are written as YAML. The Helm chart mounts no file.

Each environment’s module calls apply_overrides() after its last assignment of a setting, so nothing in Smarter’s settings can set a value after it, and before it logs its settings, so that the log shows the values that Django runs with. The order, from lowest to highest priority:

  1. smarter.settings.base

  2. smarter.settings.base_aws, in the AWS environments

  3. the environment’s module, e.g. smarter.settings.prod

  4. the overrides file

  5. DJANGO_* environment variables

An override replaces the whole setting of that name: a dictionary is not merged with Smarter’s. Settings that Smarter computes from other settings were computed before this runs, so they keep the values they were computed from, except those in DERIVED_SETTINGS, which follow the setting they are computed from. An overridden CACHES has to name its sessions cache too.

The log names each overridden setting and where its override came from, and never its value, which may be a password.

smarter.settings.overrides.ASCII_CONTROL_CHARACTERS = re.compile('[\\x00-\\x1F\\x7F]')

Characters that a copy and paste can leave in an environment variable’s value.

smarter.settings.overrides.DEFAULT_OVERRIDES_FILE = '/home/smarter_user/overrides/settings.yaml'

in the containers’.

home, where Docker Compose mounts overrides/ locally.

Type:

Where the overrides file is when SMARTER_OVERRIDES_FILE is unset

smarter.settings.overrides.DERIVED_SETTINGS = {'CELERY_REDBEAT_REDIS_URL': 'CELERY_BROKER_URL'}

Settings that Smarter computes from another setting, by name, and the setting each is computed from.

They were computed before the overrides are applied. When the deployment overrides the setting that one is computed from, and not the derived setting itself, the derived setting is recomputed from the override, e.g. DJANGO_CELERY_BROKER_URL moves RedBeat’s Redis too.

{'CELERY_REDBEAT_REDIS_URL': 'CELERY_BROKER_URL'}
smarter.settings.overrides.ENVIRONMENT_PREFIX = 'DJANGO_'

The prefix of an environment variable that overrides the Django setting named by the rest of its name.

smarter.settings.overrides.IGNORED_ENVIRONMENT_VARIABLES = ('DJANGO_SETTINGS_MODULE', 'DJANGO_SECRET_KEY')

DJANGO_* variables that this module doesn’t apply.

DJANGO_SETTINGS_MODULE is Django’s own, not a setting. DJANGO_SECRET_KEY is read by smarter_settings.django_secret_key, which rejects a placeholder, and smarter.settings.base sets SECRET_KEY from it. Applied here as well, a placeholder would replace the random key that base generates for one.

('DJANGO_SETTINGS_MODULE', 'DJANGO_SECRET_KEY')
smarter.settings.overrides.OVERRIDES_FILE_VARIABLE = 'SMARTER_OVERRIDES_FILE'

The environment variable that names the overrides file.

smarter.settings.overrides.SETTING_NAME = re.compile('^[A-Z][A-Z0-9_]*$')

upper case letters, digits and underscores, starting with a letter.

Type:

A Django setting’s name

exception smarter.settings.overrides.SmarterOverridesError[source]

Bases: Exception

The overrides file, or a DJANGO_* environment variable, can’t be applied.

smarter.settings.overrides.TRUE_STRINGS = ('1', 'true', 'yes', 'on')

The values of an environment variable that mean True, for a setting whose value is a bool.

('1', 'true', 'yes', 'on')
smarter.settings.overrides.apply_overrides(settings, environ=None)[source]

Overwrite Smarter’s Django settings in settings with the deployment’s overrides.

The overrides file’s are applied first, then the DJANGO_* environment variables’, which the Helm chart’s env sets on Kubernetes and .env sets locally. Each environment’s settings module calls it with globals(), after its last assignment of a setting and before smarter.settings.base.log_settings_diagnostics().

Parameters:
Return type:

list[str]

Returns:

The names of the settings overridden, and of the settings recomputed from them, sorted.

Raises:

SmarterOverridesError – If the file or a variable can’t be applied. Django doesn’t start.

smarter.settings.overrides.check_setting_name(name, source)[source]

Return a setting’s name, if it is one.

Parameters:
  • name (object) – The name, a key of the file’s mapping, or what follows DJANGO_.

  • source (str) – Where the name comes from, for the error.

Return type:

str

Returns:

The name.

Raises:

SmarterOverridesError – If it isn’t an upper case setting name.

smarter.settings.overrides.current_value(name, current_settings)[source]

Return a setting’s value before an environment variable overrides it, which gives the variable its type.

Parameters:
  • name (str) – The setting’s name.

  • current_settings (Mapping[str, object]) – The settings module’s namespace.

Return type:

object

Returns:

Smarter’s value, else Django’s default, e.g. [] for SILENCED_SYSTEM_CHECKS, else None.

smarter.settings.overrides.dict_value(text)[source]

Return an environment variable’s value, for a setting whose value is a dictionary.

Parameters:

text (str) – The variable’s text.

Return type:

dict

Returns:

The JSON object, or the Python dict, that it writes.

Raises:

SmarterOverridesError – If it writes neither.

smarter.settings.overrides.is_known_setting(name, settings)[source]

Return whether a setting exists before it is overridden: Django’s, a package’s, or Smarter’s.

Parameters:
  • name (str) – The setting’s name.

  • settings (Mapping[str, object]) – The settings module’s namespace.

Return type:

bool

Returns:

True if Smarter’s settings or Django’s defaults define it.

smarter.settings.overrides.literal_value(text)[source]

Return the value that an environment variable’s text writes.

Parameters:

text (str) – The text.

Return type:

object

Returns:

The JSON value it is (true, 5, ["a"], {"a": 1}), else the Python literal it is (True, ("a", "b")), else the text itself, as a string.

smarter.settings.overrides.overrides_file_path(environ)[source]

Return the path of the overrides file.

It is read from the environment, not from smarter_settings, so that it is known before any setting is: smarter_settings.overrides_file reads the same variable.

Parameters:

environ (Mapping[str, str]) – The process’s environment.

Return type:

Path

Returns:

SMARTER_OVERRIDES_FILE or OVERRIDES_FILE, else DEFAULT_OVERRIDES_FILE.

smarter.settings.overrides.overrides_from_environment(environ, current_settings)[source]

Return the overrides in the process’s DJANGO_* environment variables.

Parameters:
Return type:

dict[str, object]

Returns:

The overridden settings’ values by name.

Raises:

SmarterOverridesError – If a variable’s name or value can’t be a setting.

smarter.settings.overrides.overrides_from_file(path)[source]

Return the overrides in the overrides file.

Parameters:

path (Path) – The file’s path.

Return type:

dict[str, object]

Returns:

The overridden settings’ values by name, or an empty dict when there is no file, or it is empty.

Raises:

SmarterOverridesError – If the file isn’t YAML, isn’t a mapping, or has a key that isn’t a setting name.

smarter.settings.overrides.recompute_derived_settings(settings, overridden, sources_before)[source]

Recompute each of DERIVED_SETTINGS from the overridden setting that it is computed from.

A derived setting is left alone when it was overridden itself, when its source wasn’t overridden, or when a settings module had already given it a value of its own, i.e. not its source’s.

Parameters:
  • settings (MutableMapping[str, object]) – The settings module’s namespace, changed in place.

  • overridden (set[str]) – The names of the settings that the overrides file and variables set.

  • sources_before (Mapping[str, object]) – Each source setting’s value before the overrides.

Return type:

set[str]

Returns:

The names of the derived settings that were recomputed.

smarter.settings.overrides.sequence_value(text, sequence_type)[source]

Return an environment variable’s value, for a setting whose value is a list or a tuple.

Parameters:
  • text (str) – The variable’s text.

  • sequence_type (type) – list or tuple, the type of the setting’s value.

Return type:

object

Returns:

A JSON or Python list or tuple, else the comma-separated items, as sequence_type.

smarter.settings.overrides.value_like(text, current)[source]

Return an environment variable’s value, of the type of the setting’s current value.

Parameters:
  • text (str) – The variable’s text.

  • current (object) – The setting’s value before this variable.

Return type:

object

Returns:

For a bool, True for 1, true, yes or on, else False. For an int or float, the number. For a list or tuple, a JSON or Python list, else the comma-separated items. For a dict, a JSON object or Python dict. For a string, the text. For anything else, or no current value, literal_value().

Raises:

SmarterOverridesError – If the text isn’t a value of the setting’s type.