bin/process-zone
changeset 25 3d35d0eef197
parent 24 4ccc31fdc047
child 35 840092ee4d97
equal deleted inserted replaced
24:4ccc31fdc047 25:3d35d0eef197
    49             help="Write to output file; default stdout")
    49             help="Write to output file; default stdout")
    50 
    50 
    51     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    51     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    52             help="Encoding used for output files")
    52             help="Encoding used for output files")
    53 
    53 
    54     # read line mtimes?
       
    55     parser.add_option('--input-line-date',      action='store_true',
       
    56             help="Parse timestamp prefix from each input line (e.g. `hg blame | ...`)")
       
    57 
       
    58     # check stage
    54     # check stage
    59     parser.add_option('--check-hosts',          action='store_true',
    55     parser.add_option('--check-hosts',          action='store_true',
    60             help="Check that host/IPs are unique. Use --quiet to silence warnings, and test exit status")
    56             help="Check that host/IPs are unique. Use --quiet to silence warnings, and test exit status")
    61 
    57 
    62     parser.add_option('--check-exempt',         action='append',
    58     parser.add_option('--check-exempt',         metavar='HOST', action='append',
    63             help="Allow given names to have multiple records")
    59             help="Allow given names to have multiple records")
       
    60 
       
    61     # meta stage
       
    62     parser.add_option('--meta-zone',            action='store_true',
       
    63             help="Generate host metadata zone; requires --input-line-date")
       
    64 
       
    65     parser.add_option('--meta-ignore',          metavar='HOST', action='append',
       
    66             help="Ignore given hostnames in metadata output")
       
    67 
       
    68     parser.add_option('--input-line-date',      action='store_true',
       
    69             help="Parse timestamp prefix from each input line (e.g. `hg blame | ...`)")
    64 
    70 
    65     # forward stage
    71     # forward stage
    66     parser.add_option('--forward-zone',         action='store_true', 
    72     parser.add_option('--forward-zone',         action='store_true', 
    67             help="Generate forward zone")
    73             help="Generate forward zone")
    68 
    74 
    81 
    87 
    82     # defaults
    88     # defaults
    83     parser.set_defaults(
    89     parser.set_defaults(
    84         loglevel            = logging.WARN,
    90         loglevel            = logging.WARN,
    85 
    91 
       
    92         # XXX: combine
    86         check_exempt        = [],
    93         check_exempt        = [],
       
    94         meta_ignore         = [],
    87     )
    95     )
    88     
    96     
    89     # parse
    97     # parse
    90     options, args = parser.parse_args(argv[1:])
    98     options, args = parser.parse_args(argv[1:])
    91 
    99 
   366 def process_zone_forwards (zone, txt=False, mx=False) :
   374 def process_zone_forwards (zone, txt=False, mx=False) :
   367     """
   375     """
   368         Process zone data -> forward zone data.
   376         Process zone data -> forward zone data.
   369     """
   377     """
   370 
   378 
   371     TIMESTAMP_FORMAT='%Y/%m/%d'
       
   372 
       
   373     for r in zone :
   379     for r in zone :
   374         yield r
   380         yield r
   375 
   381 
   376         if r.type == 'A' :
   382         if r.type == 'A' :
   377             if txt :
   383             if txt :
   379                 comment = r.line.comment
   385                 comment = r.line.comment
   380 
   386 
   381                 if comment :
   387                 if comment :
   382                     yield TXTRecord(None, comment, ttl=r.ttl)
   388                     yield TXTRecord(None, comment, ttl=r.ttl)
   383 
   389 
   384                 # timestamp?
   390            
   385                 timestamp = r.line.timestamp
       
   386 
       
   387                 if timestamp :
       
   388                     yield TXTRecord(None, timestamp.strftime(TIMESTAMP_FORMAT), ttl=r.ttl)
       
   389             
       
   390             # XXX: RP, do we need it?
   391             # XXX: RP, do we need it?
   391 
   392 
   392             if mx :
   393             if mx :
   393                 # XXX: is this a good idea?
   394                 # XXX: is this a good idea?
   394                 yield ZoneRecord(None, 'MX', [10, mx], ttl=r.ttl)
   395                 yield ZoneRecord(None, 'MX', [10, mx], ttl=r.ttl)
   395 
   396 
       
   397 def process_zone_meta (zone, ignore=None) :
       
   398     """
       
   399         Process zone metadata -> output.
       
   400     """
       
   401     
       
   402     TIMESTAMP_FORMAT='%Y/%m/%d'
       
   403     
       
   404     for r in zone :
       
   405         if ignore and r.name in ignore :
       
   406             # skip
       
   407             log.debug("Ignore record: %s", r)
       
   408             continue
       
   409 
       
   410         # for hosts..
       
   411         if r.type == 'A' :
       
   412             # timestamp?
       
   413             timestamp = r.line.timestamp
       
   414 
       
   415             if timestamp :
       
   416                 yield TXTRecord(r.name, timestamp.strftime(TIMESTAMP_FORMAT), ttl=r.ttl)
       
   417      
   396 def reverse_addr (ip) :
   418 def reverse_addr (ip) :
   397     """
   419     """
   398         Return in-addr.arpa reverse for given IPv4 IP.
   420         Return in-addr.arpa reverse for given IPv4 IP.
   399     """
   421     """
   400     
   422     
   503     if options.forward_zone :
   525     if options.forward_zone :
   504         log.info("Write forward zone: %s", output)
   526         log.info("Write forward zone: %s", output)
   505 
   527 
   506         zone = list(process_zone_forwards(zone, txt=options.forward_txt, mx=options.forward_mx))
   528         zone = list(process_zone_forwards(zone, txt=options.forward_txt, mx=options.forward_mx))
   507 
   529 
       
   530     elif options.meta_zone :
       
   531         log.info("Write metadata zone: %s", output)
       
   532 
       
   533         if not options.input_line_date :
       
   534             log.error("--meta-zone requires --input-line-date")
       
   535             return 1
       
   536 
       
   537         zone = list(process_zone_meta(zone, ignore=set(options.meta_ignore)))
       
   538 
   508     elif options.reverse_zone :
   539     elif options.reverse_zone :
   509         origin = reverse_addr(options.reverse_zone)
   540         origin = reverse_addr(options.reverse_zone)
   510         domain = options.reverse_domain
   541         domain = options.reverse_domain
   511 
   542 
   512         if not domain :
   543         if not domain :