Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blank DB host value, reuse backend code #15736

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions awx/main/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from django.conf import settings
from django.db import connection as pg_connection
from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper

NOT_READY = ([], [], [])

Expand Down Expand Up @@ -95,24 +96,25 @@


def create_listener_connection():
conf = deepcopy(settings.DATABASES['default'])
conf['OPTIONS'] = deepcopy(conf.get('OPTIONS', {}))
# Variable is called settings_dict to correspond to internal use in Django itself
settings_dict = deepcopy(settings.DATABASES['default'])
settings_dict['OPTIONS'] = deepcopy(settings_dict.get('OPTIONS', {}))

Check warning on line 101 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L100-L101

Added lines #L100 - L101 were not covered by tests

# Modify the application name to distinguish from other connections the process might use
conf['OPTIONS']['application_name'] = get_application_name(settings.CLUSTER_HOST_ID, function='listener')
settings_dict['OPTIONS']['application_name'] = get_application_name(settings.CLUSTER_HOST_ID, function='listener')

Check warning on line 104 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L104

Added line #L104 was not covered by tests

# Apply overrides specifically for the listener connection
for k, v in settings.LISTENER_DATABASES.get('default', {}).items():
if k != 'OPTIONS':
conf[k] = v
settings_dict[k] = v

Check warning on line 109 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L109

Added line #L109 was not covered by tests
for k, v in settings.LISTENER_DATABASES.get('default', {}).get('OPTIONS', {}).items():
conf['OPTIONS'][k] = v
settings_dict['OPTIONS'][k] = v

Check warning on line 111 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L111

Added line #L111 was not covered by tests

# Allow password-less authentication
if 'PASSWORD' in conf:
conf['OPTIONS']['password'] = conf.pop('PASSWORD')
# Reuse the Django postgres DB backend to create params for the psycopg library
psycopg_conn_params = PsycopgDatabaseWrapper(settings_dict).get_connection_params()
psycopg_conn_params['autocommit'] = True

Check warning on line 115 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L114-L115

Added lines #L114 - L115 were not covered by tests

connection_data = f"dbname={conf['NAME']} host={conf['HOST']} user={conf['USER']} port={conf['PORT']}"
return psycopg.connect(connection_data, autocommit=True, **conf['OPTIONS'])
return psycopg.connect(**psycopg_conn_params)

Check warning on line 117 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L117

Added line #L117 was not covered by tests


@contextmanager
Expand Down
Loading