bin/pvl.hosts-import
changeset 275 4dc5cc30a278
parent 268 560ba0544254
child 279 1b72f1e0cdbe
equal deleted inserted replaced
274:81af20dd142d 275:4dc5cc30a278
    46             help="Load hosts from DNS zone")
    46             help="Load hosts from DNS zone")
    47 
    47 
    48     parser.add_option('--import-dhcp-hosts',    metavar='FILE',
    48     parser.add_option('--import-dhcp-hosts',    metavar='FILE',
    49             help="Load hosts from DHCP config")
    49             help="Load hosts from DHCP config")
    50 
    50 
       
    51     parser.add_option('--zone-comments-default-owner',  action='store_const',
       
    52             dest='zone_comments_default', const='owner',
       
    53             help="Import DNS zone comment as owner comment")
       
    54 
       
    55     parser.add_option('--zone-comments-default-host',   action='store_const',
       
    56             dest='zone_comments_default', const='host',
       
    57             help="Import DNS zone comment as host comment")
       
    58 
    51     parser.add_option('--dump-host-comments',   action='store_true',
    59     parser.add_option('--dump-host-comments',   action='store_true',
    52             help="Dump out info on imported host comments")
    60             help="Dump out info on imported host comments")
    53 
    61 
    54     # defaults
    62     # defaults
    55     parser.add_option('--hosts-domain',         metavar='DOMAIN',
    63     parser.add_option('--hosts-domain',         metavar='DOMAIN',
    86     for rr in pvl.dns.zone.ZoneRecord.load(file) :
    94     for rr in pvl.dns.zone.ZoneRecord.load(file) :
    87         if options.zone_unused and rr.name == options.zone_unused :
    95         if options.zone_unused and rr.name == options.zone_unused :
    88             log.debug("%s: skip %s", rr.name, rr)
    96             log.debug("%s: skip %s", rr.name, rr)
    89             continue
    97             continue
    90 
    98 
    91         elif rr.type == 'A' :
    99         elif rr.type in ('A', 'AAAA') :
    92             ip, = rr.data
   100             ip, = rr.data
    93 
   101 
    94             yield rr.name, 'ip', ipaddr.IPAddress(ip)
   102             yield rr.name, 'ip', ipaddr.IPAddress(ip)
    95 
   103 
    96             if rr.comment :
   104             if rr.comment :
    97                 yield rr.name, 'comment', rr.comment
   105                 yield rr.name, 'comment', rr.comment
       
   106 
       
   107             if rr.origin :
       
   108                 yield rr.name, 'domain', rr.origin
    98 
   109 
    99         elif rr.type == 'CNAME' :
   110         elif rr.type == 'CNAME' :
   100             host, = rr.data
   111             host, = rr.data
   101 
   112 
   102             yield host, 'alias', rr.name
   113             yield host, 'alias', rr.name
   192 ZONE_COMMENTS = (
   203 ZONE_COMMENTS = (
   193         re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'),
   204         re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'),
   194         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'),
   205         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'),
   195         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'),
   206         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'),
   196         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'),
   207         re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'),
   197         re.compile(r'(?P<owner>.+)'),
       
   198 )
   208 )
   199 
   209 
   200 ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>')
   210 ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>')
   201 
   211 
   202 def process_zone_comment (options, hostname, comment) :
   212 def process_zone_comment (options, hostname, comment) :
   208 
   218 
   209     for regex in ZONE_COMMENTS :
   219     for regex in ZONE_COMMENTS :
   210         match = regex.match(comment)
   220         match = regex.match(comment)
   211 
   221 
   212         if match :
   222         if match :
       
   223             matches = match.groupdict()
   213             break
   224             break
   214     else :
   225     else :
   215         log.warn("%s: unparsed comment: %s", hostname, comment)
   226         if options.zone_comments_default :
   216         return
   227             log.warn("%s: default comment: %s", hostname, comment)
   217     
   228             matches = { options.zone_comments_default: comment }
   218     matches = match.groupdict()
   229         else :
       
   230             log.warn("%s: unknown comment: %s", hostname, comment)
       
   231             return
       
   232     
   219     owner = matches.pop('owner', None)
   233     owner = matches.pop('owner', None)
   220     
   234     
   221     if owner :
   235     if owner :
   222         mail_match = ZONE_OWNER_MAIL.match(owner)
   236         mail_match = ZONE_OWNER_MAIL.match(owner)
   223 
   237 
   224         if mail_match :
   238         if mail_match :
   225             mail_matches = mail_match.groupdict()
   239             mail_matches = mail_match.groupdict()
   226             
   240             
   227             owner = mail_matches['owner']
   241             owner = mail_matches['owner']
   228             yield 'mail', mail_matches['mail'].strip()
   242             yield 'mail', mail_matches['mail'].strip()
   229 
   243     
   230     yield 'owner', owner.strip()
   244         yield 'owner', owner.strip()
   231 
   245 
   232     for field, value in matches.iteritems() :
   246     for field, value in matches.iteritems() :
   233         if value :
   247         if value :
   234             yield field, value.strip()
   248             yield field, value.strip()
   235 
   249 
   270 def process_host_owner (options, host, info) :
   284 def process_host_owner (options, host, info) :
   271     """
   285     """
   272         Return (owner, comment) for host based on info, or None.
   286         Return (owner, comment) for host based on info, or None.
   273     """
   287     """
   274 
   288 
   275     if info.get('owner').lower() in NONE_OWNERS :
   289     owner = info.get('owner')
       
   290 
       
   291     if owner and owner.lower() in NONE_OWNERS :
   276         return False
   292         return False
   277     
   293     
   278     # from ldap?
   294     # from ldap?
   279     for ldap in process_host_owner_ldap(options, host, info) :
   295     for ldap in process_host_owner_ldap(options, host, info) :
   280         user, group = ldap
   296         user, group = ldap