improved task logging

This commit is contained in:
Jan Dittberner 2015-01-29 18:31:10 +01:00
parent e4a04130e7
commit b422c554ea
2 changed files with 74 additions and 39 deletions

View file

@ -1,6 +1,7 @@
Changelog
=========
* :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

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
"""
try:
ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username)
performdelete = ldapuser.username in ldapgroup.members
if performdelete:
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 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
@ -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