bin/pvl.hosts-forward
changeset 516 9615ffc647a0
parent 489 7f1bd12e0d54
child 670 b95ad8c8bb4e
equal deleted inserted replaced
515:996ccf5356b8 516:9615ffc647a0
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 
     2 
     3 import logging; log = logging.getLogger('pvl.hosts-forward')
     3 import logging; log = logging.getLogger('pvl.hosts-forward')
     4 import optparse 
     4 import optparse 
       
     5 import os.path
     5 import pvl.args
     6 import pvl.args
     6 import pvl.hosts
     7 import pvl.hosts
     7 import pvl.hosts.zone
     8 import pvl.hosts.zone
     8 
     9 
     9 def main (argv):
    10 def main (argv):
    13 
    14 
    14     parser = optparse.OptionParser(main.__doc__)
    15     parser = optparse.OptionParser(main.__doc__)
    15     parser.add_option_group(pvl.args.parser(parser))
    16     parser.add_option_group(pvl.args.parser(parser))
    16     parser.add_option_group(pvl.hosts.config.optparser(parser))
    17     parser.add_option_group(pvl.hosts.config.optparser(parser))
    17 
    18 
    18     parser.add_option('--forward-zone',         metavar='DOMAIN',
    19     parser.add_option('--zone-origin',          metavar='DOMAIN',
    19             help="Generate forward zone for domain")
    20             help="Generated records for given zone origin")
       
    21 
       
    22     parser.add_option('--root-zone',            action='store_const', dest='zone_origin', const='.',
       
    23             help="Generate root zone")
    20     
    24     
    21     parser.add_option('--add-origin',           action='store_true',
    25     parser.add_option('--add-origin',           action='store_true',
    22             help="Include $ORIGIN directive in zone")
    26             help="Include $ORIGIN directive in zone")
    23 
    27 
    24     # input
    28     # input
    25     options, args = pvl.args.parse(parser, argv)
    29     options, args = pvl.args.parse(parser, argv)
    26     
    30  
    27     if not options.forward_zone:
    31     if options.zone_origin:
    28         log.fatal("required --forward-zone")
    32         origin = options.zone_origin
       
    33         
       
    34         log.info("using given zone origin: %s", origin)
       
    35    
       
    36     elif len(args) == 1:
       
    37         path, = args
       
    38         origin = os.path.basename(path.rstrip('/'))
       
    39 
       
    40         log.info("using given hostpath for zone origin: %s", origin)
       
    41 
       
    42     else:
       
    43         log.fatal("--zone-origin is required if passing multiple hostfiles")
    29         return 1
    44         return 1
    30     
    45     
    31     hosts = pvl.hosts.apply(options, args)
    46     hosts = pvl.hosts.apply(options, args)
    32 
    47 
    33     # process
    48     # process
    34     try:
    49     try:
    35         for rr in pvl.hosts.zone.apply_hosts_forward(hosts, options.forward_zone,
    50         for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin,
    36                 add_origin  = options.add_origin,
    51                 add_origin  = options.add_origin,
    37         ):
    52         ):
    38             print unicode(rr)
    53             print unicode(rr)
    39     except pvl.hosts.HostError as error:
    54     except pvl.hosts.HostError as error:
    40         log.error("%s", error)
    55         log.error("%s", error)