improved task logging
This commit is contained in:
parent
e4a04130e7
commit
b422c554ea
2 changed files with 74 additions and 39 deletions
|
@ -1,6 +1,7 @@
|
||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
* :feature:`-` improved logging of ldaptasks
|
||||||
* :support:`-` update bpython to 0.13.2, add explicit dependency on requests
|
* :support:`-` update bpython to 0.13.2, add explicit dependency on requests
|
||||||
2.5.1
|
2.5.1
|
||||||
* :support:`-` add explicit dependency for Pygments used by Sphinxdoc
|
* :support:`-` add explicit dependency for Pygments used by Sphinxdoc
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue