Django¶
You can use any django project you want but you might have to configure some things. The best way to start is to use the Cookiecutter-Django template. It will create the whole project skeleton for you and is ready to use with this project. This repository is using a test django project, which was created with Cookiecutter-Django.
Note that the default values in the playbooks assume that your project structure looks something like this:
myproject
├── manage.py
├── myproject
│ ├── apps
│ │ └── __init__.py
│ ├── templates
│ │ ├── 403.html
│ │ ├── 404.html
│ │ ├── 500.html
│ │ └── base.html
│ ├── __init__.py
│ ├── celery.py
├── config
│ ├── settings
│ │ ├── base.py
│ │ ├── __init__.py
│ │ ├── local.py
│ │ └── production.py
│ ├── urls.py
│ └── wsgi.py
├── README.md
└── requirements.txt
The main things to note are the locations of the manage.py, wsgi.py files and the celery app. If your project’s structure is a little different, you may need to change the values in these 3 varibales:
django_settings_file: Default"config.settings.production"django_wsgi_file: Defaultconfig.wsgicelery_worker_app: Default{{ application_name }}(in this example myproject)
For Celery it is expected that the app is {{ application_name }}. You should import the celery app in myproject/myproject/__init__.py or change the celery_worker_app variable. See the Celery documentation.
For the celerycam role, Djcelery is required to be in INSTALLED_APPS so one can track the status of the workers and tasks.
Also, if your app needs additional system packages installed, you can add them in roles/web/tasks/install_additional_packages.yml.
Settings¶
Your django project settings have to be setup in a way so that they can be set via environment variables. A good way is 12-Factor based settings via django-environ. Check the Cookiecutter-Django settings. They are setup so that the following environment variables take effect:
DJANGO_SETTINGS_MODULEDJANGO_SECRET_KEYMEDIA_ROOTSTATIC_ROOTDATABASE_URLDJANGO_EMAIL_HOSTEMAIL_PORTEMAIL_HOST_USEREMAIL_HOST_PASSWORDDJANGO_DEFAULT_FROM_EMAILBROKER_URL
Logging¶
To log to the Graylog server, you have to configure your logger. The best way is to use Graypy. It transmits logs via UDP or alternatively with RabbitMQ. Here is a sample from the test project:
LOGGING = {
'handlers': {
'graypy': {
'class': 'graypy.GELFHandler',
'host': 'localhost',
'port': 12201,
},
...
},
'loggers': {
'django.request': {
'handlers': ['graypy', ...],
'level': 'WARNING',
'propagate': True,
},
...
}
}