django-orchestra/orchestra/contrib/webapps/backends/python.py

87 lines
3.1 KiB
Python
Raw Normal View History

2015-04-16 13:15:21 +00:00
import os
import textwrap
from django.utils.translation import gettext_lazy as _
2015-04-16 13:15:21 +00:00
from orchestra.contrib.orchestration import ServiceController, replace
from . import WebAppServiceMixin
from .. import settings
2016-03-08 10:16:49 +00:00
class uWSGIPythonController(WebAppServiceMixin, ServiceController):
2015-04-20 14:23:10 +00:00
"""
2015-04-24 11:39:20 +00:00
<a href="http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html">Emperor mode</a>
2015-04-20 14:23:10 +00:00
"""
2015-04-16 13:15:21 +00:00
verbose_name = _("Python uWSGI")
default_route_match = "webapp.type.endswith('python')"
2015-04-24 11:39:20 +00:00
doc_settings = (settings, (
'WEBAPPS_UWSGI_BASE_DIR',
'WEBAPPS_PYTHON_MAX_REQUESTS',
'WEBAPPS_PYTHON_DEFAULT_MAX_WORKERS',
'WEBAPPS_PYTHON_DEFAULT_TIMEOUT',
))
2015-04-16 13:15:21 +00:00
def save(self, webapp):
context = self.get_context(webapp)
self.create_webapp_dir(context)
self.set_under_construction(context)
self.save_uwsgi(webapp, context)
def delete(self, webapp):
context = self.get_context(webapp)
self.delete_uwsgi(webapp, context)
self.delete_webapp_dir(context)
def save_uwsgi(self, webapp, context):
2015-04-20 14:23:10 +00:00
self.append("echo '%(uwsgi_config)s' > %(vassal_path)s" % context)
2015-04-16 13:15:21 +00:00
def delete_uwsgi(self, webapp, context):
2015-04-20 14:23:10 +00:00
self.append("rm -f %(vassal_path)s" % context)
2015-04-16 13:15:21 +00:00
def get_uwsgi_ini(self, context):
return textwrap.dedent("""\
2015-04-20 14:23:10 +00:00
# %(banner)s
2015-04-16 13:15:21 +00:00
[uwsgi]
plugins = python{python_version_number}
chdir = {app_path}
module = {app_name}.wsgi
chmod-socket = 660
stats = /run/uwsgi/%(deb-confnamespace)/%(deb-confname)/statsocket
vacuum = true
uid = {user}
gid = {group}
env = HOME={home}
harakiri = {timeout}
max-requests = {max_requests}
cheaper-algo = spare
cheaper = 1
workers = {workers}
cheaper-step = 1
cheaper-overload = 5"""
).format(context)
def update_uwsgi_context(self, webapp, context):
context.update({
'uwsgi_ini': self.get_uwsgi_ini(context),
'uwsgi_dir': settings.WEBAPPS_UWSGI_BASE_DIR,
2015-04-20 14:23:10 +00:00
'vassal_path': os.path.join(settings.WEBAPPS_UWSGI_BASE_DIR,
'vassals/%s' % context['app_name']),
2015-04-16 13:15:21 +00:00
})
return context
def get_context(self, webapp):
2021-02-01 20:05:11 +00:00
context = super(uWSGIPythonController, self).get_context(webapp)
2015-04-16 13:15:21 +00:00
options = webapp.get_options()
context.update({
'python_version': webapp.type_instance.get_python_version(),
'python_version_number': webapp.type_instance.get_python_version_number(),
'max_requests': settings.WEBAPPS_PYTHON_MAX_REQUESTS,
'workers': options.get('processes', settings.WEBAPPS_PYTHON_DEFAULT_MAX_WORKERS),
'timeout': options.get('timeout', settings.WEBAPPS_PYTHON_DEFAULT_TIMEOUT),
})
self.update_uwsgi_context(webapp, context)
replace(context, "'", '"')
return context