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:
The chart’s
smarter.env(templates/_helpers.tpl) builds the platform’s environment fromenvin its values. It always addsDJANGO_CELERY_BROKER_URL, the chart’s own Redis, unless the values set one. The values’envsetsDJANGO_AUTHENTICATION_BACKENDSby default. So every pod that the chart deploys overrides at least those two settings, with no change by anyone.templates/secret-env.yamlwrites that environment into the Secret<appName>-env, and every Deployment and Job reads it withenvFrom. EachDJANGO_*variable in it is in the process’s environment,os.environ.The container starts its process, and Django imports the settings module
smarter.settings.<environment>, e.g.smarter.settings.prod. The app server’s uvicorn loadssmarter.asgi, andmanage.pyruns, each of which setsDJANGO_SETTINGS_MODULEfromsmarter_settings.environment. A Celery worker or beat takes theDJANGO_SETTINGS_MODULEthatsmarter.envput in the Secret.That module imports
smarter.settings.base(andsmarter.settings.base_aws), assigns its own settings, then, on its last lines, callsapply_overrides()with itsglobals(), which overwrites each setting named by aDJANGO_*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:
smarter.settings.base_aws, in the AWS environmentsthe environment’s module, e.g.
smarter.settings.prodthe overrides file
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_FILEis 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_URLmoves 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_MODULEis Django’s own, not a setting.DJANGO_SECRET_KEYis read bysmarter_settings.django_secret_key, which rejects a placeholder, andsmarter.settings.basesetsSECRET_KEYfrom it. Applied here as well, a placeholder would replace the random key thatbasegenerates 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:
ExceptionThe 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
settingswith the deployment’s overrides.The overrides file’s are applied first, then the
DJANGO_*environment variables’, which the Helm chart’senvsets on Kubernetes and.envsets locally. Each environment’s settings module calls it withglobals(), after its last assignment of a setting and beforesmarter.settings.base.log_settings_diagnostics().- Parameters:
- Return type:
- 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:
- Return type:
- 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.
- 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:
- 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.
- smarter.settings.overrides.literal_value(text)[source]
Return the value that an environment variable’s text writes.
- 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_filereads the same variable.- Parameters:
- Return type:
- Returns:
SMARTER_OVERRIDES_FILEorOVERRIDES_FILE, elseDEFAULT_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:
- 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:
- 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_SETTINGSfrom 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:
- Return type:
- 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.
- smarter.settings.overrides.value_like(text, current)[source]
Return an environment variable’s value, of the type of the setting’s current value.
- Parameters:
- Return type:
- Returns:
For a bool, True for
1,true,yesoron, 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.