bin/pvl.hosts-import
changeset 328 9b4ee4bad3d1
parent 327 9d7d40206a74
child 379 60ce05b30227
equal deleted inserted replaced
327:9d7d40206a74 328:9b4ee4bad3d1
   144             log.warn("%s: skip non-rr line: %s", line, line.line)
   144             log.warn("%s: skip non-rr line: %s", line, line.line)
   145             continue
   145             continue
   146 
   146 
   147         host, domain = import_zone_host_name(options, rr.name, rr.origin)
   147         host, domain = import_zone_host_name(options, rr.name, rr.origin)
   148 
   148 
   149         if options.zone_unused and rr.name == options.zone_unused :
   149         if rr.type in ('A', 'AAAA') :
   150             log.debug("%s: skip %s", rr.name, rr)
       
   151             continue
       
   152 
       
   153         elif rr.type in ('A', 'AAAA') :
       
   154             ip, = rr.data
   150             ip, = rr.data
       
   151             ip = ipaddr.IPAddress(ip)
   155 
   152 
   156             type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
   153             type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
   157 
   154 
   158             yield (host, domain), type, ipaddr.IPAddress(ip)
   155             if options.zone_unused and rr.name == options.zone_unused :
       
   156                 yield (str(ip), domain), 'ip.unused', ip
       
   157             else :
       
   158                 yield (host, domain), type, ip,
   159 
   159 
   160             if rr.comment :
   160             if rr.comment :
   161                 yield (host, domain), 'comment', rr.comment
   161                 yield (host, domain), 'comment', rr.comment
   162 
   162 
   163             if line.parts[0] == '$GENERATE' :
   163             if line.parts[0] == '$GENERATE' :
   494     
   494     
   495     # process
   495     # process
   496     for (domain, host), fields in hosts.iteritems() :
   496     for (domain, host), fields in hosts.iteritems() :
   497         SINGLE_FIELDS = (
   497         SINGLE_FIELDS = (
   498                 'ip',
   498                 'ip',
       
   499                 'ip.unused',
   499                 'ip6',
   500                 'ip6',
   500                 'comment.owner',
   501                 'comment.owner',
   501                 'owner',
   502                 'owner',
   502                 'boot',
   503                 'boot',
   503         )
   504         )
   627         
   628         
   628         ip = fields.get(('ip', ))
   629         ip = fields.get(('ip', ))
   629 
   630 
   630         if ip in by_ip :
   631         if ip in by_ip :
   631             log.warn("%s: duplicate ip %s: %s", host, ip, by_ip[ip])
   632             log.warn("%s: duplicate ip %s: %s", host, ip, by_ip[ip])
   632         else :
   633         elif ip :
   633             by_ip[ip] = host
   634             by_ip[ip] = host
       
   635         else :
       
   636             log.warn("%s: no ip", host)
   634 
   637 
   635         yield (host, domain), fields
   638         yield (host, domain), fields
   636 
   639 
   637 def sort_export_hosts (options, hosts) :
   640 def sort_export_hosts (options, hosts) :
   638     """
   641     """
   648         select_domain = options.output_domain
   651         select_domain = options.output_domain
   649     else :
   652     else :
   650         select_domain = None
   653         select_domain = None
   651 
   654 
   652     for (host, domain), fields in hosts :
   655     for (host, domain), fields in hosts :
   653         ip = fields.get(('ip', ))
   656         ip = fields.get(('ip', )) or fields.get(('ip', 'unused'))
   654 
   657 
   655         log.debug("%s@%s: ip=%s", host, domain, ip)
   658         log.debug("%s@%s: ip=%s", host, domain, ip)
   656         
   659         
   657         # sort by IP
   660         # sort by IP
   658         if ip :
   661         if ip :
   697         if output_domain is False :
   700         if output_domain is False :
   698             pass
   701             pass
   699         elif domain != output_domain :
   702         elif domain != output_domain :
   700             yield u"[{domain}]".format(domain=domain)
   703             yield u"[{domain}]".format(domain=domain)
   701             output_domain = domain
   704             output_domain = domain
       
   705 
       
   706         # special handling for "unused" hosts
       
   707         if ('ip', 'unused') in fields :
       
   708             yield u"{indent}# {unused} {ip}".format(
       
   709                     indent  = '\t' if output_domain else '',
       
   710                     unused  = options.zone_unused,
       
   711                     ip      = fields[('ip', 'unused')],
       
   712             )
       
   713             yield u""
       
   714             continue
   702 
   715 
   703         # optional host-comments
   716         # optional host-comments
   704         for comment in fields.get(('comment', 'host'), ()):
   717         for comment in fields.get(('comment', 'host'), ()):
   705             yield u"{indent}# {comment}".format(
   718             yield u"{indent}# {comment}".format(
   706                     indent  = '\t' if output_domain else '',
   719                     indent  = '\t' if output_domain else '',