Skip to content

Commit

Permalink
Adds support for the database relation, including:
Browse files Browse the repository at this point in the history
 * Updating the settings so that during db-relation-changed the first
   database relation is used as the default django database.
 * documenting (via the Makefile) the custom steps required to ensure
   that the debversion postgresql type is available on the postgres
   service.
 * Adding a syncdb 'hook' (really a task), which can be called via juju
   run.
 * Removing south from the installed apps due to the broken migration,
   instead creating the tables in teh currently defined state (although
   south is still required on the python path due to being imported by
   the app).
  • Loading branch information
Michael Nelson committed Jun 23, 2014
1 parent de1d11a commit c32e2b4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Makefile
Expand Up @@ -37,8 +37,17 @@ deploy: create-tarball
@juju set ubuntu-reviews build_label=r$(REVIEWS_REVNO)
@juju deploy gunicorn
@juju deploy nrpe-external-master
@juju deploy postgresql
@juju add-relation ubuntu-reviews gunicorn
@juju add-relation ubuntu-reviews nrpe-external-master
@juju add-relation ubuntu-reviews postgresql:db
@juju set postgresql extra-packages=postgresql-9.1-debversion
@echo "Once the services are related, run 'make setup-db'"

setup-db:
@echo "Creating custom debversion postgres type and syncing the ubuntu-reviews database."
@juju run --service postgresql 'psql -U postgres ubuntu-reviews -c "CREATE EXTENSION debversion;"'
@juju run --unit=ubuntu-reviews/0 "hooks/syncdb"
@echo See the README for explorations after deploying.

curl:
Expand Down
2 changes: 1 addition & 1 deletion django-project/clickreviewsproject/urls.py
Expand Up @@ -2,5 +2,5 @@

urlpatterns = patterns(
'',
url(r'^$', include('clickreviews.api.urls'), name='api'),
url(r'^', include('clickreviews.api.urls'), name='api'),
)
8 changes: 7 additions & 1 deletion django-project/manage.py
Expand Up @@ -3,7 +3,13 @@
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "clickreviews.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "clickreviewsproject.settings")
extra_paths = [
'/srv/click-reviews.ubuntu.com/code/current/branches/scaclient/',
'/srv/click-reviews.ubuntu.com/code/current/branches/piston-mini-client/',
'/srv/click-reviews.ubuntu.com/code/current/branches/django-piston/',
]
sys.path.extend(extra_paths)

from django.core.management import execute_from_command_line

Expand Down
1 change: 1 addition & 0 deletions hooks/db-relation-changed
1 change: 1 addition & 0 deletions hooks/syncdb
3 changes: 3 additions & 0 deletions metadata.yaml
Expand Up @@ -15,3 +15,6 @@ provides:
nrpe-external-master:
interface: nrpe-external-master
scope: container
requires:
db:
interface: pgsql
14 changes: 13 additions & 1 deletion playbook.yml
Expand Up @@ -3,13 +3,13 @@

vars:
- app_label: click-reviews.ubuntu.com
- code_archive: "{{ build_label }}/rnr-server.tgz"

roles:
- role: wsgi-app
listen_port: 8080
python_path: "{{ application_dir }}/django-project:{{ current_code_dir }}/src"
wsgi_application: clickreviewsproject.wsgi:application
code_archive: "{{ build_label }}/rnr-server.tgz"
when: build_label != ''

- role: nrpe-external-master
Expand All @@ -25,6 +25,7 @@
- python-django=1.5.4-1ubuntu1~ctools0
- python-tz
- python-pip
- python-psycopg2
tags:
- install
- upgrade-charm
Expand All @@ -46,13 +47,24 @@
tags:
- install
- upgrade-charm
- db-relation-changed
notify:
- Restart wsgi

# XXX Normally our deployment build would ensure these are all available in the tarball.
- name: Install any non-distro dependencies (Don't depend on pip for a real deployment!)
pip: name={{ item.key }} version={{ item.value }}
with_dict:
south: 0.7.6
django-openid-auth: 0.2
tags:
- install
- upgrade-charm

- name: sync the database
django_manage: >
command="syncdb --noinput"
app_path="{{ application_dir }}/django-project"
pythonpath="{{ code_dir }}/current/src"
tags:
- syncdb
27 changes: 26 additions & 1 deletion templates/settings.py.j2
@@ -1,6 +1,31 @@
DEBUG = True
TEMPLATE_DEBUG = DEBUG
SECRET_KEY = '{{ django_secret_key }}'

{% if 'db' in relations %}
DATABASES = {
{% for dbrelation in relations['db'] %}
{% if loop.first %}
{% set services=relations['db'][dbrelation] %}
{% for service in services %}
{% if service.startswith('postgres') %}
{% set dbdetails = services[service] %}
{% if 'database' in dbdetails %}
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '{{ dbdetails["database"] }}',
'HOST': '{{ dbdetails["host"] }}',
'USER': '{{ dbdetails["user"] }}',
'PASSWORD': '{{ dbdetails["password"] }}',
}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
}
{% endif %}

TIME_ZONE = 'UTC'
ROOT_URLCONF = 'clickreviewsproject.urls'
WSGI_APPLICATION = 'clickreviewsproject.wsgi.application'
Expand All @@ -12,8 +37,8 @@ INSTALLED_APPS = (
'django_openid_auth',
'django.contrib.messages',
'django.contrib.staticfiles',
'reviewsapp',
'clickreviews',
'south',
)
AUTHENTICATION_BACKENDS = (
'django_openid_auth.auth.OpenIDBackend',
Expand Down

0 comments on commit c32e2b4

Please sign in to comment.