- Timestamp:
- 04/10/08 23:52:45 (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Changelog
r118 r119 24 24 - Fixed a bug in short option '-b' (basename) must have an argument. 25 25 Author: Bas van der Vlies 26 27 - In analogue with systemimager we have a -H,--host-range <number>-<number> 28 option. It must be used in combinatiion with -b,--basebane, eg: 29 pxeconfig --basename gb-r1n --host-range 1-2 -filename default.memtestZ 30 31 will produce links to file default.memtest for these hosts 32 - gb-r1n1 33 - gb-r1n2 34 35 If leading zeros are used, then host names will be padded with zeros. 36 pxeconfig --basename gb-r1n --host-range 01-02 -filename default.memtestZ 37 38 will produce links to file default.memtest for these hosts 39 - gb-r1n01 40 - gb-r1n02 41 26 42 27 43 -
trunk/pxeconfig.in
r118 r119 90 90 VERSION='1.1.0' 91 91 92 SHORTOPT_LIST='b:e:f:hin:s:rwv V'92 SHORTOPT_LIST='b:e:f:hin:s:rwvH:V' 93 93 LONGOPT_LIST=[ 'basename=', 'debug', 'end=', 'equal-width', 94 'file=', 'help', ' interactive', 'net=', 'start=',95 94 'file=', 'help', 'host-range=', 'interactive', 'net=', 95 'start=', 'remove', 'verbose', 'version', 'equal-width' 96 96 ] 97 97 … … 99 99 if VERBOSE: 100 100 print '%s' 101 102 class PxeConfig(Exception): 103 def __init__(self, msg=''): 104 self.msg = msg 105 Exception.__init__(self, msg) 106 107 def __repr__(self): 108 return self.msg 109 101 110 102 111 def ReadConfig(file): … … 199 208 os.symlink(pxe_filename, haddr) 200 209 201 def convert_network(net, prefix=''): 202 """ 203 This function checks if the give network is a Class C-network and will 204 convert the network address to a hex address if true. 205 """ 206 d = string.split(net, '.') 207 208 if len(d) != 3: 209 if prefix: 210 net = prefix + ' : ' + net 211 212 print '%s is not a valid C-class network address!!!' %net 213 sys.exit(1) 214 215 216 # Display right message for interactive/commandline 217 if prefix: 218 prefix = prefix + ' ' + net 219 else: 220 prefix = net 221 222 # Check if we have valid network values 223 r = '' 224 for i in d: 225 i = check_number(i, prefix) 226 r = '%s%02X' %(r,i) 227 228 if DEBUG: 229 print r 230 231 return r 232 233 def check_number(number, network, option_str=''): 234 """ 235 This functions checks if the input is between 0 < number < 255: 236 number : is a string 237 prefix : a string that must be displayed if an error occurs 238 """ 210 def convert_network(net): 211 """ 212 This function checks if the give network is a Class C-network and will 213 convert the network address to a hex address if true. 214 """ 215 str = 'convert_network : %s' %(net) 216 verbose(str) 217 218 d = string.split(net, '.') 219 if len(d) != 3: 220 error = '%s is not a valid C-class network address' %(net) 221 raise PxeConfig, error 222 223 # Check if we have valid network values 224 r = '' 225 for i in d: 226 i = check_number(i, True) 227 r = '%s%02X' %(r,i) 228 229 if DEBUG: 230 print r 231 232 return r 233 234 def check_number(number_str, network): 235 """ 236 number : a string. If string starts with a zero (0) then 237 EQUALWIDTH wil be set. 238 network: if true then number must ben between 0 < number < 255 239 else it must be a valid number. 240 """ 241 239 242 try: 240 n = int(number )243 n = int(number_str) 241 244 except ValueError, detail: 242 print option_str, detail243 sys.exit(1)245 error = "%s : is not a valid number" %number_str 246 raise PxeConfig, error 244 247 245 248 if not network: … … 251 254 return n 252 255 else: 253 if option_str: 254 number = option_str +' : ' + number 255 256 print '%s is not a valid network number, must be between 0 and 255' %number 257 sys.exit(1) 256 error = '%s is not a valid network number, must be between 0 and 255' %n 257 raise PxeConfig, error 258 258 259 259 def interactive(binfo): 260 260 print __doc__ 261 261 262 network = raw_input('Give network address (xxx.xxx.xxx): ') 263 naddr = convert_network(network) 264 265 start = raw_input('Starting number: ') 266 start = check_number(start, True) 267 268 end = raw_input('Ending number: ') 269 end = check_number(end, True) 262 263 while 1: 264 network = raw_input('Give network address (xxx.xxx.xxx): ') 265 266 try: 267 naddr = convert_network(network) 268 break 269 except PxeConfig, detail: 270 print '%s : not a valid C-class network number' %(network) 271 continue 272 273 while 1: 274 start = raw_input('Starting number: ') 275 276 try: 277 start = check_number(start, True) 278 break 279 except PxeConfig, detail: 280 print detail 281 continue 282 283 while 1: 284 end = raw_input('Ending number: ') 285 286 try: 287 end = check_number(end, True) 288 break 289 except PxeConfig, detail: 290 print detail 291 continue 292 270 293 271 294 pxe_filename = select_pxe_configfile() … … 289 312 try: 290 313 if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])): 291 print'%s: Filename does not exists' %binfo[FILENAME]292 sys.exit(1)314 error = '%s: Filename does not exists' %binfo[FILENAME] 315 raise Pxeconfig, detail 293 316 except KeyError, detail: 294 317 if binfo[REMOVE] : … … 302 325 303 326 if binfo.has_key(BASENAME) and binfo.has_key(NETWORK): 304 print __doc__ 305 print "The option -n/--net and -b/--basename are mutually exclusive" 306 sys.exit(1) 327 error = "The option -n/--net and -b/--basename are mutually exclusive" 328 raise PxeConfig, error 307 329 308 330 if binfo.has_key(BASENAME): … … 313 335 network_number = True 314 336 create_links = manage_links 337 315 338 else: 316 print __doc__ 317 sys.exit(1) 318 319 try: 320 binfo[START] = check_number(binfo[START], network_number, '-s/--start') 321 except KeyError, detail: 322 print __doc__ 323 print '-s/--start is missing on the command line' %(detail) 324 sys.exit(1) 325 326 try: 327 binfo[END] = check_number(binfo[END], network_number, '-e/--end') 328 except KeyError, detail: 329 print __doc__ 330 print '-e/--end is missing on the command line' %(detail) 331 sys.exit(1) 339 error = 'You have to specifiy -b,--basename or -n,--net' 340 raise PxeConfig, error 341 342 binfo[START] = check_number(binfo[START], network_number) 343 binfo[END] = check_number(binfo[END], network_number) 332 344 333 345 if DEBUG: … … 336 348 create_links(binfo) 337 349 338 350 def set_padding(binfo, start, end): 351 """ 352 find out th 353 """ 354 if len(start) > 1 and start[0] == '0': 355 binfo[EQUALWIDTH] = True 356 elif end[0] == '0': 357 binfo[EQUALWIDTH] = True 358 359 if len(start) == len(end): 360 a =1 361 362 363 def parse_hostrange(binfo, arg): 364 """ 365 Parse if arg is of format <digit-digit>, if it starts 366 with a zero (0) then set EQUALWIDTH 367 """ 368 str = 'parse_hostrange %s' %(arg) 369 verbose(str) 370 371 l = arg.split('-') 372 if len(l) < 2: 373 error = 'hostrange syntax not valid: %s (number-number)' %(arg) 374 raise PxeConfig, error 375 376 start = l[0] 377 end = l[1] 378 379 380 binfo[START] = check_number(start, False) 381 binfo[END] = check_number(end, False) 382 339 383 def check_args(argv, binfo): 340 384 """ … … 386 430 387 431 elif opt in ['-n', '--net']: 388 network = value 389 binfo[NETWORK] = convert_network(value, opt) 432 binfo[NETWORK] = convert_network(value) 390 433 391 434 elif opt in ['-r', '--remove']: … … 401 444 VERBOSE = 1 402 445 446 elif opt in ['-H', '--host-range']: 447 parse_hostrange(binfo, value) 448 403 449 elif opt in ['-V', '--version']: 404 450 print VERSION … … 415 461 addr = socket.gethostbyname(host) 416 462 except socket.error,detail: 417 print'%s not an valid hostname: %s' %(host,detail)418 sys.exit(1)463 error = '%s not an valid hostname: %s' %(host,detail) 464 raise PxeConfig, error 419 465 420 466 net = string.splitfields(addr, '.') … … 430 476 Construct hostname(s) from the supplied basename and start and end numbers 431 477 """ 432 if binfo[START] >= binfo[END]: 433 print __doc__ 434 print "Supplied wrong values for start (%d) and end (%d)" %(binfo[START], binfo[END]) 435 sys.exit(1) 478 start = binfo[START] 479 end = binfo[END] 480 481 if start > end: 482 error = '%d >= %d : start value is greater then end value' %(start, end) 483 raise PxeConfig, error 436 484 437 485 if binfo[EQUALWIDTH]: 438 width = len(str( binfo[END]))486 width = len(str(end)) 439 487 440 488 hostnames = list() 441 for i in xrange( binfo[START], binfo[END]+ 1):489 for i in xrange(start, end + 1): 442 490 if binfo[EQUALWIDTH]: 443 491 hostname = '%s%0*d' %(binfo[BASENAME], width, i) … … 477 525 sys.exit(1) 478 526 479 check_args(sys.argv, bootinfo) 527 try: 528 check_args(sys.argv, bootinfo) 529 except PxeConfig, detail: 530 print detail 531 sys.exit(1) 480 532 481 533 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.