1 | #!@PYTHON@ |
---|
2 | # |
---|
3 | # set ts=4, sw=4 |
---|
4 | # |
---|
5 | # Author: Bas van der Vlies <basv@sara.nl> |
---|
6 | # Date : 16 February 2002 |
---|
7 | # |
---|
8 | # Tester: Walter de Jong <walter@sara.nl> |
---|
9 | # |
---|
10 | # SVN info |
---|
11 | # $Id: pxeconfig.in 126 2008-04-14 07:44:08Z bas $ |
---|
12 | # |
---|
13 | # Copyright (C) 2002 |
---|
14 | # |
---|
15 | # This file is part of the pxeconfig utils |
---|
16 | # |
---|
17 | # This program is free software; you can redistribute it and/or modify it |
---|
18 | # under the terms of the GNU General Public License as published by the |
---|
19 | # Free Software Foundation; either version 2, or (at your option) any |
---|
20 | # later version. |
---|
21 | # |
---|
22 | # This program is distributed in the hope that it will be useful, |
---|
23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
25 | # GNU General Public License for more details. |
---|
26 | # |
---|
27 | # You should have received a copy of the GNU General Public License |
---|
28 | # along with this program; if not, write to the Free Software |
---|
29 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
30 | # |
---|
31 | """ |
---|
32 | Usage: pxeconfig |
---|
33 | |
---|
34 | -V,--version |
---|
35 | [-r,--remove] -i,--interactive |
---|
36 | [-r,--remove] -f,--file <filename> <hostname(s)> |
---|
37 | [-r,--remove] -n,--net <C-net> -s,--start <number> -e,--end <number> -f,--file <filename> |
---|
38 | |
---|
39 | there are serveal ways to create a range of hostnames. they all start with: |
---|
40 | * -b,--base-name <string> |
---|
41 | |
---|
42 | The different formats are: |
---|
43 | 1 [opt] -H,--host-range <number>-<number> -f,--file <filename> |
---|
44 | 2 [opt] -s,--start <number> -e,--end <number> -f,--file <filename> |
---|
45 | 3 [opt] -R,--rack <string>-<string> -N,--node <string>-<string> -f,--file <filename> |
---|
46 | |
---|
47 | opt: |
---|
48 | -r,--remove |
---|
49 | -w,--equal-width |
---|
50 | --dry-run |
---|
51 | |
---|
52 | If leading zeros are used for <number>, then host names will be padded |
---|
53 | with zeros. --equal-width option will be enabled. |
---|
54 | |
---|
55 | With this program you can configure which PXE configuration file a node |
---|
56 | will use when it boots. The program can be started interactivly or via |
---|
57 | command line options. |
---|
58 | |
---|
59 | See following link for usage and examples: |
---|
60 | - https://subtrac.sara.nl/oss/pxeconfig/wiki/PxeUsage |
---|
61 | """ |
---|
62 | |
---|
63 | import string |
---|
64 | import sys |
---|
65 | import os |
---|
66 | import glob |
---|
67 | import getopt |
---|
68 | import socket |
---|
69 | import ConfigParser |
---|
70 | import re |
---|
71 | |
---|
72 | # Global Variables |
---|
73 | # |
---|
74 | DEBUG = False |
---|
75 | VERBOSE = False |
---|
76 | DRY_RUN = False |
---|
77 | |
---|
78 | # Constants |
---|
79 | # |
---|
80 | PXE_CONF_DIR='/tftpboot/pxelinux.cfg' |
---|
81 | NETWORK='network' |
---|
82 | BASENAME='basename' |
---|
83 | FILENAME='filename' |
---|
84 | START='start' |
---|
85 | END='end' |
---|
86 | REMOVE='remove' |
---|
87 | RACK='rack' |
---|
88 | NODE='node' |
---|
89 | INTERACTIVE='interactive' |
---|
90 | EQUALWIDTH='equalwidth' |
---|
91 | VERSION='2.0.0' |
---|
92 | |
---|
93 | SHORTOPT_LIST='b:e:f:hin:s:rwvN:H:R:V' |
---|
94 | LONGOPT_LIST=[ 'basename=', 'debug', 'dry-run', 'end=', 'equal-width', |
---|
95 | 'file=', 'help', 'host-range=', 'interactive', 'net=', 'node=', |
---|
96 | 'start=', 'rack=', 'remove', 'verbose', 'version', 'equal-width' |
---|
97 | ] |
---|
98 | |
---|
99 | # regex definition |
---|
100 | # |
---|
101 | # A regulare expression to parse string for rack/node |
---|
102 | id_re = re.compile(r""" |
---|
103 | (?P<basename>[a-zA-Z-_]*) |
---|
104 | (?P<number>[0-9]+) |
---|
105 | """, re.VERBOSE) |
---|
106 | |
---|
107 | def verbose(str): |
---|
108 | if VERBOSE: |
---|
109 | print '%s' %str |
---|
110 | |
---|
111 | class PxeConfig(Exception): |
---|
112 | def __init__(self, msg=''): |
---|
113 | self.msg = msg |
---|
114 | Exception.__init__(self, msg) |
---|
115 | |
---|
116 | def __repr__(self): |
---|
117 | return self.msg |
---|
118 | |
---|
119 | |
---|
120 | def ReadConfig(file): |
---|
121 | """ |
---|
122 | Parse the config file |
---|
123 | """ |
---|
124 | if not os.path.isfile(file): |
---|
125 | print 'File %s does not exist' %file |
---|
126 | sys.exit(1) |
---|
127 | |
---|
128 | config = ConfigParser.RawConfigParser() |
---|
129 | try: |
---|
130 | config.read(file) |
---|
131 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
132 | print detail |
---|
133 | sys.exit(1) |
---|
134 | |
---|
135 | # Not yet uses |
---|
136 | # |
---|
137 | #projects = {} |
---|
138 | #for section in config.sections(): |
---|
139 | # projects[section] = {} |
---|
140 | # for option in config.options(section): |
---|
141 | # projects[section][option] = config.get(section, option) |
---|
142 | |
---|
143 | stanza = config.defaults() |
---|
144 | return stanza |
---|
145 | |
---|
146 | def select_pxe_configfile(): |
---|
147 | """ |
---|
148 | Let user choose which pxeconfig file to use. |
---|
149 | """ |
---|
150 | |
---|
151 | os.chdir(PXE_CONF_DIR) |
---|
152 | |
---|
153 | # Try to determine to which file the default symlink points, and |
---|
154 | # if it exists, remove it from the list. |
---|
155 | # |
---|
156 | try: |
---|
157 | default_file = os.readlink('default') |
---|
158 | except OSError: |
---|
159 | default_file = None |
---|
160 | pass |
---|
161 | |
---|
162 | files = glob.glob('default.*') |
---|
163 | if not files: |
---|
164 | print 'There are no pxe config files starting with: default.' |
---|
165 | sys.exit(1) |
---|
166 | |
---|
167 | if default_file: |
---|
168 | files.remove(default_file) |
---|
169 | |
---|
170 | # sort the files |
---|
171 | # |
---|
172 | files.sort() |
---|
173 | |
---|
174 | print 'Which pxe config file must we use: ?' |
---|
175 | i = 1 |
---|
176 | for file in files: |
---|
177 | print "%d : %s" %(i,file) |
---|
178 | i = i +1 |
---|
179 | |
---|
180 | while 1: |
---|
181 | index = raw_input('Select a number: ') |
---|
182 | try: |
---|
183 | index = int(index) |
---|
184 | except ValueError: |
---|
185 | index = len(files) + 1 |
---|
186 | |
---|
187 | # Is the user smart enough to select |
---|
188 | # the right value?? |
---|
189 | # |
---|
190 | if 0 < index <= len(files): break |
---|
191 | |
---|
192 | return files[index-1] |
---|
193 | |
---|
194 | def manage_links(dict): |
---|
195 | """ |
---|
196 | Create the links in the PXE_CONF_DIR, |
---|
197 | list : A list containing: network hex address, pxe config file, |
---|
198 | start number, end number |
---|
199 | """ |
---|
200 | str = 'manage_links' |
---|
201 | verbose(str) |
---|
202 | |
---|
203 | os.chdir(PXE_CONF_DIR) |
---|
204 | naddr = dict[NETWORK] |
---|
205 | pxe_filename = dict[FILENAME] |
---|
206 | |
---|
207 | for i in range(dict[START], dict[END]+1): |
---|
208 | haddr = '%s%02X' %(naddr, i) |
---|
209 | |
---|
210 | if dict[REMOVE] == True: |
---|
211 | if DEBUG or DRY_RUN: |
---|
212 | print 'removing %s/%s' %(PXE_CONF_DIR, haddr) |
---|
213 | |
---|
214 | if os.path.exists(haddr) and not DRY_RUN: |
---|
215 | os.unlink(haddr) |
---|
216 | |
---|
217 | else: |
---|
218 | if DEBUG or DRY_RUN: |
---|
219 | print 'linking %s to %s' %(haddr, pxe_filename) |
---|
220 | |
---|
221 | if not DRY_RUN: |
---|
222 | if os.path.exists(haddr): |
---|
223 | os.unlink(haddr) |
---|
224 | os.symlink(pxe_filename, haddr) |
---|
225 | |
---|
226 | def net_2_hex(net): |
---|
227 | """ |
---|
228 | This function checks if the give network is a Class C-network and will |
---|
229 | convert the network address to a hex address if true. |
---|
230 | """ |
---|
231 | str = 'net_2_hex : %s' %(net) |
---|
232 | verbose(str) |
---|
233 | |
---|
234 | d = string.split(net, '.') |
---|
235 | if len(d) != 3: |
---|
236 | error = '%s is not a valid C-class network address' %(net) |
---|
237 | raise PxeConfig, error |
---|
238 | |
---|
239 | # Check if we have valid network values |
---|
240 | r = '' |
---|
241 | for i in d: |
---|
242 | i = check_number(i, True) |
---|
243 | r = '%s%02X' %(r,i) |
---|
244 | |
---|
245 | if DEBUG: |
---|
246 | print 'C-network in hex: ', r |
---|
247 | |
---|
248 | return r |
---|
249 | |
---|
250 | def check_number(number_str, network): |
---|
251 | """ |
---|
252 | number : a string. If string starts with a zero (0) then |
---|
253 | EQUALWIDTH wil be set. |
---|
254 | network: if true then number must ben between 0 < number < 255 |
---|
255 | else it must be a valid number. |
---|
256 | """ |
---|
257 | try: |
---|
258 | n = int(number_str) |
---|
259 | except ValueError, detail: |
---|
260 | error = "%s : is not a valid number" %number_str |
---|
261 | raise PxeConfig, error |
---|
262 | |
---|
263 | if not network: |
---|
264 | return n |
---|
265 | |
---|
266 | # Check if it is a correct network value |
---|
267 | # |
---|
268 | if 0 <= n <= 255: |
---|
269 | return n |
---|
270 | else: |
---|
271 | error = '%s is not a valid network number, must be between 0 and 255' %n |
---|
272 | raise PxeConfig, error |
---|
273 | |
---|
274 | def interactive(binfo): |
---|
275 | print __doc__ |
---|
276 | |
---|
277 | |
---|
278 | while 1: |
---|
279 | network = raw_input('Give network address (xxx.xxx.xxx): ') |
---|
280 | |
---|
281 | try: |
---|
282 | naddr = net_2_hex(network) |
---|
283 | break |
---|
284 | except PxeConfig, detail: |
---|
285 | print '%s : not a valid C-class network number' %(network) |
---|
286 | continue |
---|
287 | |
---|
288 | while 1: |
---|
289 | start = raw_input('Starting number: ') |
---|
290 | |
---|
291 | try: |
---|
292 | start = check_number(start, True) |
---|
293 | break |
---|
294 | except PxeConfig, detail: |
---|
295 | print detail |
---|
296 | continue |
---|
297 | |
---|
298 | while 1: |
---|
299 | end = raw_input('Ending number: ') |
---|
300 | |
---|
301 | try: |
---|
302 | end = check_number(end, True) |
---|
303 | break |
---|
304 | except PxeConfig, detail: |
---|
305 | print detail |
---|
306 | continue |
---|
307 | |
---|
308 | |
---|
309 | pxe_filename = select_pxe_configfile() |
---|
310 | |
---|
311 | binfo[NETWORK] = naddr |
---|
312 | binfo[START] = start |
---|
313 | binfo[END] = end |
---|
314 | binfo[FILENAME] = pxe_filename |
---|
315 | |
---|
316 | if DEBUG: |
---|
317 | print network, binfo |
---|
318 | |
---|
319 | manage_links(binfo) |
---|
320 | |
---|
321 | def check_args(binfo, hostnames): |
---|
322 | """ |
---|
323 | Do you we have the right and propper values |
---|
324 | """ |
---|
325 | ### check_filename |
---|
326 | # |
---|
327 | str = 'check_args: ' |
---|
328 | verbose(str) |
---|
329 | try: |
---|
330 | if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])): |
---|
331 | error = '%s: Filename does not exists' %binfo[FILENAME] |
---|
332 | raise Pxeconfig, detail |
---|
333 | except KeyError, detail: |
---|
334 | if binfo[REMOVE] : |
---|
335 | binfo[FILENAME] = 'Does not matter' |
---|
336 | else: |
---|
337 | binfo[FILENAME] = select_pxe_configfile() |
---|
338 | |
---|
339 | if hostnames: |
---|
340 | host_2_hex(hostnames, binfo) |
---|
341 | sys.exit(0) |
---|
342 | |
---|
343 | if binfo.has_key(BASENAME) and binfo.has_key(NETWORK): |
---|
344 | error = "The option -n/--net and -b/--basename are mutually exclusive" |
---|
345 | raise PxeConfig, error |
---|
346 | |
---|
347 | if binfo.has_key(BASENAME): |
---|
348 | if binfo[RACK] and binfo[NODE]: |
---|
349 | create_links = rack_2_net |
---|
350 | else: |
---|
351 | set_padding(binfo) |
---|
352 | create_links = base_2_host |
---|
353 | |
---|
354 | elif binfo.has_key(NETWORK): |
---|
355 | binfo[START] = check_number(binfo[START], True) |
---|
356 | binfo[END] = check_number(binfo[END], True) |
---|
357 | create_links = manage_links |
---|
358 | |
---|
359 | else: |
---|
360 | error = 'You have to specifiy -b,--basename or -n,--net' |
---|
361 | raise PxeConfig, error |
---|
362 | |
---|
363 | if DEBUG: |
---|
364 | print binfo |
---|
365 | |
---|
366 | create_links(binfo) |
---|
367 | |
---|
368 | def set_padding(binfo): |
---|
369 | """ |
---|
370 | binfo : boot info |
---|
371 | network_number : must we check if start,end values are |
---|
372 | valid network numbers |
---|
373 | return: |
---|
374 | - if equal_width is requested then the length will be set to end value |
---|
375 | - if start value length > 1 and start with a zero (0), width is set to |
---|
376 | the end value |
---|
377 | - if end value starts with a zero (0), width will be set to the end |
---|
378 | value |
---|
379 | """ |
---|
380 | start_str = binfo[START] |
---|
381 | end_str = binfo[END] |
---|
382 | |
---|
383 | start = check_number(start_str, False) |
---|
384 | end = check_number(end_str, False) |
---|
385 | |
---|
386 | if binfo[EQUALWIDTH][0] == True: |
---|
387 | binfo[EQUALWIDTH][1] = len(end_str) |
---|
388 | |
---|
389 | elif len(start_str) > 1 and start_str[0] == '0': |
---|
390 | binfo[EQUALWIDTH] = [ True, len(end_str) ] |
---|
391 | |
---|
392 | elif end_str[0] == '0': |
---|
393 | binfo[EQUALWIDTH] = [ True, len(end_str) ] |
---|
394 | |
---|
395 | binfo[START] = start |
---|
396 | binfo[END] = end |
---|
397 | |
---|
398 | |
---|
399 | def parse_number_range(binfo, arg): |
---|
400 | """ |
---|
401 | Parse if arg is of format <digit-digit>, if it starts |
---|
402 | with a zero (0) then set EQUALWIDTH |
---|
403 | """ |
---|
404 | str = 'parse_hostrange %s' %(arg) |
---|
405 | verbose(str) |
---|
406 | |
---|
407 | l = arg.split('-') |
---|
408 | if len(l) < 2: |
---|
409 | error = 'hostrange syntax not valid: %s (number-number)' %(arg) |
---|
410 | raise PxeConfig, error |
---|
411 | |
---|
412 | binfo[START] = l[0] |
---|
413 | binfo[END] = l[1] |
---|
414 | |
---|
415 | def parse_string_range(binfo, arg, id): |
---|
416 | """ |
---|
417 | Parse if arg is of format <(alpha)(digit)>-(alpha)(digit)> |
---|
418 | if digit starts with a zero (0) then set EQUALWIDTH |
---|
419 | """ |
---|
420 | str = 'parse_string_range: %s %s' %(arg, id) |
---|
421 | verbose(str) |
---|
422 | |
---|
423 | l = arg.split('-') |
---|
424 | if len(l) < 2: |
---|
425 | error = '%s : range syntax not valid,eg <string>-<string>)' %(arg) |
---|
426 | raise PxeConfig, error |
---|
427 | |
---|
428 | binfo[id] = dict() |
---|
429 | binfo[id][EQUALWIDTH] = [False, 0] |
---|
430 | |
---|
431 | i = 0 |
---|
432 | for item in l: |
---|
433 | result = id_re.match(item) |
---|
434 | |
---|
435 | if result: |
---|
436 | basename = result.group('basename') |
---|
437 | number = result.group('number') |
---|
438 | |
---|
439 | if DEBUG: |
---|
440 | print 'basename = %s, number = %s' %(basename, number) |
---|
441 | |
---|
442 | if i == 0: |
---|
443 | binfo[id][BASENAME] = basename |
---|
444 | binfo[id][START] = number |
---|
445 | i += 1 |
---|
446 | else: |
---|
447 | binfo[id][END] = number |
---|
448 | |
---|
449 | else: |
---|
450 | error = '%s : string syntax is not valid, eg: <alpa>*<digit>+' %(item) |
---|
451 | raise PxeConfig, error |
---|
452 | |
---|
453 | set_padding(binfo[id]) |
---|
454 | |
---|
455 | def parse_args(argv, binfo): |
---|
456 | """ |
---|
457 | This function parses the command line options and returns the rest as |
---|
458 | an list of hostnames: |
---|
459 | argv : a list of command line options. |
---|
460 | binfo : returning a dict with the netinfo. if used non-interactively |
---|
461 | hostnames: the rest of the command lines options that are not-parseble. |
---|
462 | """ |
---|
463 | try: |
---|
464 | opts, args = getopt.gnu_getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
465 | except getopt.error, detail: |
---|
466 | print __doc__ |
---|
467 | print detail |
---|
468 | sys.exit(1) |
---|
469 | |
---|
470 | global DEBUG |
---|
471 | global VERBOSE |
---|
472 | global DRY_RUN |
---|
473 | |
---|
474 | # if nothing is specified then print usage and exit |
---|
475 | # |
---|
476 | if not opts and not args: |
---|
477 | print __doc__ |
---|
478 | sys.exit(1) |
---|
479 | |
---|
480 | # init vars |
---|
481 | # |
---|
482 | hostrange = node = rack = None |
---|
483 | |
---|
484 | # Check given options |
---|
485 | # |
---|
486 | for opt,value in opts: |
---|
487 | |
---|
488 | if opt in ['-b', '--basename']: |
---|
489 | binfo[BASENAME] = value |
---|
490 | |
---|
491 | elif opt in ['--debug']: |
---|
492 | DEBUG = True |
---|
493 | |
---|
494 | elif opt in ['--dry-run']: |
---|
495 | DRY_RUN = True |
---|
496 | |
---|
497 | elif opt in ['-e', '--end']: |
---|
498 | binfo[END] = value |
---|
499 | |
---|
500 | elif opt in ['-f', '--file']: |
---|
501 | binfo[FILENAME] = value |
---|
502 | |
---|
503 | elif opt in ['-h', '--help']: |
---|
504 | print __doc__ |
---|
505 | sys.exit(0) |
---|
506 | |
---|
507 | elif opt in ['-i', '--interactive']: |
---|
508 | interactive(binfo) |
---|
509 | sys.exit(0) |
---|
510 | |
---|
511 | elif opt in ['-n', '--net']: |
---|
512 | binfo[NETWORK] = net_2_hex(value) |
---|
513 | |
---|
514 | elif opt in ['-r', '--remove']: |
---|
515 | binfo[REMOVE] = 1 |
---|
516 | |
---|
517 | elif opt in ['-s', '--start']: |
---|
518 | binfo[START] = value |
---|
519 | |
---|
520 | elif opt in ['-w', '--equal-width']: |
---|
521 | binfo[EQUALWIDTH] = [True, 0] |
---|
522 | |
---|
523 | elif opt in ['-v', '--verbose']: |
---|
524 | VERBOSE = True |
---|
525 | |
---|
526 | elif opt in ['-H', '--host-range']: |
---|
527 | hostrange = value |
---|
528 | |
---|
529 | elif opt in ['-N', '--node']: |
---|
530 | node = value |
---|
531 | |
---|
532 | elif opt in ['-R', '--rack']: |
---|
533 | rack = value |
---|
534 | |
---|
535 | elif opt in ['-V', '--version']: |
---|
536 | print VERSION |
---|
537 | sys.exit(0) |
---|
538 | |
---|
539 | if node and rack: |
---|
540 | parse_string_range(binfo, node, NODE) |
---|
541 | parse_string_range(binfo, rack, RACK) |
---|
542 | elif hostrange: |
---|
543 | parse_number_range(binfo, hostrange) |
---|
544 | |
---|
545 | |
---|
546 | check_args(binfo, args) |
---|
547 | |
---|
548 | def host_2_hex(hosts, binfo): |
---|
549 | """ |
---|
550 | Convert hostname(s) to a net address that can be handled by manage_links function |
---|
551 | """ |
---|
552 | str = 'host_2_hex: %s' %hosts |
---|
553 | verbose(str) |
---|
554 | |
---|
555 | for host in hosts: |
---|
556 | try: |
---|
557 | addr = socket.gethostbyname(host) |
---|
558 | except socket.error,detail: |
---|
559 | error = '%s not an valid hostname: %s' %(host,detail) |
---|
560 | raise PxeConfig, error |
---|
561 | |
---|
562 | net = string.splitfields(addr, '.') |
---|
563 | cnet = string.joinfields(net[0:3], '.') |
---|
564 | |
---|
565 | binfo[NETWORK] = net_2_hex(cnet) |
---|
566 | binfo[START] = int(net[3]) |
---|
567 | binfo[END] = int(net[3]) |
---|
568 | manage_links(binfo) |
---|
569 | |
---|
570 | def base_2_host(binfo): |
---|
571 | """ |
---|
572 | Construct hostname(s) from the supplied basename and start and end numbers |
---|
573 | """ |
---|
574 | str = 'base_2_host' |
---|
575 | verbose(str) |
---|
576 | |
---|
577 | start = binfo[START] |
---|
578 | end = binfo[END] |
---|
579 | |
---|
580 | if start > end: |
---|
581 | error = '%d >= %d : start value is greater then end value' %(start, end) |
---|
582 | raise PxeConfig, error |
---|
583 | |
---|
584 | hostnames = list() |
---|
585 | for i in xrange(start, end + 1): |
---|
586 | if binfo[EQUALWIDTH][0] == True: |
---|
587 | hostname = '%s%0*d' %(binfo[BASENAME], binfo[EQUALWIDTH][1], i) |
---|
588 | else: |
---|
589 | hostname = '%s%d' %(binfo[BASENAME], i) |
---|
590 | |
---|
591 | if DEBUG: |
---|
592 | print 'host = %s, Basename = %s, number = %d' %(hostname, binfo[BASENAME], i) |
---|
593 | hostnames.append(hostname) |
---|
594 | |
---|
595 | host_2_hex(hostnames,binfo) |
---|
596 | |
---|
597 | def rack_2_net(binfo): |
---|
598 | """ |
---|
599 | """ |
---|
600 | str = 'rack_2_net' |
---|
601 | verbose(str) |
---|
602 | basename = binfo[BASENAME] |
---|
603 | start = binfo[RACK][START] |
---|
604 | end = binfo[RACK][END] |
---|
605 | |
---|
606 | if start > end: |
---|
607 | error = '%d >= %d : start value is greater then end value' %(start, end) |
---|
608 | raise PxeConfig, error |
---|
609 | |
---|
610 | for i in xrange(start, end + 1): |
---|
611 | if binfo[RACK][EQUALWIDTH][0] == True: |
---|
612 | new_base = '%s%s%0*d' %(basename, binfo[RACK][BASENAME], binfo[RACK][EQUALWIDTH][1], i) |
---|
613 | else: |
---|
614 | new_base = '%s%s%d' %(basename, binfo[RACK][BASENAME], i) |
---|
615 | |
---|
616 | # Make copy and file in the appropiate values for creating/removing links |
---|
617 | # |
---|
618 | new_bootinfo = binfo[NODE].copy() |
---|
619 | new_bootinfo[BASENAME] = '%s%s' %(new_base, binfo[NODE][BASENAME]) |
---|
620 | new_bootinfo[FILENAME] = binfo[FILENAME] |
---|
621 | new_bootinfo[REMOVE] = binfo[REMOVE] |
---|
622 | |
---|
623 | if DEBUG: |
---|
624 | print 'rack ', new_bootinfo |
---|
625 | |
---|
626 | base_2_host(new_bootinfo) |
---|
627 | |
---|
628 | def main(): |
---|
629 | # A dictionary holding the boot info |
---|
630 | # |
---|
631 | global DEBUG |
---|
632 | global PXE_CONF_DIR |
---|
633 | |
---|
634 | bootinfo = {} |
---|
635 | bootinfo[NODE] = None |
---|
636 | bootinfo[RACK] = None |
---|
637 | bootinfo[REMOVE] = False |
---|
638 | bootinfo[EQUALWIDTH] = [ False, 0 ] |
---|
639 | |
---|
640 | configfile = '@pxeconfig_conf@' |
---|
641 | settings = ReadConfig(configfile) |
---|
642 | |
---|
643 | try: |
---|
644 | PXE_CONF_DIR = settings['pxe_config_dir'] |
---|
645 | if not DEBUG: |
---|
646 | DEBUG = int(settings['debug']) |
---|
647 | |
---|
648 | except KeyError: |
---|
649 | pass |
---|
650 | |
---|
651 | PXE_CONF_DIR = os.path.realpath(PXE_CONF_DIR) |
---|
652 | if not os.path.isdir(PXE_CONF_DIR): |
---|
653 | error = 'pxeconfig directory: %s does not exists' %(PXE_CONF_DIR) |
---|
654 | raise PxeConfig, error |
---|
655 | |
---|
656 | parse_args(sys.argv, bootinfo) |
---|
657 | |
---|
658 | if __name__ == '__main__': |
---|
659 | try: |
---|
660 | main() |
---|
661 | except PxeConfig, detail: |
---|
662 | print detail |
---|
663 | sys.exit(1) |
---|