[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 | # |
---|
| 8 | # CVS info |
---|
[24] | 9 | # $Date: 2002/11/29 12:46:05 $ |
---|
| 10 | # $Revision: 1.13 $ |
---|
[2] | 11 | # |
---|
[14] | 12 | # Copyright (C) 2002 |
---|
[6] | 13 | # |
---|
[14] | 14 | # This file is part of the pxeconfig utils |
---|
[6] | 15 | # |
---|
[14] | 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. |
---|
[6] | 20 | # |
---|
[14] | 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. |
---|
[6] | 25 | # |
---|
[14] | 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 | # |
---|
[2] | 30 | """ |
---|
[19] | 31 | Usage: pxeconfig |
---|
| 32 | [-d|--directory <pxe_config_dir>] |
---|
[24] | 33 | [-V|--version] |
---|
[19] | 34 | [-n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>] |
---|
[10] | 35 | |
---|
[19] | 36 | With this program you can configure which PXE configuration file a node |
---|
| 37 | will use when it boots. The program can be started interactivly or via |
---|
| 38 | command line options. |
---|
| 39 | |
---|
| 40 | When this program is started interactive it will ask the following questions: |
---|
[2] | 41 | 1) Network address (Class C-network address only) |
---|
[9] | 42 | 2) Starting number |
---|
[2] | 43 | 3) Ending number |
---|
[9] | 44 | 4) Which PXE config file to use |
---|
[2] | 45 | |
---|
| 46 | For example, if the answers are: |
---|
| 47 | 1) 10.168.44 |
---|
| 48 | 2) 2 |
---|
| 49 | 3) 4 |
---|
| 50 | 4) default.node_install |
---|
| 51 | |
---|
| 52 | Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg: |
---|
| 53 | 0AA82C02 ----> default.node_install |
---|
| 54 | 0AA82C03 ----> default.node_install |
---|
| 55 | 0AA82C04 ----> default.node_install |
---|
| 56 | """ |
---|
| 57 | |
---|
| 58 | import string |
---|
| 59 | import sys |
---|
| 60 | import os |
---|
| 61 | import glob |
---|
[3] | 62 | import getopt |
---|
[2] | 63 | |
---|
| 64 | # DEBUG |
---|
| 65 | # |
---|
| 66 | DEBUG=0 |
---|
| 67 | |
---|
| 68 | # Constants |
---|
| 69 | # |
---|
[4] | 70 | PXE_CONF_DIR='/tftpboot/pxelinux.cfg' |
---|
[19] | 71 | NETWORK='network' |
---|
| 72 | FILENAME='filename' |
---|
| 73 | START='start' |
---|
| 74 | END='end' |
---|
[23] | 75 | VERSION='0.4.1' |
---|
[2] | 76 | |
---|
[24] | 77 | SHORTOPT_LIST='hVd:n:s:e:f:' |
---|
[23] | 78 | LONGOPT_LIST=['help', 'version', 'directory=', 'net=', 'start=', 'end=', 'file='] |
---|
[19] | 79 | |
---|
| 80 | def select_pxe_configfile(): |
---|
[2] | 81 | """ |
---|
[9] | 82 | Let user choose which pxeconfig file to use. |
---|
[2] | 83 | """ |
---|
| 84 | |
---|
[4] | 85 | os.chdir(PXE_CONF_DIR) |
---|
[2] | 86 | |
---|
[9] | 87 | # Try to determine to which file the default symlink points, and |
---|
| 88 | # if it exists, remove it from the list. |
---|
[2] | 89 | # |
---|
| 90 | try: |
---|
| 91 | default_file = os.readlink('default') |
---|
| 92 | except OSError: |
---|
| 93 | default_file = None |
---|
| 94 | pass |
---|
| 95 | |
---|
| 96 | files = glob.glob('default.*') |
---|
| 97 | if not files: |
---|
| 98 | print 'There are no pxe config files starting with: default.' |
---|
| 99 | sys.exit(1) |
---|
| 100 | |
---|
| 101 | if default_file: |
---|
| 102 | files.remove(default_file) |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | print 'Which pxe config file must we use: ?' |
---|
| 106 | i = 1 |
---|
| 107 | for file in files: |
---|
| 108 | print "%d : %s" %(i,file) |
---|
| 109 | i = i +1 |
---|
| 110 | |
---|
| 111 | while 1: |
---|
[16] | 112 | index = raw_input('Select a number: ') |
---|
[2] | 113 | try: |
---|
| 114 | index = int(index) |
---|
| 115 | except ValueError: |
---|
| 116 | index = len(files) + 1 |
---|
| 117 | |
---|
| 118 | # Is the user smart enough to select |
---|
[9] | 119 | # the right value?? |
---|
[2] | 120 | # |
---|
| 121 | if 0 < index <= len(files): break |
---|
| 122 | |
---|
| 123 | return files[index-1] |
---|
| 124 | |
---|
[19] | 125 | def create_links(dict): |
---|
[2] | 126 | """ |
---|
[4] | 127 | Create the links in the PXE_CONF_DIR, |
---|
[9] | 128 | list : A list containing: network hex address, pxe config file, |
---|
[2] | 129 | start number, end number |
---|
| 130 | """ |
---|
[4] | 131 | os.chdir(PXE_CONF_DIR) |
---|
[19] | 132 | naddr = dict[NETWORK] |
---|
| 133 | pxe_filename = dict[FILENAME] |
---|
| 134 | for i in range(dict[START], dict[END]+1): |
---|
[2] | 135 | haddr = '%s%02X' %(naddr, i) |
---|
| 136 | if DEBUG: |
---|
| 137 | print 'linking %s to %s' %(haddr, pxe_filename) |
---|
[15] | 138 | |
---|
| 139 | if os.path.exists(haddr): |
---|
| 140 | os.unlink(haddr) |
---|
| 141 | |
---|
[2] | 142 | os.symlink(pxe_filename, haddr) |
---|
| 143 | |
---|
| 144 | |
---|
[19] | 145 | def check_network(net, prefix=''): |
---|
[2] | 146 | """ |
---|
[9] | 147 | This function checks if the give network is a Class C-network and will |
---|
[2] | 148 | convert the network address to a hex address if true. |
---|
| 149 | """ |
---|
| 150 | d = string.split(net, '.') |
---|
| 151 | |
---|
| 152 | if len(d) != 3: |
---|
[19] | 153 | if prefix: |
---|
| 154 | net = prefix + ' : ' + net |
---|
| 155 | |
---|
| 156 | print '%s is not a valid C-class network address!!!' %net |
---|
[2] | 157 | sys.exit(1) |
---|
| 158 | |
---|
[19] | 159 | |
---|
| 160 | # Display right message for interactive/commandline |
---|
| 161 | if prefix: |
---|
| 162 | prefix = prefix + ' ' + net |
---|
| 163 | else: |
---|
| 164 | prefix = net |
---|
| 165 | |
---|
[2] | 166 | # Check if we have valid network values |
---|
| 167 | r = '' |
---|
| 168 | for i in d: |
---|
[19] | 169 | i = check_number(i, prefix) |
---|
[2] | 170 | r = '%s%02X' %(r,i) |
---|
| 171 | |
---|
| 172 | if DEBUG: |
---|
| 173 | print r |
---|
| 174 | |
---|
| 175 | return r |
---|
| 176 | |
---|
[19] | 177 | def check_number(number, prefix=''): |
---|
[2] | 178 | """ |
---|
| 179 | This functions checks if the input is between 0 < number < 255: |
---|
| 180 | number : is a string |
---|
[19] | 181 | prefix ; a string that must be displayed if an error occurs |
---|
[2] | 182 | """ |
---|
| 183 | try: |
---|
| 184 | n = int(number) |
---|
| 185 | except ValueError, detail: |
---|
[19] | 186 | print prefix,detail |
---|
[2] | 187 | sys.exit(1) |
---|
| 188 | |
---|
| 189 | if 0 <= n <= 255: |
---|
| 190 | return n |
---|
| 191 | else: |
---|
[19] | 192 | |
---|
| 193 | if prefix: |
---|
| 194 | number = prefix +' : ' + number |
---|
| 195 | |
---|
[2] | 196 | print '%s is not a valid number, must be between 0 and 255' %number |
---|
| 197 | sys.exit(1) |
---|
| 198 | |
---|
[19] | 199 | def interactive(binfo): |
---|
[2] | 200 | |
---|
| 201 | print __doc__ |
---|
| 202 | |
---|
[9] | 203 | network = raw_input('Give network address (xxx.xxx.xxx): ') |
---|
[2] | 204 | naddr = check_network(network) |
---|
| 205 | |
---|
| 206 | start = raw_input('Starting number: ') |
---|
| 207 | start = check_number(start) |
---|
| 208 | |
---|
| 209 | end = raw_input('Ending number: ') |
---|
| 210 | end = check_number(end) |
---|
| 211 | |
---|
[19] | 212 | pxe_filename = select_pxe_configfile() |
---|
[2] | 213 | |
---|
[19] | 214 | binfo[NETWORK] = naddr |
---|
| 215 | binfo[START] = int(start) |
---|
| 216 | binfo[END] = int(end) |
---|
| 217 | binfo[FILENAME] = pxe_filename |
---|
[2] | 218 | |
---|
[19] | 219 | if DEBUG: |
---|
| 220 | print network, binfo |
---|
[2] | 221 | |
---|
[19] | 222 | def check_cmd_line(binfo): |
---|
| 223 | if len(binfo.keys()) != 4: |
---|
| 224 | print __doc__ |
---|
[20] | 225 | print 'Not enough arguments to create the links!!' |
---|
[19] | 226 | sys.exit(1) |
---|
[6] | 227 | |
---|
[19] | 228 | # check_filename |
---|
| 229 | # |
---|
| 230 | if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])): |
---|
| 231 | print '%s: Filename does not exists' %binfo[FILENAME] |
---|
| 232 | sys.exit(1) |
---|
| 233 | |
---|
| 234 | def check_args(argv, binfo): |
---|
[3] | 235 | """ |
---|
[20] | 236 | This function parses the command line options: |
---|
| 237 | argv : a list of command line options. |
---|
| 238 | binfo: returning a dict with the netinfo. if used non-interactively |
---|
[3] | 239 | """ |
---|
[4] | 240 | global PXE_CONF_DIR |
---|
[19] | 241 | |
---|
[3] | 242 | try: |
---|
| 243 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
| 244 | except getopt.error, detail: |
---|
[20] | 245 | print __doc__ |
---|
[3] | 246 | print detail |
---|
| 247 | sys.exit(1) |
---|
| 248 | |
---|
[19] | 249 | # Check given options |
---|
| 250 | # |
---|
| 251 | for opt,value in opts: |
---|
| 252 | if opt in ['-d', '--directory']: |
---|
| 253 | if os.path.isdir(value): |
---|
| 254 | PXE_CONF_DIR = value |
---|
| 255 | else: |
---|
| 256 | print 'Directory %s does not exists\n' %value |
---|
| 257 | sys.exit(1) |
---|
[3] | 258 | |
---|
[19] | 259 | elif opt in ['-n', '--net']: |
---|
| 260 | network = value |
---|
| 261 | binfo[NETWORK] = check_network(value, opt) |
---|
[4] | 262 | |
---|
[19] | 263 | elif opt in ['-s', '--start']: |
---|
| 264 | binfo[START] = check_number(value, opt) |
---|
| 265 | |
---|
| 266 | elif opt in ['-e', '--end']: |
---|
| 267 | binfo[END] = check_number(value, opt) |
---|
| 268 | |
---|
| 269 | elif opt in ['-f', '--file']: |
---|
| 270 | binfo[FILENAME] = value |
---|
| 271 | |
---|
[24] | 272 | elif opt in ['-V', '--version']: |
---|
[23] | 273 | print VERSION |
---|
| 274 | sys.exit(0) |
---|
| 275 | |
---|
[20] | 276 | elif opt in ['-h', '--help']: |
---|
| 277 | print __doc__ |
---|
| 278 | sys.exit(0) |
---|
[19] | 279 | |
---|
[20] | 280 | |
---|
[2] | 281 | def main(): |
---|
[19] | 282 | # A dictionary holding the boot info |
---|
| 283 | # |
---|
| 284 | bootinfo = {} |
---|
| 285 | check_args(sys.argv, bootinfo) |
---|
[2] | 286 | |
---|
[19] | 287 | if bootinfo: |
---|
| 288 | check_cmd_line(bootinfo) |
---|
| 289 | else: |
---|
| 290 | interactive(bootinfo) |
---|
| 291 | |
---|
| 292 | create_links(bootinfo) |
---|
| 293 | |
---|
[2] | 294 | if __name__ == '__main__': |
---|
| 295 | main() |
---|