bin/pvl.hosts-import
changeset 301 b41902b0b9cf
parent 288 2f2f92e4c58e
child 302 f50469a1da4d
equal deleted inserted replaced
300:a720bcf96007 301:b41902b0b9cf
    40 
    40 
    41     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    41     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    42             help="Encoding used for output files")
    42             help="Encoding used for output files")
    43 
    43 
    44     # input
    44     # input
    45     parser.add_option('--import-zone-hosts',    metavar='FILE',
    45     parser.add_option('--import-zone-hosts',    metavar='FILE',     action='append',
    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',     action='append',
    49             help="Load hosts from DHCP config")
    49             help="Load hosts from DHCP config")
    50 
    50 
    51     parser.add_option('--import-dhcp-boot-server',      metavar='NEXT-SERVER',
    51     parser.add_option('--import-dhcp-boot-server',      metavar='NEXT-SERVER',
    52             help="Default boot_server for dpc hosts")
    52             help="Default boot_server for dpc hosts")
    53 
    53 
    54     parser.add_option('--zone-comments-default-owner',  action='store_const',
    54     parser.add_option('--import-zone-comments-owner',  action='store_const',
    55             dest='zone_comments_default', const='owner',
    55             dest='import_zone_comments', const='owner',
    56             help="Import DNS zone comment as owner comment")
    56             help="Import DNS zone comment as owner comment")
    57 
    57 
    58     parser.add_option('--zone-comments-default-host',   action='store_const',
    58     parser.add_option('--import-zone-comments-host',   action='store_const',
    59             dest='zone_comments_default', const='host',
    59             dest='import_zone_comments', const='host',
    60             help="Import DNS zone comment as host comment")
    60             help="Import DNS zone comment as host comment")
    61 
    61 
    62     parser.add_option('--dump-host-comments',   action='store_true',
    62     parser.add_option('--dump-host-comments',   action='store_true',
    63             help="Dump out info on imported host comments")
    63             help="Dump out info on imported host comments")
    64 
    64 
    76     parser.add_option('--output-prefix',        metavar='PREFIX',
    76     parser.add_option('--output-prefix',        metavar='PREFIX',
    77             help="Select hosts by ip prefix")
    77             help="Select hosts by ip prefix")
    78 
    78 
    79     # defaults
    79     # defaults
    80     parser.set_defaults(
    80     parser.set_defaults(
    81 
    81         import_zone_hosts   = [],
       
    82         import_dhcp_hosts   = [],
    82     )
    83     )
    83     
    84     
    84     # parse
    85     # parse
    85     options, args = parser.parse_args(argv[1:])
    86     options, args = parser.parse_args(argv[1:])
    86 
    87 
   118 
   119 
   119         elif rr.type == 'CNAME' :
   120         elif rr.type == 'CNAME' :
   120             host, = rr.data
   121             host, = rr.data
   121 
   122 
   122             yield host, 'alias', rr.name
   123             yield host, 'alias', rr.name
       
   124         
       
   125         elif rr.type == 'TXT' :
       
   126             txt, = rr.data
       
   127 
       
   128             yield host, 'comment', txt
   123 
   129 
   124         else :
   130         else :
   125             log.warn("%s: unknown rr: %s", rr.name, rr)
   131             log.warn("%s: unknown rr: %s", rr.name, rr)
   126 
   132 
   127 def import_dhcp_host (options, host, items) :
   133 def import_dhcp_host (options, host, items) :
   189     """
   195     """
   190         Process hosts from a parsed block
   196         Process hosts from a parsed block
   191     """
   197     """
   192 
   198 
   193     for block, items, blocks in blocks :
   199     for block, items, blocks in blocks :
   194         log.info("%s", block)
       
   195         
   200         
   196         block, args = block[0], block[1:]
   201         block, args = block[0], block[1:]
   197 
   202 
   198         if block == 'group' :
   203         if block == 'group' :
       
   204             log.info("group")
   199             for info in import_dhcp_hosts(options, blocks) :
   205             for info in import_dhcp_hosts(options, blocks) :
   200                 yield info
   206                 yield info
   201         elif block == 'host' :
   207         elif block == 'host' :
   202             host, = args
   208             host, = args
       
   209             
       
   210             log.info("host: %s", host)
   203 
   211 
   204             try :
   212             try :
   205                 for info in import_dhcp_host(options, host, items) :
   213                 for info in import_dhcp_host(options, host, items) :
   206                     yield info
   214                     yield info
   207             except ValueError as error :
   215             except ValueError as error :
   248             matches = match.groupdict()
   256             matches = match.groupdict()
   249 
   257 
   250             log.info("%s: matched comment: %s", hostname, comment)
   258             log.info("%s: matched comment: %s", hostname, comment)
   251             break
   259             break
   252     else :
   260     else :
   253         if options.zone_comments_default :
   261         if options.import_zone_comments :
   254             log.info("%s: default comment: %s", hostname, comment)
   262             log.info("%s: default comment: %s", hostname, comment)
   255             matches = { options.zone_comments_default: comment }
   263             matches = { options.import_zone_comments: comment }
   256         else :
   264         else :
   257             log.warn("%s: unknown comment: %s", hostname, comment)
   265             log.warn("%s: unknown comment: %s", hostname, comment)
   258             return
   266             return
   259     
   267     
   260     owner = matches.pop('owner', None)
   268     owner = matches.pop('owner', None)
   393 def apply_hosts_import (options) :
   401 def apply_hosts_import (options) :
   394     """
   402     """
   395         Import host infos from given files.
   403         Import host infos from given files.
   396     """
   404     """
   397 
   405 
   398     if options.import_zone_hosts:
   406     for zone_file in options.import_zone_hosts:
   399         for info in import_zone_hosts(options,
   407         file = pvl.args.apply_file(zone_file, 'r', options.input_charset)
   400                 pvl.args.apply_file(options.import_zone_hosts, 'r', options.input_charset)) :
   408         for info in import_zone_hosts(options, file) :
   401             yield info
   409             yield info
   402     
   410     
   403     if options.import_dhcp_hosts:
   411     for dhcp_file in options.import_dhcp_hosts:
   404         for info in import_dhcp_conf(options,
   412         file = pvl.args.apply_file(dhcp_file, 'r', options.input_charset)
   405                 pvl.args.apply_file(options.import_dhcp_hosts, 'r', options.input_charset)) :
   413         for info in import_dhcp_conf(options, file) :
   406             yield info
   414             yield info
   407        
   415        
   408 def import_hosts (options) :
   416 def import_hosts (options) :
   409     """
   417     """
   410         Import hosts from dns/dhcp.
   418         Import hosts from dns/dhcp.