django-orchestra/orchestra/apps/webapps/backends/phpfpm.py

77 lines
2.4 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
import os
import textwrap
2014-05-08 16:59:35 +00:00
from django.template import Template, Context
from django.utils.translation import ugettext_lazy as _
2014-07-09 16:17:43 +00:00
from orchestra.apps.orchestration import ServiceController
2014-05-08 16:59:35 +00:00
from . import WebAppServiceMixin
from .. import settings
2014-07-09 16:17:43 +00:00
class PHPFPMBackend(WebAppServiceMixin, ServiceController):
""" Per-webapp php application """
2014-05-08 16:59:35 +00:00
verbose_name = _("PHP-FPM")
directive = 'fpm'
2014-05-08 16:59:35 +00:00
def save(self, webapp):
2014-10-24 12:13:48 +00:00
if not self.valid_directive(webapp):
return
2014-05-08 16:59:35 +00:00
context = self.get_context(webapp)
self.create_webapp_dir(context)
self.append(textwrap.dedent("""\
{
echo -e '%(fpm_config)s' | diff -N -I'^\s*;;' %(fpm_path)s -
} || {
echo -e '%(fpm_config)s' > %(fpm_path)s
UPDATEDFPM=1
}""" % context))
2014-05-08 16:59:35 +00:00
def delete(self, webapp):
if not self.valid_directive(webapp):
return
2014-05-08 16:59:35 +00:00
context = self.get_context(webapp)
2014-10-28 09:51:27 +00:00
self.append("rm '%(fpm_path)s'" % context)
2014-05-08 16:59:35 +00:00
self.delete_webapp_dir(context)
def commit(self):
if not self.cmds:
return
2014-05-08 16:59:35 +00:00
super(PHPFPMBackend, self).commit()
self.append(textwrap.dedent("""
[[ $UPDATEDFPM == 1 ]] && {
service php5-fpm start
service php5-fpm reload
}"""))
2014-05-08 16:59:35 +00:00
def get_context(self, webapp):
if not self.valid_directive(webapp):
return
2014-05-08 16:59:35 +00:00
context = super(PHPFPMBackend, self).get_context(webapp)
context.update({
'init_vars': self.get_php_init_vars(webapp),
2014-05-08 16:59:35 +00:00
'fpm_port': webapp.get_fpm_port(),
})
context['fpm_listen'] = settings.WEBAPPS_FPM_LISTEN % context
fpm_config = Template(textwrap.dedent("""\
;; {{ banner }}
[{{ user }}]
user = {{ user }}
group = {{ group }}
listen = {{ fpm_listen | safe }}
listen.owner = {{ user }}
listen.group = {{ group }}
pm = ondemand
pm.max_children = 4
{% for name, value in init_vars %}
php_admin_value[{{ name | safe }}] = {{ value | safe }}{% endfor %}"""
))
2014-05-08 16:59:35 +00:00
context.update({
'fpm_config': fpm_config.render(Context(context)),
'fpm_path': settings.WEBAPPS_PHPFPM_POOL_PATH % context,
2014-05-08 16:59:35 +00:00
})
return context