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/02/21 13:21:51 $ |
---|
10 | # $Revision: 1.4 $ |
---|
11 | # |
---|
12 | # Copyright (C) 2000, SARA. |
---|
13 | # |
---|
14 | # Permission to use, copy, modify, and distribute this software and its |
---|
15 | # documentation for non-commercial purposes and without fee is hereby |
---|
16 | # granted, provided that the above copyright notice appears in all copies |
---|
17 | # and that both the copyright notice and this permission notice appear in |
---|
18 | # supporting documentation. |
---|
19 | # |
---|
20 | # Neither SARA (Stichting Academisch Rekencentrum Amsterdam) nor the |
---|
21 | # nor the author make any representations about the suitability of this |
---|
22 | # software for any purpose. This software is provided ``as is'' without |
---|
23 | # express or implied warranty. |
---|
24 | # |
---|
25 | # |
---|
26 | """ |
---|
27 | With this program you can configure which PXE configuration file |
---|
28 | to use when a node boots. The programm will ask the following questions: |
---|
29 | 1) Network address (Class C-network address only) |
---|
30 | 2) Starting nummber |
---|
31 | 3) Ending number |
---|
32 | 4) Which PXE config file to use. |
---|
33 | |
---|
34 | For example, if the answers are: |
---|
35 | 1) 10.168.44 |
---|
36 | 2) 2 |
---|
37 | 3) 4 |
---|
38 | 4) default.node_install |
---|
39 | |
---|
40 | Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg: |
---|
41 | 0AA82C02 ----> default.node_install |
---|
42 | 0AA82C03 ----> default.node_install |
---|
43 | 0AA82C04 ----> default.node_install |
---|
44 | """ |
---|
45 | |
---|
46 | import string |
---|
47 | import sys |
---|
48 | import os |
---|
49 | import glob |
---|
50 | import getopt |
---|
51 | |
---|
52 | # DEBUG |
---|
53 | # |
---|
54 | DEBUG=0 |
---|
55 | |
---|
56 | # Constants |
---|
57 | # |
---|
58 | PXE_CONF_DIR='/tftpboot/pxelinux.cfg' |
---|
59 | NADDR=0 |
---|
60 | PXEFILE=1 |
---|
61 | START=2 |
---|
62 | END=3 |
---|
63 | SHORTOPT_LIST='d:' |
---|
64 | LONGOPT_LIST=['directory='] |
---|
65 | |
---|
66 | def choice_pxe_configfile(): |
---|
67 | """ |
---|
68 | Let user choice which pxeconfig file to use. |
---|
69 | """ |
---|
70 | |
---|
71 | os.chdir(PXE_CONF_DIR) |
---|
72 | |
---|
73 | # Try to determine to which default file point to and |
---|
74 | # if exists remove it from te list. |
---|
75 | # |
---|
76 | try: |
---|
77 | default_file = os.readlink('default') |
---|
78 | except OSError: |
---|
79 | default_file = None |
---|
80 | pass |
---|
81 | |
---|
82 | files = glob.glob('default.*') |
---|
83 | if not files: |
---|
84 | print 'There are no pxe config files starting with: default.' |
---|
85 | sys.exit(1) |
---|
86 | |
---|
87 | if default_file: |
---|
88 | files.remove(default_file) |
---|
89 | |
---|
90 | |
---|
91 | print 'Which pxe config file must we use: ?' |
---|
92 | i = 1 |
---|
93 | for file in files: |
---|
94 | print "%d : %s" %(i,file) |
---|
95 | i = i +1 |
---|
96 | |
---|
97 | while 1: |
---|
98 | index = raw_input('Choice a number: ') |
---|
99 | try: |
---|
100 | index = int(index) |
---|
101 | except ValueError: |
---|
102 | index = len(files) + 1 |
---|
103 | |
---|
104 | # Is the user smart enough to select |
---|
105 | # the right value |
---|
106 | # |
---|
107 | if 0 < index <= len(files): break |
---|
108 | |
---|
109 | return files[index-1] |
---|
110 | |
---|
111 | def create_links(list): |
---|
112 | """ |
---|
113 | Create the links in the PXE_CONF_DIR, |
---|
114 | list : A list containing: network hex addres, pxe config file, |
---|
115 | start number, end number |
---|
116 | """ |
---|
117 | os.chdir(PXE_CONF_DIR) |
---|
118 | naddr = list[NADDR] |
---|
119 | pxe_filename = list[PXEFILE] |
---|
120 | for i in range(list[START], list[END]+1): |
---|
121 | haddr = '%s%02X' %(naddr, i) |
---|
122 | if DEBUG: |
---|
123 | print 'linking %s to %s' %(haddr, pxe_filename) |
---|
124 | os.symlink(pxe_filename, haddr) |
---|
125 | |
---|
126 | |
---|
127 | def check_network(net): |
---|
128 | """ |
---|
129 | This function check if the give network is a Class C-network and will |
---|
130 | convert the network address to a hex address if true. |
---|
131 | """ |
---|
132 | d = string.split(net, '.') |
---|
133 | |
---|
134 | if len(d) != 3: |
---|
135 | print('Only C-class nerwork addresses are supported!!!') |
---|
136 | sys.exit(1) |
---|
137 | |
---|
138 | # Check if we have valid network values |
---|
139 | r = '' |
---|
140 | for i in d: |
---|
141 | i = check_number(i) |
---|
142 | r = '%s%02X' %(r,i) |
---|
143 | |
---|
144 | if DEBUG: |
---|
145 | print r |
---|
146 | |
---|
147 | return r |
---|
148 | |
---|
149 | def check_number(number): |
---|
150 | """ |
---|
151 | This functions checks if the input is between 0 < number < 255: |
---|
152 | number : is a string |
---|
153 | """ |
---|
154 | try: |
---|
155 | n = int(number) |
---|
156 | except ValueError, detail: |
---|
157 | print detail |
---|
158 | sys.exit(1) |
---|
159 | |
---|
160 | if 0 <= n <= 255: |
---|
161 | return n |
---|
162 | else: |
---|
163 | print '%s is not a valid number, must be between 0 and 255' %number |
---|
164 | sys.exit(1) |
---|
165 | |
---|
166 | def interactive(): |
---|
167 | |
---|
168 | iptable = {} |
---|
169 | |
---|
170 | print __doc__ |
---|
171 | |
---|
172 | network = raw_input('Give network addres (xxx.xxx.xxx): ') |
---|
173 | naddr = check_network(network) |
---|
174 | |
---|
175 | start = raw_input('Starting number: ') |
---|
176 | start = check_number(start) |
---|
177 | |
---|
178 | end = raw_input('Ending number: ') |
---|
179 | end = check_number(end) |
---|
180 | |
---|
181 | pxe_filename = choice_pxe_configfile() |
---|
182 | |
---|
183 | iptable[network] = [] |
---|
184 | iptable[network].append(naddr) |
---|
185 | iptable[network].append(pxe_filename) |
---|
186 | iptable[network].append(int(start)) |
---|
187 | iptable[network].append(int(end)) |
---|
188 | |
---|
189 | for net in iptable.keys(): |
---|
190 | if DEBUG: |
---|
191 | print net, iptable[net] |
---|
192 | create_links(iptable[net]) |
---|
193 | |
---|
194 | |
---|
195 | def check_args(argv): |
---|
196 | """ |
---|
197 | command line option: |
---|
198 | -d/--directory <dir> |
---|
199 | Where <dir> is the directory where the pxe config files reside. |
---|
200 | """ |
---|
201 | global PXE_CONF_DIR |
---|
202 | try: |
---|
203 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
204 | except getopt.error, detail: |
---|
205 | print check_args.__doc__ |
---|
206 | print detail |
---|
207 | sys.exit(1) |
---|
208 | |
---|
209 | if opts: |
---|
210 | opt, PXE_CONF_DIR = opts[0] |
---|
211 | |
---|
212 | if not os.path.isdir(PXE_CONF_DIR): |
---|
213 | print 'Directory %s does not exists' %PXE_CONF_DIR |
---|
214 | sys.exit(1) |
---|
215 | |
---|
216 | def main(): |
---|
217 | check_args(sys.argv) |
---|
218 | interactive() |
---|
219 | |
---|
220 | if __name__ == '__main__': |
---|
221 | main() |
---|