Changeset 19 for trunk


Ignore:
Timestamp:
09/27/02 16:47:36 (20 years ago)
Author:
sscpbas
Message:

pxeconfig:

Added command line options so that we can use the program interactivly
and via command line

changed the info structure of a lot of functions. So the functions can
report different error messages for interactive usage and command line
usage.

Location:
trunk/pxeconfig
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/pxeconfig/changelog

    r18 r19  
    1 Always edit this file when changes are commited:
     1Always edit this file when changes are commited::
    22
    33$Log: changelog,v $
     4Revision 1.17  2002/09/27 14:47:36  sscpbas
     5pxeconfig:
     6  Added command line options so that we can use the program interactivly
     7  and via command line
     8
     9  changed the info structure of a lot of functions. So the functions can
     10  report different error messages for interactive usage and command line
     11  usage.
     12
    413Revision 1.16  2002/09/11 12:59:19  sscpbas
    514Change some header info for hexls
  • trunk/pxeconfig/pxeconfig

    r16 r19  
    77#
    88# CVS info
    9 #  $Date: 2002/08/16 07:43:21 $
    10 #  $Revision: 1.9 $
     9#  $Date: 2002/09/27 14:47:36 $
     10#  $Revision: 1.10 $
    1111#
    1212# Copyright (C) 2002
     
    2929#
    3030"""
    31 Usage: pxeconfig [-d|--directory <pxe_config_dir> ]
    32 
    33 With this program you can configure which PXE configuration file
    34 to use when a node boots. The program will ask the following questions:
     31Usage: pxeconfig
     32 [-d|--directory <pxe_config_dir>]
     33 [-n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>]
     34
     35With this program you can configure which PXE configuration file a node
     36will use when it boots. The program can be started interactivly or via
     37command line options.
     38
     39When this program is started interactive it will ask the following questions:
    3540  1) Network address (Class C-network address only)
    3641  2) Starting number
     
    6368#
    6469PXE_CONF_DIR='/tftpboot/pxelinux.cfg'
    65 NADDR=0
    66 PXEFILE=1
    67 START=2
    68 END=3
    69 SHORTOPT_LIST='d:'
    70 LONGOPT_LIST=['directory=']
    71 
    72 def choice_pxe_configfile():
     70NETWORK='network'
     71FILENAME='filename'
     72START='start'
     73END='end'
     74
     75SHORTOPT_LIST='d:n:s:e:f:'
     76LONGOPT_LIST=['directory=', 'net=', 'start=', 'end=', 'file=']
     77
     78def select_pxe_configfile():
    7379  """
    7480  Let user choose which pxeconfig file to use.
     
    115121  return files[index-1]
    116122
    117 def create_links(list):
     123def create_links(dict):
    118124  """
    119125  Create the links in the PXE_CONF_DIR,
     
    122128  """
    123129  os.chdir(PXE_CONF_DIR)
    124   naddr = list[NADDR]
    125   pxe_filename = list[PXEFILE]
    126   for i in range(list[START], list[END]+1):
     130  naddr = dict[NETWORK]
     131  pxe_filename = dict[FILENAME]
     132  for i in range(dict[START], dict[END]+1):
    127133    haddr = '%s%02X' %(naddr, i)
    128134    if DEBUG:
     
    135141
    136142
    137 def check_network(net):
     143def check_network(net, prefix=''):
    138144  """
    139145  This function checks if the give network is a Class C-network and will
     
    143149
    144150  if len(d) != 3:
    145     print('Only C-class nerwork addresses are supported!!!')
    146     sys.exit(1)
     151    if prefix:
     152      net = prefix + ' : ' + net
     153     
     154    print '%s is not a valid  C-class network address!!!' %net
     155    sys.exit(1)
     156
     157
     158  # Display right message for interactive/commandline
     159  if prefix:
     160    prefix = prefix + ' ' + net
     161  else:
     162    prefix = net
    147163
    148164  # Check if we have valid network values
    149165  r = ''
    150166  for i in d:
    151     i = check_number(i)
     167    i = check_number(i, prefix)
    152168    r = '%s%02X' %(r,i)
    153169
     
    157173  return r
    158174
    159 def check_number(number):
     175def check_number(number, prefix=''):
    160176  """
    161177  This functions checks if the input is between 0 < number < 255:
    162178     number : is a string
     179     prefix ; a string that must be displayed if an error occurs
    163180  """
    164181  try:
    165182    n = int(number)
    166183  except ValueError, detail:
    167     print detail
     184    print prefix,detail
    168185    sys.exit(1)
    169186
     
    171188    return n
    172189  else:
     190
     191    if prefix:
     192      number = prefix +' : ' + number
     193
    173194    print '%s is not a valid number, must be between 0 and 255' %number
    174195    sys.exit(1)
    175196
    176 def interactive():
    177 
    178   iptable = {}
     197def interactive(binfo):
    179198
    180199  print __doc__
     
    189208  end = check_number(end)
    190209
    191   pxe_filename = choice_pxe_configfile()
    192 
    193   iptable[network] = []
    194   iptable[network].append(naddr)
    195   iptable[network].append(pxe_filename)
    196   iptable[network].append(int(start))
    197   iptable[network].append(int(end))
    198 
    199   for net in iptable.keys():
    200     if DEBUG:
    201       print net, iptable[net]   
    202     create_links(iptable[net])
    203 
    204 
    205 def check_args(argv):
     210  pxe_filename = select_pxe_configfile()
     211
     212  binfo[NETWORK] = naddr
     213  binfo[START] = int(start)
     214  binfo[END] = int(end)
     215  binfo[FILENAME] = pxe_filename
     216
     217  if DEBUG:
     218    print network, binfo
     219
     220def check_cmd_line(binfo):
     221  if len(binfo.keys()) != 4:
     222    print 'Not enough arguments to create the links'
     223    print __doc__
     224    sys.exit(1)
     225
     226  # check_filename
     227  #
     228  if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])):
     229    print '%s: Filename does not exists' %binfo[FILENAME]
     230    sys.exit(1)
     231
     232def check_args(argv, binfo):
    206233  """
    207234  command line option:
    208     -d/--directory <dir>
     235    -d|--directory <dir>
    209236      Where <dir> is the directory where the pxe config files reside.
    210237  """
    211238  global PXE_CONF_DIR
     239
    212240  try:
    213241    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
     
    217245    sys.exit(1)
    218246
    219   if opts:
    220     opt, PXE_CONF_DIR = opts[0]
    221 
    222   if not os.path.isdir(PXE_CONF_DIR):
    223     print 'Directory %s does not exists' %PXE_CONF_DIR
    224     sys.exit(1)
     247  # Check given options
     248  #
     249  for opt,value in opts:
     250    if opt in ['-d', '--directory']:
     251      if os.path.isdir(value):
     252        PXE_CONF_DIR = value
     253      else:
     254        print 'Directory %s does not exists\n' %value
     255        sys.exit(1)
     256
     257    elif opt in ['-n', '--net']:
     258      network = value
     259      binfo[NETWORK] = check_network(value, opt)
     260
     261    elif opt in ['-s', '--start']:
     262      binfo[START] = check_number(value, opt)
     263
     264    elif opt in ['-e', '--end']:
     265      binfo[END] = check_number(value, opt)
     266
     267    elif opt in ['-f', '--file']:
     268      binfo[FILENAME] = value
     269
    225270
    226271def main():
    227   check_args(sys.argv)
    228   interactive()
     272  # A dictionary holding the boot info
     273  #
     274  bootinfo = {}
     275  check_args(sys.argv, bootinfo)
     276
     277  if bootinfo:   
     278    check_cmd_line(bootinfo)
     279  else:
     280    interactive(bootinfo)
     281
     282  create_links(bootinfo)
    229283
    230284if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.