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

66 lines
2.3 KiB
Python
Raw Normal View History

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):
""" Per-webapp fcgid application """
2014-05-08 16:59:35 +00:00
verbose_name = _("PHP-Fcgid")
directive = 'fcgi'
2015-03-04 21:06:16 +00:00
default_route_match = "webapp.type.endswith('-fcgi')"
2014-05-08 16:59:35 +00:00
def save(self, webapp):
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("mkdir -p %(wrapper_dir)s" % context)
2014-10-10 14:39:46 +00:00
self.append(textwrap.dedent("""\
{
echo -e '%(wrapper_content)s' | diff -N -I'^\s*#' %(wrapper_path)s -
} || {
echo -e '%(wrapper_content)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)
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)
self.append("rm '%(wrapper_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):
if not self.cmds:
return
2014-10-10 14:39:46 +00:00
super(PHPFcgidBackend, self).commit()
2014-10-17 13:09:56 +00:00
self.append("[[ $UPDATED_APACHE == 1 ]] && { service apache2 reload; }")
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)
init_vars = self.get_php_init_vars(webapp)
2014-05-08 16:59:35 +00:00
if init_vars:
init_vars = [ '%s="%s"' % (k,v) for k,v in init_vars ]
2014-05-08 16:59:35 +00:00
init_vars = ', -d '.join(init_vars)
context['init_vars'] = '-d %s' % init_vars
else:
context['init_vars'] = ''
wrapper_path = settings.WEBAPPS_FCGID_PATH % context
context.update({
2014-10-10 14:39:46 +00:00
'wrapper_content': textwrap.dedent("""\
#!/bin/sh
# %(banner)s
export PHPRC=/etc/%(type)s/cgi/
2014-10-17 13:09:56 +00:00
exec /usr/bin/%(type)s-cgi %(init_vars)s""" % context),
2014-05-08 16:59:35 +00:00
'wrapper_path': wrapper_path,
'wrapper_dir': os.path.dirname(wrapper_path),
})
return context