django-orchestra/orchestra/contrib/saas/backends/dokuwikimu.py

116 lines
4.1 KiB
Python
Raw Permalink Normal View History

2015-06-09 11:16:36 +00:00
import crypt
2014-09-26 15:05:20 +00:00
import os
2015-06-09 11:16:36 +00:00
import textwrap
from urllib.parse import urlparse
2014-09-26 15:05:20 +00:00
from django.utils.translation import gettext_lazy as _
2014-05-08 16:59:35 +00:00
2015-06-09 11:16:36 +00:00
from orchestra.contrib.orchestration import ServiceController
from orchestra.utils.python import random_ascii
2014-05-08 16:59:35 +00:00
from . import ApacheTrafficByHost
2014-05-08 16:59:35 +00:00
from .. import settings
2014-07-09 16:17:43 +00:00
2016-03-08 10:16:49 +00:00
class DokuWikiMuController(ServiceController):
2015-04-24 11:39:20 +00:00
"""
Creates a DokuWiki site on a DokuWiki multisite installation.
"""
2015-06-09 11:16:36 +00:00
name = 'dokuwiki'
2014-05-08 16:59:35 +00:00
verbose_name = _("DokuWiki multisite")
2015-06-09 11:16:36 +00:00
model = 'saas.SaaS'
default_route_match = "saas.service == 'dokuwiki'"
doc_settings = (settings, (
'SAAS_DOKUWIKI_TEMPLATE_PATH',
'SAAS_DOKUWIKI_FARM_PATH',
'SAAS_DOKUWIKI_USER',
'SAAS_DOKUWIKI_GROUP',
))
2014-05-08 16:59:35 +00:00
2015-06-09 11:16:36 +00:00
def save(self, saas):
context = self.get_context(saas)
self.append(textwrap.dedent("""
if [[ ! -e %(app_path)s ]]; then
mkdir %(app_path)s
tar xfz %(template)s -C %(app_path)s
chown -R %(user)s:%(group)s %(app_path)s
fi""") % context
)
if context['password']:
self.append(textwrap.dedent("""\
2015-09-20 11:35:22 +00:00
if grep '^admin:' %(users_path)s > /dev/null; then
2015-06-09 11:16:36 +00:00
sed -i 's#^admin:.*$#admin:%(password)s:admin:%(email)s:admin,user#' %(users_path)s
else
echo 'admin:%(password)s:admin:%(email)s:admin,user' >> %(users_path)s
fi""") % context
)
self.append(textwrap.dedent("""\
# Update custom domain link
find %(farm_path)s \\
-maxdepth 1 \\
-type l \\
-exec bash -c '
if [[ $(readlink {}) == "%(domain)s" && $(basename {}) != "%(custom_domain)s" ]]; then
rm {}
fi' \;\
""") % context
)
if context['custom_domain']:
self.append(textwrap.dedent("""\
if [[ ! -e %(farm_path)s/%(custom_domain)s ]]; then
ln -s %(domain)s %(farm_path)s/%(custom_domain)s
chown -h %(user)s:%(group) %(farm_path)s/%(custom_domain)s
fi""") % context
)
2014-05-08 16:59:35 +00:00
2015-06-09 11:16:36 +00:00
def delete(self, saas):
context = self.get_context(saas)
2014-05-08 16:59:35 +00:00
self.append("rm -fr %(app_path)s" % context)
self.append(textwrap.dedent("""\
# Delete custom domain link
find %(farm_path)s \\
-maxdepth 1 \\
-type l \\
-exec bash -c '
if [[ $(readlink {}) == "%(domain)s" ]]; then
rm {}
fi' \;\
""") % context
)
2014-05-08 16:59:35 +00:00
2015-06-09 11:16:36 +00:00
def get_context(self, saas):
2016-03-08 10:16:49 +00:00
context = super(DokuWikiMuController, self).get_context(saas)
domain = saas.get_site_domain()
2014-05-08 16:59:35 +00:00
context.update({
2015-04-24 11:39:20 +00:00
'template': settings.SAAS_DOKUWIKI_TEMPLATE_PATH,
'farm_path': os.path.normpath(settings.SAAS_DOKUWIKI_FARM_PATH),
'app_path': os.path.join(settings.SAAS_DOKUWIKI_FARM_PATH, domain),
2015-06-09 11:16:36 +00:00
'user': settings.SAAS_DOKUWIKI_USER,
'group': settings.SAAS_DOKUWIKI_GROUP,
'email': saas.account.email,
'custom_url': saas.custom_url,
'domain': domain,
2014-05-08 16:59:35 +00:00
})
if saas.custom_url:
custom_url = urlparse(saas.custom_url)
context.update({
'custom_domain': custom_url.netloc,
})
2015-06-09 11:16:36 +00:00
password = getattr(saas, 'password', None)
salt = random_ascii(8)
context.update({
'password': crypt.crypt(password, '$1$'+salt) if password else None,
'users_path': os.path.join(context['app_path'], 'conf/users.auth.php'),
})
return context
class DokuWikiMuTraffic(ApacheTrafficByHost):
__doc__ = ApacheTrafficByHost.__doc__
verbose_name = _("DokuWiki MU Traffic")
default_route_match = "saas.service == 'dokuwiki'"
doc_settings = (settings,
('SAAS_TRAFFIC_IGNORE_HOSTS', 'SAAS_DOKUWIKI_LOG_PATH')
)
log_path = settings.SAAS_DOKUWIKI_LOG_PATH