Changeset 105


Ignore:
Timestamp:
09/26/07 17:17:51 (17 years ago)
Author:
bas
Message:

pxeconfig.in:

  • Rearranged the code
  • add -basename function
  • repaired the --debug option
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pxeconfig.in

    r104 r105  
    3232Usage: pxeconfig
    3333
    34  [-f|--file <filename>] [hostname(s)]
    35  [-i|--interactive]
    36  [-n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>]
    37  [-b|--basename <string> s|--start <number> -e|--end <number> -f|--file <filename>]
    38  [-r|--remove]
    39  [-V|--version]
     34 [-r|--remove] -f|--file <filename>] [hostname(s)
     35 [-r|--remove] -i|--interactive
     36 [-r|--remove] -n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>
     37 [-r|--remove] -b|--basename <string> s|--start <number> -e|--end <number> -f|--file <filename>
     38 -V|--version
    4039
    4140With this program you can configure which PXE configuration file a node
     
    7170# DEBUG
    7271#
    73 DEBUG=1
     72DEBUG=0
    7473
    7574# Constants
     
    218217  return r
    219218
    220 def check_number(number, prefix=''):
    221   """
    222   This functions checks if the input is between 0 < number < 255:
    223      number : is a string
    224      prefix ; a string that must be displayed if an error occurs
    225   """
    226   try:
    227     n = int(number)
    228   except ValueError, detail:
    229     print prefix,detail
    230     sys.exit(1)
    231 
    232   if 0 <= n <= 255:
    233     return n
    234   else:
    235 
    236     if prefix:
    237       number = prefix +' : ' + number
    238 
    239     print '%s is not a valid number, must be between 0 and 255' %number
    240     sys.exit(1)
     219def check_number(number, network, option_str=''):
     220        """
     221        This functions checks if the input is between 0 < number < 255:
     222        number : is a string
     223        prefix : a string that must be displayed if an error occurs
     224        """
     225        try:
     226                n = int(number)
     227        except ValueError, detail:
     228                print option_str, detail
     229                sys.exit(1)
     230
     231        if not network:
     232                return n
     233
     234        # Check if it is a correct network value
     235        #
     236        if 0 <= n <= 255:
     237                return n
     238        else:
     239                if option_str:
     240                        number = option_str +' : ' + number
     241                       
     242                print '%s is not a valid network number, must be between 0 and 255' %number
     243                sys.exit(1)
    241244
    242245def interactive(binfo):
    243 
    244   print __doc__
    245 
    246   network = raw_input('Give network address (xxx.xxx.xxx): ')
    247   naddr = convert_network(network)
    248 
    249   start = raw_input('Starting number: ')
    250   start = check_number(start)
    251 
    252   end = raw_input('Ending number: ')
    253   end = check_number(end)
    254 
    255   pxe_filename = select_pxe_configfile()
    256 
    257   binfo[NETWORK] = naddr
    258   binfo[START] = int(start)
    259   binfo[END] = int(end)
    260   binfo[FILENAME] = pxe_filename
    261 
    262   if DEBUG:
    263     print network, binfo
    264 
    265 def check_cmd_line(binfo):
    266   if len(binfo.keys()) != 5:
    267     print __doc__
    268     print 'Not enough arguments to create the links!!'
    269     sys.exit(1)
    270 
    271   # check_filename
    272   #
    273   if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])):
    274     print '%s: Filename does not exists' %binfo[FILENAME]
    275     sys.exit(1)
     246        print __doc__
     247       
     248        network = raw_input('Give network address (xxx.xxx.xxx): ')
     249        naddr = convert_network(network)
     250       
     251        start = raw_input('Starting number: ')
     252        start = check_number(start, True)
     253       
     254        end = raw_input('Ending number: ')
     255        end = check_number(end, True)
     256       
     257        pxe_filename = select_pxe_configfile()
     258       
     259        binfo[NETWORK] = naddr
     260        binfo[START] = start
     261        binfo[END] = end
     262        binfo[FILENAME] = pxe_filename
     263       
     264        if DEBUG:
     265                print network, binfo
     266
     267        manage_links(binfo)
     268
     269def check_command_line(binfo, hostnames):
     270        """
     271        Do you we have the right and propper values
     272        """
     273        ### check_filename
     274        #
     275        try:
     276                if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])):
     277                        print '%s: Filename does not exists' %binfo[FILENAME]
     278                        sys.exit(1)
     279        except KeyError, detail:
     280                binfo[FILENAME] = select_pxe_configfile()
     281
     282        if hostnames:
     283                host_2_net(hostnames, binfo)
     284                sys.exit(0)
     285               
     286        if binfo.has_key(BASENAME) and binfo.has_key(NETWORK):
     287                print __doc__
     288                print "The option -n/--net and -b/--basename are mutually exclusive"
     289                sys.exit(1)
     290
     291        if binfo.has_key(BASENAME):
     292                network_number = False
     293                create_links =  base_2_net
     294
     295        elif binfo.has_key(NETWORK):
     296                network_number = True
     297                create_links = manage_links
     298        else:
     299                print __doc__
     300                sys.exit(1)
     301
     302        try:
     303                binfo[START] = check_number(binfo[START], network_number, '-s/--start')
     304        except KeyError, detail:
     305                print __doc__
     306                print '-s/--start is missing on the command line' %(detail)
     307                sys.exit(1)
     308
     309        try:
     310                binfo[END] = check_number(binfo[END], network_number, '-e/--end')
     311        except KeyError, detail:
     312                print __doc__
     313                print '-e/--end is missing on the command line' %(detail)
     314                sys.exit(1)
     315
     316        if DEBUG:
     317                print binfo
     318
     319        create_links(binfo)
     320
    276321
    277322def check_args(argv, binfo):
     
    292337        global DEBUG
    293338
     339        # if nothing is specified then print usage and exit
     340        #
     341        if not opts and not args:
     342                print __doc__
     343                sys.exit(1)
     344
    294345        # Check given options
    295346        #
    296347        for opt,value in opts:
    297                 if opt in ['-i', '--interactive']:
    298                         binfo[INTERACTIVE] = 1
    299                        
    300                 elif opt in ['-b', '--basename']:
     348                       
     349                if opt in ['-b', '--basename']:
    301350                        binfo[BASENAME] = value
    302351                       
     
    305354                       
    306355                elif opt in ['-e', '--end']:
    307                         binfo[END] = check_number(value, opt)
     356                        binfo[END] = value
    308357                       
    309358                elif opt in ['-f', '--file']:
     
    313362                        print __doc__
    314363                        sys.exit(0)
    315                        
     364
     365                elif opt in ['-i', '--interactive']:
     366                        interactive(binfo)
     367                        sys.exit(0)
     368
    316369                elif opt in ['-n', '--net']:
    317370                        network = value
     
    322375                       
    323376                elif opt in ['-s', '--start']:
    324                         binfo[START] = check_number(value, opt)
     377                        binfo[START] = value
    325378
    326379                elif opt in ['-V', '--version']:
    327380                        print VERSION
    328381                        sys.exit(0)
    329                        
    330         if args:
    331                 return args
     382
     383        check_command_line(binfo, args)
    332384
    333385def host_2_net(hosts, binfo):
     
    354406        Construct hostname(s) from the supplied basename and start and end numbers
    355407        """
    356         try:
    357                 if binfo[START] >= binfo[END]:
    358                         print __doc__
    359                         print "Supplied wrong values for start and end"
    360                         sys.exit(1)
    361         except KeyError, detail:
    362                 print __doc__
    363                 print '%s is missing on the command line' %(detail)
     408        if binfo[START] >= binfo[END]:
     409                print __doc__
     410                print "Supplied wrong values for start (%d) and end (%d)" %(binfo[START], binfo[END])
    364411                sys.exit(1)
    365412
     
    368415                hostname = '%s%d' %(binfo[BASENAME], i)
    369416                if DEBUG:
    370                         print 'host =%s, Basename = %s, number = %d' %(hostname, binfo[BASENAME], i)
     417                        print 'host = %s, Basename = %s, number = %d' %(hostname, binfo[BASENAME], i)
    371418                hostnames.append(hostname)
     419
    372420        host_2_net(hostnames,binfo)
    373421               
     
    385433        try:
    386434                PXE_CONF_DIR = settings['pxe_config_dir']
    387                 DEBUG = int(settings['debug'])
     435                if not DEBUG:
     436                        DEBUG = int(settings['debug'])
    388437        except KeyError:
    389438                pass
    390        
    391         hostnames = check_args(sys.argv, bootinfo)
    392         print hostnames
    393        
    394         if bootinfo[REMOVE]:
    395                 bootinfo[FILENAME] = 'remove_boot_file'
    396                
    397         # If we have args then make links for the supplied hosts
    398         # else make links for class C-networks
    399         #
    400         if hostnames:
    401                 if not bootinfo.has_key(FILENAME):
    402                         bootinfo[FILENAME] = select_pxe_configfile()
    403                 host_2_net(hostnames,bootinfo)
    404 
    405         elif bootinfo.has_key(BASENAME):
    406                 if not bootinfo.has_key(FILENAME):
    407                         bootinfo[FILENAME] = select_pxe_configfile()
    408                 base_2_net(bootinfo)
    409                
    410         else:
    411                 if bootinfo.has_key(INTERACTIVE):   
    412                         interactive(bootinfo)
    413                 else:
    414                         check_cmd_line(bootinfo)
    415                 manage_links(bootinfo)
    416 
     439
     440        check_args(sys.argv, bootinfo)
     441       
    417442if __name__ == '__main__':
    418443        main()
Note: See TracChangeset for help on using the changeset viewer.