Changeset 186 for trunk/src/pxeconfig.py


Ignore:
Timestamp:
10/28/10 16:04:25 (14 years ago)
Author:
bas
Message:

Added mac address support for environments that have dynamic ip's. Depended on arp command

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/pxeconfig.py

    r179 r186  
    174174        return r
    175175
    176 def hosts_2_hex(hosts, options):
     176def host_2_hex(host, options):
    177177        """
    178178        Convert hostname(s) to a net address that can be handled by manage_links function
     
    182182                print str
    183183
    184         for host in hosts:
    185                 try:
    186                         addr = socket.gethostbyname(host)
    187                 except socket.error,detail:
    188                         error =  '%s not an valid hostname: %s' %(host,detail)
    189                         raise PxeConfig, error
    190 
    191                 net = string.splitfields(addr, '.')
    192                 cnet = string.joinfields(net[0:3], '.')
    193 
    194                 if options.SCRIPT_HOOK:
    195                         if options.DEBUG or options.DRY_RUN or options.VERBOSE:
    196                                 print 'Executing client script hook: %s with arg: %s' %(options.SCRIPT_HOOK, addr)
    197                         if not options.DRY_RUN:
    198                                 cmd = '%s %s' %(options.SCRIPT_HOOK, addr)
    199                                 os.system(cmd)
    200 
    201                 haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
    202                 manage_links(haddr, options)
     184        try:
     185                addr = socket.gethostbyname(host)
     186        except socket.error,detail:
     187                error =  '%s not an valid hostname: %s' %(host,detail)
     188                raise PxeConfig, error
     189
     190        net = string.splitfields(addr, '.')
     191        cnet = string.joinfields(net[0:3], '.')
     192
     193        if options.SCRIPT_HOOK:
     194                if options.DEBUG or options.DRY_RUN or options.VERBOSE:
     195                        print 'Executing client script hook: %s with arg: %s' %(options.SCRIPT_HOOK, addr)
     196                if not options.DRY_RUN:
     197                        cmd = '%s %s' %(options.SCRIPT_HOOK, addr)
     198                        os.system(cmd)
     199
     200        haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
     201        manage_links(haddr, options)
     202
     203
     204def mac_2_hex(mac_addr, options):
     205        """
     206        Convert mac address to pxeconfig address
     207        """
     208        if options.DEBUG:
     209                str = 'mac_2_hex: %s' %mac_addr
     210                print mac_addr
     211
     212        haddr = '01-%s' %(mac_addr.replace(':', '-').lower())
     213        manage_links(haddr, options)
    203214
    204215def add_options(p):
     
    251262        )
    252263
    253 def parser(argv, settings):
     264def parser(argv, config, defaults):
    254265        """
    255266        Make use of sara advance parser module. It can handle regex in hostnames
     
    272283        if not options.DEBUG:
    273284                try:
    274                         if settings['debug']:
    275                                 options.DEBUG = int(settings['debug'])
     285                        if defaults['debug']:
     286                                options.DEBUG = int(defaults['debug'])
    276287                except KeyError:
    277288                        pass
     
    290301        # ...
    291302        try:
    292                 options.SCRIPT_HOOK = settings['client_script_hook']
     303                options.SCRIPT_HOOK = defaults['client_script_hook']
    293304        except KeyError, detail:
    294305                pass
     
    297308                print args, options
    298309
    299         hosts_2_hex(args, options)
    300 
     310        ##
     311        # Are the hosts wiht only mac addresses defined in the configuration file
     312
     313        for host in args:
     314                if host in config.sections():
     315                        mac_addr = config.get(host, 'mac_address')
     316                        mac_2_hex(mac_addr, options)
     317                else:
     318                        host_2_hex(host, options)
    301319
    302320def main():
     
    304322        #
    305323        global PXE_CONF_DIR
    306         settings = ReadConfig()
    307        
    308         try:
    309                 PXE_CONF_DIR = settings['pxe_config_dir']
     324        parser_config, default_settings = ReadConfig()
     325       
     326        try:
     327                PXE_CONF_DIR = default_settings['pxe_config_dir']
    310328
    311329        except KeyError:
     
    317335                raise PxeConfig, error
    318336
    319         parser(sys.argv, settings)
     337        parser(sys.argv, parser_config, default_settings)
    320338
    321339       
Note: See TracChangeset for help on using the changeset viewer.