Skip to content

Commit

Permalink
feat: use DAB to load dynaconf settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Feb 26, 2025
1 parent 64f4fe6 commit fdc3079
Show file tree
Hide file tree
Showing 20 changed files with 1,081 additions and 889 deletions.
3 changes: 2 additions & 1 deletion Taskfile.dist.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
version: "3"

env:
DJANGO_SETTINGS_MODULE: "aap_eda.settings.development"
EDA_MODE: development
DJANGO_SETTINGS_MODULE: "aap_eda.settings.default"

vars:
DOCKER_COMPOSE: '{{ default "docker-compose" .DOCKER_COMPOSE }}'
Expand Down
40 changes: 28 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dev = ["psycopg-binary"]
python = ">=3.11,<3.13"
django = ">=4.2,<4.3"
djangorestframework = "3.15.*"
dynaconf = ">=3.1.12,<3.3"
dynaconf = ">=3.2.7,<4.0"
drf-spectacular = ">=0.26.5,<0.27"
channels = { version = "4.0.*", extras = ["daphne"] }
psycopg-binary = { version = "*", optional = true }
Expand All @@ -48,7 +48,7 @@ cryptography = ">=42,<43"
kubernetes = "26.1.*"
podman = "5.2.*"
rq-scheduler = "^0.10"
django-ansible-base = { git = "https://github.com/ansible/django-ansible-base.git", tag = "2025.1.31", extras = [
django-ansible-base = { git = "https://github.com/rochacbruno/django-ansible-base.git", branch = "dynaconf_settings", extras = [
"channel-auth",
"rbac",
"redis-client",
Expand All @@ -72,6 +72,7 @@ django-flags = "^5.0.13"

[tool.poetry.group.test.dependencies]
pytest = "*"
pytest-env = "*"
pytest-django = "*"
pytest-asyncio = "*"
requests = { version = "*", python = "<4.0" }
Expand Down
4 changes: 3 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[pytest]
asyncio_mode = auto
DJANGO_SETTINGS_MODULE = aap_eda.settings.development
env =
EDA_MODE=development
DJANGO_SETTINGS_MODULE = aap_eda.settings.default
log_file_level = INFO
11 changes: 7 additions & 4 deletions src/aap_eda/core/tasking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
from django.conf import settings
from rq import results as rq_results

from aap_eda.settings import default
from aap_eda.settings import (
constants as setting_constants,
redis as redis_settings,
)

__all__ = [
"Job",
Expand Down Expand Up @@ -131,7 +134,7 @@ def _prune_redis_kwargs(**kwargs) -> dict[str, typing.Any]:
db = kwargs.get("db", None)
if (db is not None) and (kwargs.get("mode", "") == "cluster"):
del kwargs["db"]
if db != default.DEFAULT_REDIS_DB:
if db != setting_constants.DEFAULT_REDIS_DB:
logger.info(
f"clustered redis supports only the default db"
f"; db specified: {db}"
Expand All @@ -151,7 +154,7 @@ def get_redis_client(**kwargs) -> typing.Union[DABRedis, DABRedisCluster]:

def get_redis_status() -> dict:
"""Query DAB for the status of Redis."""
kwargs = default.rq_redis_client_instantiation_parameters()
kwargs = redis_settings.rq_redis_client_instantiation_parameters()
kwargs = _prune_redis_kwargs(**kwargs)
return _get_redis_status(_create_url_from_parameters(**kwargs), **kwargs)

Expand Down Expand Up @@ -329,7 +332,7 @@ def _get_necessary_client_connection(
) -> rq.Connection:
if not isinstance(connection, (DABRedis, DABRedisCluster)):
connection = get_redis_client(
**default.rq_redis_client_instantiation_parameters()
**redis_settings.rq_redis_client_instantiation_parameters()
)
return connection

Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .default import * # noqa: F403
169 changes: 169 additions & 0 deletions src/aap_eda/settings/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Application definition
INSTALLED_APPS = [
"daphne",
"flags",
# Django apps
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.staticfiles",
# Third party apps
"rest_framework",
"drf_spectacular",
"django_rq",
"django_filters",
"ansible_base.rbac",
"ansible_base.resource_registry",
"ansible_base.jwt_consumer",
"ansible_base.rest_filters",
"ansible_base.feature_flags",
# Local apps
"aap_eda.api",
"aap_eda.core",
]


MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"crum.CurrentRequestUserMiddleware",
]

ROOT_URLCONF = "aap_eda.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
],
},
},
]

WSGI_APPLICATION = "aap_eda.wsgi.application"

ASGI_APPLICATION = "aap_eda.asgi.application"

# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", # noqa: E501
},
]

# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = "en-us"

USE_I18N = True

TIME_ZONE = "UTC"

USE_TZ = True

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTH_USER_MODEL = "core.User"

REST_FRAMEWORK = {
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_PAGINATION_CLASS": "aap_eda.api.pagination.DefaultPagination",
"PAGE_SIZE": 20,
"DEFAULT_AUTHENTICATION_CLASSES": [
"aap_eda.api.authentication.SessionAuthentication",
"rest_framework.authentication.BasicAuthentication",
"aap_eda.api.authentication.WebsocketJWTAuthentication",
"ansible_base.jwt_consumer.eda.auth.EDAJWTAuthentication",
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
"ansible_base.rbac.api.permissions.AnsibleBaseObjectPermissions",
],
"TEST_REQUEST_DEFAULT_FORMAT": "json",
"DEFAULT_METADATA_CLASS": "aap_eda.api.metadata.EDAMetadata",
"EXCEPTION_HANDLER": "aap_eda.api.exceptions.api_fallback_handler",
}

DEFAULT_REDIS_DB = 0

# ---------------------------------------------------------
# TASKING SETTINGS
# ---------------------------------------------------------
RQ = {
"JOB_CLASS": "aap_eda.core.tasking.Job",
"QUEUE_CLASS": "aap_eda.core.tasking.Queue",
"SCHEDULER_CLASS": "aap_eda.core.tasking.Scheduler",
"WORKER_CLASS": "aap_eda.core.tasking.Worker",
}

# Time window in seconds to consider a worker as dead
DEFAULT_WORKER_HEARTBEAT_TIMEOUT = 60
DEFAULT_WORKER_TTL = 5

RQ_STARTUP_JOBS = []

# Id of the scheduler job it's required when we have multiple instances of
# the scheduler running to avoid duplicate jobs
RQ_PERIODIC_JOBS = [
{
"func": (
"aap_eda.tasks.orchestrator.enqueue_monitor_rulebook_processes"
),
"interval": 5,
"id": "enqueue_monitor_rulebook_processes",
},
{
"func": "aap_eda.tasks.project.monitor_project_tasks",
"interval": 30,
"id": "monitor_project_tasks",
},
]
RQ_CRON_JOBS = []

ANSIBLE_BASE_CUSTOM_VIEW_PARENT = "aap_eda.api.views.dab_base.BaseAPIView"

# ---------------------------------------------------------
# DJANGO ANSIBLE BASE RESOURCES REGISTRY SETTINGS
# ---------------------------------------------------------
ANSIBLE_BASE_RESOURCE_CONFIG_MODULE = "aap_eda.api.resource_api"

# ---------------------------------------------------------
# DJANGO ANSIBLE BASE RBAC SETTINGS
# ---------------------------------------------------------
DEFAULT_ORGANIZATION_NAME = "Default"
ANSIBLE_BASE_SERVICE_PREFIX = "eda"
ANSIBLE_BASE_TEAM_MODEL = "core.Team"
ANSIBLE_BASE_ORGANIZATION_MODEL = "core.Organization"

# Organization and object roles will come from create_initial_data
ANSIBLE_BASE_ROLE_PRECREATE = {}

ANSIBLE_BASE_ALLOW_SINGLETON_USER_ROLES = True
ANSIBLE_BASE_CHECK_RELATED_PERMISSIONS = ["view"]

DEFAULT_SYSTEM_PG_NOTIFY_CREDENTIAL_NAME = "_DEFAULT_EDA_PG_NOTIFY_CREDS"
Loading

0 comments on commit fdc3079

Please sign in to comment.