Merge branch 'release/0.5.0' into production

* release/0.5.0:
  update version, add release to changelog
  improved task logging
  update dependencies
This commit is contained in:
Jan Dittberner 2015-01-29 18:36:27 +01:00
commit 26b890b859
5 changed files with 88 additions and 44 deletions

View file

@ -1,6 +1,15 @@
Changelog
=========
* release:`0.5.0 <2015-01-29>`
* :feature:`-` improved logging of ldaptasks
* :support:`-` update bpython to 0.13.2, add explicit dependency on requests
2.5.1
* :support:`-` add explicit dependency for Pygments used by Sphinxdoc
* :support:`-` remove pyyaml dependency as json is used for message
serialization
* :support:`-` update to Django version 1.7.4
* :release:`0.4.0 <2015-01-24>`
* :feature:`-` add new task :py:func:`ldaptasks.tasks.set_ldap_user_password`

View file

@ -60,9 +60,9 @@ copyright = u'2014, 2015 Jan Dittberner'
# built documents.
#
# The short X.Y version.
version = '0.4'
version = '0.5'
# The full version, including alpha/beta/rc tags.
release = '0.4.0'
release = '0.5.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.

View file

@ -18,7 +18,13 @@ from ldapentities.models import (
)
_logger = get_task_logger(__name__)
_LOGGER = get_task_logger(__name__)
def log_and_reject(exception, message, *args):
logargs = list(args) + [exception.returncode, exception.output]
_LOGGER.error(message + "\nreturncode: %d\noutput:\n%s", *logargs)
raise Reject(message % args, requeue=False)
@shared_task
@ -38,14 +44,16 @@ def create_ldap_group(groupname, gid, descr):
"""
try:
ldapgroup = LdapGroup.objects.get(name=groupname)
_logger.info(
'ldap group with dn {0} already exists'.format(ldapgroup.dn)
)
_LOGGER.info(
'LDAP group %s with groupname %s already exists',
ldapgroup.dn, groupname)
ldapgroup.gid = gid
except LdapGroup.DoesNotExist:
ldapgroup = LdapGroup(gid=gid, name=groupname)
_LOGGER.info('created LDAP group %s', ldapgroup.dn)
ldapgroup.description = descr
ldapgroup.save()
_LOGGER.info('set description of LDAP group %s', ldapgroup.dn)
return ldapgroup.dn
@ -75,16 +83,15 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
"""
try:
ldapuser = LdapUser.objects.get(username=username)
_logger.info(
'ldap user with dn {0} already exists'.format(ldapuser.dn)
)
_LOGGER.info(
'LDAP user %s with username %s already exists',
ldapuser.dn, username)
except LdapUser.DoesNotExist:
ldapuser = LdapUser(username=username)
try:
ldapgroup = LdapGroup.objects.get(gid=gid)
except ObjectDoesNotExist as exc:
_logger.info('ldap group with gid {0} does not exist')
raise Reject(exc, requeue=False)
log_and_reject(exc, 'LDAP group with gid %d does not exist', gid)
ldapuser.uid = uid
ldapuser.group = gid
ldapuser.gecos = gecos
@ -94,14 +101,19 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
ldapuser.common_name = username
if password is not None:
ldapuser.set_password(password)
_LOGGER.info('set password for LDAP user %s', ldapuser.dn)
if ldapuser.username in ldapgroup.members:
_logger.info('user {0} is already member of {1}'.format(
ldapuser.username, ldapgroup.dn)
)
_LOGGER.info(
'LDAP user %s is already member of LDAP group %s',
ldapuser.dn, ldapgroup.dn)
else:
ldapgroup.members.append(ldapuser.username)
ldapgroup.save()
_LOGGER.info(
'LDAP user %s has been added to LDAP group %s',
ldapuser.dn, ldapgroup.dn)
ldapuser.save()
_LOGGER.info('LDAP user %s created', ldapuser.dn)
return ldapuser.dn
@ -120,9 +132,11 @@ def set_ldap_user_password(self, username, password):
try:
ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist:
_LOGGER.info('there is no LDAP user with username %s', username)
return False
ldapuser.set_password(password)
ldapuser.save()
_LOGGER.info("set new password for LDAP user %s", ldapuser.dn)
return True
@ -145,18 +159,21 @@ def add_ldap_user_to_group(self, username, groupname):
ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username)
except LdapGroup.DoesNotExist:
_logger.error('ldap group {0} does not exist'.format(groupname))
_LOGGER.error('LDAP group with groupname %s does not exist', groupname)
except LdapUser.DoesNotExist as exc:
_logger.error('ldap user {0} does not exist'.format(username))
_LOGGER.error('LDAP user with username %s does not exist', username)
self.retry(exc=exc, time_limit=5)
else:
if not ldapuser.username in ldapgroup.members:
if ldapuser.username not in ldapgroup.members:
ldapgroup.members.append(ldapuser.username)
ldapgroup.save()
else:
_logger.info('ldap user {0} is already in group {1}'.format(
_LOGGER.info(
'LDAP user %s has been added to LDAP group %s',
ldapuser.username, ldapgroup.dn)
else:
_LOGGER.info(
'LDAP user %s is already in LDAP group %s',
ldapuser.username, ldapgroup.dn)
)
return True
return False
@ -172,13 +189,26 @@ def remove_ldap_user_from_group(username, groupname):
:rtype: boolean
"""
ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username)
performdelete = ldapuser.username in ldapgroup.members
if performdelete:
ldapgroup.members.remove(ldapuser.username)
ldapgroup.save()
return performdelete
try:
ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username)
except LdapGroup.DoesNotExist:
_LOGGER.error('LDAP group with groupname %s does not exist', groupname)
except LdapUser.DoesNotExist:
_LOGGER.error('LDAP user with username %s does not exist', username)
else:
if ldapuser.username in ldapgroup.members:
ldapgroup.members.remove(ldapuser.username)
_LOGGER.info(
'removed LDAP user %s from LDAP group %s',
ldapuser.dn, ldapgroup.dn)
ldapgroup.save()
return True
else:
_LOGGER.info(
'LDAP user %s is not a member of LDAP group %s',
ldapuser.dn, ldapgroup.dn)
return False
@shared_task
@ -194,21 +224,24 @@ def delete_ldap_user(username):
try:
ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist:
_logger.info('there is no ldap user with uid {0}'.format(
username)
)
_LOGGER.info('there is no LDAP user with username %s', username)
else:
try:
ldapgroup = LdapGroup.objects.get(gid=ldapuser.group)
except LdapGroup.DoesNotExist:
_logger.info('group {0} for user {1} does not exist'.format(
ldapuser.group, ldapuser.username)
)
_LOGGER.info(
'LDAP group %s of LDAP user %s does not exist',
ldapuser.group, ldapuser.dn)
else:
if ldapuser.username in ldapgroup.members:
ldapgroup.members.remove(ldapuser.username)
ldapgroup.save()
_LOGGER.info(
'removed LDAP user %s from LDAP group %s',
ldapuser.dn, ldapgroup.dn)
userdn = ldapuser.dn
ldapuser.delete()
_LOGGER.info('deleted LDAP user %s', userdn)
return True
return False
@ -226,17 +259,18 @@ def delete_ldap_group_if_empty(groupname):
try:
ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist:
_logger.info('ldap group with name {0} does not exist'.format(
groupname)
)
_LOGGER.info('LDAP group with groupname %s does not exist', groupname)
else:
if len(ldapgroup.members) == 0:
groupdn = ldapgroup.dn
ldapgroup.delete()
_LOGGER.info(
'deleted LDAP group %s', groupdn)
return True
else:
_logger.info('ldap group {0} still has {1} members'.format(
_LOGGER.info(
'LDAP group %s has not been deleted. It still has %d members',
ldapgroup.dn, len(ldapgroup.members))
)
return False
@ -253,10 +287,10 @@ def delete_ldap_group(groupname):
try:
ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist:
_logger.info('ldap group with name {0} does not exist'.format(
groupname)
)
_LOGGER.info('LDAP group with name %s does not exist', groupname)
else:
groupdn = ldapgroup.dn
ldapgroup.delete()
_LOGGER.info('deleted LDAP group %s', groupdn)
return True
return False

View file

@ -1,9 +1,9 @@
Django==1.7.1
Django==1.7.4
django-ldapdb==0.3.2
bpython==0.13.1
bpython==0.13.2
django-braces==1.4.0
django-model-utils==2.2
logutils==0.3.3
celery==3.1.17
passlib==1.6.2
pyaml==14.12.10
requests==2.5.1

View file

@ -5,3 +5,4 @@ django-debug-toolbar==1.2.2
Sphinx==1.2.3
sqlparse==0.1.14
releases==0.7.0
Pygments==2.0.2