Merge branch 'feature/homedir-creation'

* feature/homedir-creation:
  create directories for new users
  add stub tasks for file system operations
This commit is contained in:
Jan Dittberner 2014-12-26 15:12:45 +01:00
commit 3350ce9460
5 changed files with 103 additions and 212 deletions

View file

@ -280,10 +280,11 @@ CELERY_RESULT_PERSISTENT = True
CELERY_TASK_RESULT_EXPIRES = None CELERY_TASK_RESULT_EXPIRES = None
CELERY_ROUTES = ( CELERY_ROUTES = (
'osusers.tasks.LdapRouter', 'osusers.tasks.LdapRouter',
'osusers.tasks.FileRouter',
) )
CELERY_ACCEPT_CONTENT = ['yaml'] CELERY_ACCEPT_CONTENT = ['pickle', 'yaml', 'json']
CELERY_TASK_SERIALIZER = 'yaml' CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'yaml' CELERY_RESULT_SERIALIZER = 'json'
########## END CELERY CONFIGURATION ########## END CELERY CONFIGURATION

View file

@ -4,12 +4,9 @@ from django.contrib import admin
from .models import ( from .models import (
AdditionalGroup, AdditionalGroup,
DeleteTaskResult,
Group, Group,
GroupTaskResult,
Shadow, Shadow,
User, User,
UserTaskResult,
) )
PASSWORD_MISMATCH_ERROR = _("Passwords don't match") PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
@ -129,45 +126,5 @@ class GroupAdmin(admin.ModelAdmin):
return actions return actions
class TaskResultAdmin(admin.ModelAdmin):
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return obj is None or obj.is_finished
def get_queryset(self, request):
qs = super(TaskResultAdmin, self).get_queryset(request)
for entry in qs:
entry.update_taskstatus()
return qs
class DeleteTaskResultAdmin(TaskResultAdmin):
readonly_fields = ['task_uuid', 'task_name', 'modeltype', 'modelname',
'is_finished', 'is_success', 'state', 'result_body']
list_display = ('task_uuid', 'task_name', 'modeltype', 'modelname',
'is_finished', 'state')
class GroupTaskResultAdmin(TaskResultAdmin):
readonly_fields = [
'task_uuid', 'task_name', 'group', 'is_finished', 'is_success',
'state', 'result_body'
]
list_display = ('task_uuid', 'task_name', 'group', 'is_finished', 'state')
class UserTaskResultAdmin(TaskResultAdmin):
readonly_fields = [
'task_uuid', 'task_name', 'user', 'is_finished', 'is_success', 'state',
'result_body'
]
list_display = ('task_uuid', 'task_name', 'user', 'is_finished', 'state')
admin.site.register(Group, GroupAdmin) admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin) admin.site.register(User, UserAdmin)
admin.site.register(DeleteTaskResult, DeleteTaskResultAdmin)
admin.site.register(GroupTaskResult, GroupTaskResultAdmin)
admin.site.register(UserTaskResult, UserTaskResultAdmin)

View file

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('osusers', '0001_initial'),
]
operations = [
migrations.DeleteModel(
name='DeleteTaskResult',
),
migrations.RemoveField(
model_name='grouptaskresult',
name='group',
),
migrations.DeleteModel(
name='GroupTaskResult',
),
migrations.RemoveField(
model_name='usertaskresult',
name='user',
),
migrations.DeleteModel(
name='UserTaskResult',
),
]

View file

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from datetime import date from datetime import date
import logging
import os import os
from django.db import models, transaction from django.db import models, transaction
@ -21,42 +22,23 @@ from .tasks import (
add_ldap_user_to_group, add_ldap_user_to_group,
create_ldap_group, create_ldap_group,
create_ldap_user, create_ldap_user,
delete_file_mail_userdir,
delete_file_sftp_userdir,
delete_ldap_group_if_empty, delete_ldap_group_if_empty,
delete_ldap_user, delete_ldap_user,
remove_ldap_user_from_group, remove_ldap_user_from_group,
setup_file_mail_userdir,
setup_file_sftp_userdir,
) )
logger = logging.getLogger(__name__)
CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL = _( CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL = _(
"You can not use a user's primary group.") "You can not use a user's primary group.")
class TaskResult(TimeStampedModel, models.Model):
task_uuid = models.CharField(primary_key=True, max_length=64, blank=False)
task_name = models.CharField(max_length=255, blank=False, db_index=True)
is_finished = models.BooleanField(default=False)
is_success = models.BooleanField(default=False)
state = models.CharField(max_length=10)
result_body = models.TextField(blank=True)
class Meta:
abstract = True
def _set_result_fields(self, asyncresult):
if not self.is_finished:
result = asyncresult.get(no_ack=False)
self.is_finished = True
self.is_success = asyncresult.state == 'SUCCESS'
self.state = asyncresult.state
self.result_body = str(result)
def update_taskstatus(self):
asyncresult = AsyncResult(self.task_uuid)
self._set_result_fields(asyncresult)
self.save()
class GroupManager(models.Manager): class GroupManager(models.Manager):
def get_next_gid(self): def get_next_gid(self):
@ -88,85 +70,17 @@ class Group(TimeStampedModel, models.Model):
@transaction.atomic @transaction.atomic
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super(Group, self).save(*args, **kwargs) super(Group, self).save(*args, **kwargs)
GroupTaskResult.objects.create_grouptaskresult( dn = create_ldap_group.delay(
self, self.groupname, self.gid, self.descr).get()
create_ldap_group.delay(self.groupname, self.gid, self.descr), logger.info("created LDAP group with dn %s", dn)
'create_ldap_group'
)
return self return self
@transaction.atomic @transaction.atomic
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
DeleteTaskResult.objects.create_deletetaskresult( delete_ldap_group_if_empty.delay(self.groupname).get()
'group', self.groupname,
delete_ldap_group_if_empty.delay(self.groupname),
'delete_ldap_group_if_empty'
)
super(Group, self).delete(*args, **kwargs) super(Group, self).delete(*args, **kwargs)
class TaskResultManager(models.Manager):
def create(self, asyncresult, task_name):
result = self.model(
task_uuid=asyncresult.task_id, task_name=task_name
)
result._set_result_fields(asyncresult)
return result
class DeleteTaskResultManager(TaskResultManager):
def create_deletetaskresult(
self, modeltype, modelname, asyncresult, task_name
):
taskresult = super(DeleteTaskResultManager, self).create(
asyncresult, task_name)
taskresult.modeltype = modeltype
taskresult.modelname = modelname
taskresult.save()
return taskresult
@python_2_unicode_compatible
class DeleteTaskResult(TaskResult):
modeltype = models.CharField(max_length=20, db_index=True)
modelname = models.CharField(max_length=255)
objects = DeleteTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {modeltype} {modelname}".format(
task_uuid=self.task_uuid, task_name=self.task_name,
modeltype=self.modeltype, modelname=self.modelname)
class GroupTaskResultManager(TaskResultManager):
def create_grouptaskresult(
self, group, asyncresult, task_name, commit=False
):
taskresult = super(GroupTaskResultManager, self).create(
asyncresult, task_name)
taskresult.group = group
taskresult.save()
return taskresult
@python_2_unicode_compatible
class GroupTaskResult(TaskResult):
group = models.ForeignKey(Group)
objects = GroupTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {group}".format(
task_uuid=self.task_uuid, task_name=self.task_name,
group=self.group)
class UserManager(models.Manager): class UserManager(models.Manager):
def get_next_uid(self): def get_next_uid(self):
@ -238,72 +152,39 @@ class User(TimeStampedModel, models.Model):
self.shadow = Shadow.objects.create_shadow( self.shadow = Shadow.objects.create_shadow(
user=self, password=password user=self, password=password
) )
UserTaskResult.objects.create_usertaskresult( dn = create_ldap_user.delay(
self, self.username, self.uid, self.group.gid, self.gecos,
create_ldap_user.delay( self.homedir, self.shell, password
self.username, self.uid, self.group.gid, self.gecos, ).get()
self.homedir, self.shell, password logging.info("set LDAP password for %s", dn)
),
'create_ldap_user',
commit=True
)
@transaction.atomic @transaction.atomic
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
UserTaskResult.objects.create_usertaskresult( dn = create_ldap_user.delay(
self, self.username, self.uid, self.group.gid, self.gecos,
create_ldap_user.delay( self.homedir, self.shell, password=None).get()
self.username, self.uid, self.group.gid, self.gecos, sftp_dir = setup_file_sftp_userdir.delay(self.username).get()
self.homedir, self.shell, password=None mail_dir = setup_file_mail_userdir.delay(self.username).get()
), logger.info(
'create_ldap_user' "created user %(user)s with LDAP dn %(dn)s, home directory "
) "%(homedir)s and mail base directory %(maildir)s.", {
'user': self, 'dn': dn,
'homedir': sftp_dir, 'maildir': mail_dir
})
return super(User, self).save(*args, **kwargs) return super(User, self).save(*args, **kwargs)
@transaction.atomic @transaction.atomic
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
for group in [ delete_file_mail_userdir.delay(self.username).get()
ag.group for ag in AdditionalGroup.objects.filter(user=self) delete_file_sftp_userdir.delay(self.username).get()
]: for group in [ag.group for ag in self.additionalgroup_set.all()]:
DeleteTaskResult.objects.create_deletetaskresult( remove_ldap_user_from_group.delay(
'usergroup', self.username, group.groupname).get()
'{0} in {1}'.format(self.username, group.groupname), delete_ldap_user.delay(self.username).get()
remove_ldap_user_from_group.delay(
self.username, group.groupname),
'remove_ldap_user_from_group',
)
DeleteTaskResult.objects.create_deletetaskresult(
'user', self.username,
delete_ldap_user.delay(self.username),
'delete_ldap_user'
)
self.group.delete() self.group.delete()
super(User, self).delete(*args, **kwargs) super(User, self).delete(*args, **kwargs)
class UserTaskResultManager(TaskResultManager):
def create_usertaskresult(
self, user, asyncresult, task_name, commit=False
):
taskresult = self.create(asyncresult, task_name)
taskresult.user = user
taskresult.save()
return taskresult
@python_2_unicode_compatible
class UserTaskResult(TaskResult):
user = models.ForeignKey(User)
objects = UserTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {user}".format(
task_uuid=self.task_uuid, task_name=self.task_name, user=self.user)
class ShadowManager(models.Manager): class ShadowManager(models.Manager):
@transaction.atomic @transaction.atomic
@ -382,23 +263,14 @@ class AdditionalGroup(TimeStampedModel, models.Model):
@transaction.atomic @transaction.atomic
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
GroupTaskResult.objects.create_grouptaskresult( add_ldap_user_to_group.delay(
self.group, self.user.username, self.group.groupname).get()
add_ldap_user_to_group.delay(
self.user.username, self.group.groupname),
'add_ldap_user_to_group'
)
super(AdditionalGroup, self).save(*args, **kwargs) super(AdditionalGroup, self).save(*args, **kwargs)
@transaction.atomic @transaction.atomic
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
DeleteTaskResult.objects.create_deletetaskresult( remove_ldap_user_from_group.delay(
'usergroup', self.user.username, self.group.groupname).get()
str(self),
remove_ldap_user_from_group.delay(
self.user.username, self.group.groupname),
'remove_ldap_user_from_group'
)
super(AdditionalGroup, self).delete(*args, **kwargs) super(AdditionalGroup, self).delete(*args, **kwargs)
def __str__(self): def __str__(self):

View file

@ -13,6 +13,16 @@ class LdapRouter(object):
return None return None
class FileRouter(object):
def route_for_task(self, task, args=None, kwargs=None):
if 'file' in task:
return {'exchange': 'file',
'exchange_type': 'direct',
'queue': 'file'}
return None
@shared_task @shared_task
def create_ldap_group(groupname, gid, descr): def create_ldap_group(groupname, gid, descr):
pass pass
@ -41,3 +51,23 @@ def delete_ldap_user(username):
@shared_task @shared_task
def delete_ldap_group_if_empty(groupname): def delete_ldap_group_if_empty(groupname):
pass pass
@shared_task
def setup_file_sftp_userdir(username):
pass
@shared_task
def delete_file_sftp_userdir(username):
pass
@shared_task
def setup_file_mail_userdir(username):
pass
@shared_task
def delete_file_mail_userdir(username):
pass