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

62 lines
2.0 KiB
Python
Raw Normal View History

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
from orchestra.apps.resources import ServiceMonitor
2014-05-08 16:59:35 +00:00
from . import settings
2014-07-09 16:17:43 +00:00
class SystemUserBackend(ServiceController):
2014-05-08 16:59:35 +00:00
verbose_name = _("System User")
model = 'users.User'
ignore_fields = ['last_login']
2014-07-09 16:17:43 +00:00
2014-05-08 16:59:35 +00:00
def save(self, user):
context = self.get_context(user)
if user.is_main:
self.append(
"if [[ $( id %(username)s ) ]]; then \n"
" usermod --password '%(password)s' %(username)s \n"
"else \n"
" useradd %(username)s --password '%(password)s' \\\n"
" --shell /bin/false \n"
"fi" % context
)
self.append("mkdir -p %(home)s" % context)
self.append("chown %(username)s.%(username)s %(home)s" % context)
else:
self.delete(user)
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" % context)
self.append("userdel %(username)s" % context)
def get_context(self, user):
context = {
'username': user.username,
'password': user.password if user.is_active else '*%s' % user.password,
}
context['home'] = settings.USERS_SYSTEMUSER_HOME % context
return context
2014-07-09 16:17:43 +00:00
class SystemUserDisk(ServiceMonitor):
model = 'users.User'
resource = ServiceMonitor.DISK
verbose_name = _('System user disk')
def monitor(self, user):
context = self.get_context(user)
self.append("du -s %(home)s | {\n"
" read value\n"
" echo '%(username)s' $value\n"
"}" % context)
def process(self, output):
# TODO transaction
for line in output.readlines():
username, value = line.strip().slpit()
History.store(object_id=user_id, value=value)