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 |
---|
9 | # $Date: 2002/08/16 07:43:21 $ |
---|
10 | # $Revision: 1.9 $ |
---|
11 | # |
---|
12 | # Copyright (C) 2002 |
---|
13 | # |
---|
14 | # This file is part of the pxeconfig utils |
---|
15 | # |
---|
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. |
---|
20 | # |
---|
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. |
---|
25 | # |
---|
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 | # |
---|
30 | """ |
---|
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: |
---|
35 | 1) Network address (Class C-network address only) |
---|
36 | 2) Starting number |
---|
37 | 3) Ending number |
---|
38 | 4) Which PXE config file to use |
---|
39 | |
---|
40 | For example, if the answers are: |
---|
41 | 1) 10.168.44 |
---|
42 | 2) 2 |
---|
43 | 3) 4 |
---|
44 | 4) default.node_install |
---|
45 | |
---|
46 | Then 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 | |
---|
52 | import string |
---|
53 | import sys |
---|
54 | import os |
---|
55 | import glob |
---|
56 | import getopt |
---|
57 | |
---|
58 | # DEBUG |
---|
59 | # |
---|
60 | DEBUG=0 |
---|
61 | |
---|
62 | # Constants |
---|
63 | # |
---|
64 | PXE_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(): |
---|
73 | """ |
---|
74 | Let user choose which pxeconfig file to use. |
---|
75 | """ |
---|
76 | |
---|
77 | os.chdir(PXE_CONF_DIR) |
---|
78 | |
---|
79 | # Try to determine to which file the default symlink points, and |
---|
80 | # if it exists, remove it from the list. |
---|
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('Select 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 |
---|
111 | # the right value?? |
---|
112 | # |
---|
113 | if 0 < index <= len(files): break |
---|
114 | |
---|
115 | return files[index-1] |
---|
116 | |
---|
117 | def create_links(list): |
---|
118 | """ |
---|
119 | Create the links in the PXE_CONF_DIR, |
---|
120 | list : A list containing: network hex address, pxe config file, |
---|
121 | start number, end number |
---|
122 | """ |
---|
123 | os.chdir(PXE_CONF_DIR) |
---|
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 | |
---|
131 | if os.path.exists(haddr): |
---|
132 | os.unlink(haddr) |
---|
133 | |
---|
134 | os.symlink(pxe_filename, haddr) |
---|
135 | |
---|
136 | |
---|
137 | def check_network(net): |
---|
138 | """ |
---|
139 | This function checks if the give network is a Class C-network and will |
---|
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 | |
---|
159 | def 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 | |
---|
176 | def interactive(): |
---|
177 | |
---|
178 | iptable = {} |
---|
179 | |
---|
180 | print __doc__ |
---|
181 | |
---|
182 | network = raw_input('Give network address (xxx.xxx.xxx): ') |
---|
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 | |
---|
204 | |
---|
205 | def check_args(argv): |
---|
206 | """ |
---|
207 | command line option: |
---|
208 | -d/--directory <dir> |
---|
209 | Where <dir> is the directory where the pxe config files reside. |
---|
210 | """ |
---|
211 | global PXE_CONF_DIR |
---|
212 | try: |
---|
213 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
214 | except getopt.error, detail: |
---|
215 | print check_args.__doc__ |
---|
216 | print detail |
---|
217 | sys.exit(1) |
---|
218 | |
---|
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) |
---|
225 | |
---|
226 | def main(): |
---|
227 | check_args(sys.argv) |
---|
228 | interactive() |
---|
229 | |
---|
230 | if __name__ == '__main__': |
---|
231 | main() |
---|