Changeset 119


Ignore:
Timestamp:
04/10/08 23:52:45 (16 years ago)
Author:
bas
Message:

pxeconfig.in:

  • Rewrite of code o use execeptions
  • implemented --host-range, -H
  • added better padding, must still be inproved
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Changelog

    r118 r119  
    2424 - Fixed a bug in short option '-b' (basename) must have an argument.
    2525   Author: Bas van der Vlies
     26
     27 - In analogue with systemimager we have a -H,--host-range <number>-<number>
     28   option. It must be used in combinatiion with -b,--basebane, eg:
     29     pxeconfig --basename gb-r1n --host-range 1-2 -filename default.memtestZ
     30
     31   will produce links to file default.memtest for these hosts
     32      - gb-r1n1
     33      - gb-r1n2
     34
     35  If leading zeros are used, then host names will be padded with zeros.
     36     pxeconfig --basename gb-r1n --host-range 01-02 -filename default.memtestZ
     37
     38  will produce links to file default.memtest for these hosts
     39      - gb-r1n01
     40      - gb-r1n02
     41
    2642   
    2743
  • trunk/pxeconfig.in

    r118 r119  
    9090VERSION='1.1.0'
    9191
    92 SHORTOPT_LIST='b:e:f:hin:s:rwvV'
     92SHORTOPT_LIST='b:e:f:hin:s:rwvH:V'
    9393LONGOPT_LIST=[ 'basename=', 'debug', 'end=', 'equal-width',
    94     'file=', 'help', 'interactive', 'net=', 'start=',
    95         'remove', 'verbose', 'version', 'equal-width'
     94    'file=', 'help', 'host-range=', 'interactive', 'net=',
     95    'start=', 'remove', 'verbose', 'version', 'equal-width'
    9696        ]
    9797
     
    9999        if VERBOSE:
    100100                print '%s'
     101
     102class PxeConfig(Exception):
     103        def __init__(self, msg=''):
     104                self.msg = msg
     105                Exception.__init__(self, msg)
     106
     107        def __repr__(self):
     108                return self.msg
     109
    101110
    102111def ReadConfig(file):
     
    199208       os.symlink(pxe_filename, haddr)
    200209
    201 def convert_network(net, prefix=''):
    202   """
    203   This function checks if the give network is a Class C-network and will
    204   convert the network address to a hex address if true.
    205   """
    206   d = string.split(net, '.')
    207 
    208   if len(d) != 3:
    209     if prefix:
    210       net = prefix + ' : ' + net
    211      
    212     print '%s is not a valid  C-class network address!!!' %net
    213     sys.exit(1)
    214 
    215 
    216   # Display right message for interactive/commandline
    217   if prefix:
    218     prefix = prefix + ' ' + net
    219   else:
    220     prefix = net
    221 
    222   # Check if we have valid network values
    223   r = ''
    224   for i in d:
    225     i = check_number(i, prefix)
    226     r = '%s%02X' %(r,i)
    227 
    228   if DEBUG:
    229     print r
    230 
    231   return r
    232 
    233 def check_number(number, network, option_str=''):
    234         """
    235         This functions checks if the input is between 0 < number < 255:
    236         number : is a string
    237         prefix : a string that must be displayed if an error occurs
    238         """
     210def convert_network(net):
     211        """
     212        This function checks if the give network is a Class C-network and will
     213        convert the network address to a hex address if true.
     214        """
     215        str = 'convert_network : %s' %(net)
     216        verbose(str)
     217       
     218        d = string.split(net, '.')
     219        if len(d) != 3:
     220                error = '%s is not a valid  C-class network address' %(net)
     221                raise PxeConfig, error
     222
     223        # Check if we have valid network values
     224        r = ''
     225        for i in d:
     226                i = check_number(i, True)
     227                r = '%s%02X' %(r,i)
     228
     229        if DEBUG:
     230                print r
     231
     232        return r
     233
     234def check_number(number_str, network):
     235        """
     236        number : a string. If string starts with a zero (0) then
     237                 EQUALWIDTH wil be set.
     238        network: if true then number must ben between 0 < number < 255
     239                 else it must be a valid number.
     240        """
     241
    239242        try:
    240                 n = int(number)
     243                n = int(number_str)
    241244        except ValueError, detail:
    242                 print option_str, detail
    243                 sys.exit(1)
     245                error = "%s : is not a valid number" %number_str
     246                raise PxeConfig, error
    244247
    245248        if not network:
     
    251254                return n
    252255        else:
    253                 if option_str:
    254                         number = option_str +' : ' + number
    255                        
    256                 print '%s is not a valid network number, must be between 0 and 255' %number
    257                 sys.exit(1)
     256                error = '%s is not a valid network number, must be between 0 and 255' %n
     257                raise PxeConfig, error
    258258
    259259def interactive(binfo):
    260260        print __doc__
    261261       
    262         network = raw_input('Give network address (xxx.xxx.xxx): ')
    263         naddr = convert_network(network)
    264        
    265         start = raw_input('Starting number: ')
    266         start = check_number(start, True)
    267        
    268         end = raw_input('Ending number: ')
    269         end = check_number(end, True)
     262
     263        while 1:
     264                network = raw_input('Give network address (xxx.xxx.xxx): ')
     265
     266                try:
     267                        naddr = convert_network(network)
     268                        break
     269                except PxeConfig, detail:
     270                        print '%s : not a valid C-class network number' %(network)
     271                        continue
     272
     273        while 1:
     274                start = raw_input('Starting number: ')
     275
     276                try:
     277                        start = check_number(start, True)
     278                        break
     279                except PxeConfig, detail:
     280                        print detail
     281                        continue
     282               
     283        while 1:
     284                end = raw_input('Ending number: ')
     285
     286                try:
     287                        end = check_number(end, True)
     288                        break
     289                except PxeConfig, detail:
     290                        print detail
     291                        continue
     292       
    270293       
    271294        pxe_filename = select_pxe_configfile()
     
    289312        try:
    290313                if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])):
    291                         print '%s: Filename does not exists' %binfo[FILENAME]
    292                         sys.exit(1)
     314                        error = '%s: Filename does not exists' %binfo[FILENAME]
     315                        raise Pxeconfig, detail
    293316        except KeyError, detail:
    294317                if binfo[REMOVE] :
     
    302325               
    303326        if binfo.has_key(BASENAME) and binfo.has_key(NETWORK):
    304                 print __doc__
    305                 print "The option -n/--net and -b/--basename are mutually exclusive"
    306                 sys.exit(1)
     327                error =  "The option -n/--net and -b/--basename are mutually exclusive"
     328                raise PxeConfig, error 
    307329
    308330        if binfo.has_key(BASENAME):
     
    313335                network_number = True
    314336                create_links = manage_links
     337
    315338        else:
    316                 print __doc__
    317                 sys.exit(1)
    318 
    319         try:
    320                 binfo[START] = check_number(binfo[START], network_number, '-s/--start')
    321         except KeyError, detail:
    322                 print __doc__
    323                 print '-s/--start is missing on the command line' %(detail)
    324                 sys.exit(1)
    325 
    326         try:
    327                 binfo[END] = check_number(binfo[END], network_number, '-e/--end')
    328         except KeyError, detail:
    329                 print __doc__
    330                 print '-e/--end is missing on the command line' %(detail)
    331                 sys.exit(1)
     339                error = 'You have to specifiy -b,--basename or -n,--net'
     340                raise PxeConfig, error
     341
     342        binfo[START] = check_number(binfo[START], network_number)
     343        binfo[END] = check_number(binfo[END], network_number)
    332344
    333345        if DEBUG:
     
    336348        create_links(binfo)
    337349
    338 
     350def set_padding(binfo, start, end):
     351        """
     352        find out th
     353        """
     354        if len(start) > 1 and start[0] == '0':
     355                binfo[EQUALWIDTH] = True
     356        elif end[0] == '0':
     357                binfo[EQUALWIDTH] = True
     358
     359        if len(start) == len(end):
     360                a =1   
     361               
     362
     363def parse_hostrange(binfo, arg):
     364        """
     365        Parse if arg is of format <digit-digit>, if it starts
     366        with a zero (0) then set EQUALWIDTH
     367        """
     368        str = 'parse_hostrange %s' %(arg)
     369        verbose(str)
     370
     371        l = arg.split('-')
     372        if len(l) < 2:
     373                error =  'hostrange syntax not valid: %s (number-number)' %(arg)
     374                raise PxeConfig, error
     375
     376        start = l[0]
     377        end = l[1]
     378
     379
     380        binfo[START] = check_number(start, False)
     381        binfo[END] = check_number(end, False)
     382       
    339383def check_args(argv, binfo):
    340384        """
     
    386430
    387431                elif opt in ['-n', '--net']:
    388                         network = value
    389                         binfo[NETWORK] = convert_network(value, opt)
     432                        binfo[NETWORK] = convert_network(value)
    390433                       
    391434                elif opt in ['-r', '--remove']:
     
    401444                        VERBOSE = 1
    402445
     446                elif opt in ['-H', '--host-range']:
     447                        parse_hostrange(binfo, value)
     448                       
    403449                elif opt in ['-V', '--version']:
    404450                        print VERSION
     
    415461                        addr = socket.gethostbyname(host)
    416462                except socket.error,detail:
    417                         print '%s not an valid hostname: %s' %(host,detail)
    418                         sys.exit(1)
     463                        error = '%s not an valid hostname: %s' %(host,detail)
     464                        raise PxeConfig, error
    419465                       
    420466                net = string.splitfields(addr, '.')
     
    430476        Construct hostname(s) from the supplied basename and start and end numbers
    431477        """
    432         if binfo[START] >= binfo[END]:
    433                 print __doc__
    434                 print "Supplied wrong values for start (%d) and end (%d)" %(binfo[START], binfo[END])
    435                 sys.exit(1)
     478        start = binfo[START]
     479        end = binfo[END]
     480
     481        if start > end:
     482                error = '%d >= %d : start value is greater then end value' %(start, end)
     483                raise PxeConfig, error
    436484
    437485        if binfo[EQUALWIDTH]:
    438                 width = len(str(binfo[END]))
     486                width = len(str(end))
    439487
    440488        hostnames = list()
    441         for i in xrange(binfo[START], binfo[END] + 1):
     489        for i in xrange(start, end + 1):
    442490                if binfo[EQUALWIDTH]:
    443491                        hostname = '%s%0*d' %(binfo[BASENAME], width, i)
     
    477525                sys.exit(1)
    478526
    479         check_args(sys.argv, bootinfo)
     527        try:
     528                check_args(sys.argv, bootinfo)
     529        except PxeConfig, detail:
     530                print detail
     531                sys.exit(1)
    480532       
    481533if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.