Skip to content

Commit 82208a2

Browse files
authored
Merge pull request #33 from jfmatth/enh/django-5
Update to django 5 and various other changes.
2 parents 8face2f + 11aa920 commit 82208a2

16 files changed

+322
-72
lines changed

LICENSE

-21
This file was deleted.

Pipfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
django = "<6"
8+
psycopg = {extras = ["binary", "pool"], version = "*"}
9+
10+
[dev-packages]
11+
12+
[requires]
13+
python_version = "3.10"

Pipfile.lock

+141
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Procfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
release: ./bin/provision-database
2-
wsgi: pikudjango.wsgi:application
1+
wsgi: pikupostgres.wsgi:application
2+
release: ./bin/stage_release.sh
3+
cron: 0 0 * * * ./bin/uwsgi_cron_midnight.sh

README.md

+81-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,93 @@
1-
# Sample Django Application for `piku`
1+
# Piku Django Postgres
22

3-
> Contributed by @chr15m
3+
Provides an example Django app using Postgres locally on the PIKU server.
44

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```
69

7-
To publish this app to `piku`, clone this repository run the following commands:
10+
## Installation
811

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
1213
```
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
1336

14-
Then you can set up an SSL cert and connect a domain by setting config variables like this:
37+
\<fqdn\>/admin
1538

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
1841
```
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
1951

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)
2154

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
2559
```
60+
# Django specifics
2661

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+
```

bin/provision-database

-4
This file was deleted.

bin/provision-database.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
echo "Db/server name: [${NGINX_SERVER_NAME}]"
4+
5+
if [ "${NGINX_SERVER_NAME}" = "" ]
6+
then
7+
echo "Skipping db creation as NGINX_SERVER_NAME is not set."
8+
else
9+
psql "${NGINX_SERVER_NAME}" -c ";" 2>/dev/null && echo "Database ${NGINX_SERVER_NAME} already exists" || createdb "${NGINX_SERVER_NAME}"
10+
fi
11+
12+
./manage.py migrate --no-input

bin/stage_release.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./manage.py collectstatic --no-input

bin/uwsgi_cron_midnight.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python manage.py clearsessions -v 3

manage.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
def main():
8-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pikudjango.settings')
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pikupostgres.settings')
910
try:
1011
from django.core.management import execute_from_command_line
1112
except ImportError as exc:
File renamed without changes.

pikupostgres/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for pikupostgres project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pikupostgres.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)