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