# HG changeset patch # User Tero Marttila # Date 1387196231 -7200 # Node ID dff7dcf0013fa007f9494e1aba99d712bf0d7cbd # Parent e58baab6b4cd2790a546782610ab17e4efbea754 pvl.dns-hosts: find owners from ldap diff -r e58baab6b4cd -r dff7dcf0013f bin/pvl.dns-hosts --- a/bin/pvl.dns-hosts Mon Dec 16 11:48:04 2013 +0200 +++ b/bin/pvl.dns-hosts Mon Dec 16 14:17:11 2013 +0200 @@ -7,6 +7,7 @@ import pvl.args, optparse import pvl.dns.zone import pvl.dhcp.config +import pvl.ldap.args import collections import re @@ -30,6 +31,7 @@ # logging parser.add_option_group(pvl.args.parser(parser)) + parser.add_option_group(pvl.ldap.args.parser(parser)) parser.add_option('-c', '--input-charset', metavar='CHARSET', default='utf-8', help="Encoding used for input files") @@ -44,6 +46,9 @@ parser.add_option('--import-dhcp-hosts', metavar='FILE', help="Load hosts from DHCP config") + parser.add_option('--dump-host-comments', action='store_true', + help="Dump out info on imported host comments") + # defaults parser.add_option('--hosts-domain', metavar='DOMAIN', help="Default domain for hosts") @@ -226,14 +231,69 @@ mail_matches = mail_match.groupdict() owner = mail_matches['owner'] - yield 'mail', mail_matches['mail'] + yield 'mail', mail_matches['mail'].strip() - yield 'owner', owner + yield 'owner', owner.strip() for field, value in matches.iteritems() : if value : - yield field, value + yield field, value.strip() + +HOST_OWNERS = { + u'tech': 'root', + u'atk': 'root', + u'toimisto': 'root', +} + +def process_host_owner (options, host, info) : + """ + Yield guesses for user from LDAP. + """ + + if info.get('owner').lower() in HOST_OWNERS : + yield HOST_OWNERS[info.get('owner').lower()] + + if info.get('mail') : + for user in options.ldap.users.filter( + { 'mailLocalAddress': info['mail'] }, + { 'uid': info['mail'] }, + ) : + yield user['uid'] + + if info.get('group') and info.get('owner') : + groups = options.ldap.groups.filter(cn=info['group']) + + for group in groups : + for user in options.ldap.users.filter({ + 'gidNumber': group['gidNumber'], + 'cn': info['owner'], + }) : + yield user['uid'] + + if info.get('owner') : + for user in options.ldap.users.filter({ + 'cn': info['owner'], + }) : + yield user['uid'] + +def process_host_comments (options, host, info) : + """ + Process host fields from comment. + + Attempts to find owner from LDAP.. + """ + + log.debug("%s: %s", host, info) + for owner in process_host_owner(options, host, info) : + log.info("%s: %s", host, owner) + + yield 'owner', owner, + + # only use the first match + break + else : + log.warn("%s: no owner: %s", host, info) def process_hosts_comments (options, import_hosts) : """ @@ -246,16 +306,21 @@ continue fields = dict(process_zone_comment(options, host, value)) + + if options.dump_host_comments : + print u"{host:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {hostinfo}".format( + host = host, + comment = value, + group = fields.get('group', ''), + owner = fields.get('owner', ''), + mail = fields.get('mail', ''), + hostinfo = fields.get('host', ''), + ).encode('utf-8') + - print u"{host:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {hostinfo}".format( - host = host, - comment = value, - group = fields.get('group', ''), - owner = fields.get('owner', ''), - mail = fields.get('mail', ''), - hostinfo = fields.get('host', ''), - ).encode('utf-8') - + for field, value in process_host_comments(options, host, fields) : + yield host, field, value + def process_hosts_import (options, import_hosts) : """ Import host definitions from given infos @@ -271,6 +336,8 @@ def main (argv) : options, args = parse_options(argv) + + options.ldap = pvl.ldap.args.apply(options) if args : # direct from file