Page MenuHomePhabricator

My first kubernetes + python3 + django app tutorial
Closed, ResolvedPublic

Description

https://wikitech.wikimedia.org/wiki/Help:Toolforge/My_first_Django_OAuth_tool

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 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.

@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 Backlog to Ready to be worked on 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

@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!

@Tobias1984: Hi! This task has been assigned to you a while ago. Could you maybe share an update? Do you still plan to work on this task, or do you need any help?

If this task has been resolved in the meantime: Please update the task status (via Add Action...Change Status in the dropdown menu).
If this task is not resolved and only if you do not plan to work on this task anymore: Please consider removing yourself as assignee (via Add Action...Assign / Claim in the dropdown menu): That would allow others to work on this (in theory), as others won't think that someone is already working on this. Thanks! :)

@Aklapper I guess the tutorial is mature enough to not need a ticket open :)

Thanks Tobias1984! (I just gave it a read.)