|
1 |
| -# Sample Django Application for `piku` |
| 1 | +# Piku Django Postgres |
2 | 2 |
|
3 |
| -> Contributed by @chr15m |
| 3 | +Provides an example Django app using Postgres locally on the PIKU server. |
4 | 4 |
|
5 |
| -This is a simple Django app that demonstrates running the Django `collectstatic` and `migrate` tasks in the `release` worker phase. |
| 5 | +## Requirements |
| 6 | +1. Piku |
| 7 | +2. Postgres installed on PIKU vm |
| 8 | +``` ./piku-bootstrap install postgres.yml``` |
6 | 9 |
|
7 |
| -To publish this app to `piku`, clone this repository run the following commands: |
| 10 | +## Installation |
8 | 11 |
|
9 |
| -```bash |
10 |
| -git remote add piku piku@your_server:sample_django_app |
11 |
| -git push piku master |
| 12 | +### Clone this repo and push it to your Piku server |
12 | 13 | ```
|
| 14 | +git clone ... |
| 15 | +git remote add piku piku@<your server here>:<app> |
| 16 | +git push piku |
| 17 | +``` |
| 18 | +### Set the hostname for NGINX |
| 19 | +``` |
| 20 | +piku config:set NGINX_SERVER_NAME=<your fqdn> |
| 21 | +``` |
| 22 | +### Provision the Postgres Database |
| 23 | +The Database is given the same name as your NGINX_SERVER_NAME via createdb and runs Django's migrate commmand ```python manage.py migrate``` |
| 24 | +``` |
| 25 | +piku run -- ./bin/provision-database.sh |
| 26 | +``` |
| 27 | + |
| 28 | +### Create your superuser |
| 29 | +``` |
| 30 | +piku shell |
| 31 | +./manage.py createsuperuser |
| 32 | +``` |
| 33 | + |
| 34 | +### Navigate to the admin page |
| 35 | +Since debug hasn't been set, the default page won't work. You can validate the deployment works by hitting the admin page |
13 | 36 |
|
14 |
| -Then you can set up an SSL cert and connect a domain by setting config variables like this: |
| 37 | +\<fqdn\>/admin |
15 | 38 |
|
16 |
| -```bash |
17 |
| -ssh piku@your_server config:set django_example NGINX_SERVER_NAME=your_server NGINX_HTTPS_ONLY=1 |
| 39 | +# Piku features utilized |
| 40 | +This app utilizes several of Piku's ```Procfile``` features |
18 | 41 | ```
|
| 42 | +wsgi: pikupostgres.wsgi:application |
| 43 | +release: ./bin/stage_release.sh |
| 44 | +cron: 0 0 * * * ./bin/uwsgi_cron_midnight.sh |
| 45 | +``` |
| 46 | +## wsgi |
| 47 | +Runs this python app as a WSGI process |
| 48 | + |
| 49 | +## release |
| 50 | +Each time you push your code, Piku will run a 'release' cycle and run this code. ```./bin/stage_release.sh``` only runs Django's collectstatic function, but you can add more |
19 | 51 |
|
20 |
| -You can create a super user and set a password like this: |
| 52 | +## cron |
| 53 | +Piku uses UWSGI to schedule Cron jobs. ```./bin/uwsgi_cron_midnight.sh``` runs a Django clearsessions command every day at 0:00 (midnight) |
21 | 54 |
|
22 |
| -```bash |
23 |
| -piku run django_example -- ./manage.py createsuperuser --email [email protected] --username admin --no-input |
24 |
| -piku run django_example -- ./manage.py changepassword admin |
| 55 | +## NGINX static file mapping |
| 56 | +The ```ENV``` file defines a feature that has NGINX serve this application static files directly from the folder |
| 57 | +``` |
| 58 | +NGINX_STATIC_PATHS=/static:static |
25 | 59 | ```
|
| 60 | +# Django specifics |
26 | 61 |
|
27 |
| -You will not see a prompt after the second command but you can type a new password anyway and hit enter. |
| 62 | +## Django settings.py |
| 63 | + |
| 64 | +There are a few minor changes to Django to utilize Piku. These are added to the end of ```settings.py``` |
| 65 | +1. ```DEBUG``` - Debug is set to False unless the environment variable ```DEBUG``` is defined (```piku config:set DEBUG=true```) |
| 66 | +2. ```ALLOWED_HOSTS``` uses the NGINX_SERVER_NAME or DEBUG for security purposes |
| 67 | +3. ```DATABASES``` is updated if NGINX_SERVER_NAME is defined |
| 68 | +4. ```STATIC_ROOT``` is defined to reference the /static folder (tied to the above NGINX setting) |
| 69 | + |
| 70 | +## Database Changes |
| 71 | +Django makes it easy to modify the schema for your Models. However, you'll need to update the running DB via 'manage.py migrate' before pushing the code changes. Piku provides an easy way to accomplish this: |
| 72 | + |
| 73 | +``` |
| 74 | +piku stop |
| 75 | +piku shell |
| 76 | +./manage.py migrate |
| 77 | +``` |
| 78 | +After this, you can ```git push piku``` and you should be OK |
| 79 | + |
| 80 | +# Pipenv considerations |
| 81 | +If you use Pipenv for virtual environments, and given that Piku uses requirements.txt for it's python packages, you need to generate this before being pushed to piku. A freindly devops method is to use GIT's pre-commit hook. |
| 82 | + |
| 83 | +Add the following file to ./.git/hooks/pre-commit (as +x) |
| 84 | +``` |
| 85 | +#!/bin/bash |
| 86 | +
|
| 87 | +# Generate requirements.txt from Pipfile |
| 88 | +pipenv sync |
| 89 | +pipenv requirements > requirements.txt |
| 90 | +
|
| 91 | +# Add requirements.txt to the commit |
| 92 | +git add requirements.txt |
| 93 | +``` |
0 commit comments