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 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>` * :release:`0.4.0 <2015-01-24>`
* :feature:`-` add new task :py:func:`ldaptasks.tasks.set_ldap_user_password` * :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. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = '0.4' version = '0.5'
# The full version, including alpha/beta/rc tags. # 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 # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # 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 @shared_task
@ -38,14 +44,16 @@ def create_ldap_group(groupname, gid, descr):
""" """
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
_logger.info( _LOGGER.info(
'ldap group with dn {0} already exists'.format(ldapgroup.dn) 'LDAP group %s with groupname %s already exists',
) ldapgroup.dn, groupname)
ldapgroup.gid = gid ldapgroup.gid = gid
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
ldapgroup = LdapGroup(gid=gid, name=groupname) ldapgroup = LdapGroup(gid=gid, name=groupname)
_LOGGER.info('created LDAP group %s', ldapgroup.dn)
ldapgroup.description = descr ldapgroup.description = descr
ldapgroup.save() ldapgroup.save()
_LOGGER.info('set description of LDAP group %s', ldapgroup.dn)
return ldapgroup.dn return ldapgroup.dn
@ -75,16 +83,15 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
""" """
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
_logger.info( _LOGGER.info(
'ldap user with dn {0} already exists'.format(ldapuser.dn) 'LDAP user %s with username %s already exists',
) ldapuser.dn, username)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
ldapuser = LdapUser(username=username) ldapuser = LdapUser(username=username)
try: try:
ldapgroup = LdapGroup.objects.get(gid=gid) ldapgroup = LdapGroup.objects.get(gid=gid)
except ObjectDoesNotExist as exc: except ObjectDoesNotExist as exc:
_logger.info('ldap group with gid {0} does not exist') log_and_reject(exc, 'LDAP group with gid %d does not exist', gid)
raise Reject(exc, requeue=False)
ldapuser.uid = uid ldapuser.uid = uid
ldapuser.group = gid ldapuser.group = gid
ldapuser.gecos = gecos ldapuser.gecos = gecos
@ -94,14 +101,19 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
ldapuser.common_name = username ldapuser.common_name = username
if password is not None: if password is not None:
ldapuser.set_password(password) ldapuser.set_password(password)
_LOGGER.info('set password for LDAP user %s', ldapuser.dn)
if ldapuser.username in ldapgroup.members: if ldapuser.username in ldapgroup.members:
_logger.info('user {0} is already member of {1}'.format( _LOGGER.info(
ldapuser.username, ldapgroup.dn) 'LDAP user %s is already member of LDAP group %s',
) ldapuser.dn, ldapgroup.dn)
else: else:
ldapgroup.members.append(ldapuser.username) ldapgroup.members.append(ldapuser.username)
ldapgroup.save() ldapgroup.save()
_LOGGER.info(
'LDAP user %s has been added to LDAP group %s',
ldapuser.dn, ldapgroup.dn)
ldapuser.save() ldapuser.save()
_LOGGER.info('LDAP user %s created', ldapuser.dn)
return ldapuser.dn return ldapuser.dn
@ -120,9 +132,11 @@ def set_ldap_user_password(self, username, password):
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
_LOGGER.info('there is no LDAP user with username %s', username)
return False return False
ldapuser.set_password(password) ldapuser.set_password(password)
ldapuser.save() ldapuser.save()
_LOGGER.info("set new password for LDAP user %s", ldapuser.dn)
return True return True
@ -145,18 +159,21 @@ def add_ldap_user_to_group(self, username, groupname):
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapGroup.DoesNotExist: 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: 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) self.retry(exc=exc, time_limit=5)
else: else:
if not ldapuser.username in ldapgroup.members: if ldapuser.username not in ldapgroup.members:
ldapgroup.members.append(ldapuser.username) ldapgroup.members.append(ldapuser.username)
ldapgroup.save() ldapgroup.save()
else: _LOGGER.info(
_logger.info('ldap user {0} is already in group {1}'.format( '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) ldapuser.username, ldapgroup.dn)
)
return True return True
return False return False
@ -172,13 +189,26 @@ def remove_ldap_user_from_group(username, groupname):
:rtype: boolean :rtype: boolean
""" """
try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
performdelete = ldapuser.username in ldapgroup.members except LdapGroup.DoesNotExist:
if performdelete: _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) ldapgroup.members.remove(ldapuser.username)
_LOGGER.info(
'removed LDAP user %s from LDAP group %s',
ldapuser.dn, ldapgroup.dn)
ldapgroup.save() ldapgroup.save()
return performdelete return True
else:
_LOGGER.info(
'LDAP user %s is not a member of LDAP group %s',
ldapuser.dn, ldapgroup.dn)
return False
@shared_task @shared_task
@ -194,21 +224,24 @@ def delete_ldap_user(username):
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
_logger.info('there is no ldap user with uid {0}'.format( _LOGGER.info('there is no LDAP user with username %s', username)
username)
)
else: else:
try: try:
ldapgroup = LdapGroup.objects.get(gid=ldapuser.group) ldapgroup = LdapGroup.objects.get(gid=ldapuser.group)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_logger.info('group {0} for user {1} does not exist'.format( _LOGGER.info(
ldapuser.group, ldapuser.username) 'LDAP group %s of LDAP user %s does not exist',
) ldapuser.group, ldapuser.dn)
else: else:
if ldapuser.username in ldapgroup.members: if ldapuser.username in ldapgroup.members:
ldapgroup.members.remove(ldapuser.username) ldapgroup.members.remove(ldapuser.username)
ldapgroup.save() ldapgroup.save()
_LOGGER.info(
'removed LDAP user %s from LDAP group %s',
ldapuser.dn, ldapgroup.dn)
userdn = ldapuser.dn
ldapuser.delete() ldapuser.delete()
_LOGGER.info('deleted LDAP user %s', userdn)
return True return True
return False return False
@ -226,17 +259,18 @@ def delete_ldap_group_if_empty(groupname):
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_logger.info('ldap group with name {0} does not exist'.format( _LOGGER.info('LDAP group with groupname %s does not exist', groupname)
groupname)
)
else: else:
if len(ldapgroup.members) == 0: if len(ldapgroup.members) == 0:
groupdn = ldapgroup.dn
ldapgroup.delete() ldapgroup.delete()
_LOGGER.info(
'deleted LDAP group %s', groupdn)
return True return True
else: 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)) ldapgroup.dn, len(ldapgroup.members))
)
return False return False
@ -253,10 +287,10 @@ def delete_ldap_group(groupname):
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_logger.info('ldap group with name {0} does not exist'.format( _LOGGER.info('LDAP group with name %s does not exist', groupname)
groupname)
)
else: else:
groupdn = ldapgroup.dn
ldapgroup.delete() ldapgroup.delete()
_LOGGER.info('deleted LDAP group %s', groupdn)
return True return True
return False return False

View file

@ -1,9 +1,9 @@
Django==1.7.1 Django==1.7.4
django-ldapdb==0.3.2 django-ldapdb==0.3.2
bpython==0.13.1 bpython==0.13.2
django-braces==1.4.0 django-braces==1.4.0
django-model-utils==2.2 django-model-utils==2.2
logutils==0.3.3 logutils==0.3.3
celery==3.1.17 celery==3.1.17
passlib==1.6.2 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 Sphinx==1.2.3
sqlparse==0.1.14 sqlparse==0.1.14
releases==0.7.0 releases==0.7.0
Pygments==2.0.2