Skip to content

Commit

Permalink
prod settings check
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jan 29, 2025
1 parent 91d605f commit 9cd5a01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 70 deletions.
36 changes: 34 additions & 2 deletions awx/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
import os
from ansible_base.lib.dynamic_config import factory, export, load_standard_settings_files
from .application_name import merge_application_name
from dynaconf import Validator
Expand Down Expand Up @@ -28,8 +29,18 @@
################################################################################################

# Load extra config
DYNACONF.load_file("/etc/tower/settings.py")
DYNACONF.load_file("/etc/tower/conf.d/*.py")
# Load settings from any .py files in the global conf.d directory specified in
# the environment, defaulting to /etc/tower/conf.d/.
settings_dir = os.environ.get('AWX_SETTINGS_DIR', '/etc/tower/conf.d/')
settings_files_path = os.path.join(settings_dir, '*.py')
# Load remaining settings from the global settings file specified in the
# environment, defaulting to /etc/tower/settings.py.
settings_file_path = os.environ.get('AWX_SETTINGS_FILE', '/etc/tower/settings.py')
# Attempt to load settings from /etc/tower/settings.py first, followed by
# /etc/tower/conf.d/*.py.
DYNACONF.load_file(settings_file_path)
DYNACONF.load_file(settings_files_path)

if DYNACONF.get_environ("AWX_KUBE_DEVEL"):
DYNACONF.load_file("kube_defaults.py")
else:
Expand All @@ -38,6 +49,27 @@
# Load new standard settings files from /etc/ansible-automation-platform/config/awx/
load_standard_settings_files(DYNACONF)

# Check at least one required setting file has been loaded
# NOTE: This potentially could be moved to a validator
if "production" in DYNACONF.current_env.lower():
required_settings_paths = [
os.path.dirname(settings_file_path),
"/etc/ansible-automation-platform/",
]
# check if at least one file has been loaded any of the required paths
# use DYNACONF._loaded_files to check any filename inside the paths
# if not loaded then raise an ImproperlyConfigured error
for path in required_settings_paths:
if any(path in f for f in DYNACONF._loaded_files):
break
else:
from django.core.exceptions import ImproperlyConfigured

msg = 'No AWX configuration found at %s.' % required_settings_paths
msg += '\nDefine the AWX_SETTINGS_FILE environment variable to '
msg += 'specify an alternate path.'
raise ImproperlyConfigured(msg)

# Load envvars at the end to allow them to override everything
env_loader.load(DYNACONF, identifier="awx.settings")

Expand Down
68 changes: 0 additions & 68 deletions awx/settings/production_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@

# Production settings for AWX project.

# Python
import os
import copy
import errno
import sys
import traceback

# Django Split Settings
from split_settings.tools import optional, include

# Load default settings.
from .defaults import * # NOQA

DEBUG = False
TEMPLATE_DEBUG = DEBUG
Expand All @@ -39,60 +28,3 @@
'/etc/pki/ca-trust:/etc/pki/ca-trust:O',
'/usr/share/pki:/usr/share/pki:O',
]

# # Store a snapshot of default settings at this point before loading any
# # customizable config files.
# this_module = sys.modules[__name__]
# local_vars = dir(this_module)
# DEFAULTS_SNAPSHOT = {} # define after we save local_vars so we do not snapshot the snapshot
# for setting in local_vars:
# if setting.isupper():
# DEFAULTS_SNAPSHOT[setting] = copy.deepcopy(getattr(this_module, setting))

# del local_vars # avoid temporary variables from showing up in dir(settings)
# del this_module
# #
# ###############################################################################################
# #
# # Any settings defined after this point will be marked as as a read_only database setting
# #
# ################################################################################################

# Load settings from any .py files in the global conf.d directory specified in
# the environment, defaulting to /etc/tower/conf.d/.
settings_dir = os.environ.get('AWX_SETTINGS_DIR', '/etc/tower/conf.d/')
settings_files = os.path.join(settings_dir, '*.py')

# Load remaining settings from the global settings file specified in the
# environment, defaulting to /etc/tower/settings.py.
settings_file = os.environ.get('AWX_SETTINGS_FILE', '/etc/tower/settings.py')

# Attempt to load settings from /etc/tower/settings.py first, followed by
# /etc/tower/conf.d/*.py.
try:
include(settings_file, optional(settings_files), scope=locals())
except ImportError:
traceback.print_exc()
sys.exit(1)
except IOError:
from django.core.exceptions import ImproperlyConfigured

included_file = locals().get('__included_file__', '')
if not included_file or included_file == settings_file:
# The import doesn't always give permission denied, so try to open the
# settings file directly.
try:
e = None
open(settings_file)
except IOError:
pass
if e and e.errno == errno.EACCES:
SECRET_KEY = 'permission-denied'
LOGGING = {}
else:
msg = 'No AWX configuration found at %s.' % settings_file
msg += '\nDefine the AWX_SETTINGS_FILE environment variable to '
msg += 'specify an alternate path.'
raise ImproperlyConfigured(msg)
else:
raise

0 comments on commit 9cd5a01

Please sign in to comment.