From c9fdecc8e8bb4d462175bf962045e59b7f7b69d7 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Wed, 11 Nov 2015 07:39:18 +0100 Subject: [PATCH] Add local_settings.d/ dir for settings snippets All files ending with '.py' in local_settings.d/ are evaluated after reading the settings from local_settings.py . That means changing the settings can be done without changing local_settings.py . Change-Id: I0b25ef2c4dada23f8cb66aa5e8af44b3a1cba3b9 Implements: blueprint local-settings-override-mechanism --- doc/source/topics/settings.rst | 17 +++++++++++++++++ .../_10_set_custom_theme.py.example | 2 ++ openstack_dashboard/settings.py | 13 +++++++++++++ ...ngs-override-mechanism-6c8632432cf1f44c.yaml | 4 ++++ 4 files changed, 36 insertions(+) create mode 100644 openstack_dashboard/local/local_settings.d/_10_set_custom_theme.py.example create mode 100644 releasenotes/notes/bp-local-settings-override-mechanism-6c8632432cf1f44c.yaml diff --git a/doc/source/topics/settings.rst b/doc/source/topics/settings.rst index e3940e14d..3ec3b0896 100644 --- a/doc/source/topics/settings.rst +++ b/doc/source/topics/settings.rst @@ -292,6 +292,23 @@ Most of the following settings are defined in ``openstack_dashboard/local/local_settings.py``, which should be copied from ``openstack_dashboard/local/local_settings.py.example``. +Since Mitaka, there is also a way to drop file snippets into +``openstack_dashboard/local/local_settings.d/``. These snippets must end with +``.py`` and must contain valid Python code. The snippets are loaded after +``local_settings.py`` is evaluated so you are able to override settings from +``local_settings.py`` without the need to change this file. +Snippets are evaluated in alphabetical order by file name. +It's good style to name the files in ``local_settings.d/`` like +``_ZZ_another_setting.py`` where ``ZZ`` is a number. The file must start with +an underscore (``_``) because Python can not load files starting with a number. +So given that you have 3 files, ``local_settings.py``, +``local_settings.d/_10_setting_one.py`` and ``local_settings.d/_20_settings_two.py``, +the settings from ``local_settings.py`` are evaluated first. Settings from +``local_settings.d/_10_settings_one.py`` override settings from ``local_settings.py`` +and settings from ``local_settings.d/_20_settings_two.py`` override all other settings +because that's the file which is evaluated last. + + ``AUTHENTICATION_URLS`` ----------------------- diff --git a/openstack_dashboard/local/local_settings.d/_10_set_custom_theme.py.example b/openstack_dashboard/local/local_settings.d/_10_set_custom_theme.py.example new file mode 100644 index 000000000..43fe4971d --- /dev/null +++ b/openstack_dashboard/local/local_settings.d/_10_set_custom_theme.py.example @@ -0,0 +1,2 @@ +# override the CUSTOM_THEME_PATH variable with this settings snippet +# CUSTOM_THEME_PATH="themes/material" diff --git a/openstack_dashboard/settings.py b/openstack_dashboard/settings.py index 4ff22b0a9..c6ba04590 100644 --- a/openstack_dashboard/settings.py +++ b/openstack_dashboard/settings.py @@ -267,6 +267,19 @@ try: except ImportError: logging.warning("No local_settings file found.") +# allow to drop settings snippets into a local_settings_dir +LOCAL_SETTINGS_DIR_PATH = os.path.join(ROOT_PATH, "local", "local_settings.d") +if os.path.exists(LOCAL_SETTINGS_DIR_PATH): + for (dirpath, dirnames, filenames) in os.walk(LOCAL_SETTINGS_DIR_PATH): + for filename in sorted(filenames): + if filename.endswith(".py"): + try: + execfile(os.path.join(dirpath, filename)) + except Exception as e: + logging.exception( + "Can not exec settings snippet %s" % (filename)) + + if not WEBROOT.endswith('/'): WEBROOT += '/' if LOGIN_URL is None: diff --git a/releasenotes/notes/bp-local-settings-override-mechanism-6c8632432cf1f44c.yaml b/releasenotes/notes/bp-local-settings-override-mechanism-6c8632432cf1f44c.yaml new file mode 100644 index 000000000..a9361b9c0 --- /dev/null +++ b/releasenotes/notes/bp-local-settings-override-mechanism-6c8632432cf1f44c.yaml @@ -0,0 +1,4 @@ +--- +features: + - Allow to override settings from local_settings.py with + file snippets dropped into local_settings.d/ directory.