bin/pvl.hosts-import
changeset 280 8118854bfa6f
parent 279 1b72f1e0cdbe
child 281 b236f689ba22
equal deleted inserted replaced
279:1b72f1e0cdbe 280:8118854bfa6f
    45     parser.add_option('--import-zone-hosts',    metavar='FILE',
    45     parser.add_option('--import-zone-hosts',    metavar='FILE',
    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',
    49             help="Load hosts from DHCP config")
    49             help="Load hosts from DHCP config")
       
    50 
       
    51     parser.add_option('--import-dhcp-boot-server',      metavar='NEXT-SERVER',
       
    52             help="Default boot_server for dpc hosts")
    50 
    53 
    51     parser.add_option('--zone-comments-default-owner',  action='store_const',
    54     parser.add_option('--zone-comments-default-owner',  action='store_const',
    52             dest='zone_comments_default', const='owner',
    55             dest='zone_comments_default', const='owner',
    53             help="Import DNS zone comment as owner comment")
    56             help="Import DNS zone comment as owner comment")
    54 
    57 
   125 
   128 
   126     hostname = None
   129     hostname = None
   127     ethernet = []
   130     ethernet = []
   128     fixed_address = None
   131     fixed_address = None
   129 
   132 
       
   133     boot_server = options.import_dhcp_boot_server
       
   134     boot_filename = None
       
   135 
   130     for item in items :
   136     for item in items :
   131         item, args = item[0], item[1:]
   137         item, args = item[0], item[1:]
   132 
   138 
   133         if item == 'hardware' :
   139         if item == 'hardware' :
   134             _ethernet, ethernet = args
   140             _ethernet, ethernet = args
   140 
   146 
   141             if option == 'host-name' :
   147             if option == 'host-name' :
   142                 hostname, = args
   148                 hostname, = args
   143             else :
   149             else :
   144                 log.warn("host %s: ignore unknown option: %s", host, option)
   150                 log.warn("host %s: ignore unknown option: %s", host, option)
       
   151         elif item == 'next-sever' :
       
   152             boot_server, = args
       
   153         elif item == 'filename' :
       
   154             boot_filename, = args
   145         else :
   155         else :
   146             log.warn("host %s: ignore unknown item: %s", host, item)
   156             log.warn("host %s: ignore unknown item: %s", host, item)
   147 
   157 
   148     # determine hostname
   158     # determine hostname
   149     suffix = None
   159     suffix = None
   154         hostname = host
   164         hostname = host
   155 
   165 
   156     if fixed_address and not re.match(r'\d+\.\d+\.\d+.\d+', fixed_address) :
   166     if fixed_address and not re.match(r'\d+\.\d+\.\d+.\d+', fixed_address) :
   157         hostname, domain = fixed_address.split('.', 1)
   167         hostname, domain = fixed_address.split('.', 1)
   158 
   168 
       
   169     if not (hostname or ethernet) :
       
   170         log.warn("%s: no hostname/ethernet: %s/%s", host, hostname, ethernet)
       
   171         return
       
   172         
       
   173     yield hostname, 'ethernet', ethernet
   159     #if suffix :
   174     #if suffix :
   160     #    yield hostname, ('ethernet', suffix), ethernet
   175     #    yield hostname, ('ethernet', suffix), ethernet
   161     if hostname and ethernet :
   176 
   162         yield hostname, 'ethernet', ethernet
   177     if boot_server and boot_filename :
   163     else :
   178         yield hostname, 'boot', "{server}:{filename}".format(
   164         log.warn("%s: no hostname/ethernet: %s/%s", host, hostname, ethernet)
   179                 server      = boot_server,
       
   180                 filename    = boot_filename,
       
   181         )
       
   182     elif boot_filename :
       
   183         yield hostname, 'boot', "{filename}".format(filename=boot_filename)
   165 
   184 
   166 def import_dhcp_hosts (options, blocks) :
   185 def import_dhcp_hosts (options, blocks) :
   167     """
   186     """
   168         Process hosts from a parsed block
   187         Process hosts from a parsed block
   169     """
   188     """
   181 
   200 
   182             try :
   201             try :
   183                 for info in import_dhcp_host(options, host, items) :
   202                 for info in import_dhcp_host(options, host, items) :
   184                     yield info
   203                     yield info
   185             except ValueError as error :
   204             except ValueError as error :
   186                 log.warn("%s: invalid host: %s", host, error)
   205                 log.exception("%s: invalid host: %s", host, error)
   187         else:
   206         else:
   188             log.warn("ignore unknown block: %s", block)
   207             log.warn("ignore unknown block: %s", block)
   189 
   208 
   190 def import_dhcp_conf (options, file) :
   209 def import_dhcp_conf (options, file) :
   191     items, blocks = pvl.dhcp.config.DHCPConfigParser().load(file)
   210     items, blocks = pvl.dhcp.config.DHCPConfigParser().load(file)
   441         for field, fmt in (
   460         for field, fmt in (
   442                 ('ip',              None),
   461                 ('ip',              None),
   443                 ('ethernet',        None),
   462                 ('ethernet',        None),
   444                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
   463                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
   445                 ('alias',           None),
   464                 ('alias',           None),
       
   465                 ('boot',            None),
   446         ) :
   466         ) :
   447             if not fmt :
   467             if not fmt :
   448                 fmt = u"\t{field:15} = {value}"
   468                 fmt = u"\t{field:15} = {value}"
   449             
   469             
   450             values = fields.get(field, ())
   470             values = fields.get(field, ())