Changeset 104 for trunk/pxeconfig.in


Ignore:
Timestamp:
09/21/07 17:41:50 (17 years ago)
Author:
bas
Message:

pxeconfig.in:

  • Added -b/--basename to getopt
  • Improved code layout for some function, ts=4
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pxeconfig.in

    r103 r104  
    11#!@PYTHON@
     2#
     3# set ts=4, sw=4
    24#
    35# Author: Bas van der Vlies <basv@sara.nl>
     
    3335 [-i|--interactive]
    3436 [-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>]
    3538 [-r|--remove]
    3639 [-V|--version]
     
    6871# DEBUG
    6972#
    70 DEBUG=0
     73DEBUG=1
    7174
    7275# Constants
     
    7477PXE_CONF_DIR='/tftpboot/pxelinux.cfg'
    7578NETWORK='network'
     79BASENAME='basename'
    7680FILENAME='filename'
    7781START='start'
     
    7983REMOVE='remove'
    8084INTERACTIVE='interactive'
    81 VERSION=0.8.0
    82 
    83 SHORTOPT_LIST='hVn:s:e:f:ri'
    84 LONGOPT_LIST=['help', 'version', 'net=', 'start=', 'end=', 'file=', 'remove', 'debug' , 'interactive' ]
     85VERSION='0.8.0'
     86
     87SHORTOPT_LIST='be:f:hin:s:rV'
     88LONGOPT_LIST=['basename=', 'debug', 'end=', 'file=', 'help', 'interactive', 'net=', 'start=', 'remove', 'version' ]
    8589
    8690def ReadConfig(file):
     
    272276
    273277def check_args(argv, binfo):
    274   """
    275   This function parses the command line options and returns the rest as
    276   an list of hostnames:
    277     argv     : a list of command line options.
    278     binfo    : returning a dict with the netinfo. if used non-interactively
    279     hostnames: the rest of the command lines options that are not-parseble.
    280   """
    281   try:
    282     opts, args = getopt.gnu_getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
    283   except getopt.error, detail:
    284     print __doc__
    285     print detail
    286     sys.exit(1)
    287 
    288   # Check given options
    289   #
    290   for opt,value in opts:
    291     if opt in ['-i', '--interactive']:
    292       binfo[INTERACTIVE] = 1
    293 
    294     elif opt in ['-n', '--net']:
    295       network = value
    296       binfo[NETWORK] = convert_network(value, opt)
    297 
    298     elif opt in ['-s', '--start']:
    299       binfo[START] = check_number(value, opt)
    300 
    301     elif opt in ['-e', '--end']:
    302       binfo[END] = check_number(value, opt)
    303 
    304     elif opt in ['-f', '--file']:
    305       binfo[FILENAME] = value
    306 
    307     elif opt in ['-V', '--version']:
    308       print VERSION
    309       sys.exit(0)
    310 
    311     elif opt in ['-r', '--remove']:
    312       binfo[REMOVE] = 1
    313 
    314     elif opt in ['--debug']:
    315       DEBUG = 1
    316 
    317     elif opt in ['-h', '--help']:
    318       print __doc__
    319       sys.exit(0)
    320 
    321   if args:
    322         return args
    323 
    324 def hosts_links(hosts, binfo):
     278        """
     279        This function parses the command line options and returns the rest as
     280        an list of hostnames:
     281        argv     : a list of command line options.
     282        binfo    : returning a dict with the netinfo. if used non-interactively
     283        hostnames: the rest of the command lines options that are not-parseble.
     284        """
     285        try:
     286                opts, args = getopt.gnu_getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
     287        except getopt.error, detail:
     288                print __doc__
     289                print detail
     290                sys.exit(1)
     291       
     292        global DEBUG
     293
     294        # Check given options
     295        #
     296        for opt,value in opts:
     297                if opt in ['-i', '--interactive']:
     298                        binfo[INTERACTIVE] = 1
     299                       
     300                elif opt in ['-b', '--basename']:
     301                        binfo[BASENAME] = value
     302                       
     303                elif opt in ['--debug']:
     304                        DEBUG = 1
     305                       
     306                elif opt in ['-e', '--end']:
     307                        binfo[END] = check_number(value, opt)
     308                       
     309                elif opt in ['-f', '--file']:
     310                        binfo[FILENAME] = value
     311                       
     312                elif opt in ['-h', '--help']:
     313                        print __doc__
     314                        sys.exit(0)
     315                       
     316                elif opt in ['-n', '--net']:
     317                        network = value
     318                        binfo[NETWORK] = convert_network(value, opt)
     319                       
     320                elif opt in ['-r', '--remove']:
     321                        binfo[REMOVE] = 1
     322                       
     323                elif opt in ['-s', '--start']:
     324                        binfo[START] = check_number(value, opt)
     325
     326                elif opt in ['-V', '--version']:
     327                        print VERSION
     328                        sys.exit(0)
     329                       
     330        if args:
     331                return args
     332
     333def host_2_net(hosts, binfo):
     334        """
     335        Convert hostsname to a net address that can be handled by manage_links function
     336        """
    325337        for host in hosts:
    326338                try:
     
    338350                manage_links(binfo)
    339351
    340 
     352def base_2_net(binfo):
     353        """
     354        Construct hostname(s) from the supplied basename and start and end numbers
     355        """
     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)
     364                sys.exit(1)
     365
     366        hostnames = list()
     367        for i in xrange(binfo[START], binfo[END] + 1):
     368                hostname = '%s%d' %(binfo[BASENAME], i)
     369                if DEBUG:
     370                        print 'host =%s, Basename = %s, number = %d' %(hostname, binfo[BASENAME], i)
     371                hostnames.append(hostname)
     372        host_2_net(hostnames,binfo)
     373               
    341374def main():
    342   # A dictionary holding the boot info
    343   #
    344   global PXE_CONF_DIR
    345   global DEBUG
    346 
    347   bootinfo = {}
    348   bootinfo[REMOVE] = 0
    349 
    350   configfile = '@pxeconfig_conf@'
    351   settings = ReadConfig(configfile)
    352 
    353   try:
    354         PXE_CONF_DIR = settings['pxe_config_dir']
    355         DEBUG = int(settings['debug'])
    356   except KeyError:
    357         pass
    358 
    359   hostnames = check_args(sys.argv, bootinfo)
    360 
    361   if bootinfo[REMOVE]:
    362      bootinfo[FILENAME] = 'remove_boot_file'
    363 
    364 
    365   # If we have args then make links for the supplied hosts
    366   # else make links for class C-networks
    367   #
    368   if hostnames:
    369         if not bootinfo.has_key(FILENAME):
    370               bootinfo[FILENAME] = select_pxe_configfile()
    371         hosts_links(hostnames,bootinfo)
    372   else:
    373         if bootinfo.has_key(INTERACTIVE):   
    374                 interactive(bootinfo)
    375         else:
    376                 check_cmd_line(bootinfo)
    377         manage_links(bootinfo)
     375        # A dictionary holding the boot info
     376        #
     377        global DEBUG
     378       
     379        bootinfo = {}
     380        bootinfo[REMOVE] = 0
     381       
     382        configfile = '@pxeconfig_conf@'
     383        settings = ReadConfig(configfile)
     384       
     385        try:
     386                PXE_CONF_DIR = settings['pxe_config_dir']
     387                DEBUG = int(settings['debug'])
     388        except KeyError:
     389                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)
    378416
    379417if __name__ == '__main__':
    380   main()
     418        main()
Note: See TracChangeset for help on using the changeset viewer.