From e4a04130e7705df689e236e1c1fea89adf9dd450 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 29 Jan 2015 17:16:14 +0100 Subject: [PATCH 1/3] update dependencies - update to Django 1.7.4 - update to bpython 0.13.2 - add explicit dependencies for Pygments and requests - remove unused pyyaml dependency --- docs/changelog.rst | 7 +++++++ requirements/base.txt | 6 +++--- requirements/local.txt | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 61c8dda..79817fb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,13 @@ Changelog ========= +* :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` diff --git a/requirements/base.txt b/requirements/base.txt index 17520d2..b42ce77 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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 diff --git a/requirements/local.txt b/requirements/local.txt index 4bdfc63..af0d444 100644 --- a/requirements/local.txt +++ b/requirements/local.txt @@ -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 From b422c554ea370048b027215c036419241e1b5643 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 29 Jan 2015 18:31:10 +0100 Subject: [PATCH 2/3] improved task logging --- docs/changelog.rst | 1 + gvaldap/ldaptasks/tasks.py | 112 ++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 39 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 79817fb..ab68e1c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 diff --git a/gvaldap/ldaptasks/tasks.py b/gvaldap/ldaptasks/tasks.py index a1af2c7..cf1a110 100644 --- a/gvaldap/ldaptasks/tasks.py +++ b/gvaldap/ldaptasks/tasks.py @@ -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 From b7ecbfc0dc6b66734b227d79890a8278ddf21938 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 29 Jan 2015 18:35:40 +0100 Subject: [PATCH 3/3] update version, add release to changelog --- docs/changelog.rst | 1 + docs/conf.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ab68e1c..c1da00d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,7 @@ 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 diff --git a/docs/conf.py b/docs/conf.py index 9036feb..a6d6b4d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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.