Page MenuHomePhabricator

My first kubernetes + python3 + django app tutorial
Open, LowPublic

Description

We have some docs for Flask apps, but Django is also a popular python framework. The convention of --mount /tool=~/www/python/src/app.py and a callable named app doesn't cleanly match up with the generated files for a Django application. It should be possible to setup however with a bit of custom code.

See also:

Related to: T134494

Event Timeline

Restricted Application added a project: Cloud-Services. · View Herald TranscriptOct 26 2016, 1:48 PM
Restricted Application added a subscriber: Aklapper. · View Herald Transcript

Creating a ~www/python/src/app.py file like this would be a starting point to iterate on until the app can be made to work:

1from django.core.wsgi import get_wsgi_application
2app = get_wsgi_application()

I now used this for the app.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "commons_app_web.settings")

app = get_wsgi_application()

The variable really has to be named app otherwise the webserver complains.

I still have to figure out how to get the static files to work. The current project structure looks like this:

.
└── www
    └── python
        └── src
            ├── app.py      <------
            ├── commons_app_web
            ├── main_site
            │   ├── migrations
            │   ├── static
            │   │   ├── css
            │   │   ├── css-dev
            │   │   │   ├── base
            │   │   │   ├── plugins
            │   │   │   └── sections
            │   │   ├── images
            │   │   ├── js
            │   │   └── js-dev
            │   └── templates
            │       └── main_site
            └── static
                ├── admin
                │   ├── css
                │   ├── fonts
                │   ├── img
                │   │   └── gis
                │   └── js
                │       ├── admin
                │       └── vendor
                │           ├── jquery
                │           └── xregexp
                ├── css
                ├── css-dev
                │   ├── base
                │   ├── plugins
                │   └── sections
                ├── images
                ├── js
                └── js-dev

The framework currently constructs the static links like this: https://tools.wmflabs.org/static/css/style.css, but I am not sure where they are really located on the server.

bd808 added a comment.EditedOct 28 2016, 11:54 PM

@Tobias1984 To actually serve your static content check out https://uwsgi-docs.readthedocs.io/en/latest/StaticFiles.html and put your custom uwsgi settings in ~/www/python/uwsgi.ini. The URL would end up being https://tools.wmflabs.org/$TOOL/path/you/mount/them/at.

@yuvipanda says that the uwsgi.ini settings can override basically all of the built-in args given by the launcher as well so we can probably figure out how to avoid the custom app.py loader script too.

I now defined that static URL like this in Django's settings.py:

STATIC_URL = '/commons-app-web/static/'

that means the images are for example requested from:

https://tools.wmflabs.org/commons-app-web/static/images/f-droid-badge.png

I copied all the static files to ~/www/python/static and set the uwsgi.ini to:

[uwsgi]
check-static = ~/www/python/static

The problem is that this is not caught by the webserver and the request is handled and rerouted by Django.

What am I missing?

Untested, but the docs say:

--check-static /customers/foobar/app001/public
If uWSGI receives a request for /foo.png will first check for the existence of /customers/foobar/app001/public/foo.png and if it is not found it will invoke your app.

If your request is for /TOOL/static/images/f-droid-badge.png and the file is at ~/www/python/static/images/f-droid-badge.png, I think the uwsgi config would be check-static = ~/www/python.

@bd808 Thanks for the pointer. This time I finally got it to work. I still had issues with the ~ path. Using the absolute path works:

This uwsgi.ini configuration works (using the above STATIC_URL):

check-static = /data/project/commons-app-web/www/python
scfc triaged this task as Low priority.Feb 16 2017, 9:36 PM
scfc moved this task from Triage to Backlog on the Toolforge board.

@scfc Should we perhaps close this issue. It seems the issue I had with the paths has been solved and it is now also documented on the wiki: https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Web/Kubernetes#Django

Tobias1984 updated the task description. (Show Details)Mar 8 2017, 7:29 PM

@bd808 I would still like to leave this task open until the workshop at the Prague and Vienna hackathon is over to see how well the tutorial works with a live audience. Are there any things you you think the task still needs?

@bd808 I would still like to leave this task open until the workshop at the Prague and Vienna hackathon is over to see how well the tutorial works with a live audience.

Getting one or two people to test the tutorial is a good idea. There were several small bugs in the Flask tutorial I wrote that were found by having someone else try to follow it.

Are there any things you you think the task still needs?

The only thing that jumps out at me is the sqlite usage for the django db. Using sqlite on an NFS share is not the best way to store data in Tool Labs. It would be nice to see instructions on using the shared MariaDB/MySQL database servers as the data store instead.

It would be nice to see instructions on using the shared MariaDB/MySQL database servers as the data store instead.

Excellent idea. I will work on that too!

Abbe98 added a subscriber: Abbe98.Aug 21 2017, 5:37 PM