2014-05-08 16:59:35 +00:00
|
|
|
import os
|
2014-10-14 13:50:19 +00:00
|
|
|
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):
|
2014-10-14 13:50:19 +00:00
|
|
|
""" Per-webapp php application """
|
2014-05-08 16:59:35 +00:00
|
|
|
verbose_name = _("PHP-FPM")
|
2015-03-11 16:32:33 +00:00
|
|
|
default_route_match = "webapp.type_class.php_execution == 'fpm'"
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def save(self, webapp):
|
|
|
|
context = self.get_context(webapp)
|
|
|
|
self.create_webapp_dir(context)
|
2015-03-10 16:57:23 +00:00
|
|
|
self.set_under_construction(context)
|
2014-10-14 13:50:19 +00:00
|
|
|
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
|
2015-03-10 16:57:23 +00:00
|
|
|
}""") % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def delete(self, webapp):
|
|
|
|
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):
|
2014-11-10 10:04:52 +00:00
|
|
|
if not self.cmds:
|
|
|
|
return
|
2014-05-08 16:59:35 +00:00
|
|
|
super(PHPFPMBackend, self).commit()
|
2014-10-14 13:50:19 +00:00
|
|
|
self.append(textwrap.dedent("""
|
2015-03-10 16:57:23 +00:00
|
|
|
if [[ $UPDATEDFPM == 1 ]]; then
|
2014-10-14 13:50:19 +00:00
|
|
|
service php5-fpm reload
|
2015-03-10 11:46:48 +00:00
|
|
|
service php5-fpm start
|
2015-03-10 16:57:23 +00:00
|
|
|
fi"""))
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-03-10 16:57:23 +00:00
|
|
|
def get_fpm_config(self, webapp, context):
|
2014-05-08 16:59:35 +00:00
|
|
|
context.update({
|
2015-03-11 16:32:33 +00:00
|
|
|
'init_vars': webapp.type_instance.get_php_init_vars(),
|
2014-05-08 16:59:35 +00:00
|
|
|
'fpm_port': webapp.get_fpm_port(),
|
2015-03-10 16:57:23 +00:00
|
|
|
'max_children': webapp.get_options().get('processes', False),
|
|
|
|
'request_terminate_timeout': webapp.get_options().get('timeout', False),
|
2014-05-08 16:59:35 +00:00
|
|
|
})
|
|
|
|
context['fpm_listen'] = settings.WEBAPPS_FPM_LISTEN % context
|
2014-10-14 13:50:19 +00:00
|
|
|
fpm_config = Template(textwrap.dedent("""\
|
|
|
|
;; {{ banner }}
|
|
|
|
[{{ user }}]
|
|
|
|
user = {{ user }}
|
|
|
|
group = {{ group }}
|
|
|
|
|
|
|
|
listen = {{ fpm_listen | safe }}
|
|
|
|
listen.owner = {{ user }}
|
|
|
|
listen.group = {{ group }}
|
|
|
|
pm = ondemand
|
2015-03-10 16:57:23 +00:00
|
|
|
{% if max_children %}pm.max_children = {{ max_children }}{% endif %}
|
|
|
|
{% if request_terminate_timeout %}request_terminate_timeout = {{ request_terminate_timeout }}{% endif %}
|
|
|
|
{% for name, value in init_vars.iteritems %}
|
|
|
|
php_admin_value[{{ name | safe }}] = {{ value | safe }}{% endfor %}
|
|
|
|
"""
|
2014-10-14 13:50:19 +00:00
|
|
|
))
|
2015-03-10 16:57:23 +00:00
|
|
|
return fpm_config.render(Context(context))
|
|
|
|
|
|
|
|
def get_context(self, webapp):
|
|
|
|
context = super(PHPFPMBackend, self).get_context(webapp)
|
2014-05-08 16:59:35 +00:00
|
|
|
context.update({
|
2015-03-10 16:57:23 +00:00
|
|
|
'fpm_config': self.get_fpm_config(webapp, context),
|
2014-10-14 13:50:19 +00:00
|
|
|
'fpm_path': settings.WEBAPPS_PHPFPM_POOL_PATH % context,
|
2014-05-08 16:59:35 +00:00
|
|
|
})
|
|
|
|
return context
|