Skip to content

Commit efe1456

Browse files
author
Michael Nelson
committed
Simplify database settings.
1 parent c32e2b4 commit efe1456

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

playbook.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@
4545
owner: "{{ wsgi_user }}"
4646
group: "{{ wsgi_group }}"
4747
tags:
48-
- install
49-
- upgrade-charm
48+
- config-changed
49+
notify:
50+
- Restart wsgi
51+
52+
- name: Write the database settings
53+
template:
54+
src: "templates/database_settings.py.j2"
55+
dest: "{{ application_dir }}/django-project/clickreviewsproject/database_settings.py"
56+
owner: "{{ wsgi_user }}"
57+
group: "{{ wsgi_group }}"
58+
tags:
5059
- db-relation-changed
5160
notify:
5261
- Restart wsgi
62+
when: "'database' in current_relation"
5363

5464
# XXX Normally our deployment build would ensure these are all available in the tarball.
5565
- name: Install any non-distro dependencies (Don't depend on pip for a real deployment!)

templates/database_settings.py.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DATABASES = {
2+
'default': {
3+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
4+
'NAME': '{{ current_relation["database"] }}',
5+
'HOST': '{{ current_relation["host"] }}',
6+
'USER': '{{ current_relation["user"] }}',
7+
'PASSWORD': '{{ current_relation["password"] }}',
8+
}
9+
}

templates/settings.py.j2

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,7 @@ DEBUG = True
22
TEMPLATE_DEBUG = DEBUG
33
SECRET_KEY = '{{ django_secret_key }}'
44

5-
{% if 'db' in relations %}
6-
DATABASES = {
7-
{% for dbrelation in relations['db'] %}
8-
{% if loop.first %}
9-
{% set services=relations['db'][dbrelation] %}
10-
{% for service in services %}
11-
{% if service.startswith('postgres') %}
12-
{% set dbdetails = services[service] %}
13-
{% if 'database' in dbdetails %}
14-
'default': {
15-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
16-
'NAME': '{{ dbdetails["database"] }}',
17-
'HOST': '{{ dbdetails["host"] }}',
18-
'USER': '{{ dbdetails["user"] }}',
19-
'PASSWORD': '{{ dbdetails["password"] }}',
20-
}
21-
{% endif %}
22-
{% endif %}
23-
{% endfor %}
24-
{% endif %}
25-
{% endfor %}
26-
}
27-
{% endif %}
5+
from database_settings import DATABASES
286

297
TIME_ZONE = 'UTC'
308
ROOT_URLCONF = 'clickreviewsproject.urls'

0 commit comments

Comments
 (0)