]>
Commit | Line | Data |
---|---|---|
a6f80f0e | 1 | #!/usr/bin/python |
904908bc RP |
2 | # |
3 | # Copyright 2014 Cumulus Networks, Inc. All rights reserved. | |
4 | # Author: Roopa Prabhu, roopa@cumulusnetworks.com | |
5 | # | |
6 | # ifupdown -- | |
7 | # tool to configure network interfaces | |
8 | # | |
a6f80f0e | 9 | import sys |
10 | import os | |
e6c9d007 | 11 | import argcomplete |
a6f80f0e | 12 | import argparse |
14dc390d | 13 | import ConfigParser |
14 | import StringIO | |
a6f80f0e | 15 | import logging |
261379f0 | 16 | import logging.handlers |
61636dcc | 17 | import resource |
d40e96ee | 18 | from ifupdown.ifupdownmain import * |
19 | from ifupdown.utils import * | |
a6f80f0e | 20 | |
1025163e | 21 | IFUPDOWN2_VERSION = '20170223-1' |
ac40c032 | 22 | |
a6f80f0e | 23 | lockfile="/run/network/.lock" |
14dc390d | 24 | configfile="/etc/network/ifupdown2/ifupdown2.conf" |
25 | configmap_g=None | |
a6f80f0e | 26 | logger = None |
3dcc1d0e | 27 | interfacesfileiobuf=None |
1e6d7bd7 | 28 | interfacesfilename=None |
03d5166b | 29 | ENVPATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
a6f80f0e | 30 | |
f802fe3c | 31 | def run_up(args): |
a6f80f0e | 32 | logger.debug('args = %s' %str(args)) |
33 | ||
34 | try: | |
739f665b | 35 | iflist = args.iflist |
36 | if len(args.iflist) == 0: | |
37 | iflist = None | |
a6f80f0e | 38 | logger.debug('creating ifupdown object ..') |
f802fe3c | 39 | cachearg=(False if (iflist or args.nocache or |
d08d5f54 | 40 | args.perfmode or args.noact) |
41 | else True) | |
14dc390d | 42 | ifupdown_handle = ifupdownMain(config=configmap_g, |
43 | force=args.force, | |
f802fe3c | 44 | withdepends=args.withdepends, |
45 | perfmode=args.perfmode, | |
f802fe3c | 46 | dryrun=args.noact, |
20dd6242 | 47 | cache=cachearg, |
48 | addons_enable=not args.noaddons, | |
14dc390d | 49 | statemanager_enable=not args.noaddons, |
1e6d7bd7 | 50 | interfacesfile=interfacesfilename, |
3dcc1d0e | 51 | interfacesfileiobuf=interfacesfileiobuf, |
52 | interfacesfileformat=args.interfacesfileformat) | |
20dd6242 | 53 | if args.noaddons: |
54 | ifupdown_handle.up(['up'], args.all, args.CLASS, iflist, | |
55 | excludepats=args.excludepats, | |
9dce3561 | 56 | printdependency=args.printdependency, |
ad25e7bb RP |
57 | syntaxcheck=args.syntaxcheck, type=args.type, |
58 | skipupperifaces=args.skipupperifaces) | |
20dd6242 | 59 | else: |
60 | ifupdown_handle.up(['pre-up', 'up', 'post-up'], | |
d08d5f54 | 61 | args.all, args.CLASS, iflist, |
739f665b | 62 | excludepats=args.excludepats, |
9dce3561 | 63 | printdependency=args.printdependency, |
ad25e7bb RP |
64 | syntaxcheck=args.syntaxcheck, type=args.type, |
65 | skipupperifaces=args.skipupperifaces) | |
f802fe3c | 66 | except: |
67 | raise | |
739f665b | 68 | |
f802fe3c | 69 | def run_down(args): |
70 | logger.debug('args = %s' %str(args)) | |
71 | ||
72 | try: | |
73 | iflist = args.iflist | |
f802fe3c | 74 | logger.debug('creating ifupdown object ..') |
14dc390d | 75 | ifupdown_handle = ifupdownMain(config=configmap_g, force=args.force, |
f802fe3c | 76 | withdepends=args.withdepends, |
77 | perfmode=args.perfmode, | |
f802fe3c | 78 | dryrun=args.noact, |
20dd6242 | 79 | addons_enable=not args.noaddons, |
14dc390d | 80 | statemanager_enable=not args.noaddons, |
1e6d7bd7 | 81 | interfacesfile=interfacesfilename, |
3dcc1d0e | 82 | interfacesfileiobuf=interfacesfileiobuf, |
83 | interfacesfileformat=args.interfacesfileformat) | |
f802fe3c | 84 | |
85 | ifupdown_handle.down(['pre-down', 'down', 'post-down'], | |
86 | args.all, args.CLASS, iflist, | |
87 | excludepats=args.excludepats, | |
5c721925 | 88 | printdependency=args.printdependency, |
2da58137 RP |
89 | usecurrentconfig=args.usecurrentconfig, |
90 | type=args.type) | |
f802fe3c | 91 | except: |
92 | raise | |
93 | ||
94 | def run_query(args): | |
95 | logger.debug('args = %s' %str(args)) | |
96 | ||
97 | try: | |
98 | iflist = args.iflist | |
f802fe3c | 99 | if args.checkcurr: |
100 | qop='query-checkcurr' | |
101 | elif args.running: | |
f802fe3c | 102 | qop='query-running' |
103 | elif args.raw: | |
104 | qop='query-raw' | |
105 | elif args.syntaxhelp: | |
106 | qop = 'query-syntax' | |
107 | elif args.printdependency: | |
108 | qop = 'query-dependency' | |
5c721925 | 109 | elif args.printsavedstate: |
110 | qop = 'query-savedstate' | |
f802fe3c | 111 | else: |
112 | qop='query' | |
83c1f241 | 113 | cachearg=(False if (iflist or args.nocache or |
114 | args.perfmode or args.syntaxhelp or | |
115 | (qop != 'query-checkcurr' and | |
116 | qop != 'query-running')) else True) | |
c798b0f4 | 117 | if not iflist and qop == 'query-running': |
118 | iflist = [i for i in os.listdir('/sys/class/net/') | |
119 | if os.path.isdir('/sys/class/net/%s' %i)] | |
83c1f241 | 120 | logger.debug('creating ifupdown object ..') |
14dc390d | 121 | ifupdown_handle = ifupdownMain(config=configmap_g, |
122 | withdepends=args.withdepends, | |
83c1f241 | 123 | perfmode=args.perfmode, |
14dc390d | 124 | cache=cachearg, |
1e6d7bd7 | 125 | interfacesfile=interfacesfilename, |
3dcc1d0e | 126 | interfacesfileiobuf=interfacesfileiobuf, |
6e16e5ae N |
127 | interfacesfileformat=args.interfacesfileformat, |
128 | withdefaults=args.withdefaults) | |
4c56a7c1 RP |
129 | # list implies all auto interfaces (this is how ifupdown behaves) |
130 | if args.list: | |
131 | args.all = True | |
132 | ifupdown_handle.query([qop], args.all, args.list, args.CLASS, iflist, | |
f802fe3c | 133 | excludepats=args.excludepats, |
134 | printdependency=args.printdependency, | |
6e16e5ae | 135 | format=args.format, type=args.type) |
f802fe3c | 136 | except: |
137 | raise | |
138 | ||
f802fe3c | 139 | def run_reload(args): |
140 | logger.debug('args = %s' %str(args)) | |
141 | ||
142 | try: | |
143 | logger.debug('creating ifupdown object ..') | |
14dc390d | 144 | ifupdown_handle = ifupdownMain(config=configmap_g, |
1e6d7bd7 | 145 | interfacesfile=interfacesfilename, |
14dc390d | 146 | withdepends=args.withdepends, |
cbdde74e RP |
147 | perfmode=args.perfmode, |
148 | dryrun=args.noact) | |
20dd6242 | 149 | ifupdown_handle.reload(['pre-up', 'up', 'post-up'], |
150 | ['pre-down', 'down', 'post-down'], | |
1720c392 | 151 | auto=args.all, allow=args.CLASS, ifacenames=None, |
14dc390d | 152 | excludepats=args.excludepats, |
2da58137 | 153 | usecurrentconfig=args.usecurrentconfig, |
cfa06db6 | 154 | syntaxcheck=args.syntaxcheck, |
2da58137 | 155 | currentlyup=args.currentlyup) |
a6f80f0e | 156 | except: |
157 | raise | |
158 | ||
a6f80f0e | 159 | def init(args): |
160 | global logger | |
3dcc1d0e | 161 | global interfacesfileiobuf |
1e6d7bd7 | 162 | global interfacesfilename |
a6f80f0e | 163 | |
164 | log_level = logging.WARNING | |
fe0a57d3 | 165 | if args.verbose: |
a6f80f0e | 166 | log_level = logging.INFO |
fe0a57d3 | 167 | if args.debug: |
a6f80f0e | 168 | log_level = logging.DEBUG |
169 | ||
170 | try: | |
261379f0 RP |
171 | if hasattr(args, 'syslog') and args.syslog: |
172 | root_logger = logging.getLogger() | |
173 | syslog_handler = logging.handlers.SysLogHandler(address='/dev/log', | |
174 | facility=logging.handlers.SysLogHandler.LOG_DAEMON) | |
175 | logging.addLevelName(logging.ERROR, 'error') | |
176 | logging.addLevelName(logging.WARNING, 'warning') | |
177 | logging.addLevelName(logging.DEBUG, 'debug') | |
178 | logging.addLevelName(logging.INFO, 'info') | |
179 | root_logger.setLevel(log_level) | |
180 | syslog_handler.setFormatter(logging.Formatter( | |
181 | '%(name)s: %(levelname)s: %(message)s')) | |
182 | root_logger.addHandler(syslog_handler) | |
183 | logger = logging.getLogger('ifupdown') | |
184 | else: | |
185 | logging.basicConfig(level=log_level, | |
186 | format='%(levelname)s: %(message)s') | |
187 | logging.addLevelName(logging.ERROR, 'error') | |
188 | logging.addLevelName(logging.WARNING, 'warning') | |
189 | logging.addLevelName(logging.DEBUG, 'debug') | |
190 | logging.addLevelName(logging.INFO, 'info') | |
191 | logger = logging.getLogger('ifupdown') | |
a6f80f0e | 192 | except: |
193 | raise | |
194 | ||
1e6d7bd7 ST |
195 | if hasattr(args, 'interfacesfile') and args.interfacesfile != None: |
196 | # Check to see if -i option is allowed by config file | |
c28fc55e ST |
197 | # But for ifquery, we will not check this |
198 | if (not sys.argv[0].endswith('ifquery') and | |
4b5208d8 | 199 | configmap_g.get('disable_cli_interfacesfile','0') == '1'): |
1e6d7bd7 ST |
200 | logger.error('disable_cli_interfacesfile is set so users ' |
201 | 'not allowed to specify interfaces file on cli.') | |
202 | exit(1) | |
203 | if args.interfacesfile == '-': | |
204 | # If interfaces file is stdin, read | |
205 | interfacesfileiobuf = sys.stdin.read() | |
206 | else: | |
207 | interfacesfilename = args.interfacesfile | |
208 | else: | |
209 | # if the ifupdown2 config file does not have it, default to standard | |
210 | interfacesfilename = configmap_g.get('default_interfaces_configfile', | |
211 | '/etc/network/interfaces') | |
212 | ||
213 | ||
214 | ||
a6f80f0e | 215 | |
216 | def deinit(): | |
eab25b7c | 217 | {} |
a6f80f0e | 218 | |
219 | def update_argparser(argparser): | |
cca03c30 | 220 | """ base parser, common to all commands """ |
37c0543d | 221 | |
222 | argparser.add_argument('-a', '--all', action='store_true', required=False, | |
cca03c30 | 223 | help='process all interfaces marked \"auto\"') |
a6f80f0e | 224 | argparser.add_argument('iflist', metavar='IFACE', |
f802fe3c | 225 | nargs='*', help='interface list separated by spaces. ' + |
226 | 'IFACE list is mutually exclusive with -a option.') | |
a6f80f0e | 227 | argparser.add_argument('-v', '--verbose', dest='verbose', |
228 | action='store_true', help='verbose') | |
229 | argparser.add_argument('-d', '--debug', dest='debug', | |
230 | action='store_true', | |
231 | help='output debug info') | |
232 | argparser.add_argument('-q', '--quiet', dest='quiet', | |
233 | action='store_true', | |
234 | help=argparse.SUPPRESS) | |
eab25b7c | 235 | argparser.add_argument('--allow', dest='CLASS', |
236 | help='ignore non-\"allow-CLASS\" interfaces') | |
14dc390d | 237 | argparser.add_argument('-w', '--with-depends', dest='withdepends', |
f802fe3c | 238 | action='store_true', help='run with all dependent interfaces.'+ |
239 | ' This option is redundant when \'-a\' is specified. With ' + | |
240 | '\'-a\' interfaces are always executed in dependency order') | |
3e8ee54f | 241 | argparser.add_argument('--perfmode', dest='perfmode', |
eab25b7c | 242 | action='store_true', help=argparse.SUPPRESS) |
14dc390d | 243 | #argparser.add_argument('-j', '--jobs', dest='jobs', type=int, |
244 | # default=-1, choices=range(1,12), help=argparse.SUPPRESS) | |
739f665b | 245 | argparser.add_argument('--nocache', dest='nocache', action='store_true', |
246 | help=argparse.SUPPRESS) | |
3e8ee54f | 247 | argparser.add_argument('-X', '--exclude', dest='excludepats', |
37c0543d | 248 | action='append', |
249 | help='Exclude interfaces from the list of interfaces' + | |
f802fe3c | 250 | ' to operate on. Can be specified multiple times.') |
14dc390d | 251 | argparser.add_argument('-i', '--interfaces', dest='interfacesfile', |
1e6d7bd7 ST |
252 | default=None, |
253 | help='Specify interfaces file instead of file defined ' + | |
254 | 'in ifupdown2.conf file') | |
3dcc1d0e | 255 | argparser.add_argument('-t', '--interfaces-format', |
256 | dest='interfacesfileformat', | |
257 | default='native', | |
258 | choices=['native', 'json'], | |
259 | help='interfaces file format') | |
2da58137 RP |
260 | argparser.add_argument('-T', '--type', |
261 | dest='type', | |
262 | default=None, | |
263 | choices=['iface', 'vlan'], | |
264 | help='type of interface entry (iface or vlan).' + | |
265 | 'This option can be used in case of ambiguity between ' + | |
266 | 'a vlan interface and an iface interface of the same name') | |
a6f80f0e | 267 | |
268 | def update_ifupdown_argparser(argparser): | |
cca03c30 | 269 | """ common arg parser for ifup and ifdown """ |
a6f80f0e | 270 | argparser.add_argument('-f', '--force', dest='force', |
271 | action='store_true', | |
272 | help='force run all operations') | |
261379f0 RP |
273 | argparser.add_argument('-l', '--syslog', dest='syslog', |
274 | action='store_true', | |
275 | help=argparse.SUPPRESS) | |
5c721925 | 276 | group = argparser.add_mutually_exclusive_group(required=False) |
277 | group.add_argument('-n', '--no-act', dest='noact', | |
3e8ee54f | 278 | action='store_true', help='print out what would happen,' + |
279 | 'but don\'t do it') | |
14dc390d | 280 | group.add_argument('-p', '--print-dependency', |
cca03c30 | 281 | dest='printdependency', choices=['list', 'dot'], |
282 | help='print iface dependency') | |
d40e96ee | 283 | group.add_argument('--no-scripts', '--admin-state', |
20dd6242 | 284 | dest='noaddons', action='store_true', |
14dc390d | 285 | help='dont run any addon modules/scripts. Only bring the ' + |
286 | 'interface administratively up/down') | |
a6f80f0e | 287 | |
288 | def update_ifup_argparser(argparser): | |
9dce3561 | 289 | argparser.add_argument('-s', '--syntax-check', dest='syntaxcheck', |
290 | action='store_true', | |
291 | help='Only run the interfaces file parser') | |
ad25e7bb RP |
292 | argparser.add_argument('-k', '--skip-upperifaces', dest='skipupperifaces', |
293 | action='store_true', | |
294 | help='ifup by default tries to add newly created interfaces' + | |
295 | ' into its upper/parent interfaces. Eg. if a bridge port is' + | |
296 | ' created as a result of ifup on the port, ifup automatically' + | |
297 | ' adds the port to the bridge. This option can be used to ' + | |
298 | 'disable this default behaviour') | |
a6f80f0e | 299 | update_ifupdown_argparser(argparser) |
300 | ||
301 | def update_ifdown_argparser(argparser): | |
302 | update_ifupdown_argparser(argparser) | |
522bf8e6 | 303 | argparser.add_argument('-u', '--use-current-config', |
5c721925 | 304 | dest='usecurrentconfig', action='store_true', |
14dc390d | 305 | help='By default ifdown looks at the saved state for ' + |
306 | 'interfaces to bring down. This option allows ifdown to ' + | |
307 | 'look at the current interfaces file. Useful when your ' + | |
308 | 'state file is corrupted or you want down to use the latest ' | |
309 | 'from the interfaces file') | |
a6f80f0e | 310 | |
311 | def update_ifquery_argparser(argparser): | |
cca03c30 | 312 | """ arg parser for ifquery options """ |
313 | ||
37c0543d | 314 | # -l is same as '-a', only here for backward compatibility |
4c56a7c1 RP |
315 | argparser.add_argument('-l', '--list', action='store_true', dest='list', |
316 | help='list all matching known interfaces') | |
a6f80f0e | 317 | group = argparser.add_mutually_exclusive_group(required=False) |
360d5f8e | 318 | group.add_argument('-r', '--running', dest='running', |
eab25b7c | 319 | action='store_true', |
320 | help='query running state of an interface') | |
360d5f8e | 321 | group.add_argument('-c', '--check', dest='checkcurr', |
739f665b | 322 | action='store_true', |
360d5f8e | 323 | help='check interface file contents against ' + |
324 | 'running state of an interface') | |
51a80991 | 325 | group.add_argument('-x', '--raw', action='store_true', dest='raw', |
d08d5f54 | 326 | help='print raw config file entries') |
5c721925 | 327 | group.add_argument('--print-savedstate', action='store_true', |
328 | dest='printsavedstate', | |
329 | help=argparse.SUPPRESS) | |
522bf8e6 | 330 | argparser.add_argument('-o', '--format', dest='format', default='native', |
14dc390d | 331 | choices=['native', 'json'], |
332 | help='interface display format') | |
333 | argparser.add_argument('-p', '--print-dependency', | |
d08d5f54 | 334 | dest='printdependency', choices=['list', 'dot'], |
335 | help='print interface dependency') | |
14dc390d | 336 | argparser.add_argument('-s', '--syntax-help', action='store_true', |
d08d5f54 | 337 | dest='syntaxhelp', |
338 | help='print supported interface config syntax') | |
669b422a RP |
339 | argparser.add_argument('--with-defaults', action='store_true', |
340 | dest='withdefaults', | |
341 | help='check policy default file contents, ' + | |
342 | 'for unconfigured attributes, against ' + | |
343 | 'running state of an interface') | |
37c0543d | 344 | |
739f665b | 345 | def update_ifreload_argparser(argparser): |
f802fe3c | 346 | """ parser for ifreload """ |
2da58137 RP |
347 | group = argparser.add_mutually_exclusive_group(required=True) |
348 | group.add_argument('-a', '--all', action='store_true', | |
f802fe3c | 349 | help='process all interfaces marked \"auto\"') |
2da58137 RP |
350 | group.add_argument('-c', '--currently-up', dest='currentlyup', |
351 | action='store_true', | |
352 | help='only reload auto and other interfaces that are ' + | |
353 | 'currently up') | |
1720c392 RP |
354 | group.add_argument('--allow', dest='CLASS', |
355 | help='ignore non-\"allow-CLASS\" interfaces') | |
f802fe3c | 356 | argparser.add_argument('iflist', metavar='IFACE', |
357 | nargs='*', help=argparse.SUPPRESS) | |
358 | argparser.add_argument('-n', '--no-act', dest='noact', | |
cbdde74e RP |
359 | action='store_true', help='print out what would happen,' + |
360 | 'but don\'t do it') | |
f802fe3c | 361 | argparser.add_argument('-v', '--verbose', dest='verbose', |
362 | action='store_true', help='verbose') | |
363 | argparser.add_argument('-d', '--debug', dest='debug', | |
37c0543d | 364 | action='store_true', |
f802fe3c | 365 | help='output debug info') |
14dc390d | 366 | argparser.add_argument('-w', '--with-depends', dest='withdepends', |
f802fe3c | 367 | action='store_true', help=argparse.SUPPRESS) |
368 | argparser.add_argument('--perfmode', dest='perfmode', | |
369 | action='store_true', help=argparse.SUPPRESS) | |
370 | argparser.add_argument('--nocache', dest='nocache', action='store_true', | |
371 | help=argparse.SUPPRESS) | |
372 | argparser.add_argument('-X', '--exclude', dest='excludepats', | |
373 | action='append', | |
374 | help=argparse.SUPPRESS) | |
14dc390d | 375 | #argparser.add_argument('-j', '--jobs', dest='jobs', type=int, |
376 | # default=-1, choices=range(1,12), help=argparse.SUPPRESS) | |
3dcc1d0e | 377 | #argparser.add_argument('-i', '--interfaces', dest='interfacesfile', |
378 | # default='/etc/network/interfaces', | |
379 | # help='use interfaces file instead of default ' + | |
380 | # '/etc/network/interfaces') | |
522bf8e6 | 381 | argparser.add_argument('-u', '--use-current-config', |
14dc390d | 382 | dest='usecurrentconfig', action='store_true', |
383 | help='By default ifreload looks at saved state for ' + | |
384 | 'interfaces to bring down. With this option ifreload will' | |
385 | ' only look at the current interfaces file. Useful when your ' + | |
386 | 'state file is corrupted or you want down to use the latest ' | |
387 | 'from the interfaces file') | |
261379f0 RP |
388 | argparser.add_argument('-l', '--syslog', dest='syslog', |
389 | action='store_true', | |
390 | help=argparse.SUPPRESS) | |
d462f1fa RP |
391 | argparser.add_argument('-f', '--force', dest='force', |
392 | action='store_true', | |
393 | help='force run all operations') | |
cfa06db6 RP |
394 | argparser.add_argument('-s', '--syntax-check', dest='syntaxcheck', |
395 | action='store_true', | |
396 | help='Only run the interfaces file parser') | |
37c0543d | 397 | |
0c792103 | 398 | def update_common_argparser(argparser): |
041ac313 JF |
399 | ''' general parsing rules ''' |
400 | ||
041ac313 JF |
401 | argparser.add_argument('-V', '--version', |
402 | action='version', | |
ac40c032 | 403 | version='ifupdown2:%(prog)s ' + IFUPDOWN2_VERSION, |
041ac313 JF |
404 | help='display current ifupdown2 version') |
405 | ||
a6f80f0e | 406 | def parse_args(argsv, op): |
cca03c30 | 407 | if op == 'query': |
37c0543d | 408 | descr = 'query interfaces (all or interface list)' |
91067b3d | 409 | elif op == 'reload': |
32c7378d RP |
410 | descr = 'reload interface configuration.\n\n \ |
411 | ifreload downs interfaces that were removed\n \ | |
412 | from the interfaces file and subsequently runs ifup\n \ | |
413 | on all interfaces. ifreload is non-disruptive. It will fix\n \ | |
414 | running config to match what is configured in the interfaces\n \ | |
415 | file without bringing the interface down. It may flap an interface\n\ | |
416 | for some attribute changes. Please see man ifreload\n \ | |
417 | for details.' | |
cca03c30 | 418 | else: |
419 | descr = 'interface management' | |
a6f80f0e | 420 | argparser = argparse.ArgumentParser(description=descr) |
f802fe3c | 421 | if op == 'reload': |
739f665b | 422 | update_ifreload_argparser(argparser) |
f802fe3c | 423 | else: |
424 | update_argparser(argparser) | |
425 | if op == 'up': | |
426 | update_ifup_argparser(argparser) | |
427 | elif op == 'down': | |
428 | update_ifdown_argparser(argparser) | |
429 | elif op == 'query': | |
430 | update_ifquery_argparser(argparser) | |
0c792103 | 431 | update_common_argparser(argparser) |
e6c9d007 | 432 | argcomplete.autocomplete(argparser) |
a6f80f0e | 433 | return argparser.parse_args(argsv) |
434 | ||
f802fe3c | 435 | handlers = {'up' : run_up, |
436 | 'down' : run_down, | |
437 | 'query' : run_query, | |
438 | 'reload' : run_reload } | |
439 | ||
9dce3561 | 440 | def validate_args(op, args): |
3dcc1d0e | 441 | #if op == 'up' and args.syntaxcheck: |
442 | # if args.iflist or args.all: | |
443 | # print 'ignoring interface options ..' | |
444 | # return True | |
4c56a7c1 | 445 | if op == 'query' and (args.syntaxhelp or args.list): |
9dce3561 | 446 | return True |
fffdae9c | 447 | if op == 'reload': |
1720c392 RP |
448 | if not args.all and not args.currentlyup and not args.CLASS: |
449 | print '\'-a\' or \'-c\' or \'-allow\' option is required' | |
fffdae9c RP |
450 | return False |
451 | elif (not args.iflist and | |
452 | not args.all and not args.CLASS): | |
9dce3561 | 453 | print '\'-a\' option or interface list are required' |
454 | return False | |
455 | if args.iflist and args.all: | |
456 | print '\'-a\' option and interface list are mutually exclusive' | |
457 | return False | |
fffdae9c | 458 | if op != 'reload' and args.CLASS and (args.all or args.iflist): |
5ee3e1a8 RP |
459 | print ('\'--allow\' option is mutually exclusive ' + |
460 | 'with interface list and \'-a\'') | |
461 | return False | |
9dce3561 | 462 | return True |
463 | ||
cebe79c9 | 464 | def read_config(args): |
14dc390d | 465 | global configmap_g |
466 | ||
a193d8d1 JF |
467 | with open(configfile, 'r') as f: |
468 | config = f.read() | |
14dc390d | 469 | configStr = '[ifupdown2]\n' + config |
470 | configFP = StringIO.StringIO(configStr) | |
471 | parser = ConfigParser.RawConfigParser() | |
472 | parser.readfp(configFP) | |
473 | configmap_g = dict(parser.items('ifupdown2')) | |
474 | ||
8e113d63 RP |
475 | # Preprocess config map |
476 | configval = configmap_g.get('multiple_vlan_aware_bridge_support', '0') | |
477 | if configval == '0': | |
478 | # if multiple bridges not allowed, set the bridge-vlan-aware | |
479 | # attribute in the 'no_repeats' config, so that the ifupdownmain | |
480 | # module can catch it appropriately | |
481 | configmap_g['no_repeats'] = {'bridge-vlan-aware' : 'yes'} | |
482 | ||
cebe79c9 RP |
483 | |
484 | configval = configmap_g.get('link_master_slave', '0') | |
485 | if configval == '1': | |
486 | # link_master_slave is only valid when all is set | |
487 | if hasattr(args, 'all') and not args.all: | |
488 | configmap_g['link_master_slave'] = '0' | |
489 | ||
490 | configval = configmap_g.get('delay_admin_state_change', '0') | |
491 | if configval == '1': | |
492 | # reset link_master_slave if delay_admin_state_change is on | |
493 | configmap_g['link_master_slave'] = '0' | |
494 | ||
a6f80f0e | 495 | def main(argv): |
496 | """ main function """ | |
f802fe3c | 497 | args = None |
a6f80f0e | 498 | try: |
499 | op = None | |
fe0a57d3 | 500 | if argv[0].endswith('ifup'): |
a6f80f0e | 501 | op = 'up' |
fe0a57d3 | 502 | elif argv[0].endswith('ifdown'): |
a6f80f0e | 503 | op = 'down' |
fe0a57d3 | 504 | elif argv[0].endswith('ifquery'): |
a6f80f0e | 505 | op = 'query' |
fe0a57d3 | 506 | elif argv[0].endswith('ifreload'): |
739f665b | 507 | op = 'reload' |
a6f80f0e | 508 | else: |
509 | print ('Unexpected executable.' + | |
510 | ' Should be \'ifup\' or \'ifdown\' or \'ifquery\'') | |
511 | exit(1) | |
a6f80f0e | 512 | # Command line arg parser |
513 | args = parse_args(argv[1:], op) | |
9dce3561 | 514 | if not validate_args(op, args): |
a6f80f0e | 515 | exit(1) |
522bf8e6 RP |
516 | |
517 | if not sys.argv[0].endswith('ifquery') and not os.geteuid() == 0: | |
518 | print 'error: must be root to run this command' | |
519 | exit(1) | |
520 | ||
521 | if not sys.argv[0].endswith('ifquery') and not utils.lockFile(lockfile): | |
522 | print 'Another instance of this program is already running.' | |
523 | exit(0) | |
524 | ||
cebe79c9 | 525 | read_config(args) |
a6f80f0e | 526 | init(args) |
f802fe3c | 527 | handlers.get(op)(args) |
a6f80f0e | 528 | except Exception, e: |
fe0a57d3 | 529 | if not str(e): |
eab25b7c | 530 | exit(1) |
f802fe3c | 531 | if args and args.debug: |
a6f80f0e | 532 | raise |
533 | else: | |
f802fe3c | 534 | if logger: |
535 | logger.error(str(e)) | |
536 | else: | |
537 | print str(e) | |
86fc62e2 | 538 | #if args and not args.debug: |
539 | # print '\nrerun the command with \'-d\' for a detailed errormsg' | |
eab25b7c | 540 | exit(1) |
a6f80f0e | 541 | finally: |
542 | deinit() | |
543 | ||
a6f80f0e | 544 | if __name__ == "__main__": |
545 | ||
03d5166b | 546 | # required during boot |
547 | os.putenv('PATH', ENVPATH) | |
61636dcc | 548 | resource.setrlimit(resource.RLIMIT_CORE, (0,0)) |
a6f80f0e | 549 | main(sys.argv) |