bin/pvl.hosts-import
changeset 268 560ba0544254
parent 266 4eb3d73d852c
child 275 4dc5cc30a278
equal deleted inserted replaced
267:8c0c1b6e6aff 268:560ba0544254
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 
     2 
     3 """
     3 """
     4     Manipulate host definitions for dns/dhcp.
     4     Import hosts from existing BIND or dhcpd files.
     5 """
     5 """
     6 
     6 
     7 import pvl.args, optparse
     7 import pvl.args
     8 import pvl.dns.zone
     8 import pvl.dns.zone
     9 import pvl.dhcp.config
     9 import pvl.dhcp.config
    10 import pvl.ldap.args
    10 import pvl.ldap.args
    11 
    11 
    12 import ipaddr
    12 import ipaddr
   141 
   141 
   142     if fixed_address and not re.match(r'\d+\.\d+\.\d+.\d+', fixed_address) :
   142     if fixed_address and not re.match(r'\d+\.\d+\.\d+.\d+', fixed_address) :
   143         hostname, domain = fixed_address.split('.', 1)
   143         hostname, domain = fixed_address.split('.', 1)
   144 
   144 
   145     #if suffix :
   145     #if suffix :
   146     #    yield hostname, 'ethernet:{suffix}'.format(suffix=suffix), ethernet
   146     #    yield hostname, ('ethernet', suffix), ethernet
   147     if hostname and ethernet :
   147     if hostname and ethernet :
   148         yield hostname, 'ethernet', ethernet
   148         yield hostname, 'ethernet', ethernet
   149     else :
   149     else :
   150         log.warn("%s: no hostname/ethernet: %s/%s", host, hostname, ethernet)
   150         log.warn("%s: no hostname/ethernet: %s/%s", host, hostname, ethernet)
   151 
   151 
   306     elif owner :
   306     elif owner :
   307         owner, comment = owner
   307         owner, comment = owner
   308         
   308         
   309         log.info("%s: %s (%s)", host, owner, comment)
   309         log.info("%s: %s (%s)", host, owner, comment)
   310         
   310         
   311         yield 'owner-comment', comment
   311         yield 'comment-owner', comment
   312         yield 'owner', owner,
   312         yield 'owner', owner,
   313 
   313 
   314     else :
   314     else :
   315         log.warn("%s: unknown owner: %s", host, info)
   315         log.warn("%s: unknown owner: %s", host, info)
   316         yield 'comment', "owner: {group} / {owner}".format(
   316         yield 'comment-owner', "{group} / {owner}".format(
   317                 group   = info.get('group', ''),
   317                 group   = info.get('group', ''),
   318                 owner   = info.get('owner', ''),
   318                 owner   = info.get('owner', ''),
   319         )
   319         )
   320     
   320     
   321     if info.get('host') :
   321     if info.get('host') :
   322         yield 'comment', info['host']
   322         yield 'comment-host', info['host']
   323 
   323 
   324 def process_hosts_comments (options, import_hosts) :
   324 def process_hosts_comments (options, import_hosts) :
   325     """
   325     """
   326         Parse out comments from host imports..
   326         Parse out comments from host imports..
   327     """
   327     """
   352         Import host infos from given files.
   352         Import host infos from given files.
   353     """
   353     """
   354 
   354 
   355     if options.import_zone_hosts:
   355     if options.import_zone_hosts:
   356         for info in import_zone_hosts(options,
   356         for info in import_zone_hosts(options,
   357                 pvl.args.apply_file(options.import_zone_hosts)) :
   357                 pvl.args.apply_file(options.import_zone_hosts, 'r', options.input_charset)) :
   358             yield info
   358             yield info
   359     
   359     
   360     if options.import_dhcp_hosts:
   360     if options.import_dhcp_hosts:
   361         for info in import_dhcp_conf(options,
   361         for info in import_dhcp_conf(options,
   362                 pvl.args.apply_file(options.import_dhcp_hosts)) :
   362                 pvl.args.apply_file(options.import_dhcp_hosts, 'r', options.input_charset)) :
   363             yield info
   363             yield info
   364        
   364        
   365 def import_hosts (options) :
   365 def import_hosts (options) :
   366     """
   366     """
   367         Import hosts from dns/dhcp.
   367         Import hosts from dns/dhcp.
   387     for host, fields in hosts :
   387     for host, fields in hosts :
   388         ip = fields.get('ip')
   388         ip = fields.get('ip')
   389         
   389         
   390         # sort by IP
   390         # sort by IP
   391         if ip :
   391         if ip :
   392             sort = ip = ip[0]
   392             sort = ip[0]
   393         else :
   393         else :
   394             # fake, to sort correctly
   394             # fake, to sort correctly
   395             sort = ipaddr.IPAddress(0)
   395             sort = ipaddr.IPAddress(0)
   396         
   396         
   397         # select
   397         # select
   410 
   410 
   411     # filter + sort
   411     # filter + sort
   412     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   412     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   413 
   413 
   414     for host, fields in hosts :
   414     for host, fields in hosts :
   415         for comment in fields.get('comment', ()) :
   415         for comment in fields.get('comment-host', ()):
   416             print >>file, u"# {comment}".format(comment=comment)
   416             print >>file, u"# {comment}".format(comment=comment)
   417 
   417 
   418         print >>file, u"[{host}]".format(host=host)
   418         print >>file, u"[{host}]".format(host=host)
   419         
   419         
   420         for field, fmt in (
   420         for field, fmt in (
   421                 ('ip',              None),
   421                 ('ip',              None),
   422                 ('ethernet',        None),
   422                 ('ethernet',        None),
   423                 ('owner',           u"\t{field:15} = {value} # {fields[owner-comment][0]}"),
   423                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
       
   424                 ('alias',           None),
   424         ) :
   425         ) :
   425             if not fmt :
   426             if not fmt :
   426                 fmt = u"\t{field:15} = {value}"
   427                 fmt = u"\t{field:15} = {value}"
   427 
   428             
   428             for value in fields.get(field, ()) :
   429             values = fields.get(field, ())
       
   430 
       
   431             if len(values) > 1 :
       
   432                 for index, value in enumerate(values, 1) :
       
   433                     print >>file, fmt.format(
       
   434                             field   = "{field}.{index}".format(field=field, index=index),
       
   435                             value   = value,
       
   436                             fields  = fields
       
   437                     )
       
   438             elif len(values) > 0 :
       
   439                 value, = values
   429                 print >>file, fmt.format(field=field, value=value, fields=fields)
   440                 print >>file, fmt.format(field=field, value=value, fields=fields)
   430         
   441         
   431         print >>file
   442         print >>file
   432 
   443 
   433 def main (argv) :
   444 def main (argv) :