source: trunk/pxeconfig/pxeconfig @ 15

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

Added a Changelog for the distribution and fix an error when pxeconfig runs
a second time to change the pxe boot file.

File size: 5.1 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
[15]9#  $Date: 2002/08/16 05:55:13 $
10#  $Revision: 1.8 $
[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)
[15]130
131    if os.path.exists(haddr):
132       os.unlink(haddr)
133
[2]134    os.symlink(pxe_filename, haddr)
135
136
137def check_network(net):
138  """
[9]139  This function checks if the give network is a Class C-network and will
[2]140  convert the network address to a hex address if true.
141  """
142  d = string.split(net, '.')
143
144  if len(d) != 3:
145    print('Only C-class nerwork addresses are supported!!!')
146    sys.exit(1)
147
148  # Check if we have valid network values
149  r = ''
150  for i in d:
151    i = check_number(i)
152    r = '%s%02X' %(r,i)
153
154  if DEBUG:
155    print r
156
157  return r
158
159def check_number(number):
160  """
161  This functions checks if the input is between 0 < number < 255:
162     number : is a string
163  """
164  try:
165    n = int(number)
166  except ValueError, detail:
167    print detail
168    sys.exit(1)
169
170  if 0 <= n <= 255:
171    return n
172  else:
173    print '%s is not a valid number, must be between 0 and 255' %number
174    sys.exit(1)
175
176def interactive():
177
178  iptable = {}
179
180  print __doc__
181
[9]182  network = raw_input('Give network address (xxx.xxx.xxx): ') 
[2]183  naddr = check_network(network)
184
185  start = raw_input('Starting number: ') 
186  start = check_number(start)
187
188  end = raw_input('Ending number: ') 
189  end = check_number(end)
190
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
[6]204
[3]205def check_args(argv):
206  """
[6]207  command line option:
208    -d/--directory <dir>
209      Where <dir> is the directory where the pxe config files reside.
[3]210  """
[4]211  global PXE_CONF_DIR
[3]212  try:
213    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
214  except getopt.error, detail:
[6]215    print check_args.__doc__
[3]216    print detail
217    sys.exit(1)
218
219  if opts:
[4]220    opt, PXE_CONF_DIR = opts[0]
[3]221
[4]222  if not os.path.isdir(PXE_CONF_DIR):
223    print 'Directory %s does not exists' %PXE_CONF_DIR
224    sys.exit(1)
225
[2]226def main():
[3]227  check_args(sys.argv)
[2]228  interactive()
229
230if __name__ == '__main__':
231  main()
Note: See TracBrowser for help on using the repository browser.