bin/pvl.hosts-import
changeset 285 e18aaec99e54
parent 281 b236f689ba22
child 288 2f2f92e4c58e
equal deleted inserted replaced
284:eb7e2a4c6880 285:e18aaec99e54
   111 
   111 
   112             if rr.comment :
   112             if rr.comment :
   113                 yield rr.name, 'comment', rr.comment
   113                 yield rr.name, 'comment', rr.comment
   114 
   114 
   115             if rr.origin :
   115             if rr.origin :
   116                 yield rr.name, 'domain', rr.origin
   116                 # not a fqdn
       
   117                 yield rr.name, 'domain', rr.origin.rstrip('.')
   117 
   118 
   118         elif rr.type == 'CNAME' :
   119         elif rr.type == 'CNAME' :
   119             host, = rr.data
   120             host, = rr.data
   120 
   121 
   121             yield host, 'alias', rr.name
   122             yield host, 'alias', rr.name
   443 
   444 
   444         yield sort, host, fields
   445         yield sort, host, fields
   445 
   446 
   446 def export_hosts (options, hosts) :
   447 def export_hosts (options, hosts) :
   447     """
   448     """
   448         Export hosts to file.
   449         Generate hosts config lines for given hosts.
   449     """
   450     """
   450 
   451 
   451     file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
   452     FMT = u"\t{field:15} = {value}"
       
   453 
       
   454     yield u"[{domain}]".format(domain=options.hosts_domain)
   452 
   455 
   453     # filter + sort
   456     # filter + sort
   454     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   457     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   455 
   458 
   456     for host, fields in hosts :
   459     for host, fields in hosts :
   457         for comment in fields.get('comment-host', ()):
   460         for comment in fields.get('comment-host', ()):
   458             print >>file, u"# {comment}".format(comment=comment)
   461             yield u"# {comment}".format(comment=comment)
   459 
   462 
   460         print >>file, u"[{host}]".format(host=host)
   463         yield u"[[{host}]]".format(host=host)
   461         
   464         
       
   465         for domain in fields.get('domain', ()) :
       
   466             if domain != options.hosts_domain :
       
   467                 yield FMT.format(field='domain', value=domain)
       
   468 
   462         for field, fmt in (
   469         for field, fmt in (
   463                 ('ip',              None),
   470                 ('ip',              FMT),
   464                 ('ip6',             None),
   471                 ('ip6',             FMT),
   465                 ('ethernet',        None),
   472                 ('ethernet',        FMT),
   466                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
   473                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
   467                 ('alias',           None),
   474                 ('alias',           FMT),
   468                 ('boot',            None),
   475                 ('boot',            FMT),
   469         ) :
   476         ) :
   470             if not fmt :
       
   471                 fmt = u"\t{field:15} = {value}"
       
   472             
       
   473             values = fields.get(field, ())
   477             values = fields.get(field, ())
   474 
   478 
   475             if len(values) > 1 :
   479             if len(values) > 1 :
   476                 for index, value in enumerate(values, 1) :
   480                 for index, value in enumerate(values, 1) :
   477                     print >>file, fmt.format(
   481                     yield fmt.format(
   478                             field   = "{field}.{index}".format(field=field, index=index),
   482                             field   = "{field}.{index}".format(field=field, index=index),
   479                             value   = value,
   483                             value   = value,
   480                             fields  = fields
   484                             fields  = fields
   481                     )
   485                     )
   482             elif len(values) > 0 :
   486             elif len(values) > 0 :
   483                 value, = values
   487                 value, = values
   484                 print >>file, fmt.format(field=field, value=value, fields=fields)
   488                 yield fmt.format(field=field, value=value, fields=fields)
   485         
   489     
   486         print >>file
   490         yield ""
       
   491    
       
   492 def apply_hosts_export (options, hosts) :
       
   493     """
       
   494         Export hosts to file.
       
   495     """
       
   496 
       
   497     file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
       
   498 
       
   499     for line in export_hosts(options, hosts) :
       
   500         print >>file, line
   487 
   501 
   488 def main (argv) :
   502 def main (argv) :
   489     options, args = parse_options(argv)
   503     options, args = parse_options(argv)
   490 
   504 
   491     options.ldap = pvl.ldap.args.apply(options)
   505     options.ldap = pvl.ldap.args.apply(options)
   497         # import
   511         # import
   498         hosts = import_hosts(options)
   512         hosts = import_hosts(options)
   499    
   513    
   500     # output
   514     # output
   501     if options.output_hosts :
   515     if options.output_hosts :
   502         export_hosts(options, hosts)
   516         apply_hosts_export(options, hosts)
   503 
   517 
   504 if __name__ == '__main__':
   518 if __name__ == '__main__':
   505     pvl.args.main(main)
   519     pvl.args.main(main)