Skip to content

Commit

Permalink
Add license file for dynaconf
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Feb 17, 2025
1 parent 8cc6f5b commit 0981fdf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
16 changes: 9 additions & 7 deletions awx/main/tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,31 @@ def test_default_settings():

def test_django_conf_settings_is_awx_settings():
"""Ensure that the settings loaded from dynaconf are the same as the settings delivered to django."""
assert settings.REST_FRAMEWORK is REST_FRAMEWORK
assert settings.REST_FRAMEWORK == REST_FRAMEWORK


def test_dynaconf_is_awx_settings():
"""Ensure that the settings loaded from dynaconf are the same as the settings delivered to django."""
assert settings.DYNACONF.REST_FRAMEWORK is REST_FRAMEWORK
assert settings.DYNACONF.REST_FRAMEWORK == REST_FRAMEWORK


def test_production_settings_can_be_directly_imported():
def test_production_settings_can_be_directly_imported(monkeypatch):
"""Ensure that the production settings can be directly imported."""
monkeypatch.setenv('AWX_MODE', 'production')
from awx.settings.production import REST_FRAMEWORK
from awx.settings.production import DEBUG

assert settings.REST_FRAMEWORK is REST_FRAMEWORK
assert settings.REST_FRAMEWORK == REST_FRAMEWORK
assert DEBUG is False


def test_development_settings_can_be_directly_imported():
def test_development_settings_can_be_directly_imported(monkeypatch):
"""Ensure that the development settings can be directly imported."""
monkeypatch.setenv('AWX_MODE', 'development')
from awx.settings.development import REST_FRAMEWORK
from awx.settings.development import DEBUG # actually set on defaults.py and not overridden in development.py

assert settings.REST_FRAMEWORK is REST_FRAMEWORK
assert settings.REST_FRAMEWORK == REST_FRAMEWORK
assert DEBUG is True


Expand Down Expand Up @@ -84,4 +86,4 @@ def test_merge_application_name():
"DATABASES__default__ENGINE": "django.db.backends.postgresql",
"CLUSTER_HOST_ID": "test-cluster-host-id",
}
assert merge_application_name(settings) == {"DATABASES__default__OPTIONS__application_name": "test-cluster-host-id"}
assert "test-cluster-host-id" in merge_application_name(settings)["DATABASES__default__OPTIONS__application_name"]
6 changes: 5 additions & 1 deletion awx/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
load_standard_settings_files,
validate,
)
from .functions import merge_application_name, toggle_feature_flags
from .functions import merge_application_name, toggle_feature_flags, add_backwards_compatibility


add_backwards_compatibility()


# Create a the standard DYNACONF instance which will come with DAB defaults
# This loads defaults.py and environment specific file e.g: development_defaults.py
Expand Down
38 changes: 38 additions & 0 deletions awx/settings/functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Any
from dynaconf import Dynaconf
from dynaconf.utils.functional import empty
Expand Down Expand Up @@ -30,3 +31,40 @@ def merge_application_name(settings):
if "sqlite3" not in settings.get("DATABASES__default__ENGINE", ""):
data["DATABASES__default__OPTIONS__application_name"] = get_application_name(settings.get("CLUSTER_HOST_ID"))
return data


def add_backwards_compatibility():
"""Add backwards compatibility for AWX_MODE.
Before dynaconf integration the usage of AWX settings was supported to be just
DJANGO_SETTINGS_MODULE=awx.settings.production or DJANGO_SETTINGS_MODULE=awx.settings.development
(development_quiet and development_kube were also supported).
With dynaconf the DJANGO_SETTINGS_MODULE should be set always to "awx.settings" as the only entry point
for settings and then "AWX_MODE" can be set to any of production,development,quiet,kube
or a combination of them separated by comma.
E.g:
export DJANGO_SETTINGS_MODULE=awx.settings
export AWX_MODE=production
awx-manage [command]
dynaconf [command]
If pointing `DJANGO_SETTINGS_MODULE` to `awx.settings.production` or `awx.settings.development` then
this function will set `AWX_MODE` to the correct value.
"""
django_settings_module = os.getenv("DJANGO_SETTINGS_MODULE", "awx.settings")
if django_settings_module == "awx.settings":
return

current_mode = os.getenv("AWX_MODE", "")
for _module_name in ["development", "production", "development_quiet", "development_kube"]:
if django_settings_module == f"awx.settings.{_module_name}":
_mode = current_mode.split(",")
if "development_" in _module_name and "development" not in current_mode:
_mode.append("development")
_mode_fragment = _module_name.replace("development_", "")
if _mode_fragment not in _mode:
_mode.append(_mode_fragment)
os.environ["AWX_MODE"] = ",".join(_mode)
21 changes: 21 additions & 0 deletions licenses/dynaconf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Bruno Rocha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 0981fdf

Please sign in to comment.