Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/reference/extensions/django-framework.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.. _django-framework-reference:

django-framework
----------------

The Django extension streamlines the process of building Django application
rocks.

It facilitates the installation of Django application dependencies, including
Gunicorn, inside the rock. Additionally, it transfers your project files to
``/django/app`` within the rock.

A statsd-exporter is installed alongside the Gunicorn server to export Gunicorn
server metrics.

.. note::
The Django extension is compatible with the ``bare`` and ``ubuntu@22.04``
bases.

Project requirements
====================

There are 2 requirements to be able to use the ``django-framework`` extension:

1. There must be a ``requirements.txt`` file in the root directory of the
project with ``Django`` declared as a dependency.
2. The project must be named the same as the ``name`` in ``rockcraft.yaml`` with
any ``-`` replaced by ``_``, i.e., the ``manage.py`` must be located at
``./<Rock name with - replaced by _>/<Rock name with - replaced by _>/manage.py``
relative to the ``rockcraft.yaml`` file.

Useful links
============

- :ref:`build-a-rock-for-a-django-application`
1 change: 1 addition & 0 deletions docs/reference/extensions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ initiating a new rock.
:maxdepth: 1

flask-framework
django-framework
1 change: 1 addition & 0 deletions docs/tutorial/code/django/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Django
151 changes: 151 additions & 0 deletions docs/tutorial/code/django/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
###########################################
# IMPORTANT
# Comments matter!
# The docs use the wrapping comments as
# markers for including said instructions
# as snippets in the docs.
###########################################
summary: Getting started with Django tutorial

environment:

execute: |
# [docs:create-venv]
sudo apt-get update && sudo apt-get install python3-venv -y
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# [docs:create-venv-end]

# [docs:create-project]
django-admin startproject django_hello_world
# [docs:create-project-end]

cd django_hello_world
python manage.py runserver &
retry -n 5 --wait 2 curl localhost:8000

# [docs:curl-django]
curl localhost:8000
# [docs:curl-django-end]

kill $!

# [docs:create-rockcraft-yaml]
cd ..
rockcraft init --profile django-framework
# [docs:create-rockcraft-yaml-end]
sed -i "s/name: .*/name: django-hello-world/g" rockcraft.yaml

# [docs:pack]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
# [docs:pack-end]

# [docs:ls-rock]
ls *.rock -l --block-size=MB
# [docs:ls-rock-end]

# [docs:skopeo-copy]
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:django-hello-world_0.1_amd64.rock \
docker-daemon:django-hello-world:0.1
# [docs:skopeo-copy-end]

# [docs:docker-images]
sudo docker images django-hello-world:0.1
# [docs:docker-images-end]

# [docs:docker-run]
sudo docker run --rm -d -p 8000:8000 \
--name django-hello-world django-hello-world:0.1
# [docs:docker-run-end]
retry -n 5 --wait 2 curl localhost:8000

# [docs:curl-django-rock]
curl localhost:8000
# [docs:curl-django-rock-end]

# [docs:get-logs]
sudo docker exec django-hello-world pebble logs django
# [docs:get-logs-end]

# [docs:stop-docker]
sudo docker stop django-hello-world
sudo docker rmi django-hello-world:0.1
# [docs:stop-docker-end]

# [docs:change-base]
sed -i \
"s/base: .*/base: bare\nbuild-base: ubuntu@22.04/g" \
rockcraft.yaml
# [docs:change-base-end]
sed -i "s/version: .*/version: 0.1-chiselled/g" rockcraft.yaml

# [docs:chisel-pack]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
# [docs:chisel-pack-end]

# [docs:ls-bare-rock]
ls *.rock -l --block-size=MB
# [docs:ls-bare-rock-end]

# [docs:docker-run-chisel]
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:django-hello-world_0.1-chiselled_amd64.rock \
docker-daemon:django-hello-world:0.1-chiselled
sudo docker images django-hello-world:0.1-chiselled
sudo docker run --rm -d -p 8000:8000 \
--name django-hello-world django-hello-world:0.1-chiselled
# [docs:docker-run-chisel-end]
retry -n 5 --wait 2 curl localhost:8000

# [docs:curl-django-bare-rock]
curl localhost:8000
# [docs:curl-django-bare-rock-end]

# [docs:stop-docker-chisel]
sudo docker stop django-hello-world
sudo docker rmi django-hello-world:0.1-chiselled
# [docs:stop-docker-chisel-end]

# [docs:create-time-app]
cd django_hello_world
django-admin startapp time_app
# [docs:create-time-app-end]
cat ../time_app_views.py > time_app/views.py
cat ../time_app_urls.py > time_app/urls.py
cat ../urls.py > django_hello_world/urls.py

cd ..
sed -i "s/version: .*/version: 0.2/g" rockcraft.yaml

# [docs:docker-run-update]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
sudo rockcraft.skopeo --insecure-policy copy \
oci-archive:django-hello-world_0.2_amd64.rock \
docker-daemon:django-hello-world:0.2
sudo docker images django-hello-world:0.2
sudo docker run --rm -d -p 8000:8000 \
--name django-hello-world django-hello-world:0.2
# [docs:docker-run-update-end]
retry -n 5 --wait 2 curl localhost:8000/time/

# [docs:curl-time]
curl localhost:8000/time/
# [docs:curl-time-end]

# [docs:stop-docker-updated]
sudo docker stop django-hello-world
sudo docker rmi django-hello-world:0.2
# [docs:stop-docker-updated-end]

# [docs:cleanup]
# exit and delete the virtual environment
deactivate
rm -rf .venv django_hello_world
# delete all the files created during the tutorial
rm django-hello-world_0.1_amd64.rock \
django-hello-world_0.1-chiselled_amd64.rock \
django-hello-world_0.2_amd64.rock \
rockcraft.yaml requirements.txt
# [docs:cleanup-end]
7 changes: 7 additions & 0 deletions docs/tutorial/code/django/time_app_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from . import views

urlpatterns = [
path("", views.index, name="index"),
]
7 changes: 7 additions & 0 deletions docs/tutorial/code/django/time_app_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import datetime

from django.http import HttpResponse


def index(request):
return HttpResponse(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
7 changes: 7 additions & 0 deletions docs/tutorial/code/django/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path("time/", include("time_app.urls")),
path("admin/", admin.site.urls),
]
2 changes: 1 addition & 1 deletion docs/tutorial/code/flask/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ execute: |
sudo docker rmi flask-hello-world:0.1-chiselled
# [docs:stop-docker-chisel-end]

cat time-app.py > app.py
cat time_app.py > app.py
sed -i "s/version: .*/version: 0.2/g" rockcraft.yaml
rockcraft clean

Expand Down
Loading