| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | #!/usr/bin/python | 
					
						
							| 
									
										
										
										
											2014-07-22 11:15:56 -07:00
										 |  |  | # | 
					
						
							|  |  |  | # Copyright 2014 Cumulus Networks, Inc. All rights reserved. | 
					
						
							|  |  |  | # Author: Roopa Prabhu, roopa@cumulusnetworks.com | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # ifupdown -- | 
					
						
							|  |  |  | #    tool to configure network interfaces | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | import sys | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2014-03-13 12:06:18 -07:00
										 |  |  | import argcomplete | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  | import ConfigParser | 
					
						
							|  |  |  | import StringIO | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2014-11-07 15:18:42 -08:00
										 |  |  | import logging.handlers | 
					
						
							| 
									
										
										
										
											2014-10-13 09:55:21 -07:00
										 |  |  | import resource | 
					
						
							| 
									
										
										
										
											2016-03-28 21:23:03 +02:00
										 |  |  | import pkg_resources | 
					
						
							| 
									
										
										
										
											2014-04-25 16:09:14 -07:00
										 |  |  | from ifupdown.ifupdownmain import * | 
					
						
							|  |  |  | from ifupdown.utils import * | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | lockfile="/run/network/.lock" | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  | configfile="/etc/network/ifupdown2/ifupdown2.conf" | 
					
						
							|  |  |  | configmap_g=None | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | logger = None | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  | interfacesfileiobuf=None | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  | interfacesfilename=None | 
					
						
							| 
									
										
										
										
											2014-04-14 22:56:51 -07:00
										 |  |  | ENVPATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  | def run_up(args): | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     logger.debug('args = %s' %str(args)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |         iflist = args.iflist | 
					
						
							|  |  |  |         if len(args.iflist) == 0: | 
					
						
							|  |  |  |             iflist = None | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |         logger.debug('creating ifupdown object ..') | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |         cachearg=(False if (iflist or args.nocache or | 
					
						
							| 
									
										
										
										
											2014-02-08 09:05:32 -08:00
										 |  |  |                             args.perfmode or args.noact) | 
					
						
							|  |  |  |                             else True) | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |         ifupdown_handle = ifupdownMain(config=configmap_g, | 
					
						
							|  |  |  |                                        force=args.force, | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                                        withdepends=args.withdepends, | 
					
						
							|  |  |  |                                        perfmode=args.perfmode, | 
					
						
							|  |  |  |                                        dryrun=args.noact, | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |                                        cache=cachearg, | 
					
						
							|  |  |  |                                        addons_enable=not args.noaddons, | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                                        statemanager_enable=not args.noaddons, | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |                                        interfacesfile=interfacesfilename, | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |                                        interfacesfileiobuf=interfacesfileiobuf, | 
					
						
							|  |  |  |                                        interfacesfileformat=args.interfacesfileformat) | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |         if args.noaddons: | 
					
						
							|  |  |  |             ifupdown_handle.up(['up'], args.all, args.CLASS, iflist, | 
					
						
							|  |  |  |                                excludepats=args.excludepats, | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |                                printdependency=args.printdependency, | 
					
						
							| 
									
										
										
										
											2014-11-25 10:19:35 -08:00
										 |  |  |                                syntaxcheck=args.syntaxcheck, type=args.type, | 
					
						
							|  |  |  |                                skipupperifaces=args.skipupperifaces) | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             ifupdown_handle.up(['pre-up', 'up', 'post-up'], | 
					
						
							| 
									
										
										
										
											2014-02-08 09:05:32 -08:00
										 |  |  |                                args.all, args.CLASS, iflist, | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |                                excludepats=args.excludepats, | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |                                printdependency=args.printdependency, | 
					
						
							| 
									
										
										
										
											2014-11-25 10:19:35 -08:00
										 |  |  |                                syntaxcheck=args.syntaxcheck, type=args.type, | 
					
						
							|  |  |  |                                skipupperifaces=args.skipupperifaces) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     except: | 
					
						
							|  |  |  |         raise | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  | def run_down(args): | 
					
						
							|  |  |  |     logger.debug('args = %s' %str(args)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         iflist = args.iflist | 
					
						
							|  |  |  |         logger.debug('creating ifupdown object ..') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |         ifupdown_handle = ifupdownMain(config=configmap_g, force=args.force, | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                                        withdepends=args.withdepends, | 
					
						
							|  |  |  |                                        perfmode=args.perfmode, | 
					
						
							|  |  |  |                                        dryrun=args.noact, | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |                                        addons_enable=not args.noaddons, | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                                        statemanager_enable=not args.noaddons, | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |                                        interfacesfile=interfacesfilename, | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |                                        interfacesfileiobuf=interfacesfileiobuf, | 
					
						
							|  |  |  |                                        interfacesfileformat=args.interfacesfileformat) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ifupdown_handle.down(['pre-down', 'down', 'post-down'], | 
					
						
							|  |  |  |                              args.all, args.CLASS, iflist, | 
					
						
							|  |  |  |                              excludepats=args.excludepats, | 
					
						
							| 
									
										
										
										
											2014-03-06 06:41:28 -08:00
										 |  |  |                              printdependency=args.printdependency, | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |                              usecurrentconfig=args.usecurrentconfig, | 
					
						
							|  |  |  |                              type=args.type) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     except: | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def run_query(args): | 
					
						
							|  |  |  |     logger.debug('args = %s' %str(args)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         iflist = args.iflist | 
					
						
							|  |  |  |         if args.checkcurr: | 
					
						
							|  |  |  |             qop='query-checkcurr' | 
					
						
							|  |  |  |         elif args.running: | 
					
						
							|  |  |  |             qop='query-running' | 
					
						
							|  |  |  |         elif args.raw: | 
					
						
							|  |  |  |             qop='query-raw' | 
					
						
							|  |  |  |         elif args.syntaxhelp: | 
					
						
							|  |  |  |             qop = 'query-syntax' | 
					
						
							|  |  |  |         elif args.printdependency: | 
					
						
							|  |  |  |             qop = 'query-dependency' | 
					
						
							| 
									
										
										
										
											2014-03-06 06:41:28 -08:00
										 |  |  |         elif args.printsavedstate: | 
					
						
							|  |  |  |             qop = 'query-savedstate' | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             qop='query' | 
					
						
							| 
									
										
										
										
											2014-02-15 21:39:13 -08:00
										 |  |  |         cachearg=(False if (iflist or args.nocache or | 
					
						
							|  |  |  |                             args.perfmode or args.syntaxhelp or | 
					
						
							|  |  |  |                             (qop != 'query-checkcurr' and | 
					
						
							|  |  |  |                             qop != 'query-running')) else True) | 
					
						
							| 
									
										
										
										
											2014-03-18 16:38:00 -07:00
										 |  |  |         if not iflist and qop == 'query-running': | 
					
						
							|  |  |  |             iflist = [i for i in os.listdir('/sys/class/net/') | 
					
						
							|  |  |  |                         if os.path.isdir('/sys/class/net/%s' %i)] | 
					
						
							| 
									
										
										
										
											2014-02-15 21:39:13 -08:00
										 |  |  |         logger.debug('creating ifupdown object ..') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |         ifupdown_handle = ifupdownMain(config=configmap_g, | 
					
						
							|  |  |  |                                        withdepends=args.withdepends, | 
					
						
							| 
									
										
										
										
											2014-02-15 21:39:13 -08:00
										 |  |  |                                        perfmode=args.perfmode, | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                                        cache=cachearg, | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |                                        interfacesfile=interfacesfilename, | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |                                        interfacesfileiobuf=interfacesfileiobuf, | 
					
						
							|  |  |  |                                        interfacesfileformat=args.interfacesfileformat) | 
					
						
							| 
									
										
										
										
											2014-02-15 21:39:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |         ifupdown_handle.query([qop], args.all, args.CLASS, iflist, | 
					
						
							|  |  |  |                               excludepats=args.excludepats, | 
					
						
							|  |  |  |                               printdependency=args.printdependency, | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |                               format=args.format, type=args.type) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     except: | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def run_reload(args): | 
					
						
							|  |  |  |     logger.debug('args = %s' %str(args)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         logger.debug('creating ifupdown object ..') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |         ifupdown_handle = ifupdownMain(config=configmap_g, | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |                                        interfacesfile=interfacesfilename, | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                                        withdepends=args.withdepends, | 
					
						
							|  |  |  |                                        perfmode=args.perfmode) | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |         ifupdown_handle.reload(['pre-up', 'up', 'post-up'], | 
					
						
							|  |  |  |                                ['pre-down', 'down', 'post-down'], | 
					
						
							| 
									
										
										
										
											2015-11-13 10:06:16 -08:00
										 |  |  |                                auto=args.all, allow=args.CLASS, ifacenames=None, | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                                excludepats=args.excludepats, | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |                                usecurrentconfig=args.usecurrentconfig, | 
					
						
							| 
									
										
										
										
											2015-11-22 16:26:14 -08:00
										 |  |  |                                syntaxcheck=args.syntaxcheck, | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |                                currentlyup=args.currentlyup) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     except: | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init(args): | 
					
						
							|  |  |  |     global logger | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |     global interfacesfileiobuf | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |     global interfacesfilename | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     log_level = logging.WARNING | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |     if args.verbose: | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |         log_level = logging.INFO | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |     if args.debug: | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |         log_level = logging.DEBUG | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2014-11-07 15:18:42 -08:00
										 |  |  |         if hasattr(args, 'syslog') and args.syslog: | 
					
						
							|  |  |  |             root_logger = logging.getLogger() | 
					
						
							|  |  |  |             syslog_handler = logging.handlers.SysLogHandler(address='/dev/log', | 
					
						
							|  |  |  |                              facility=logging.handlers.SysLogHandler.LOG_DAEMON) | 
					
						
							|  |  |  |             logging.addLevelName(logging.ERROR, 'error') | 
					
						
							|  |  |  |             logging.addLevelName(logging.WARNING, 'warning') | 
					
						
							|  |  |  |             logging.addLevelName(logging.DEBUG, 'debug') | 
					
						
							|  |  |  |             logging.addLevelName(logging.INFO, 'info') | 
					
						
							|  |  |  |             root_logger.setLevel(log_level) | 
					
						
							|  |  |  |             syslog_handler.setFormatter(logging.Formatter( | 
					
						
							|  |  |  |                 '%(name)s: %(levelname)s: %(message)s')) | 
					
						
							|  |  |  |             root_logger.addHandler(syslog_handler) | 
					
						
							|  |  |  |             logger = logging.getLogger('ifupdown') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             logging.basicConfig(level=log_level, | 
					
						
							|  |  |  |                 format='%(levelname)s: %(message)s') | 
					
						
							|  |  |  |             logging.addLevelName(logging.ERROR, 'error') | 
					
						
							|  |  |  |             logging.addLevelName(logging.WARNING, 'warning') | 
					
						
							|  |  |  |             logging.addLevelName(logging.DEBUG, 'debug') | 
					
						
							|  |  |  |             logging.addLevelName(logging.INFO, 'info') | 
					
						
							|  |  |  |             logger = logging.getLogger('ifupdown') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     except: | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |     if hasattr(args, 'interfacesfile') and args.interfacesfile != None: | 
					
						
							|  |  |  |         # Check to see if -i option is allowed by config file | 
					
						
							| 
									
										
										
										
											2015-10-09 18:00:49 -04:00
										 |  |  |         # But for ifquery, we will not check this | 
					
						
							|  |  |  |         if (not sys.argv[0].endswith('ifquery') and | 
					
						
							| 
									
										
										
										
											2015-10-09 16:09:10 -07:00
										 |  |  |                 configmap_g.get('disable_cli_interfacesfile','0') == '1'): | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |             logger.error('disable_cli_interfacesfile is set so users ' | 
					
						
							|  |  |  |                          'not allowed to specify interfaces file on cli.') | 
					
						
							|  |  |  |             exit(1) | 
					
						
							|  |  |  |         if args.interfacesfile == '-': | 
					
						
							|  |  |  |             # If interfaces file is stdin, read | 
					
						
							|  |  |  |             interfacesfileiobuf = sys.stdin.read() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             interfacesfilename = args.interfacesfile | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         # if the ifupdown2 config file does not have it, default to standard | 
					
						
							|  |  |  |         interfacesfilename = configmap_g.get('default_interfaces_configfile', | 
					
						
							|  |  |  |                                              '/etc/network/interfaces') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def deinit(): | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |     {} | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def update_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |     """ base parser, common to all commands """ | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     argparser.add_argument('-a', '--all', action='store_true', required=False, | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |                 help='process all interfaces marked \"auto\"') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     argparser.add_argument('iflist', metavar='IFACE', | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 nargs='*', help='interface list separated by spaces. ' + | 
					
						
							|  |  |  |                 'IFACE list is mutually exclusive with -a option.') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     argparser.add_argument('-v', '--verbose', dest='verbose', | 
					
						
							|  |  |  |                 action='store_true', help='verbose') | 
					
						
							|  |  |  |     argparser.add_argument('-d', '--debug', dest='debug', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='output debug info') | 
					
						
							|  |  |  |     argparser.add_argument('-q', '--quiet', dest='quiet', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |     argparser.add_argument('--allow', dest='CLASS',  | 
					
						
							|  |  |  |                 help='ignore non-\"allow-CLASS\" interfaces') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     argparser.add_argument('-w', '--with-depends', dest='withdepends', | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 action='store_true', help='run with all dependent interfaces.'+ | 
					
						
							|  |  |  |                 ' This option is redundant when \'-a\' is specified. With ' + | 
					
						
							|  |  |  |                 '\'-a\' interfaces are always executed in dependency order') | 
					
						
							| 
									
										
										
										
											2013-11-13 16:07:15 -08:00
										 |  |  |     argparser.add_argument('--perfmode', dest='perfmode', | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |                 action='store_true', help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     #argparser.add_argument('-j', '--jobs', dest='jobs', type=int, | 
					
						
							|  |  |  |     #            default=-1, choices=range(1,12), help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |     argparser.add_argument('--nocache', dest='nocache', action='store_true', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2013-11-13 16:07:15 -08:00
										 |  |  |     argparser.add_argument('-X', '--exclude', dest='excludepats', | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  |                 action='append', | 
					
						
							|  |  |  |                 help='Exclude interfaces from the list of interfaces' + | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 ' to operate on. Can be specified multiple times.') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     argparser.add_argument('-i', '--interfaces', dest='interfacesfile', | 
					
						
							| 
									
										
										
										
											2015-08-20 22:59:44 -04:00
										 |  |  |                 default=None, | 
					
						
							|  |  |  |                 help='Specify interfaces file instead of file defined ' + | 
					
						
							|  |  |  |                 'in ifupdown2.conf file') | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |     argparser.add_argument('-t', '--interfaces-format', | 
					
						
							|  |  |  |                 dest='interfacesfileformat', | 
					
						
							|  |  |  |                 default='native', | 
					
						
							|  |  |  |                 choices=['native', 'json'], | 
					
						
							|  |  |  |                 help='interfaces file format') | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |     argparser.add_argument('-T', '--type', | 
					
						
							|  |  |  |                 dest='type', | 
					
						
							|  |  |  |                 default=None, | 
					
						
							|  |  |  |                 choices=['iface', 'vlan'], | 
					
						
							|  |  |  |                 help='type of interface entry (iface or vlan).' + | 
					
						
							|  |  |  |                      'This option can be used in case of ambiguity between ' + | 
					
						
							|  |  |  |                      'a vlan interface and an iface interface of the same name') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def update_ifupdown_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |     """ common arg parser for ifup and ifdown """ | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     argparser.add_argument('-f', '--force', dest='force', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='force run all operations') | 
					
						
							| 
									
										
										
										
											2014-11-07 15:18:42 -08:00
										 |  |  |     argparser.add_argument('-l', '--syslog', dest='syslog', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-03-06 06:41:28 -08:00
										 |  |  |     group = argparser.add_mutually_exclusive_group(required=False) | 
					
						
							|  |  |  |     group.add_argument('-n', '--no-act', dest='noact', | 
					
						
							| 
									
										
										
										
											2013-11-13 16:07:15 -08:00
										 |  |  |                 action='store_true', help='print out what would happen,' + | 
					
						
							|  |  |  |                 'but don\'t do it') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     group.add_argument('-p', '--print-dependency', | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |                 dest='printdependency', choices=['list', 'dot'], | 
					
						
							|  |  |  |                 help='print iface dependency') | 
					
						
							| 
									
										
										
										
											2014-04-25 16:09:14 -07:00
										 |  |  |     group.add_argument('--no-scripts', '--admin-state', | 
					
						
							| 
									
										
										
										
											2014-02-26 08:09:44 -08:00
										 |  |  |                 dest='noaddons',  action='store_true', | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                 help='dont run any addon modules/scripts. Only bring the ' + | 
					
						
							|  |  |  |                     'interface administratively up/down') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def update_ifup_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |     argparser.add_argument('-s', '--syntax-check', dest='syntaxcheck', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='Only run the interfaces file parser') | 
					
						
							| 
									
										
										
										
											2014-11-25 10:19:35 -08:00
										 |  |  |     argparser.add_argument('-k', '--skip-upperifaces', dest='skipupperifaces', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='ifup by default tries to add newly created interfaces' + | 
					
						
							|  |  |  |                 ' into its upper/parent interfaces. Eg. if a bridge port is' + | 
					
						
							|  |  |  |                 ' created as a result of ifup on the port, ifup automatically' + | 
					
						
							|  |  |  |                 ' adds the port to the bridge. This option can be used to ' + | 
					
						
							|  |  |  |                 'disable this default behaviour') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     update_ifupdown_argparser(argparser) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def update_ifdown_argparser(argparser): | 
					
						
							|  |  |  |     update_ifupdown_argparser(argparser) | 
					
						
							| 
									
										
										
										
											2014-06-06 23:00:24 -07:00
										 |  |  |     argparser.add_argument('-u', '--use-current-config', | 
					
						
							| 
									
										
										
										
											2014-03-06 06:41:28 -08:00
										 |  |  |                 dest='usecurrentconfig',  action='store_true', | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                 help='By default ifdown looks at the saved state for ' + | 
					
						
							|  |  |  |                 'interfaces to bring down. This option allows ifdown to ' + | 
					
						
							|  |  |  |                 'look at the current interfaces file. Useful when your ' + | 
					
						
							|  |  |  |                 'state file is corrupted or you want down to use the latest ' | 
					
						
							|  |  |  |                 'from the interfaces file') | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def update_ifquery_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |     """ arg parser for ifquery options """ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  |     # -l is same as '-a', only here for backward compatibility | 
					
						
							| 
									
										
										
										
											2013-11-13 16:07:15 -08:00
										 |  |  |     argparser.add_argument('-l', '--list', action='store_true', dest='all', | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     group = argparser.add_mutually_exclusive_group(required=False) | 
					
						
							| 
									
										
										
										
											2014-02-11 08:09:28 -08:00
										 |  |  |     group.add_argument('-r', '--running', dest='running', | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |                        action='store_true', | 
					
						
							|  |  |  |                        help='query running state of an interface') | 
					
						
							| 
									
										
										
										
											2014-02-11 08:09:28 -08:00
										 |  |  |     group.add_argument('-c', '--check', dest='checkcurr', | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |                        action='store_true', | 
					
						
							| 
									
										
										
										
											2014-02-11 08:09:28 -08:00
										 |  |  |                        help='check interface file contents against ' + | 
					
						
							|  |  |  |                        'running state of an interface') | 
					
						
							| 
									
										
										
										
											2014-06-07 08:14:10 -07:00
										 |  |  |     group.add_argument('-x', '--raw', action='store_true', dest='raw', | 
					
						
							| 
									
										
										
										
											2014-02-08 09:05:32 -08:00
										 |  |  |                        help='print raw config file entries') | 
					
						
							| 
									
										
										
										
											2014-03-06 06:41:28 -08:00
										 |  |  |     group.add_argument('--print-savedstate', action='store_true', | 
					
						
							|  |  |  |                        dest='printsavedstate', | 
					
						
							|  |  |  |                        help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-06-06 23:00:24 -07:00
										 |  |  |     argparser.add_argument('-o', '--format', dest='format', default='native', | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                            choices=['native', 'json'], | 
					
						
							|  |  |  |                            help='interface display format') | 
					
						
							|  |  |  |     argparser.add_argument('-p', '--print-dependency', | 
					
						
							| 
									
										
										
										
											2014-02-08 09:05:32 -08:00
										 |  |  |                            dest='printdependency', choices=['list', 'dot'], | 
					
						
							|  |  |  |                            help='print interface dependency') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     argparser.add_argument('-s', '--syntax-help', action='store_true', | 
					
						
							| 
									
										
										
										
											2014-02-08 09:05:32 -08:00
										 |  |  |                            dest='syntaxhelp', | 
					
						
							|  |  |  |                            help='print supported interface config syntax') | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  | def update_ifreload_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     """ parser for ifreload """ | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |     group = argparser.add_mutually_exclusive_group(required=True) | 
					
						
							|  |  |  |     group.add_argument('-a', '--all', action='store_true', | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 help='process all interfaces marked \"auto\"') | 
					
						
							| 
									
										
										
										
											2014-11-11 21:58:12 -08:00
										 |  |  |     group.add_argument('-c', '--currently-up', dest='currentlyup', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='only reload auto and other interfaces that are ' + | 
					
						
							|  |  |  |                 'currently up') | 
					
						
							| 
									
										
										
										
											2015-11-13 10:06:16 -08:00
										 |  |  |     group.add_argument('--allow', dest='CLASS', | 
					
						
							|  |  |  |                 help='ignore non-\"allow-CLASS\" interfaces') | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     argparser.add_argument('iflist', metavar='IFACE', | 
					
						
							|  |  |  |                 nargs='*', help=argparse.SUPPRESS) | 
					
						
							|  |  |  |     argparser.add_argument('-n', '--no-act', dest='noact', | 
					
						
							|  |  |  |                 action='store_true', help=argparse.SUPPRESS) | 
					
						
							|  |  |  |     argparser.add_argument('-v', '--verbose', dest='verbose', | 
					
						
							|  |  |  |                 action='store_true', help='verbose') | 
					
						
							|  |  |  |     argparser.add_argument('-d', '--debug', dest='debug', | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  |                 action='store_true', | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 help='output debug info') | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     argparser.add_argument('-w', '--with-depends', dest='withdepends', | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |                 action='store_true', help=argparse.SUPPRESS) | 
					
						
							|  |  |  |     argparser.add_argument('--perfmode', dest='perfmode', | 
					
						
							|  |  |  |                 action='store_true', help=argparse.SUPPRESS) | 
					
						
							|  |  |  |     argparser.add_argument('--nocache', dest='nocache', action='store_true', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							|  |  |  |     argparser.add_argument('-X', '--exclude', dest='excludepats', | 
					
						
							|  |  |  |                 action='append', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     #argparser.add_argument('-j', '--jobs', dest='jobs', type=int, | 
					
						
							|  |  |  |     #            default=-1, choices=range(1,12), help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |     #argparser.add_argument('-i', '--interfaces', dest='interfacesfile', | 
					
						
							|  |  |  |     #            default='/etc/network/interfaces', | 
					
						
							|  |  |  |     #            help='use interfaces file instead of default ' + | 
					
						
							|  |  |  |     #            '/etc/network/interfaces') | 
					
						
							| 
									
										
										
										
											2014-06-06 23:00:24 -07:00
										 |  |  |     argparser.add_argument('-u', '--use-current-config', | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |                 dest='usecurrentconfig',  action='store_true', | 
					
						
							|  |  |  |                 help='By default ifreload looks at saved state for ' + | 
					
						
							|  |  |  |                 'interfaces to bring down. With this option ifreload will' | 
					
						
							|  |  |  |                 ' only look at the current interfaces file. Useful when your ' + | 
					
						
							|  |  |  |                 'state file is corrupted or you want down to use the latest ' | 
					
						
							|  |  |  |                 'from the interfaces file') | 
					
						
							| 
									
										
										
										
											2014-11-07 15:18:42 -08:00
										 |  |  |     argparser.add_argument('-l', '--syslog', dest='syslog', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2015-01-03 00:27:35 -08:00
										 |  |  |     argparser.add_argument('-f', '--force', dest='force', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='force run all operations') | 
					
						
							| 
									
										
										
										
											2015-11-22 16:26:14 -08:00
										 |  |  |     argparser.add_argument('-s', '--syntax-check', dest='syntaxcheck', | 
					
						
							|  |  |  |                 action='store_true', | 
					
						
							|  |  |  |                 help='Only run the interfaces file parser') | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 01:50:47 +02:00
										 |  |  | def update_common_argparser(argparser): | 
					
						
							| 
									
										
										
										
											2016-03-28 21:23:03 +02:00
										 |  |  |     ''' general parsing rules ''' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     package = pkg_resources.get_distribution("ifupdown2") | 
					
						
							|  |  |  |     argparser.add_argument('-V', '--version', | 
					
						
							|  |  |  |                 action='version', | 
					
						
							|  |  |  |                 version='ifupdown2:%(prog)s ' + package.version, | 
					
						
							|  |  |  |                 help='display current ifupdown2 version') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | def parse_args(argsv, op): | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |     if op == 'query': | 
					
						
							| 
									
										
										
										
											2014-01-30 22:36:41 -08:00
										 |  |  |         descr = 'query interfaces (all or interface list)' | 
					
						
							| 
									
										
										
										
											2014-03-27 16:36:54 -07:00
										 |  |  |     elif op == 'reload': | 
					
						
							|  |  |  |         descr = 'reload interface configuration.' | 
					
						
							| 
									
										
										
										
											2014-01-17 23:10:12 -08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         descr = 'interface management' | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     argparser = argparse.ArgumentParser(description=descr) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     if op == 'reload': | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |         update_ifreload_argparser(argparser) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         update_argparser(argparser) | 
					
						
							|  |  |  |         if op == 'up': | 
					
						
							|  |  |  |             update_ifup_argparser(argparser) | 
					
						
							|  |  |  |         elif op == 'down': | 
					
						
							|  |  |  |             update_ifdown_argparser(argparser) | 
					
						
							|  |  |  |         elif op == 'query': | 
					
						
							|  |  |  |             update_ifquery_argparser(argparser) | 
					
						
							| 
									
										
										
										
											2016-03-29 01:50:47 +02:00
										 |  |  |     update_common_argparser(argparser) | 
					
						
							| 
									
										
										
										
											2014-03-13 12:06:18 -07:00
										 |  |  |     argcomplete.autocomplete(argparser) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     return argparser.parse_args(argsv) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  | handlers = {'up' : run_up, | 
					
						
							|  |  |  |             'down' : run_down, | 
					
						
							|  |  |  |             'query' : run_query, | 
					
						
							|  |  |  |             'reload' : run_reload } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  | def validate_args(op, args): | 
					
						
							| 
									
										
										
										
											2014-04-28 22:33:33 -07:00
										 |  |  |     #if op == 'up' and args.syntaxcheck: | 
					
						
							|  |  |  |     #    if args.iflist or args.all: | 
					
						
							|  |  |  |     #        print 'ignoring interface options ..' | 
					
						
							|  |  |  |     #    return True | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |     if op == 'query' and args.syntaxhelp: | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2014-07-07 11:30:52 -07:00
										 |  |  |     if op == 'reload': | 
					
						
							| 
									
										
										
										
											2015-11-13 10:06:16 -08:00
										 |  |  |         if not args.all and not args.currentlyup and not args.CLASS: | 
					
						
							|  |  |  |             print '\'-a\' or \'-c\' or \'-allow\' option is required' | 
					
						
							| 
									
										
										
										
											2014-07-07 11:30:52 -07:00
										 |  |  |             return False | 
					
						
							|  |  |  |     elif (not args.iflist and | 
					
						
							|  |  |  |             not args.all and not args.CLASS): | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |         print '\'-a\' option or interface list are required' | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     if args.iflist and args.all: | 
					
						
							|  |  |  |         print '\'-a\' option and interface list are mutually exclusive' | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2014-07-07 11:30:52 -07:00
										 |  |  |     if op != 'reload' and args.CLASS and (args.all or args.iflist): | 
					
						
							| 
									
										
										
										
											2014-06-25 15:23:04 -07:00
										 |  |  |         print ('\'--allow\' option is mutually exclusive ' + | 
					
						
							|  |  |  |                'with interface list and \'-a\'') | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 22:07:49 -08:00
										 |  |  | def read_config(args): | 
					
						
							| 
									
										
										
										
											2014-04-18 14:09:20 -07:00
										 |  |  |     global configmap_g | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     config = open(configfile, 'r').read() | 
					
						
							|  |  |  |     configStr = '[ifupdown2]\n' + config | 
					
						
							|  |  |  |     configFP =  StringIO.StringIO(configStr) | 
					
						
							|  |  |  |     parser = ConfigParser.RawConfigParser() | 
					
						
							|  |  |  |     parser.readfp(configFP) | 
					
						
							|  |  |  |     configmap_g = dict(parser.items('ifupdown2')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:55:51 -08:00
										 |  |  |     # Preprocess config map | 
					
						
							|  |  |  |     configval = configmap_g.get('multiple_vlan_aware_bridge_support', '0') | 
					
						
							|  |  |  |     if configval == '0': | 
					
						
							|  |  |  |         # if multiple bridges not allowed, set the bridge-vlan-aware | 
					
						
							|  |  |  |         # attribute in the 'no_repeats' config, so that the ifupdownmain | 
					
						
							|  |  |  |         # module can catch it appropriately | 
					
						
							|  |  |  |         configmap_g['no_repeats'] = {'bridge-vlan-aware' : 'yes'} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 22:07:49 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     configval = configmap_g.get('link_master_slave', '0') | 
					
						
							|  |  |  |     if configval == '1': | 
					
						
							|  |  |  |         # link_master_slave is only valid when all is set | 
					
						
							|  |  |  |         if hasattr(args, 'all') and not args.all: | 
					
						
							|  |  |  |             configmap_g['link_master_slave'] = '0' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     configval = configmap_g.get('delay_admin_state_change', '0') | 
					
						
							|  |  |  |     if configval == '1': | 
					
						
							|  |  |  |         # reset link_master_slave if delay_admin_state_change is on | 
					
						
							|  |  |  |         configmap_g['link_master_slave'] = '0' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  | def main(argv): | 
					
						
							|  |  |  |     """ main function """ | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |     args = None | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     try: | 
					
						
							|  |  |  |         op = None | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |         if argv[0].endswith('ifup'): | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |             op = 'up' | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |         elif argv[0].endswith('ifdown'): | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |             op = 'down' | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |         elif argv[0].endswith('ifquery'): | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |             op = 'query' | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |         elif argv[0].endswith('ifreload'): | 
					
						
							| 
									
										
										
										
											2014-01-16 06:46:17 -08:00
										 |  |  |             op = 'reload' | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             print ('Unexpected executable.' + | 
					
						
							|  |  |  |                    ' Should be \'ifup\' or \'ifdown\' or \'ifquery\'') | 
					
						
							|  |  |  |             exit(1) | 
					
						
							|  |  |  |         # Command line arg parser | 
					
						
							|  |  |  |         args = parse_args(argv[1:], op) | 
					
						
							| 
									
										
										
										
											2014-03-28 06:03:14 -07:00
										 |  |  |         if not validate_args(op, args): | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |             exit(1) | 
					
						
							| 
									
										
										
										
											2014-06-06 23:00:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not sys.argv[0].endswith('ifquery') and not os.geteuid() == 0: | 
					
						
							|  |  |  |             print 'error: must be root to run this command' | 
					
						
							|  |  |  |             exit(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not sys.argv[0].endswith('ifquery') and not utils.lockFile(lockfile): | 
					
						
							|  |  |  |             print 'Another instance of this program is already running.' | 
					
						
							|  |  |  |             exit(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 22:07:49 -08:00
										 |  |  |         read_config(args) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |         init(args) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |         handlers.get(op)(args) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     except Exception, e: | 
					
						
							| 
									
										
										
										
											2014-02-19 21:30:55 -08:00
										 |  |  |         if not str(e): | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |             exit(1) | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |         if args and args.debug: | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |             raise | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2014-02-13 21:37:26 -08:00
										 |  |  |             if logger: | 
					
						
							|  |  |  |                 logger.error(str(e)) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 print str(e) | 
					
						
							| 
									
										
										
										
											2014-05-09 09:10:49 -07:00
										 |  |  |             #if args and not args.debug: | 
					
						
							|  |  |  |             #    print '\nrerun the command with \'-d\' for a detailed errormsg' | 
					
						
							| 
									
										
										
										
											2013-11-10 22:35:40 -08:00
										 |  |  |         exit(1) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     finally: | 
					
						
							|  |  |  |         deinit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 22:56:51 -07:00
										 |  |  |     # required during boot | 
					
						
							|  |  |  |     os.putenv('PATH', ENVPATH) | 
					
						
							| 
									
										
										
										
											2014-10-13 09:55:21 -07:00
										 |  |  |     resource.setrlimit(resource.RLIMIT_CORE, (0,0)) | 
					
						
							| 
									
										
										
										
											2013-11-04 06:06:11 -08:00
										 |  |  |     main(sys.argv) |