pvl.hosts-import: support AAAA, make default comment handling an option
authorTero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 20:01:47 +0200
changeset 275 4dc5cc30a278
parent 274 81af20dd142d
child 276 8db56588cefc
pvl.hosts-import: support AAAA, make default comment handling an option
bin/pvl.hosts-import
--- a/bin/pvl.hosts-import	Mon Dec 16 20:01:21 2013 +0200
+++ b/bin/pvl.hosts-import	Mon Dec 16 20:01:47 2013 +0200
@@ -48,6 +48,14 @@
     parser.add_option('--import-dhcp-hosts',    metavar='FILE',
             help="Load hosts from DHCP config")
 
+    parser.add_option('--zone-comments-default-owner',  action='store_const',
+            dest='zone_comments_default', const='owner',
+            help="Import DNS zone comment as owner comment")
+
+    parser.add_option('--zone-comments-default-host',   action='store_const',
+            dest='zone_comments_default', const='host',
+            help="Import DNS zone comment as host comment")
+
     parser.add_option('--dump-host-comments',   action='store_true',
             help="Dump out info on imported host comments")
 
@@ -88,7 +96,7 @@
             log.debug("%s: skip %s", rr.name, rr)
             continue
 
-        elif rr.type == 'A' :
+        elif rr.type in ('A', 'AAAA') :
             ip, = rr.data
 
             yield rr.name, 'ip', ipaddr.IPAddress(ip)
@@ -96,6 +104,9 @@
             if rr.comment :
                 yield rr.name, 'comment', rr.comment
 
+            if rr.origin :
+                yield rr.name, 'domain', rr.origin
+
         elif rr.type == 'CNAME' :
             host, = rr.data
 
@@ -194,7 +205,6 @@
         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'),
         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'),
         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'),
-        re.compile(r'(?P<owner>.+)'),
 )
 
 ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>')
@@ -210,12 +220,16 @@
         match = regex.match(comment)
 
         if match :
+            matches = match.groupdict()
             break
     else :
-        log.warn("%s: unparsed comment: %s", hostname, comment)
-        return
+        if options.zone_comments_default :
+            log.warn("%s: default comment: %s", hostname, comment)
+            matches = { options.zone_comments_default: comment }
+        else :
+            log.warn("%s: unknown comment: %s", hostname, comment)
+            return
     
-    matches = match.groupdict()
     owner = matches.pop('owner', None)
     
     if owner :
@@ -226,8 +240,8 @@
             
             owner = mail_matches['owner']
             yield 'mail', mail_matches['mail'].strip()
-
-    yield 'owner', owner.strip()
+    
+        yield 'owner', owner.strip()
 
     for field, value in matches.iteritems() :
         if value :
@@ -272,7 +286,9 @@
         Return (owner, comment) for host based on info, or None.
     """
 
-    if info.get('owner').lower() in NONE_OWNERS :
+    owner = info.get('owner')
+
+    if owner and owner.lower() in NONE_OWNERS :
         return False
     
     # from ldap?