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 20, 2025
1 parent 6436ffa commit 0eb7b4e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
39 changes: 22 additions & 17 deletions awx/main/tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from django.conf import settings
from awx.settings import REST_FRAMEWORK
from awx.settings.functions import toggle_feature_flags, merge_application_name

LOCAL_SETTINGS = (
'ALLOWED_HOSTS',
'BROADCAST_WEBSOCKET_PORT',
Expand All @@ -18,11 +14,15 @@

def test_postprocess_auth_basic_enabled():
"""The final loaded settings should have basic auth enabled."""
from awx.settings import REST_FRAMEWORK

assert 'awx.api.authentication.LoggedBasicAuthentication' in REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES']


def test_default_settings():
"""Ensure that all default settings are present in the snapshot."""
from django.conf import settings

for k in dir(settings):
if k not in settings.DEFAULTS_SNAPSHOT or k in LOCAL_SETTINGS:
continue
Expand All @@ -33,34 +33,35 @@ 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
from django.conf import settings
from awx.settings import 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
from django.conf import settings
from awx.settings import REST_FRAMEWORK

assert settings.DYNACONF.REST_FRAMEWORK == REST_FRAMEWORK

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

assert settings.REST_FRAMEWORK is 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 django.conf import settings
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


def test_toggle_feature_flags():
"""Ensure that the toggle_feature_flags function works as expected."""
from awx.settings.functions import toggle_feature_flags

settings = {
"FLAGS": {
"FEATURE_SOME_PLATFORM_FLAG_ENABLED": [
Expand All @@ -80,8 +81,12 @@ def test_toggle_feature_flags():

def test_merge_application_name():
"""Ensure that the merge_application_name function works as expected."""
from awx.settings.functions import merge_application_name

settings = {
"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"}
result = merge_application_name(settings)["DATABASES__default__OPTIONS__application_name"]
assert result.startswith("awx-")
assert "test-cluster" in result
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.
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ djangorestframework-yaml==2.0.0
# via -r /awx_devel/requirements/requirements.in
durationpy==0.9
# via kubernetes
dynaconf==3.2.9
dynaconf==3.2.10
# via -r /awx_devel/requirements/requirements.in
enum-compat==0.0.3
# via asn1
Expand Down

0 comments on commit 0eb7b4e

Please sign in to comment.