bin/pvl.hosts-import
changeset 302 f50469a1da4d
parent 301 b41902b0b9cf
child 303 b8ba5df799be
equal deleted inserted replaced
301:b41902b0b9cf 302:f50469a1da4d
     7 import pvl.args
     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 collections
    12 import ipaddr
    13 import ipaddr
       
    14 import logging; log = logging.getLogger('pvl.hosts-import')
    13 import optparse
    15 import optparse
    14 import collections
    16 import os.path
    15 import re
    17 import re
    16 import logging; log = logging.getLogger('pvl.hosts-import')
       
    17 
    18 
    18 __version__ = '0.1'
    19 __version__ = '0.1'
    19 
    20 
    20 def parse_options (argv) :
    21 def parse_options (argv) :
    21     """
    22     """
    43 
    44 
    44     # input
    45     # input
    45     parser.add_option('--import-zone-hosts',    metavar='FILE',     action='append',
    46     parser.add_option('--import-zone-hosts',    metavar='FILE',     action='append',
    46             help="Load hosts from DNS zone")
    47             help="Load hosts from DNS zone")
    47 
    48 
    48     parser.add_option('--import-dhcp-hosts',    metavar='FILE',     action='append',
    49     parser.add_option('--import-zone-origin',   metavar='ORIGIN',
    49             help="Load hosts from DHCP config")
    50             help="Initial origin for given zone file; default is basename")
    50 
       
    51     parser.add_option('--import-dhcp-boot-server',      metavar='NEXT-SERVER',
       
    52             help="Default boot_server for dpc hosts")
       
    53 
    51 
    54     parser.add_option('--import-zone-comments-owner',  action='store_const',
    52     parser.add_option('--import-zone-comments-owner',  action='store_const',
    55             dest='import_zone_comments', const='owner',
    53             dest='import_zone_comments', const='owner',
    56             help="Import DNS zone comment as owner comment")
    54             help="Import DNS zone comment as owner comment")
    57 
    55 
    58     parser.add_option('--import-zone-comments-host',   action='store_const',
    56     parser.add_option('--import-zone-comments-host',   action='store_const',
    59             dest='import_zone_comments', const='host',
    57             dest='import_zone_comments', const='host',
    60             help="Import DNS zone comment as host comment")
    58             help="Import DNS zone comment as host comment")
       
    59 
       
    60     parser.add_option('--import-dhcp-hosts',    metavar='FILE',     action='append',
       
    61             help="Load hosts from DHCP config")
       
    62 
       
    63     parser.add_option('--import-dhcp-boot-server',      metavar='NEXT-SERVER',
       
    64             help="Default boot_server for dpc hosts")
       
    65 
    61 
    66 
    62     parser.add_option('--dump-host-comments',   action='store_true',
    67     parser.add_option('--dump-host-comments',   action='store_true',
    63             help="Dump out info on imported host comments")
    68             help="Dump out info on imported host comments")
    64 
    69 
    65     # defaults
    70     # defaults
    88     # apply
    93     # apply
    89     pvl.args.apply(options, argv[0])
    94     pvl.args.apply(options, argv[0])
    90 
    95 
    91     return options, args
    96     return options, args
    92 
    97 
       
    98 def import_zone_host_name (options, name, origin) :
       
    99     """
       
   100         Import zone name from rr
       
   101     """
       
   102 
       
   103     if '.' in name :
       
   104         host, domain = name.split('.', 1)
       
   105         domain = pvl.dns.zone.join(domain, origin)
       
   106     else :
       
   107         host = name
       
   108         domain = origin
       
   109             
       
   110     if domain :
       
   111         # not a fqdn
       
   112         domain = domain.rstrip('.')
       
   113 
       
   114         log.info("%s: %s@%s", name, host, domain)
       
   115     else :
       
   116         log.warn("%s: no domain", name)
       
   117 
       
   118     return host, domain
       
   119 
    93 def import_zone_hosts (options, file) :
   120 def import_zone_hosts (options, file) :
    94     """
   121     """
    95         Yield host info from zonefile records.
   122         Yield host info from zonefile records.
    96     """
   123     """
    97 
   124 
       
   125     origin = options.import_zone_origin or os.path.basename(file.name)
       
   126 
    98     for rr in pvl.dns.zone.ZoneRecord.load(file,
   127     for rr in pvl.dns.zone.ZoneRecord.load(file,
       
   128             # used to determine domain
       
   129             origin          = origin,
       
   130 
    99             # generated hosts need to imported by hand...
   131             # generated hosts need to imported by hand...
   100             expand_generate = False,
   132             expand_generate = False,
   101     ) :
   133     ) :
       
   134         host, domain = import_zone_host_name(options, rr.name, rr.origin)
       
   135 
   102         if options.zone_unused and rr.name == options.zone_unused :
   136         if options.zone_unused and rr.name == options.zone_unused :
   103             log.debug("%s: skip %s", rr.name, rr)
   137             log.debug("%s: skip %s", rr.name, rr)
   104             continue
   138             continue
   105 
   139 
   106         elif rr.type in ('A', 'AAAA') :
   140         elif rr.type in ('A', 'AAAA') :
   107             ip, = rr.data
   141             ip, = rr.data
   108 
   142 
   109             type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
   143             type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
   110 
   144 
   111             yield rr.name, type, ipaddr.IPAddress(ip)
   145             yield host, 'domain', domain
       
   146             yield host, type, ipaddr.IPAddress(ip)
   112 
   147 
   113             if rr.comment :
   148             if rr.comment :
   114                 yield rr.name, 'comment', rr.comment
   149                 yield host, 'comment', rr.comment
   115 
   150 
   116             if rr.origin :
       
   117                 # not a fqdn
       
   118                 yield rr.name, 'domain', rr.origin.rstrip('.')
       
   119 
   151 
   120         elif rr.type == 'CNAME' :
   152         elif rr.type == 'CNAME' :
   121             host, = rr.data
   153             alias, = rr.data
   122 
   154             alias_host, alias_domain = import_zone_host_name(options, alias, rr.origin)
   123             yield host, 'alias', rr.name
   155             
       
   156             if domain == alias_domain :
       
   157                 yield alias_host, 'alias', host
       
   158             else :
       
   159                 yield alias_host, 'alias', pvl.dns.zone.join(host, domain)
   124         
   160         
   125         elif rr.type == 'TXT' :
   161         elif rr.type == 'TXT' :
   126             txt, = rr.data
   162             txt, = rr.data
   127 
   163 
   128             yield host, 'comment', txt
   164             yield host, 'comment', txt
   129 
   165 
   130         else :
   166         else :
   131             log.warn("%s: unknown rr: %s", rr.name, rr)
   167             log.warn("%s: unknown rr: %s", host, rr)
   132 
   168 
   133 def import_dhcp_host (options, host, items) :
   169 def import_dhcp_host (options, host, items) :
   134     """
   170     """
   135         Yield host infos from a dhcp host ... { ... }
   171         Yield host infos from a dhcp host ... { ... }
   136     """
   172     """