2014-05-08 16:59:35 +00:00
|
|
|
import os
|
2014-10-10 14:39:46 +00:00
|
|
|
import textwrap
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
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 PHPFcgidBackend(WebAppServiceMixin, ServiceController):
|
2014-10-14 13:50:19 +00:00
|
|
|
""" Per-webapp fcgid application """
|
2014-05-08 16:59:35 +00:00
|
|
|
verbose_name = _("PHP-Fcgid")
|
2015-03-10 16:57:23 +00:00
|
|
|
directive = 'fcgid'
|
2015-03-11 16:32:33 +00:00
|
|
|
default_route_match = "webapp.type_class.php_execution == 'fcgid'"
|
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-05-08 16:59:35 +00:00
|
|
|
self.append("mkdir -p %(wrapper_dir)s" % context)
|
2014-10-10 14:39:46 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
{
|
2015-03-10 16:57:23 +00:00
|
|
|
echo -e '%(wrapper)s' | diff -N -I'^\s*#' %(wrapper_path)s -
|
2014-10-10 14:39:46 +00:00
|
|
|
} || {
|
2015-03-10 16:57:23 +00:00
|
|
|
echo -e '%(wrapper)s' > %(wrapper_path)s; UPDATED_APACHE=1
|
|
|
|
}""") % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.append("chmod +x %(wrapper_path)s" % context)
|
2015-02-24 09:34:26 +00:00
|
|
|
self.append("chown -R %(user)s:%(group)s %(wrapper_dir)s" % context)
|
2015-03-10 16:57:23 +00:00
|
|
|
if context['cmd_options']:
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
{
|
|
|
|
echo -e '%(cmd_options)s' | diff -N -I'^\s*#' %(cmd_options_path)s -
|
|
|
|
} || {
|
|
|
|
echo -e '%(cmd_options)s' > %(cmd_options_path)s; UPDATED_APACHE=1
|
|
|
|
}""" ) % context
|
|
|
|
)
|
2015-03-11 16:32:33 +00:00
|
|
|
else:
|
|
|
|
self.append("rm -f %(cmd_options_path)s" % context)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def delete(self, webapp):
|
|
|
|
context = self.get_context(webapp)
|
2015-03-10 21:51:10 +00:00
|
|
|
self.append("rm -f '%(wrapper_path)s'" % context)
|
|
|
|
self.append("rm -f '%(cmd_options_path)s'" % context)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.delete_webapp_dir(context)
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
def commit(self):
|
2015-03-10 16:57:23 +00:00
|
|
|
self.append('if [[ $UPDATED_APACHE == 1 ]]; then service apache2 reload; fi')
|
|
|
|
|
|
|
|
def get_fcgid_wrapper(self, webapp, context):
|
|
|
|
opt = webapp.type_instance
|
|
|
|
# Format PHP init vars
|
2015-03-11 16:32:33 +00:00
|
|
|
init_vars = opt.get_php_init_vars()
|
2015-03-10 16:57:23 +00:00
|
|
|
if init_vars:
|
|
|
|
init_vars = [ '-d %s="%s"' % (k,v) for k,v in init_vars.iteritems() ]
|
|
|
|
init_vars = ', '.join(init_vars)
|
|
|
|
|
|
|
|
context.update({
|
2015-03-11 16:32:33 +00:00
|
|
|
'php_binary': opt.get_php_binary_path(),
|
|
|
|
'php_rc': opt.get_php_rc_path(),
|
2015-03-10 16:57:23 +00:00
|
|
|
'php_init_vars': init_vars,
|
|
|
|
})
|
|
|
|
return textwrap.dedent("""\
|
|
|
|
#!/bin/sh
|
|
|
|
# %(banner)s
|
|
|
|
export PHPRC=%(php_rc)s
|
|
|
|
exec %(php_binary)s %(php_init_vars)s""") % context
|
|
|
|
|
|
|
|
def get_fcgid_cmd_options(self, webapp, context):
|
|
|
|
maps = {
|
|
|
|
'MaxProcesses': webapp.get_options().get('processes', None),
|
|
|
|
'IOTimeout': webapp.get_options().get('timeout', None),
|
|
|
|
}
|
|
|
|
cmd_options = []
|
|
|
|
for directive, value in maps.iteritems():
|
|
|
|
if value:
|
|
|
|
cmd_options.append("%s %s" % (directive, value))
|
|
|
|
if cmd_options:
|
2015-03-10 21:51:10 +00:00
|
|
|
head = '# %(banner)s\nFcgidCmdOptions %(wrapper_path)s' % context
|
|
|
|
cmd_options.insert(0, head)
|
2015-03-10 16:57:23 +00:00
|
|
|
return ' \\\n '.join(cmd_options)
|
2014-10-10 14:39:46 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_context(self, webapp):
|
|
|
|
context = super(PHPFcgidBackend, self).get_context(webapp)
|
2015-03-11 16:32:33 +00:00
|
|
|
wrapper_path = settings.WEBAPPS_FCGID_WRAPPER_PATH % context
|
2014-05-08 16:59:35 +00:00
|
|
|
context.update({
|
2015-03-10 16:57:23 +00:00
|
|
|
'wrapper': self.get_fcgid_wrapper(webapp, context),
|
2014-05-08 16:59:35 +00:00
|
|
|
'wrapper_path': wrapper_path,
|
|
|
|
'wrapper_dir': os.path.dirname(wrapper_path),
|
|
|
|
})
|
2015-03-10 16:57:23 +00:00
|
|
|
context.update({
|
|
|
|
'cmd_options': self.get_fcgid_cmd_options(webapp, context),
|
|
|
|
'cmd_options_path': settings.WEBAPPS_FCGID_CMD_OPTIONS_PATH % context,
|
|
|
|
})
|
2014-05-08 16:59:35 +00:00
|
|
|
return context
|