You are browsing a read-only backup copy of Wikitech. The primary site can be found at wikitech.wikimedia.org
Help:Toolforge/Web/Python: Difference between revisions
imported>Xephyr826 |
imported>BryanDavis |
||
Line 1: | Line 1: | ||
{{Template:Toolforge nav}} | |||
== Overview == | == Overview == | ||
This page describes Python-specific instructions for deploying a web server on [[Help:Toolforge/Web|Toolforge]]. Python web servers on Toolforege use [https://uwsgi-docs.readthedocs.io/en/latest/ uWSGI] which is a [[W:Web Server Gateway Interface|Web Server Gateway Interface]] server for Python web applications. uWSGI can run applicaions built with Flask, Django, and other Python web application frameworks. | This page describes Python-specific instructions for deploying a web server on [[Help:Toolforge/Web|Toolforge]]. Python web servers on Toolforege use [https://uwsgi-docs.readthedocs.io/en/latest/ uWSGI] which is a [[W:Web Server Gateway Interface|Web Server Gateway Interface]] (WSGI) server for Python web applications. uWSGI can run applicaions built with Flask, Django, and other Python web application frameworks. | ||
== Conventions == | |||
The Toolforge <code>webservice</code> command starts Python applications using [[w:Convention over configuration|convention rather than configuration]]. These conventions are expected by the Toolforge tooling: | |||
* Your WSGI application's entry point must be found in ''$HOME/www/python/src/app.py'' in a variable named <code>app</code> ([https://github.com/legoktm/fab-proxy/blob/08ec0de522cf0308e7c0687ee4f895d88696b3e4/app.py#L7 example]). | |||
* Python libraries will be loaded from a virtual environment located in ''$HOME/www/python/venv''. | |||
** On the Kubernetes backend, you '''must''' use a [[#venv|virtual environment]] to install and load any libraries you depend on outside of the Python standard library. | |||
* Additional [https://uwsgi-docs.readthedocs.io/en/latest/Configuration.html#ini-files configuration for uWSGI] can be provided in a ''$HOME/www/python/uwsgi.ini'' file. | |||
** Examples of configuration parameters can be found in the [https://uwsgi-docs.readthedocs.io/en/latest/Snippets.html uWSGI manual]. | |||
** Headers can be added using <code>route = .* addheader:Access-Control-Allow-Origin: *</code> | |||
* Logs will be written to <code>$HOME/uwsgi.log</code> | |||
== Starting a Python web service == | == Starting a Python web service == | ||
To start a Python web service, use the [[Help:Toolforge/Web#Using_the_webservice_command|<code>webservice start</code>]] command. | To start a Python web service, use the [[Help:Toolforge/Web#Using_the_webservice_command|<code>webservice start</code>]] command. For example: | ||
; Python3.7 with a default uwsgi configuration: | |||
: <code>webservice --backend=kubernetes python3.7 start</code> | : <code>webservice --backend=kubernetes python3.7 start</code> | ||
; Python3.5 with a default uwsgi configuration (deprecated): | |||
: <code>webservice --backend=kubernetes python3.5 start</code> | : <code>webservice --backend=kubernetes python3.5 start</code> | ||
; Python3.4 with a default uwsgi configuration (deprecated): | |||
: <code>webservice --backend=kubernetes python start</code> | |||
; Python2 with a default uwsgi configuration (deprecated) | |||
: <code>webservice --backend=kubernetes python2 start</code> | : <code>webservice --backend=kubernetes python2 start</code> | ||
; Python2 on Grid Engine with a default uwsgi configuration | |||
: <code>webservice --backend=gridengine uwsgi-python start</code> | : <code>webservice --backend=gridengine uwsgi-python start</code> | ||
; Python2 or Python3 on Grid Engine with a user supplied uwsgi configuration | |||
: <code>webservice --backend=gridengine uwsgi-plain start</code> | : <code>webservice --backend=gridengine uwsgi-plain start</code> | ||
== | == {{Anchor|venv}}Virtual Environments and Packages == | ||
A [https://docs.python.org/3/tutorial/venv.html virtual environment] ('''venv''') is a self-contained directory tree that contains a Python installation for a particular version of Python plus a number of additional packages. Using a venv allows you to install local Python packages for your tool. | |||
The fundamental thing to remember is that a venv created directly on the bastion will only work with ''--backend=gridengine'', and a venv created inside a webservice shell work only with ''--backend=kubernetes'' and the same Python runtime version. | |||
=== Creating a virtual environment === | |||
# <code>webservice --backend=kubernetes python3.7 shell</code> (choose a different python version as appropriate for your project) | |||
# <code>mkdir -p $HOME/www/python</code> | |||
# <code>python3 -m venv $HOME/www/python/venv</code> | |||
# <code>source $HOME/www/python/venv/bin/activate</code> | |||
<code>webservice --backend=kubernetes python3.7 | |||
# <code>mkdir -p | |||
# <code>python3 -m venv | |||
# <code>source | |||
# <code>pip install --upgrade pip wheel</code> (This brings in newest pip, which is required for wheel support) | # <code>pip install --upgrade pip wheel</code> (This brings in newest pip, which is required for wheel support) | ||
# Install the libraries you need ( | # Install the libraries you need (for example <code>pip install -r $HOME/www/python/src/requirements.txt</code>) | ||
# exit out of webservice shell | # exit out of webservice shell | ||
# <code>webservice --backend=kubernetes python3.7 start</code> | # <code>webservice --backend=kubernetes python3.7 start</code> | ||
Line 74: | Line 53: | ||
Step 1 can possibly freeze with an error message <code>Pod is not ready in time</code>. Retrying the command again should fix it. | Step 1 can possibly freeze with an error message <code>Pod is not ready in time</code>. Retrying the command again should fix it. | ||
Steps 2-6 can be automated by using the <code>webservice-python-bootstrap</code> script inside the webservice shell. If you want to create a brand new virtualenv in case you're switching Python versions or have new dependencies, | Steps 2-6 can be automated by using the <code>webservice-python-bootstrap</code> script inside the webservice shell. If you want to create a brand new virtualenv in case you're switching Python versions or have new dependencies, use <code>webservice-python-bootstrap --fresh</code>. | ||
== Using a uwsgi app with a default entry point that is not app.py == | |||
The <code> | The default uwsgi configuration for the uwsgi webservice backend expects to find the uwsgi entry point as the variable <code>app</code> loaded from the <code>$HOME/www/python/src/app.py</code> module. If your application has another entry point, the easiest thing to do is create a <code>$HOME/www/python/src/app.py</code> module, import your entry point, and expose it as <code>app</code>. See [[#Deploying a Django application|Deploying a Django application]] for an example of this pattern. | ||
== | == {{anchor|Making a Django app work}}Deploying a Django application == | ||
[[w:Django (web framework)|Django]] is a popular web framework for developing Python applications. A typical Django application will need a few changes to run with Toolforge's opinionated uWSGI configuration. | |||
=== Create an app.py entry point === | |||
{{Codesample|name=$HOME/www/python/src/app.py|lang=python|scheme=light|code= | |||
= | |||
import os | import os | ||
Line 156: | Line 71: | ||
app = get_wsgi_application() | app = get_wsgi_application() | ||
}} | |||
To correctly locate the static files configure | === Static files === | ||
To correctly locate the static files, first configure ''$HOME/www/python/uwsgi.ini'' to look for static files in your tool's ''$HOME/www/python/src/static'' directory: | |||
{{Codesample|name=$HOME/www/python/uwsgi.ini|lang=ini|scheme=light|code= | |||
[uwsgi] | [uwsgi] | ||
check-static = /data/project/<YOUR-TOOL-NAME>/www/python | check-static = /data/project/<YOUR-TOOL-NAME>/www/python/src/static | ||
}} | |||
Next configure your Django app to use this location by editing the app's ''settings.py'' file: | |||
{{Codesample|name=settings.py|lang=python|scheme=light|code= | |||
STATIC_URL = ' | STATIC_URL = '/static/' | ||
STATIC_ROOT = os.path.join(BASE_DIR, 'static') | STATIC_ROOT = os.path.join(BASE_DIR, 'static') | ||
}} | |||
Finally deploy your static files into ''$HOME/www/python/src/static''. Typically this will be done by running <code>python manage.py collectstatic</code>. | |||
=== MySQL and utf8mb4 === | |||
{{Tracked|T198508}} | |||
The version of MySQL/MariaDB currently used by ToolsDB will not work transparently with Django and the [https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html utf8mb4] character set. <code>ROW_FORMAT=DYNAMIC</code> must be used on all tables which will index utf8mb4 encoded character fields longer than 191 characters. This configuration, together with database server configuration to enable ''innodb_large_prefix'', the Barracuda file format, and file per table storage, enables index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables. | |||
Django does not have a feature flag or setting for adding the needed <code>ROW_FORMAT=DYNAMIC</code> configuration to its database migrations. One way to work around this issue is by using a [[gerrit:436592|custom database engine]] as {{U|Volans}} did for his Debmonitor project. Another possible workaround is manually modifying your migration files as {{U|BryanDavis}} [https://bd808.com/blog/2017/04/17/making-django-migrations-that-work-with-mysql-55-and-utf8mb4/ documented in a blog post]. | |||
== Logs == | == Logs == | ||
You can find | You can find your application's log messages in ''$HOME/uwsgi.log''. | ||
{{:Help:Cloud Services communication}} | |||
==See also== | |||
* [[Help:Toolforge/Web]] | |||
* [[Help:Toolforge/Python]] | |||
* [[Help:Toolforge/How to#Python]] | |||
[[Category:Toolforge]] | |||
[[Category:Documentation]] |
Revision as of 00:36, 11 August 2020
Overview
This page describes Python-specific instructions for deploying a web server on Toolforge. Python web servers on Toolforege use uWSGI which is a Web Server Gateway Interface (WSGI) server for Python web applications. uWSGI can run applicaions built with Flask, Django, and other Python web application frameworks.
Conventions
The Toolforge webservice
command starts Python applications using convention rather than configuration. These conventions are expected by the Toolforge tooling:
- Your WSGI application's entry point must be found in $HOME/www/python/src/app.py in a variable named
app
(example). - Python libraries will be loaded from a virtual environment located in $HOME/www/python/venv.
- On the Kubernetes backend, you must use a virtual environment to install and load any libraries you depend on outside of the Python standard library.
- Additional configuration for uWSGI can be provided in a $HOME/www/python/uwsgi.ini file.
- Examples of configuration parameters can be found in the uWSGI manual.
- Headers can be added using
route = .* addheader:Access-Control-Allow-Origin: *
- Logs will be written to
$HOME/uwsgi.log
Starting a Python web service
To start a Python web service, use the webservice start
command. For example:
- Python3.7 with a default uwsgi configuration
webservice --backend=kubernetes python3.7 start
- Python3.5 with a default uwsgi configuration (deprecated)
webservice --backend=kubernetes python3.5 start
- Python3.4 with a default uwsgi configuration (deprecated)
webservice --backend=kubernetes python start
- Python2 with a default uwsgi configuration (deprecated)
webservice --backend=kubernetes python2 start
- Python2 on Grid Engine with a default uwsgi configuration
webservice --backend=gridengine uwsgi-python start
- Python2 or Python3 on Grid Engine with a user supplied uwsgi configuration
webservice --backend=gridengine uwsgi-plain start
Virtual Environments and Packages
A virtual environment (venv) is a self-contained directory tree that contains a Python installation for a particular version of Python plus a number of additional packages. Using a venv allows you to install local Python packages for your tool.
The fundamental thing to remember is that a venv created directly on the bastion will only work with --backend=gridengine, and a venv created inside a webservice shell work only with --backend=kubernetes and the same Python runtime version.
Creating a virtual environment
webservice --backend=kubernetes python3.7 shell
(choose a different python version as appropriate for your project)mkdir -p $HOME/www/python
python3 -m venv $HOME/www/python/venv
source $HOME/www/python/venv/bin/activate
pip install --upgrade pip wheel
(This brings in newest pip, which is required for wheel support)- Install the libraries you need (for example
pip install -r $HOME/www/python/src/requirements.txt
) - exit out of webservice shell
webservice --backend=kubernetes python3.7 start
Step 1 can possibly freeze with an error message Pod is not ready in time
. Retrying the command again should fix it.
Steps 2-6 can be automated by using the webservice-python-bootstrap
script inside the webservice shell. If you want to create a brand new virtualenv in case you're switching Python versions or have new dependencies, use webservice-python-bootstrap --fresh
.
Using a uwsgi app with a default entry point that is not app.py
The default uwsgi configuration for the uwsgi webservice backend expects to find the uwsgi entry point as the variable app
loaded from the $HOME/www/python/src/app.py
module. If your application has another entry point, the easiest thing to do is create a $HOME/www/python/src/app.py
module, import your entry point, and expose it as app
. See Deploying a Django application for an example of this pattern.
Deploying a Django application
Django is a popular web framework for developing Python applications. A typical Django application will need a few changes to run with Toolforge's opinionated uWSGI configuration.
Create an app.py entry point
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<YOUR-TOOL-NAME>.settings")
app = get_wsgi_application()
Static files
To correctly locate the static files, first configure $HOME/www/python/uwsgi.ini to look for static files in your tool's $HOME/www/python/src/static directory:
[uwsgi]
check-static = /data/project/<YOUR-TOOL-NAME>/www/python/src/static
Next configure your Django app to use this location by editing the app's settings.py file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Finally deploy your static files into $HOME/www/python/src/static. Typically this will be done by running python manage.py collectstatic
.
MySQL and utf8mb4
The version of MySQL/MariaDB currently used by ToolsDB will not work transparently with Django and the utf8mb4 character set. ROW_FORMAT=DYNAMIC
must be used on all tables which will index utf8mb4 encoded character fields longer than 191 characters. This configuration, together with database server configuration to enable innodb_large_prefix, the Barracuda file format, and file per table storage, enables index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables.
Django does not have a feature flag or setting for adding the needed ROW_FORMAT=DYNAMIC
configuration to its database migrations. One way to work around this issue is by using a custom database engine as Volans did for his Debmonitor project. Another possible workaround is manually modifying your migration files as BryanDavis documented in a blog post.
Logs
You can find your application's log messages in $HOME/uwsgi.log.
Communication and support
Support and administration of the WMCS resources is provided by the Wikimedia Foundation Cloud Services team and Wikimedia movement volunteers. Please reach out with questions and join the conversation:
- Chat in real time in the IRC channel #wikimedia-cloud connect, the bridged Telegram channel, or the bridged Mattermost channel
- Discuss via email after you subscribed to the cloud@ mailing list