django-orchestra/orchestra/apps/systemusers/backends.py

160 lines
6.2 KiB
Python
Raw Normal View History

import os
2014-07-25 15:17:50 +00:00
import textwrap
2014-05-08 16:59:35 +00:00
from django.utils.translation import ugettext_lazy as _
from orchestra.apps.orchestration import ServiceController
from orchestra.apps.resources import ServiceMonitor
2014-05-08 16:59:35 +00:00
from . import settings
2014-05-08 16:59:35 +00:00
class SystemUserBackend(ServiceController):
verbose_name = _("System user")
model = 'systemusers.SystemUser'
actions = ('save', 'delete', 'grant_permission')
def save(self, user):
context = self.get_context(user)
groups = ','.join(self.get_groups(user))
context['groups_arg'] = '--groups %s' % groups if groups else ''
self.append(textwrap.dedent("""
if [[ $( id %(username)s ) ]]; then
usermod %(username)s --password '%(password)s' --shell %(shell)s %(groups_arg)s
else
2014-10-02 15:58:27 +00:00
useradd %(username)s --home %(home)s --password '%(password)s' --shell %(shell)s %(groups_arg)s
fi
mkdir -p %(home)s
chmod 750 %(home)s
2015-02-24 09:34:26 +00:00
chown %(username)s:%(username)s %(home)s""" % context
))
2014-10-28 09:51:27 +00:00
for member in settings.SYSTEMUSERS_DEFAULT_GROUP_MEMBERS:
context['member'] = member
self.append('usermod -a -G %(username)s %(member)s' % context)
if not user.is_main:
self.append('usermod -a -G %(username)s %(mainusername)s' % context)
def delete(self, user):
context = self.get_context(user)
self.append("{ sleep 2 && killall -u %(username)s -s KILL; } &" % context)
self.append("killall -u %(username)s || true" % context)
self.append("userdel %(username)s || true" % context)
self.append("groupdel %(username)s || true" % context)
2014-10-17 10:04:47 +00:00
self.delete_home(context, user)
def grant_permission(self, user):
context = self.get_context(user)
# TODO setacl
2014-10-17 10:04:47 +00:00
def delete_home(self, context, user):
if user.home.rstrip('/') == user.get_base_home().rstrip('/'):
2014-10-02 15:58:27 +00:00
# TODO delete instead of this shit
2014-10-07 13:08:59 +00:00
context['deleted'] = context['home'].rstrip('/') + '.deleted'
2014-10-02 15:58:27 +00:00
self.append("mv %(home)s %(deleted)s" % context)
def get_groups(self, user):
if user.is_main:
2014-10-02 15:58:27 +00:00
return user.account.systemusers.exclude(username=user.username).values_list('username', flat=True)
2014-10-28 09:51:27 +00:00
return list(user.groups.values_list('username', flat=True))
def get_context(self, user):
context = {
'object_id': user.pk,
'username': user.username,
'password': user.password if user.active else '*%s' % user.password,
'shell': user.shell,
'mainusername': user.username if user.is_main else user.account.username,
2014-10-02 15:58:27 +00:00
'home': user.get_home()
}
return context
class SystemUserDisk(ServiceMonitor):
model = 'systemusers.SystemUser'
resource = ServiceMonitor.DISK
verbose_name = _('Systemuser disk')
def prepare(self):
super(SystemUserDisk, self).prepare()
self.append(textwrap.dedent("""\
function monitor () {
{ du -bs "$1" || echo 0; } | awk {'print $1'}
}"""
))
def monitor(self, user):
context = self.get_context(user)
if user.is_main or os.path.normpath(user.home) == user.get_base_home():
self.append("echo %(object_id)s $(monitor %(home)s)" % context)
else:
2015-03-02 10:37:25 +00:00
# Home is already included in other user home
self.append("echo %(object_id)s 0" % context)
def get_context(self, user):
return {
'object_id': user.pk,
'home': user.home,
}
2014-05-08 16:59:35 +00:00
2014-07-09 16:17:43 +00:00
class FTPTraffic(ServiceMonitor):
model = 'systemusers.SystemUser'
resource = ServiceMonitor.TRAFFIC
2014-11-20 15:34:59 +00:00
verbose_name = _('Systemuser FTP traffic')
def prepare(self):
super(FTPTraffic, self).prepare()
2014-10-27 17:34:14 +00:00
current_date = self.current_date.strftime("%Y-%m-%d %H:%M:%S %Z")
self.append(textwrap.dedent("""\
function monitor () {
OBJECT_ID=$1
2014-11-20 15:34:59 +00:00
INI_DATE=$(date "+%%Y%%m%%d%%H%%M%%S" -d "$2")
2015-03-02 10:37:25 +00:00
END_DATE=$(date '+%%Y%%m%%d%%H%%M%%S' -d '%s')
USERNAME="$3"
LOG_FILE="$4"
2014-11-20 15:34:59 +00:00
{
2015-03-02 10:37:25 +00:00
grep "UPLOAD\|DOWNLOAD" ${LOG_FILE} \\
2014-11-20 15:34:59 +00:00
| grep " \\[${USERNAME}\\] " \\
| awk -v ini="${INI_DATE}" -v end="${END_DATE}" '
BEGIN {
sum = 0
months["Jan"] = "01"
months["Feb"] = "02"
months["Mar"] = "03"
months["Apr"] = "04"
months["May"] = "05"
months["Jun"] = "06"
months["Jul"] = "07"
months["Aug"] = "08"
months["Sep"] = "09"
months["Oct"] = "10"
months["Nov"] = "11"
months["Dec"] = "12"
} {
2015-03-02 10:37:25 +00:00
# Fri Jul 11 13:23:17 2014
split($4, time, ":")
2014-11-20 15:34:59 +00:00
# line_date = year month day hour minute second
2015-03-02 10:37:25 +00:00
line_date = $5 months[$2] $3 time[1] time[2] time[3]
2014-11-20 15:34:59 +00:00
if ( line_date > ini && line_date < end) {
2015-03-02 10:37:25 +00:00
sum += $(NF-2)
2014-11-20 15:34:59 +00:00
}
} END {
print sum
}' || [[ $? == 1 ]] && true
} | xargs echo ${OBJECT_ID}
}""" % current_date))
def monitor(self, user):
context = self.get_context(user)
self.append(
2014-11-20 15:34:59 +00:00
'monitor {object_id} "{last_date}" "{username}" {log_file}'.format(**context)
2014-10-27 17:34:14 +00:00
)
def get_context(self, user):
return {
2014-10-27 17:34:14 +00:00
'log_file': '%s{,.1}' % settings.SYSTEMUSERS_FTP_LOG_PATH,
2014-10-27 17:52:42 +00:00
'last_date': self.get_last_date(user.pk).strftime("%Y-%m-%d %H:%M:%S %Z"),
'object_id': user.pk,
'username': user.username,
}