source: trunk/pxeconfig/pxeconfig @ 14

Last change on this file since 14 was 14, checked in by sscpbas, 22 years ago

Placed alle files under GPL license. So everybody can use it.

File size: 5.0 KB
RevLine 
[2]1#!/usr/bin/env python
2#
3# Author: Bas van der Vlies <basv@sara.nl>
4# Date  : 16 February 2002
5#
6# Tester: Walter de Jong <walter@sara.nl>
7#
8# CVS info
[14]9#  $Date: 2002/08/15 06:36:47 $
10#  $Revision: 1.7 $
[2]11#
[14]12# Copyright (C) 2002
[6]13#
[14]14# This file is part of the pxeconfig utils
[6]15#
[14]16# This program is free software; you can redistribute it and/or modify it
17# under the terms of the GNU General Public License as published by the
18# Free Software Foundation; either version 2, or (at your option) any
19# later version.
[6]20#
[14]21# This program is distributed in the hope that it will be useful,
22# but WITHOUT ANY WARRANTY; without even the implied warranty of
23# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24# GNU General Public License for more details.
[6]25#
[14]26# You should have received a copy of the GNU General Public License
27# along with this program; if not, write to the Free Software
28# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
29#
[2]30"""
[10]31Usage: pxeconfig [-d|--directory <pxe_config_dir> ]
32
[2]33With this program you can configure which PXE configuration file
[9]34to use when a node boots. The program will ask the following questions:
[2]35  1) Network address (Class C-network address only)
[9]36  2) Starting number
[2]37  3) Ending number
[9]38  4) Which PXE config file to use
[2]39
40For example, if the answers are:
41  1) 10.168.44
42  2) 2
43  3) 4
44  4) default.node_install
45
46Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg:
47  0AA82C02 ----> default.node_install
48  0AA82C03 ----> default.node_install
49  0AA82C04 ----> default.node_install
50"""
51
52import string
53import sys
54import os
55import glob
[3]56import getopt
[2]57
58# DEBUG
59#
60DEBUG=0
61
62# Constants
63#
[4]64PXE_CONF_DIR='/tftpboot/pxelinux.cfg'
[2]65NADDR=0
66PXEFILE=1
67START=2
68END=3
[3]69SHORTOPT_LIST='d:'
70LONGOPT_LIST=['directory=']
[2]71
72def choice_pxe_configfile():
73  """
[9]74  Let user choose which pxeconfig file to use.
[2]75  """
76
[4]77  os.chdir(PXE_CONF_DIR)
[2]78
[9]79  # Try to determine to which file the default symlink points, and
80  # if it exists, remove it from the list.
[2]81  #
82  try:
83    default_file = os.readlink('default')
84  except OSError:
85    default_file = None
86    pass
87
88  files = glob.glob('default.*')
89  if not files:
90    print 'There are no pxe config files starting with: default.'
91    sys.exit(1)
92
93  if default_file:
94    files.remove(default_file)
95 
96
97  print 'Which pxe config file must we use: ?' 
98  i = 1   
99  for file in files:
100    print "%d : %s" %(i,file)
101    i = i +1
102
103  while 1:
104    index = raw_input('Choice a number: ')
105    try:
106      index = int(index)
107    except ValueError:
108      index = len(files) + 1
109
110    # Is the user smart enough to select
[9]111    # the right value??
[2]112    #
113    if 0 < index <= len(files): break
114
115  return files[index-1]
116
117def create_links(list):
118  """
[4]119  Create the links in the PXE_CONF_DIR,
[9]120    list : A list containing: network hex address, pxe config file,
[2]121           start number, end number
122  """
[4]123  os.chdir(PXE_CONF_DIR)
[2]124  naddr = list[NADDR]
125  pxe_filename = list[PXEFILE]
126  for i in range(list[START], list[END]+1):
127    haddr = '%s%02X' %(naddr, i)
128    if DEBUG:
129      print 'linking %s to %s' %(haddr, pxe_filename)
130    os.symlink(pxe_filename, haddr)
131
132
133def check_network(net):
134  """
[9]135  This function checks if the give network is a Class C-network and will
[2]136  convert the network address to a hex address if true.
137  """
138  d = string.split(net, '.')
139
140  if len(d) != 3:
141    print('Only C-class nerwork addresses are supported!!!')
142    sys.exit(1)
143
144  # Check if we have valid network values
145  r = ''
146  for i in d:
147    i = check_number(i)
148    r = '%s%02X' %(r,i)
149
150  if DEBUG:
151    print r
152
153  return r
154
155def check_number(number):
156  """
157  This functions checks if the input is between 0 < number < 255:
158     number : is a string
159  """
160  try:
161    n = int(number)
162  except ValueError, detail:
163    print detail
164    sys.exit(1)
165
166  if 0 <= n <= 255:
167    return n
168  else:
169    print '%s is not a valid number, must be between 0 and 255' %number
170    sys.exit(1)
171
172def interactive():
173
174  iptable = {}
175
176  print __doc__
177
[9]178  network = raw_input('Give network address (xxx.xxx.xxx): ') 
[2]179  naddr = check_network(network)
180
181  start = raw_input('Starting number: ') 
182  start = check_number(start)
183
184  end = raw_input('Ending number: ') 
185  end = check_number(end)
186
187  pxe_filename = choice_pxe_configfile()
188
189  iptable[network] = []
190  iptable[network].append(naddr)
191  iptable[network].append(pxe_filename)
192  iptable[network].append(int(start))
193  iptable[network].append(int(end))
194
195  for net in iptable.keys():
196    if DEBUG:
197      print net, iptable[net]   
198    create_links(iptable[net])
199
[6]200
[3]201def check_args(argv):
202  """
[6]203  command line option:
204    -d/--directory <dir>
205      Where <dir> is the directory where the pxe config files reside.
[3]206  """
[4]207  global PXE_CONF_DIR
[3]208  try:
209    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
210  except getopt.error, detail:
[6]211    print check_args.__doc__
[3]212    print detail
213    sys.exit(1)
214
215  if opts:
[4]216    opt, PXE_CONF_DIR = opts[0]
[3]217
[4]218  if not os.path.isdir(PXE_CONF_DIR):
219    print 'Directory %s does not exists' %PXE_CONF_DIR
220    sys.exit(1)
221
[2]222def main():
[3]223  check_args(sys.argv)
[2]224  interactive()
225
226if __name__ == '__main__':
227  main()
Note: See TracBrowser for help on using the repository browser.