django-orchestra-test/orchestra/apps/mails/backends.py

178 lines
6.2 KiB
Python
Raw Normal View History

2014-08-22 15:31:44 +00:00
import os
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from orchestra.apps.orchestration import ServiceController
from orchestra.apps.resources import ServiceMonitor
from . import settings
2014-09-26 15:05:20 +00:00
from .models import Address
2014-08-22 15:31:44 +00:00
class MailSystemUserBackend(ServiceController):
verbose_name = _("Mail system user")
2014-09-23 11:13:50 +00:00
model = 'mails.Mailbox'
2014-09-30 14:46:29 +00:00
# TODO related_models = ('resources__content_type') ?? needed for updating disk usage from resource.data
2014-08-22 15:31:44 +00:00
DEFAULT_GROUP = 'postfix'
def create_user(self, context):
self.append(
"if [[ $( id %(username)s ) ]]; then \n"
" usermod -p '%(password)s' %(username)s \n"
"else \n"
" useradd %(username)s --password '%(password)s' \\\n"
" --shell /dev/null \n"
"fi" % context
)
self.append("mkdir -p %(home)s" % context)
self.append("chown %(username)s.%(group)s %(home)s" % context)
def generate_filter(self, mailbox, context):
now = timezone.now().strftime("%B %d, %Y, %H:%M")
context['filtering'] = (
"# Sieve Filter\n"
"# Generated by Orchestra %s\n\n" % now
)
2014-09-30 14:46:29 +00:00
if mailbox.custom_filtering:
2014-08-22 15:31:44 +00:00
context['filtering'] += mailbox.custom_filtering
else:
context['filtering'] += settings.EMAILS_DEFAUL_FILTERING
context['filter_path'] = os.path.join(context['home'], '.orchestra.sieve')
self.append("echo '%(filtering)s' > %(filter_path)s" % context)
2014-09-30 14:46:29 +00:00
def set_quota(self, mailbox, context):
if not hasattr(mailbox, 'resources'):
return
context.update({
'maildir_path': '~%(username)s/Maildir' % context,
'maildirsize_path': '~%(username)s/Maildir/maildirsize' % context,
'quota': mailbox.resources.disk.allocated*1000*1000,
})
self.append("mkdir -p %(maildir_path)s" % context)
self.append(
"sed -i '1s/.*/%(quota)s,S/' %(maildirsize_path)s || {"
" echo '%(quota)s,S' > %(maildirsize_path)s && "
" chown %(username)s %(maildirsize_path)s;"
"}" % context
)
2014-08-22 15:31:44 +00:00
def save(self, mailbox):
context = self.get_context(mailbox)
self.create_user(context)
2014-09-30 14:46:29 +00:00
self.set_quota(mailbox, context)
2014-08-22 15:31:44 +00:00
self.generate_filter(mailbox, context)
def delete(self, mailbox):
context = self.get_context(mailbox)
self.append("{ sleep 2 && killall -u %(username)s -s KILL; } &" % context)
self.append("killall -u %(username)s" % context)
self.append("userdel %(username)s" % context)
self.append("rm -fr %(home)s" % context)
def get_context(self, mailbox):
context = {
2014-09-30 14:46:29 +00:00
'name': mailbox.name,
2014-09-29 12:22:45 +00:00
'username': mailbox.name,
'password': mailbox.password if mailbox.is_active else '*%s' % mailbox.password,
2014-08-22 15:31:44 +00:00
'group': self.DEFAULT_GROUP
}
context['home'] = settings.EMAILS_HOME % context
return context
class PostfixAddressBackend(ServiceController):
verbose_name = _("Postfix address")
2014-09-23 11:13:50 +00:00
model = 'mails.Address'
2014-08-22 15:31:44 +00:00
def include_virtdomain(self, context):
self.append(
'[[ $(grep "^\s*%(domain)s\s*$" %(virtdomains)s) ]]'
' || { echo "%(domain)s" >> %(virtdomains)s; UPDATED=1; }' % context
)
def exclude_virtdomain(self, context):
domain = context['domain']
if not Address.objects.filter(domain=domain).exists():
self.append('sed -i "s/^%(domain)s//" %(virtdomains)s' % context)
def update_virtusertable(self, context):
self.append(
'LINE="%(email)s\t%(destination)s"\n'
'if [[ ! $(grep "^%(email)s\s" %(virtusertable)s) ]]; then\n'
' echo "$LINE" >> %(virtusertable)s\n'
' UPDATED=1\n'
'else\n'
' if [[ ! $(grep "^${LINE}$" %(virtusertable)s) ]]; then\n'
' sed -i "s/^%(email)s\s.*$/${LINE}/" %(virtusertable)s\n'
' UPDATED=1\n'
' fi\n'
'fi' % context
)
def exclude_virtusertable(self, context):
self.append(
'if [[ $(grep "^%(email)s\s") ]]; then\n'
' sed -i "s/^%(email)s\s.*$//" %(virtusertable)s\n'
' UPDATED=1\n'
'fi'
)
def save(self, address):
context = self.get_context(address)
self.include_virtdomain(context)
self.update_virtusertable(context)
def delete(self, address):
context = self.get_context(address)
self.exclude_virtdomain(context)
self.exclude_virtusertable(context)
def commit(self):
context = self.get_context_files()
self.append('[[ $UPDATED == 1 ]] && { '
'postmap %(virtdomains)s;'
'postmap %(virtusertable)s;'
'}' % context)
def get_context_files(self):
return {
'virtdomains': settings.EMAILS_VIRTDOMAINS_PATH,
'virtusertable': settings.EMAILS_VIRTUSERTABLE_PATH,
}
def get_context(self, address):
context = self.get_context_files()
context.update({
'domain': address.domain,
'email': address.email,
'destination': address.destination,
})
return context
class AutoresponseBackend(ServiceController):
verbose_name = _("Mail autoresponse")
model = 'mail.Autoresponse'
class MaildirDisk(ServiceMonitor):
2014-09-23 11:13:50 +00:00
model = 'mails.Mailbox'
2014-08-22 15:31:44 +00:00
resource = ServiceMonitor.DISK
verbose_name = _("Maildir disk usage")
def monitor(self, mailbox):
context = self.get_context(mailbox)
self.append(
"SIZE=$(sed -n '2p' %(maildir_path)s | cut -d' ' -f1)\n"
"echo %(object_id)s ${SIZE:-0}" % context
)
def get_context(self, mailbox):
2014-09-26 15:05:20 +00:00
context = MailSystemUserBackend().get_context(mailbox)
2014-08-22 15:31:44 +00:00
context['home'] = settings.EMAILS_HOME % context
2014-09-30 14:46:29 +00:00
context['rr_path'] = os.path.join(context['home'], 'Maildir/maildirsize')
2014-08-22 15:31:44 +00:00
context['object_id'] = mailbox.pk
return context