[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 | # |
---|
[29] | 8 | # SVN info |
---|
| 9 | # $Id: pxeconfig 58 2005-09-26 09:27:35Z bas $ |
---|
[2] | 10 | # |
---|
[14] | 11 | # Copyright (C) 2002 |
---|
[6] | 12 | # |
---|
[14] | 13 | # This file is part of the pxeconfig utils |
---|
[6] | 14 | # |
---|
[14] | 15 | # This program is free software; you can redistribute it and/or modify it |
---|
| 16 | # under the terms of the GNU General Public License as published by the |
---|
| 17 | # Free Software Foundation; either version 2, or (at your option) any |
---|
| 18 | # later version. |
---|
[6] | 19 | # |
---|
[14] | 20 | # This program is distributed in the hope that it will be useful, |
---|
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 23 | # GNU General Public License for more details. |
---|
[6] | 24 | # |
---|
[14] | 25 | # You should have received a copy of the GNU General Public License |
---|
| 26 | # along with this program; if not, write to the Free Software |
---|
| 27 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 28 | # |
---|
[2] | 29 | """ |
---|
[19] | 30 | Usage: pxeconfig |
---|
[49] | 31 | |
---|
[19] | 32 | [-d|--directory <pxe_config_dir>] |
---|
[53] | 33 | [-f|--file <filename>] [hostname(s)] |
---|
[58] | 34 | [-i|--interactive] |
---|
[53] | 35 | [-n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>] |
---|
| 36 | [-r|--remove] |
---|
[24] | 37 | [-V|--version] |
---|
[10] | 38 | |
---|
[19] | 39 | With this program you can configure which PXE configuration file a node |
---|
| 40 | will use when it boots. The program can be started interactivly or via |
---|
| 41 | command line options. |
---|
| 42 | |
---|
| 43 | When this program is started interactive it will ask the following questions: |
---|
[2] | 44 | 1) Network address (Class C-network address only) |
---|
[9] | 45 | 2) Starting number |
---|
[2] | 46 | 3) Ending number |
---|
[9] | 47 | 4) Which PXE config file to use |
---|
[2] | 48 | |
---|
| 49 | For example, if the answers are: |
---|
| 50 | 1) 10.168.44 |
---|
| 51 | 2) 2 |
---|
| 52 | 3) 4 |
---|
| 53 | 4) default.node_install |
---|
| 54 | |
---|
| 55 | Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg: |
---|
| 56 | 0AA82C02 ----> default.node_install |
---|
| 57 | 0AA82C03 ----> default.node_install |
---|
| 58 | 0AA82C04 ----> default.node_install |
---|
| 59 | """ |
---|
| 60 | |
---|
| 61 | import string |
---|
| 62 | import sys |
---|
| 63 | import os |
---|
| 64 | import glob |
---|
[3] | 65 | import getopt |
---|
[47] | 66 | import socket |
---|
[2] | 67 | |
---|
| 68 | # DEBUG |
---|
| 69 | # |
---|
| 70 | DEBUG=0 |
---|
| 71 | |
---|
| 72 | # Constants |
---|
| 73 | # |
---|
[4] | 74 | PXE_CONF_DIR='/tftpboot/pxelinux.cfg' |
---|
[19] | 75 | NETWORK='network' |
---|
| 76 | FILENAME='filename' |
---|
| 77 | START='start' |
---|
| 78 | END='end' |
---|
[57] | 79 | VERSION='0.6.1' |
---|
[53] | 80 | REMOVE='remove' |
---|
[55] | 81 | INTERACTIVE='interactive' |
---|
[2] | 82 | |
---|
[55] | 83 | SHORTOPT_LIST='hVd:n:s:e:f:ri' |
---|
| 84 | LONGOPT_LIST=['help', 'version', 'directory=', 'net=', 'start=', 'end=', 'file=', 'remove', 'debug' , 'interactive' ] |
---|
[19] | 85 | |
---|
| 86 | def select_pxe_configfile(): |
---|
[2] | 87 | """ |
---|
[9] | 88 | Let user choose which pxeconfig file to use. |
---|
[2] | 89 | """ |
---|
| 90 | |
---|
[4] | 91 | os.chdir(PXE_CONF_DIR) |
---|
[2] | 92 | |
---|
[9] | 93 | # Try to determine to which file the default symlink points, and |
---|
| 94 | # if it exists, remove it from the list. |
---|
[2] | 95 | # |
---|
| 96 | try: |
---|
| 97 | default_file = os.readlink('default') |
---|
| 98 | except OSError: |
---|
| 99 | default_file = None |
---|
| 100 | pass |
---|
| 101 | |
---|
| 102 | files = glob.glob('default.*') |
---|
| 103 | if not files: |
---|
| 104 | print 'There are no pxe config files starting with: default.' |
---|
| 105 | sys.exit(1) |
---|
| 106 | |
---|
| 107 | if default_file: |
---|
| 108 | files.remove(default_file) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | print 'Which pxe config file must we use: ?' |
---|
| 112 | i = 1 |
---|
| 113 | for file in files: |
---|
| 114 | print "%d : %s" %(i,file) |
---|
| 115 | i = i +1 |
---|
| 116 | |
---|
| 117 | while 1: |
---|
[16] | 118 | index = raw_input('Select a number: ') |
---|
[2] | 119 | try: |
---|
| 120 | index = int(index) |
---|
| 121 | except ValueError: |
---|
| 122 | index = len(files) + 1 |
---|
| 123 | |
---|
| 124 | # Is the user smart enough to select |
---|
[9] | 125 | # the right value?? |
---|
[2] | 126 | # |
---|
| 127 | if 0 < index <= len(files): break |
---|
| 128 | |
---|
| 129 | return files[index-1] |
---|
| 130 | |
---|
[53] | 131 | def manage_links(dict): |
---|
[2] | 132 | """ |
---|
[4] | 133 | Create the links in the PXE_CONF_DIR, |
---|
[9] | 134 | list : A list containing: network hex address, pxe config file, |
---|
[2] | 135 | start number, end number |
---|
| 136 | """ |
---|
[4] | 137 | os.chdir(PXE_CONF_DIR) |
---|
[19] | 138 | naddr = dict[NETWORK] |
---|
| 139 | pxe_filename = dict[FILENAME] |
---|
| 140 | for i in range(dict[START], dict[END]+1): |
---|
[2] | 141 | haddr = '%s%02X' %(naddr, i) |
---|
[15] | 142 | |
---|
[53] | 143 | if dict[REMOVE]: |
---|
| 144 | if DEBUG: |
---|
| 145 | print 'removing %s/%s' %(PXE_CONF_DIR, haddr) |
---|
| 146 | if os.path.exists(haddr): |
---|
| 147 | os.unlink(haddr) |
---|
| 148 | else: |
---|
| 149 | if DEBUG: |
---|
| 150 | print 'linking %s to %s' %(haddr, pxe_filename) |
---|
| 151 | if os.path.exists(haddr): |
---|
| 152 | os.unlink(haddr) |
---|
| 153 | os.symlink(pxe_filename, haddr) |
---|
[15] | 154 | |
---|
[47] | 155 | def convert_network(net, prefix=''): |
---|
[2] | 156 | """ |
---|
[9] | 157 | This function checks if the give network is a Class C-network and will |
---|
[2] | 158 | convert the network address to a hex address if true. |
---|
| 159 | """ |
---|
| 160 | d = string.split(net, '.') |
---|
| 161 | |
---|
| 162 | if len(d) != 3: |
---|
[19] | 163 | if prefix: |
---|
| 164 | net = prefix + ' : ' + net |
---|
| 165 | |
---|
| 166 | print '%s is not a valid C-class network address!!!' %net |
---|
[2] | 167 | sys.exit(1) |
---|
| 168 | |
---|
[19] | 169 | |
---|
| 170 | # Display right message for interactive/commandline |
---|
| 171 | if prefix: |
---|
| 172 | prefix = prefix + ' ' + net |
---|
| 173 | else: |
---|
| 174 | prefix = net |
---|
| 175 | |
---|
[2] | 176 | # Check if we have valid network values |
---|
| 177 | r = '' |
---|
| 178 | for i in d: |
---|
[19] | 179 | i = check_number(i, prefix) |
---|
[2] | 180 | r = '%s%02X' %(r,i) |
---|
| 181 | |
---|
| 182 | if DEBUG: |
---|
| 183 | print r |
---|
| 184 | |
---|
| 185 | return r |
---|
| 186 | |
---|
[19] | 187 | def check_number(number, prefix=''): |
---|
[2] | 188 | """ |
---|
| 189 | This functions checks if the input is between 0 < number < 255: |
---|
| 190 | number : is a string |
---|
[19] | 191 | prefix ; a string that must be displayed if an error occurs |
---|
[2] | 192 | """ |
---|
| 193 | try: |
---|
| 194 | n = int(number) |
---|
| 195 | except ValueError, detail: |
---|
[19] | 196 | print prefix,detail |
---|
[2] | 197 | sys.exit(1) |
---|
| 198 | |
---|
| 199 | if 0 <= n <= 255: |
---|
| 200 | return n |
---|
| 201 | else: |
---|
[19] | 202 | |
---|
| 203 | if prefix: |
---|
| 204 | number = prefix +' : ' + number |
---|
| 205 | |
---|
[2] | 206 | print '%s is not a valid number, must be between 0 and 255' %number |
---|
| 207 | sys.exit(1) |
---|
| 208 | |
---|
[19] | 209 | def interactive(binfo): |
---|
[2] | 210 | |
---|
| 211 | print __doc__ |
---|
| 212 | |
---|
[9] | 213 | network = raw_input('Give network address (xxx.xxx.xxx): ') |
---|
[47] | 214 | naddr = convert_network(network) |
---|
[2] | 215 | |
---|
| 216 | start = raw_input('Starting number: ') |
---|
| 217 | start = check_number(start) |
---|
| 218 | |
---|
| 219 | end = raw_input('Ending number: ') |
---|
| 220 | end = check_number(end) |
---|
| 221 | |
---|
[19] | 222 | pxe_filename = select_pxe_configfile() |
---|
[2] | 223 | |
---|
[19] | 224 | binfo[NETWORK] = naddr |
---|
| 225 | binfo[START] = int(start) |
---|
| 226 | binfo[END] = int(end) |
---|
| 227 | binfo[FILENAME] = pxe_filename |
---|
[2] | 228 | |
---|
[19] | 229 | if DEBUG: |
---|
| 230 | print network, binfo |
---|
[2] | 231 | |
---|
[19] | 232 | def check_cmd_line(binfo): |
---|
| 233 | if len(binfo.keys()) != 4: |
---|
| 234 | print __doc__ |
---|
[20] | 235 | print 'Not enough arguments to create the links!!' |
---|
[19] | 236 | sys.exit(1) |
---|
[6] | 237 | |
---|
[19] | 238 | # check_filename |
---|
| 239 | # |
---|
| 240 | if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])): |
---|
| 241 | print '%s: Filename does not exists' %binfo[FILENAME] |
---|
| 242 | sys.exit(1) |
---|
| 243 | |
---|
| 244 | def check_args(argv, binfo): |
---|
[3] | 245 | """ |
---|
[47] | 246 | This function parses the command line options and returns the rest as |
---|
| 247 | an list of hostnames: |
---|
| 248 | argv : a list of command line options. |
---|
| 249 | binfo : returning a dict with the netinfo. if used non-interactively |
---|
| 250 | hostnames: the rest of the command lines options that are not-parseble. |
---|
[3] | 251 | """ |
---|
[4] | 252 | global PXE_CONF_DIR |
---|
[53] | 253 | global DEBUG |
---|
[19] | 254 | |
---|
[3] | 255 | try: |
---|
| 256 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
| 257 | except getopt.error, detail: |
---|
[20] | 258 | print __doc__ |
---|
[3] | 259 | print detail |
---|
| 260 | sys.exit(1) |
---|
| 261 | |
---|
[19] | 262 | # Check given options |
---|
| 263 | # |
---|
| 264 | for opt,value in opts: |
---|
[55] | 265 | |
---|
[19] | 266 | if opt in ['-d', '--directory']: |
---|
| 267 | if os.path.isdir(value): |
---|
| 268 | PXE_CONF_DIR = value |
---|
| 269 | else: |
---|
| 270 | print 'Directory %s does not exists\n' %value |
---|
| 271 | sys.exit(1) |
---|
[3] | 272 | |
---|
[55] | 273 | elif opt in ['-i', '--interactive']: |
---|
| 274 | binfo[INTERACTIVE] = 1 |
---|
| 275 | |
---|
[19] | 276 | elif opt in ['-n', '--net']: |
---|
| 277 | network = value |
---|
[47] | 278 | binfo[NETWORK] = convert_network(value, opt) |
---|
[4] | 279 | |
---|
[19] | 280 | elif opt in ['-s', '--start']: |
---|
| 281 | binfo[START] = check_number(value, opt) |
---|
| 282 | |
---|
| 283 | elif opt in ['-e', '--end']: |
---|
| 284 | binfo[END] = check_number(value, opt) |
---|
| 285 | |
---|
| 286 | elif opt in ['-f', '--file']: |
---|
| 287 | binfo[FILENAME] = value |
---|
| 288 | |
---|
[24] | 289 | elif opt in ['-V', '--version']: |
---|
[23] | 290 | print VERSION |
---|
| 291 | sys.exit(0) |
---|
| 292 | |
---|
[53] | 293 | elif opt in ['-r', '--remove']: |
---|
| 294 | binfo[REMOVE] = 1 |
---|
| 295 | |
---|
| 296 | elif opt in ['--debug']: |
---|
| 297 | DEBUG = 1 |
---|
| 298 | |
---|
[20] | 299 | elif opt in ['-h', '--help']: |
---|
| 300 | print __doc__ |
---|
| 301 | sys.exit(0) |
---|
[19] | 302 | |
---|
[51] | 303 | if args: |
---|
| 304 | return args |
---|
[20] | 305 | |
---|
[47] | 306 | def hosts_links(hosts, binfo): |
---|
| 307 | for host in hosts: |
---|
| 308 | try: |
---|
| 309 | addr = socket.gethostbyname(host) |
---|
| 310 | except socket.error,detail: |
---|
| 311 | print '%s not an valid hostname: %s' %(host,detail) |
---|
| 312 | sys.exit(1) |
---|
| 313 | |
---|
| 314 | net = string.splitfields(addr, '.') |
---|
| 315 | cnet = string.joinfields(net[0:3], '.') |
---|
| 316 | |
---|
| 317 | binfo[NETWORK] = convert_network(cnet) |
---|
| 318 | binfo[START] = int(net[3]) |
---|
| 319 | binfo[END] = int(net[3]) |
---|
[53] | 320 | manage_links(binfo) |
---|
[47] | 321 | |
---|
| 322 | |
---|
[2] | 323 | def main(): |
---|
[19] | 324 | # A dictionary holding the boot info |
---|
| 325 | # |
---|
| 326 | bootinfo = {} |
---|
[53] | 327 | bootinfo[REMOVE] = 0 |
---|
[2] | 328 | |
---|
[47] | 329 | hostnames = check_args(sys.argv, bootinfo) |
---|
| 330 | |
---|
[53] | 331 | if bootinfo[REMOVE]: |
---|
| 332 | bootinfo[FILENAME] = 'remove_boot_file' |
---|
[47] | 333 | |
---|
[53] | 334 | |
---|
[47] | 335 | # If we supplied args then make links for the supplied hosts |
---|
| 336 | # else make links for class C-networks |
---|
| 337 | # |
---|
| 338 | if hostnames: |
---|
| 339 | if not bootinfo.has_key(FILENAME): |
---|
[53] | 340 | bootinfo[FILENAME] = select_pxe_configfile() |
---|
[47] | 341 | hosts_links(hostnames,bootinfo) |
---|
[19] | 342 | else: |
---|
[55] | 343 | if bootinfo.has_key(INTERACTIVE): |
---|
| 344 | interactive(bootinfo) |
---|
| 345 | else: |
---|
[47] | 346 | check_cmd_line(bootinfo) |
---|
[53] | 347 | manage_links(bootinfo) |
---|
[19] | 348 | |
---|
[2] | 349 | if __name__ == '__main__': |
---|
| 350 | main() |
---|