diff --git a/addons/ethtool.py b/addons/ethtool.py index 21faf39..1fbda65 100644 --- a/addons/ethtool.py +++ b/addons/ethtool.py @@ -10,6 +10,7 @@ try: from ipaddr import IPNetwork from sets import Set from ifupdown.iface import * + from ifupdown.utils import utils from ifupdownaddons.utilsbase import * from ifupdownaddons.modulebase import moduleBase from ifupdownaddons.iproute2 import iproute2 @@ -114,7 +115,7 @@ class ethtool(moduleBase,utilsBase): # something. this prevents unconfigured ifaces from resetting to default self.ifaceobjs_modified_configs.append(ifaceobj.name) cmd = 'ethtool -s %s %s' %(ifaceobj.name, cmd) - self.exec_command(cmd) + utils.exec_command(cmd) except Exception, e: self.log_error('%s: %s' %(ifaceobj.name, str(e)), ifaceobj) else: @@ -188,16 +189,16 @@ class ethtool(moduleBase,utilsBase): running_attr = None try: if attr == 'autoneg': - output=self.exec_commandl(['ethtool', ifaceobj.name]) + output = utils.exec_commandl(['ethtool', ifaceobj.name]) running_attr = self.get_autoneg(ethtool_output=output) else: running_attr = self.read_file_oneline('/sys/class/net/%s/%s' % \ (ifaceobj.name, attr)) - except: + except Exception as e: # for nonexistent interfaces, we get an error (rc = 256 or 19200) self.logger.debug('ethtool: problems calling ethtool or reading' - ' /sys/class on iface %s for attr %s' % \ - (ifaceobj.name,attr)) + ' /sys/class on iface %s for attr %s: %s' % + (ifaceobj.name, attr, str(e))) return running_attr diff --git a/addons/usercmds.py b/addons/usercmds.py index 12cef6c..738a8ac 100644 --- a/addons/usercmds.py +++ b/addons/usercmds.py @@ -4,9 +4,7 @@ # Author: Roopa Prabhu, roopa@cumulusnetworks.com # -import subprocess import ifupdownaddons -import signal from ifupdown.utils import utils import ifupdown.ifupdownflags as ifupdownflags @@ -29,46 +27,16 @@ class usercmds(ifupdownaddons.modulebase.moduleBase): 'post-down' : {'help' : 'run command after bringing interface down'}}} - def _exec_user_cmd(self, cmd): - """ exec's commands using subprocess Popen - - special wrapper using use closefds=True and shell=True - for user commands - """ - - cmd_returncode = 0 - try: - self.logger.info('executing %s' %cmd) - if ifupdownflags.flags.DRYRUN: - return - ch = subprocess.Popen(cmd, - stdout=subprocess.PIPE, - shell=True, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmd_returncode = ch.wait() - cmdout = ch.communicate()[0] - except Exception, e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(cmd, str(e))) - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception(cmdout) - return cmdout - def _run_command(self, ifaceobj, op): cmd_list = ifaceobj.get_attr_value(op) if cmd_list: for cmd in cmd_list: - self.logger.info('executing cmd \'%s\'' %cmd) try: - self._exec_user_cmd(cmd) + utils.exec_user_command(cmd) except Exception, e: if not self.ignore_error(str(e)): - self.logger.warn('%s: %s cmd \'%s\' failed (%s)' - %(ifaceobj.name, op, cmd, str(e).strip('\n'))) + self.logger.warn('%s: %s %s' % (ifaceobj.name, op, + str(e).strip('\n'))) pass _run_ops = {'pre-up' : _run_command, diff --git a/addons/vrf.py b/addons/vrf.py index f79706e..b5ecb04 100644 --- a/addons/vrf.py +++ b/addons/vrf.py @@ -6,10 +6,11 @@ import os import signal -import errno -import subprocess +import errno +import fcntl import atexit from ifupdown.iface import * +from ifupdown.utils import utils import ifupdown.policymanager as policymanager import ifupdownaddons import ifupdown.rtnetlink_api as rtnetlink_api @@ -64,18 +65,18 @@ class vrf(moduleBase): self.logger.debug('vrf: removing file failed (%s)' %str(e)) try: - ip_rules = self.exec_command('/sbin/ip rule show').splitlines() + ip_rules = utils.exec_command('/sbin/ip rule show').splitlines() self.ip_rule_cache = [' '.join(r.split()) for r in ip_rules] except Exception, e: self.ip_rule_cache = [] - self.logger.warn('%s' %str(e)) + self.logger.warn('vrf: cache v4: %s' % str(e)) try: - ip_rules = self.exec_command('/sbin/ip -6 rule show').splitlines() + ip_rules = utils.exec_command('/sbin/ip -6 rule show').splitlines() self.ip6_rule_cache = [' '.join(r.split()) for r in ip_rules] except Exception, e: self.ip6_rule_cache = [] - self.logger.warn('%s' %str(e)) + self.logger.warn('vrf: cache v6: %s' % str(e)) #self.logger.debug("vrf: ip rule cache") #self.logger.info(self.ip_rule_cache) @@ -110,25 +111,25 @@ class vrf(moduleBase): iproute2_vrf_map_force_rewrite = False # read or create /etc/iproute2/rt_tables.d/ifupdown2.vrf_map if os.path.exists(self.iproute2_vrf_filename): - vrf_map_fd = open(self.iproute2_vrf_filename, 'r+') - lines = vrf_map_fd.readlines() - for l in lines: - l = l.strip() - if l[0] == '#': - continue - try: - (table, vrf_name) = l.strip().split() - if self.iproute2_vrf_map.get(int(table)): - # looks like the existing file has - # duplicate entries, force rewrite of the - # file - iproute2_vrf_map_force_rewrite = True + with open(self.iproute2_vrf_filename, 'r+') as vrf_map_fd: + lines = vrf_map_fd.readlines() + for l in lines: + l = l.strip() + if l[0] == '#': continue - self.iproute2_vrf_map[int(table)] = vrf_name - except Exception, e: - self.logger.info('vrf: iproute2_vrf_map: unable to parse %s' - %l) - pass + try: + (table, vrf_name) = l.strip().split() + if self.iproute2_vrf_map.get(int(table)): + # looks like the existing file has + # duplicate entries, force rewrite of the + # file + iproute2_vrf_map_force_rewrite = True + continue + self.iproute2_vrf_map[int(table)] = vrf_name + except Exception, e: + self.logger.info('vrf: iproute2_vrf_map: unable to parse %s' + %l) + pass vrfs = self.ipcmd.link_get_vrfs() running_vrf_map = {} @@ -188,6 +189,7 @@ class vrf(moduleBase): try: self.iproute2_vrf_map_fd = open(self.iproute2_vrf_filename, '%s' %fmode) + fcntl.fcntl(self.iproute2_vrf_map_fd, fcntl.F_SETFD, fcntl.FD_CLOEXEC) except Exception, e: self.log_warn('vrf: error opening %s (%s)' %(self.iproute2_vrf_filename, str(e))) @@ -381,25 +383,25 @@ class vrf(moduleBase): if rule in self.ip_rule_cache: rule_cmd = ip_rule_cmd %('', pref, 'oif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'iif', vrf_dev_name, vrf_dev_name) if rule in self.ip_rule_cache: rule_cmd = ip_rule_cmd %('', pref, 'iif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'oif', vrf_dev_name, vrf_dev_name) if rule in self.ip6_rule_cache: rule_cmd = ip_rule_cmd %('-6', pref, 'oif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'iif', vrf_dev_name, vrf_dev_name) if rule in self.ip6_rule_cache: rule_cmd = ip_rule_cmd %('-6', pref, 'iif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) def _add_vrf_rules(self, vrf_dev_name, vrf_table): pref = 200 @@ -410,17 +412,17 @@ class vrf(moduleBase): rule = '0: from all lookup local' if rule in self.ip_rule_cache: try: - self.exec_command('ip rule del pref 0') - self.exec_command('ip rule add pref 32765 table local') + utils.exec_command('ip rule del pref 0') + utils.exec_command('ip rule add pref 32765 table local') except Exception, e: - self.logger.info('%s' %str(e)) + self.logger.info('%s: %s' % (vrf_dev_name, str(e))) pass if rule in self.ip6_rule_cache: try: - self.exec_command('ip -6 rule del pref 0') - self.exec_command('ip -6 rule add pref 32765 table local') + utils.exec_command('ip -6 rule del pref 0') + utils.exec_command('ip -6 rule add pref 32765 table local') except Exception, e: - self.logger.info('%s' %str(e)) + self.logger.info('%s: %s' % (vrf_dev_name, str(e))) pass #Example ip rule @@ -431,25 +433,25 @@ class vrf(moduleBase): if rule not in self.ip_rule_cache: rule_cmd = ip_rule_cmd %('', pref, 'oif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'iif', vrf_dev_name, vrf_dev_name) if rule not in self.ip_rule_cache: rule_cmd = ip_rule_cmd %('', pref, 'iif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'oif', vrf_dev_name, vrf_dev_name) if rule not in self.ip6_rule_cache: rule_cmd = ip_rule_cmd %('-6', pref, 'oif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) rule = ip_rule_out_format %(pref, 'iif', vrf_dev_name, vrf_dev_name) if rule not in self.ip6_rule_cache: rule_cmd = ip_rule_cmd %('-6', pref, 'iif', vrf_dev_name, vrf_dev_name) - self.exec_command(rule_cmd) + utils.exec_command(rule_cmd) def _add_vrf_slaves(self, ifaceobj, ifaceobj_getfunc=None): running_slaves = self.ipcmd.link_get_lowers(ifaceobj.name) @@ -572,8 +574,11 @@ class vrf(moduleBase): if ifupdownflags.flags.PERFMODE: mode = "boot" if self.vrf_helper: - self.exec_command('%s create %s %s %s' %(self.vrf_helper, - ifaceobj.name, vrf_table, mode)) + utils.exec_command('%s create %s %s %s' % + (self.vrf_helper, + ifaceobj.name, + vrf_table, + mode)) def _up_vrf_dev(self, ifaceobj, vrf_table, add_slaves=True, ifaceobj_getfunc=None): @@ -612,8 +617,7 @@ class vrf(moduleBase): #ESTAB 0 0 10.0.1.84:ssh 10.0.1.228:45186 #users:(("sshd",pid=2528,fd=3)) cmdl = ['/bin/ss', '-t', '-p'] - for line in subprocess.check_output(cmdl, stderr=subprocess.STDOUT, - shell=False).splitlines(): + for line in utils.exec_commandl(cmdl).splitlines(): citems = line.split() addr = None if '%' in citems[3]: @@ -628,10 +632,8 @@ class vrf(moduleBase): if not proc: return - pid = subprocess.check_output(['/bin/ps', '--no-headers', - '-fp', str(os.getppid())], - stderr=subprocess.STDOUT, - shell=False).split()[2] + cmdl = ['/bin/ps', '--no-headers', '-fp', str(os.getppid())] + pid = utils.exec_commandl(cmdl).split()[2] self.logger.info("%s: killing active ssh sessions: %s" %(ifacename, str(proc))) @@ -697,8 +699,11 @@ class vrf(moduleBase): if ifupdownflags.flags.PERFMODE: mode = "boot" if self.vrf_helper: - self.exec_command('%s delete %s %s %s' %(self.vrf_helper, - ifaceobj.name, vrf_table, mode)) + utils.exec_command('%s delete %s %s %s' % + (self.vrf_helper, + ifaceobj.name, + vrf_table, + mode)) def _down_vrf_dev(self, ifaceobj, vrf_table, ifaceobj_getfunc=None): @@ -804,7 +809,7 @@ class vrf(moduleBase): return if self.vrf_helper: try: - self.exec_command('%s verify %s %s' + utils.exec_command('%s verify %s %s' %(self.vrf_helper, ifaceobj.name, config_table)) ifaceobjcurr.update_config_with_status('vrf-helper', diff --git a/addons/vrrpd.py b/addons/vrrpd.py index c6718bc..8385ac7 100644 --- a/addons/vrrpd.py +++ b/addons/vrrpd.py @@ -8,6 +8,7 @@ try: from ipaddr import IPNetwork from sets import Set from ifupdown.iface import * + from ifupdown.utils import utils from ifupdownaddons.modulebase import moduleBase from ifupdownaddons.iproute2 import iproute2 import ifupdown.ifupdownflags as ifupdownflags @@ -15,7 +16,6 @@ try: import glob import logging import signal - import subprocess import re except ImportError, e: raise ImportError (str(e) + "- required module not found") @@ -43,8 +43,8 @@ class vrrpd(moduleBase): targetpids = [] pidstr = '' try: - pidstr = subprocess.check_output(['/bin/pidof', - '%s' %cmdname]).strip('\n') + cmdl = ['/bin/pidof', cmdname] + pidstr = utils.exec_commandl(cmdl, stderr=None).strip('\n') except: pass if not pidstr: @@ -95,13 +95,13 @@ class vrrpd(moduleBase): '(virtual ip not found)') return cmd = '/usr/sbin/vrrpd -n -D -i %s %s' %(ifaceobj.name, cmd) - self.exec_command(cmd) + utils.exec_command(cmd) cmd = '/usr/sbin/ifplugd -i %s -b -f -u0 -d1 -I -p -q' %ifaceobj.name if self._check_if_process_is_running('/usr/sbin/ifplugd', cmd): self.logger.info('%s: ifplugd already running' %ifaceobj.name) return - self.exec_command(cmd) + utils.exec_command(cmd) def _kill_pid_from_file(self, pidfilename): if os.path.exists(pidfilename): @@ -116,7 +116,7 @@ class vrrpd(moduleBase): if not attrval: return try: - self.exec_command('/usr/sbin/ifplugd -k -i %s' %ifaceobj.name) + utils.exec_command('/usr/sbin/ifplugd -k -i %s' % ifaceobj.name) except Exception, e: self.logger.debug('%s: ifplugd down error (%s)' %(ifaceobj.name, str(e))) diff --git a/ifupdown/ifupdownbase.py b/ifupdown/ifupdownbase.py index acf75d2..e7fa99e 100644 --- a/ifupdown/ifupdownbase.py +++ b/ifupdown/ifupdownbase.py @@ -8,15 +8,11 @@ # import logging -import subprocess import re import os import rtnetlink_api as rtnetlink_api -import signal -import shlex from iface import * -from ifupdown.utils import utils import ifupdownflags as ifupdownflags class ifupdownBase(object): @@ -25,31 +21,6 @@ class ifupdownBase(object): modulename = self.__class__.__name__ self.logger = logging.getLogger('ifupdown.' + modulename) - def exec_command(self, cmd, cmdenv=None, nowait=False): - cmd_returncode = 0 - cmdout = '' - try: - self.logger.info('executing ' + cmd) - if ifupdownflags.flags.DRYRUN: - return cmdout - ch = subprocess.Popen(shlex.split(cmd), - stdout=subprocess.PIPE, - shell=False, env=cmdenv, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmdout = ch.communicate()[0] - cmd_returncode = ch.wait() - except OSError, e: - raise Exception('could not execute ' + cmd + - '(' + str(e) + ')') - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('error executing cmd \'%s\'' %cmd + - '\n(' + cmdout.strip('\n ') + ')') - return cmdout - def ignore_error(self, errmsg): if (ifupdownflags.flags.FORCE == True or re.search(r'exists', errmsg, re.IGNORECASE | re.MULTILINE) is not None): diff --git a/ifupdown/networkinterfaces.py b/ifupdown/networkinterfaces.py index a35e33c..eacf386 100644 --- a/ifupdown/networkinterfaces.py +++ b/ifupdown/networkinterfaces.py @@ -408,9 +408,8 @@ class networkInterfaces(): self._filestack.append(filename) self.logger.info('processing interfaces file %s' %filename) try: - f = open(filename) - filedata = f.read() - f.close() + with open(filename) as f: + filedata = f.read() except Exception, e: self.logger.warn('error processing file %s (%s)', filename, str(e)) @@ -424,8 +423,8 @@ class networkInterfaces(): #object_hook=ifaceJsonDecoder.json_object_hook) elif filename: self.logger.info('processing interfaces file %s' %filename) - fp = open(filename) - ifacedicts = json.load(fp) + with open(filename) as fp: + ifacedicts = json.load(fp) #object_hook=ifaceJsonDecoder.json_object_hook) # we need to handle both lists and non lists formats (e.g. {{}}) diff --git a/ifupdown/policymanager.py b/ifupdown/policymanager.py index b3f0e08..b5f3123 100644 --- a/ifupdown/policymanager.py +++ b/ifupdown/policymanager.py @@ -49,8 +49,8 @@ class policymanager(): for filename in default_files: system_array = {} try: - fd = open(filename,'r') - system_array = json.load(fd) + with open(filename, 'r') as fd: + system_array = json.load(fd) self.logger.debug('reading %s system policy defaults config' \ % filename) except Exception, e: @@ -69,8 +69,8 @@ class policymanager(): for filename in user_files: user_array = {} try: - fd = open(filename,'r') - user_array = json.load(fd) + with open(filename, 'r') as fd: + user_array = json.load(fd) self.logger.debug('reading %s policy user defaults config' \ % filename) except Exception, e: diff --git a/ifupdown/scheduler.py b/ifupdown/scheduler.py index d906a11..09ac1a6 100644 --- a/ifupdown/scheduler.py +++ b/ifupdown/scheduler.py @@ -20,6 +20,7 @@ from graph import * from collections import deque from threading import * from ifupdownbase import * +from ifupdown.utils import utils from sets import Set class ifaceSchedulerFlags(): @@ -112,9 +113,9 @@ class ifaceScheduler(): ifupdownobj.logger.debug('%s: %s : running script %s' %(ifacename, op, mname)) try: - ifupdownobj.exec_command(mname, cmdenv=cenv) + utils.exec_command(mname, env=cenv) except Exception, e: - ifupdownobj.log_error(str(e)) + ifupdownobj.log_error('%s: %s %s' % (ifacename, op, str(e))) @classmethod def run_iface_list_ops(cls, ifupdownobj, ifaceobjs, ops): diff --git a/ifupdown/utils.py b/ifupdown/utils.py index 458999e..381b4bd 100644 --- a/ifupdown/utils.py +++ b/ifupdown/utils.py @@ -6,10 +6,15 @@ # utils -- # helper class # + import os -import fcntl import re +import shlex +import fcntl import signal +import logging +import subprocess +import ifupdownflags from functools import partial @@ -20,6 +25,8 @@ def signal_handler_f(ps, sig, frame): raise KeyboardInterrupt class utils(): + logger = logging.getLogger('ifupdown') + DEVNULL = open(os.devnull, 'w') @classmethod def importName(cls, modulename, name): @@ -35,6 +42,7 @@ class utils(): try: fp = os.open(lockfile, os.O_CREAT | os.O_TRUNC | os.O_WRONLY) fcntl.flock(fp, fcntl.LOCK_EX | fcntl.LOCK_NB) + fcntl.fcntl(fp, fcntl.F_SETFD, fcntl.FD_CLOEXEC) except IOError: return False return True @@ -75,3 +83,99 @@ class utils(): def disable_subprocess_signal_forwarding(cls, sig): signal.signal(sig, signal.SIG_DFL) + @classmethod + def _log_command_exec(cls, cmd, stdin): + if stdin: + cls.logger.info('executing %s [%s]' % (cmd, stdin)) + else: + cls.logger.info('executing %s' % cmd) + + @classmethod + def _format_error(cls, cmd, cmd_returncode, cmd_output, stdin): + if type(cmd) is list: + cmd = ' '.join(cmd) + if stdin: + cmd = '%s [%s]' % (cmd, stdin) + if cmd_output: + return 'cmd \'%s\' failed: returned %d (%s)' % \ + (cmd, cmd_returncode, cmd_output) + else: + return 'cmd \'%s\' failed: returned %d' % (cmd, cmd_returncode) + + @classmethod + def _execute_subprocess(cls, cmd, + env=None, + shell=False, + close_fds=False, + stdout=True, + stdin=None, + stderr=subprocess.STDOUT): + """ + exec's commands using subprocess Popen + Args: + cmd, should be shlex.split if not shell + returns: output + + Note: close_fds=True is affecting performance (2~3 times slower) + """ + if ifupdownflags.flags.DRYRUN: + return '' + + cmd_output = None + try: + ch = subprocess.Popen(cmd, + env=env, + shell=shell, + close_fds=close_fds, + stdin=subprocess.PIPE if stdin else None, + stdout=subprocess.PIPE if stdout else cls.DEVNULL, + stderr=stderr) + utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) + if stdout or stdin: + cmd_output = ch.communicate(input=stdin)[0] + cmd_returncode = ch.wait() + except Exception as e: + raise Exception('cmd \'%s\' failed (%s)' % (' '.join(cmd), str(e))) + finally: + utils.disable_subprocess_signal_forwarding(signal.SIGINT) + if cmd_returncode != 0: + raise Exception(cls._format_error(cmd, + cmd_returncode, + cmd_output, + stdin)) + return cmd_output + + @classmethod + def exec_user_command(cls, cmd, close_fds=False, stdout=True, + stdin=None, stderr=subprocess.STDOUT): + cls._log_command_exec(cmd, stdin) + return cls._execute_subprocess(cmd, + shell=True, + close_fds=close_fds, + stdout=stdout, + stdin=stdin, + stderr=stderr) + + @classmethod + def exec_command(cls, cmd, env=None, close_fds=False, stdout=True, + stdin=None, stderr=subprocess.STDOUT): + cls._log_command_exec(cmd, stdin) + return cls._execute_subprocess(shlex.split(cmd), + env=env, + close_fds=close_fds, + stdout=stdout, + stdin=stdin, + stderr=stderr) + + @classmethod + def exec_commandl(cls, cmdl, env=None, close_fds=False, stdout=True, + stdin=None, stderr=subprocess.STDOUT): + cls._log_command_exec(' '.join(cmdl), stdin) + return cls._execute_subprocess(cmdl, + env=env, + close_fds=close_fds, + stdout=stdout, + stdin=stdin, + stderr=stderr) + +fcntl.fcntl(utils.DEVNULL, fcntl.F_SETFD, fcntl.FD_CLOEXEC) diff --git a/ifupdownaddons/bondutil.py b/ifupdownaddons/bondutil.py index 823b911..366adfa 100644 --- a/ifupdownaddons/bondutil.py +++ b/ifupdownaddons/bondutil.py @@ -7,6 +7,7 @@ import os import re import ifupdown.ifupdownflags as ifupdownflags +from ifupdown.utils import utils from ifupdown.iface import * from utilsbase import * from iproute2 import * @@ -314,9 +315,8 @@ class bondutil(utilsBase): '/bonding/slaves') ipcmd = iproute2() try: - f = open(sysfs_bond_path, 'r') - slaves = f.readline().strip().split() - f.close() + with open(sysfs_bond_path, 'r') as f: + slaves = f.readline().strip().split() except IOError, e: raise Exception('error reading slaves of bond %s' %bondname + '(' + str(e) + ')') @@ -334,7 +334,7 @@ class bondutil(utilsBase): self._cache_del([bondname, 'linkinfo', 'slaves']) def load_bonding_module(self): - return self.exec_command('modprobe -q bonding') + return utils.exec_command('modprobe -q bonding') def create_bond(self, bondname): if self.bond_exists(bondname): diff --git a/ifupdownaddons/bridgeutils.py b/ifupdownaddons/bridgeutils.py index edffa71..6c6e091 100644 --- a/ifupdownaddons/bridgeutils.py +++ b/ifupdownaddons/bridgeutils.py @@ -9,6 +9,7 @@ from utilsbase import * import os import re import logging +from ifupdown.utils import utils import ifupdown.ifupdownflags as ifupdownflags from cache import * @@ -63,7 +64,7 @@ class brctl(utilsBase): battrs = {} bports = {} - brout = self.exec_command('/sbin/brctl showstp %s' %bridgename) + brout = utils.exec_command('/sbin/brctl showstp %s' % bridgename) chunks = re.split(r'\n\n', brout, maxsplit=0, flags=re.MULTILINE) try: @@ -133,9 +134,9 @@ class brctl(utilsBase): except: pass if not bridgename: - brctlout = self.exec_command('/sbin/brctl show') + brctlout = utils.exec_command('/sbin/brctl show') else: - brctlout = self.exec_command('/sbin/brctl show ' + bridgename) + brctlout = utils.exec_command('/sbin/brctl show %s' % bridgename) if not brctlout: return @@ -202,13 +203,13 @@ class brctl(utilsBase): def create_bridge(self, bridgename): if self.bridge_exists(bridgename): return - self.exec_command('/sbin/brctl addbr %s' %bridgename) + utils.exec_command('/sbin/brctl addbr %s' % bridgename) self._cache_update([bridgename], {}) def delete_bridge(self, bridgename): if not self.bridge_exists(bridgename): return - self.exec_command('/sbin/brctl delbr %s' %bridgename) + utils.exec_command('/sbin/brctl delbr %s' % bridgename) self._cache_invalidate() def add_bridge_port(self, bridgename, bridgeportname): @@ -216,8 +217,8 @@ class brctl(utilsBase): ports = self._cache_get([bridgename, 'linkinfo', 'ports']) if ports and ports.get(bridgeportname): return - self.exec_command('/sbin/brctl addif ' + bridgename + ' ' + - bridgeportname) + utils.exec_command('/sbin/brctl addif %s %s' % + (bridgename, bridgeportname)) self._cache_update([bridgename, 'linkinfo', 'ports', bridgeportname], {}) @@ -226,8 +227,8 @@ class brctl(utilsBase): ports = self._cache_get([bridgename, 'linkinfo', 'ports']) if not ports or not ports.get(bridgeportname): return - self.exec_command('/sbin/brctl delif ' + bridgename + ' ' + - bridgeportname) + utils.exec_command('/sbin/brctl delif %s %s' % + (bridgename, bridgeportname)) self._cache_delete([bridgename, 'linkinfo', 'ports', 'bridgeportname']) @@ -240,16 +241,19 @@ class brctl(utilsBase): curval = portattrs.get(k) if curval and curval == v: continue - self.exec_command('/sbin/brctl set%s %s %s %s' - %(k, bridgename, bridgeportname, v)) + utils.exec_command('/sbin/brctl set%s %s %s %s' % + (k, bridgename, bridgeportname, v)) def set_bridgeport_attr(self, bridgename, bridgeportname, attrname, attrval): if self._cache_check([bridgename, 'linkinfo', 'ports', bridgeportname, attrname], attrval): return - self.exec_command('/sbin/brctl set%s %s %s %s' %(attrname, bridgename, - bridgeportname, attrval)) + utils.exec_command('/sbin/brctl set%s %s %s %s' % + (attrname, + bridgename, + bridgeportname, + attrval)) def set_bridge_attrs(self, bridgename, attrdict): for k, v in attrdict.iteritems(): @@ -258,8 +262,8 @@ class brctl(utilsBase): if self._cache_check([bridgename, 'linkinfo', k], v): continue try: - self.exec_command('/sbin/brctl set%s %s %s' - %(k, bridgename, v)) + cmd = '/sbin/brctl set%s %s %s' % (k, bridgename, v) + utils.exec_command(cmd) except Exception, e: self.logger.warn('%s: %s' %(bridgename, str(e))) pass @@ -267,8 +271,8 @@ class brctl(utilsBase): def set_bridge_attr(self, bridgename, attrname, attrval): if self._cache_check([bridgename, 'linkinfo', attrname], attrval): return - self.exec_command('/sbin/brctl set%s %s %s' - %(attrname, bridgename, attrval)) + utils.exec_command('/sbin/brctl set%s %s %s' % + (attrname, bridgename, attrval)) def get_bridge_attrs(self, bridgename): return self._cache_get([bridgename, 'linkinfo']) @@ -282,7 +286,7 @@ class brctl(utilsBase): bridgeportname, attrname]) def set_stp(self, bridge, stp_state): - self.exec_command('/sbin/brctl stp ' + bridge + ' ' + stp_state) + utils.exec_command('/sbin/brctl stp %s %s' % (bridge, stp_state)) def get_stp(self, bridge): sysfs_stpstate = '/sys/class/net/%s/bridge/stp_state' %bridge @@ -314,22 +318,21 @@ class brctl(utilsBase): return preprocess_func(value) def set_ageing(self, bridge, ageing): - self.exec_command('/sbin/brctl setageing ' + bridge + ' ' + ageing) + utils.exec_command('/sbin/brctl setageing %s %s' % (bridge, ageing)) def get_ageing(self, bridge): return self.read_value_from_sysfs('/sys/class/net/%s/bridge/ageing_time' %bridge, self.conv_value_to_user) - def set_bridgeprio(self, bridge, bridgeprio): - self.exec_command('/sbin/brctl setbridgeprio ' + bridge + ' ' + - bridgeprio) + def set_bridgeprio(self, bridge, prio): + utils.exec_command('/sbin/brctl setbridgeprio %s %s' % (bridge, prio)) def get_bridgeprio(self, bridge): return self.read_file_oneline( '/sys/class/net/%s/bridge/priority' %bridge) def set_fd(self, bridge, fd): - self.exec_command('/sbin/brctl setfd ' + bridge + ' ' + fd) + utils.exec_command('/sbin/brctl setfd %s %s' % (bridge, fd)) def get_fd(self, bridge): return self.read_value_from_sysfs( @@ -341,51 +344,51 @@ class brctl(utilsBase): raise Exception('set_gcint not implemented') def set_hello(self, bridge, hello): - self.exec_command('/sbin/brctl sethello ' + bridge + ' ' + hello) + utils.exec_command('/sbin/brctl sethello %s %s' % (bridge, hello)) def get_hello(self, bridge): return self.read_value_from_sysfs('/sys/class/net/%s/bridge/hello_time' %bridge, self.conv_value_to_user) def set_maxage(self, bridge, maxage): - self.exec_command('/sbin/brctl setmaxage ' + bridge + ' ' + maxage) + utils.exec_command('/sbin/brctl setmaxage %s %s' % (bridge, maxage)) def get_maxage(self, bridge): return self.read_value_from_sysfs('/sys/class/net/%s/bridge/max_age' %bridge, self.conv_value_to_user) def set_pathcost(self, bridge, port, pathcost): - self.exec_command('/sbin/brctl setpathcost %s' %bridge + ' %s' %port + - ' %s' %pathcost) + utils.exec_command('/sbin/brctl setpathcost %s %s %s' % + (bridge, port, pathcost)) def get_pathcost(self, bridge, port): return self.read_file_oneline('/sys/class/net/%s/brport/path_cost' %port) def set_portprio(self, bridge, port, prio): - self.exec_command('/sbin/brctl setportprio %s' %bridge + ' %s' %port + - ' %s' %prio) + utils.exec_command('/sbin/brctl setportprio %s %s %s' % + (bridge, port, prio)) def get_portprio(self, bridge, port): return self.read_file_oneline('/sys/class/net/%s/brport/priority' %port) def set_hashmax(self, bridge, hashmax): - self.exec_command('/sbin/brctl sethashmax %s' %bridge + ' %s' %hashmax) + utils.exec_command('/sbin/brctl sethashmax %s %s' % (bridge, hashmax)) def get_hashmax(self, bridge): return self.read_file_oneline('/sys/class/net/%s/bridge/hash_max' %bridge) def set_hashel(self, bridge, hashel): - self.exec_command('/sbin/brctl sethashel %s' %bridge + ' %s' %hashel) + utils.exec_command('/sbin/brctl sethashel %s %s' % (bridge, hashel)) def get_hashel(self, bridge): return self.read_file_oneline('/sys/class/net/%s/bridge/hash_elasticity' %bridge) def set_mclmc(self, bridge, mclmc): - self.exec_command('/sbin/brctl setmclmc %s' %bridge + ' %s' %mclmc) + utils.exec_command('/sbin/brctl setmclmc %s %s' % (bridge, mclmc)) def get_mclmc(self, bridge): return self.read_file_oneline( @@ -393,24 +396,21 @@ class brctl(utilsBase): %bridge) def set_mcrouter(self, bridge, mcrouter): - self.exec_command('/sbin/brctl setmcrouter %s' %bridge + - ' %s' %mcrouter) + utils.exec_command('/sbin/brctl setmcrouter %s %s' % (bridge, mcrouter)) def get_mcrouter(self, bridge): return self.read_file_oneline( '/sys/class/net/%s/bridge/multicast_router' %bridge) def set_mcsnoop(self, bridge, mcsnoop): - self.exec_command('/sbin/brctl setmcsnoop %s' %bridge + - ' %s' %mcsnoop) + utils.exec_command('/sbin/brctl setmcsnoop %s %s' % (bridge, mcsnoop)) def get_mcsnoop(self, bridge): return self.read_file_oneline( '/sys/class/net/%s/bridge/multicast_snooping' %bridge) def set_mcsqc(self, bridge, mcsqc): - self.exec_command('/sbin/brctl setmcsqc %s' %bridge + - ' %s' %mcsqc) + utils.exec_command('/sbin/brctl setmcsqc %s %s' % (bridge, mcsqc)) def get_mcsqc(self, bridge): return self.read_file_oneline( @@ -418,8 +418,8 @@ class brctl(utilsBase): %bridge) def set_mcqifaddr(self, bridge, mcqifaddr): - self.exec_command('/sbin/brctl setmcqifaddr %s' %bridge + - ' %s' %mcqifaddr) + utils.exec_command('/sbin/brctl setmcqifaddr %s %s' % + (bridge, mcqifaddr)) def get_mcqifaddr(self, bridge): return self.read_file_oneline( @@ -427,8 +427,8 @@ class brctl(utilsBase): %bridge) def set_mcquerier(self, bridge, mcquerier): - self.exec_command('/sbin/brctl setmcquerier %s' %bridge + - ' %s' %mcquerier) + utils.exec_command('/sbin/brctl setmcquerier %s %s' % + (bridge, mcquerier)) def get_mcquerier(self, bridge): return self.read_file_oneline( @@ -448,15 +448,15 @@ class brctl(utilsBase): self.logger.warn('mcqv4src \'%s\' invalid IPv4 address' %mcquerier) return - self.exec_command('/sbin/brctl setmcqv4src %s' %bridge + - ' %d %s' %(vlan, mcquerier)) + utils.exec_command('/sbin/brctl setmcqv4src %s %d %s' % + (bridge, vlan, mcquerier)) def del_mcqv4src(self, bridge, vlan): - self.exec_command('/sbin/brctl delmcqv4src %s %d' %(bridge, vlan)) + utils.exec_command('/sbin/brctl delmcqv4src %s %d' % (bridge, vlan)) def get_mcqv4src(self, bridge, vlan=None): mcqv4src = {} - mcqout = self.exec_command('/sbin/brctl showmcqv4src %s' %bridge) + mcqout = utils.exec_command('/sbin/brctl showmcqv4src %s' % bridge) if not mcqout: return None mcqlines = mcqout.splitlines() for l in mcqlines[1:]: @@ -470,8 +470,7 @@ class brctl(utilsBase): return mcqv4src def set_mclmi(self, bridge, mclmi): - self.exec_command('/sbin/brctl setmclmi %s' %bridge + - ' %s' %mclmi) + utils.exec_command('/sbin/brctl setmclmi %s %s' % (bridge, mclmi)) def get_mclmi(self, bridge): return self.read_file_oneline( @@ -479,8 +478,7 @@ class brctl(utilsBase): %bridge) def set_mcmi(self, bridge, mcmi): - self.exec_command('/sbin/brctl setmcmi %s' %bridge + - ' %s' %mcmi) + utils.exec_command('/sbin/brctl setmcmi %s %s' % (bridge, mcmi)) def get_mcmi(self, bridge): return self.read_file_oneline( diff --git a/ifupdownaddons/dhclient.py b/ifupdownaddons/dhclient.py index 6056a66..81b45f4 100644 --- a/ifupdownaddons/dhclient.py +++ b/ifupdownaddons/dhclient.py @@ -4,11 +4,10 @@ # Author: Roopa Prabhu, roopa@cumulusnetworks.com # +from ifupdown.utils import utils from utilsbase import * -import subprocess import os -FNULL = open(os.devnull, 'w') class dhclient(utilsBase): """ This class contains helper methods to interact with the dhclient @@ -38,7 +37,7 @@ class dhclient(utilsBase): cmd_aslist.extend(cmd) else: cmd_aslist = cmd - self.subprocess_check_call(cmd_aslist) + utils.exec_commandl(cmd_aslist, stdout=None, stderr=None) def stop(self, ifacename, cmd_prefix=None): if os.path.exists('/sbin/dhclient3'): diff --git a/ifupdownaddons/iproute2.py b/ifupdownaddons/iproute2.py index 37971f2..b1f4b6d 100644 --- a/ifupdownaddons/iproute2.py +++ b/ifupdownaddons/iproute2.py @@ -8,6 +8,7 @@ import os import glob import shlex import signal +import subprocess from ifupdown.utils import utils from collections import OrderedDict @@ -275,9 +276,8 @@ class iproute2(utilsBase): self.ipbatch_pause = False return try: - self.exec_command_talk_stdin('ip -force -batch -', - stdinbuf=self.ipbatchbuf) - except Exception: + utils.exec_command('ip -force -batch -', stdin=self.ipbatchbuf) + except: raise finally: self.ipbatchbuf = '' @@ -288,17 +288,17 @@ class iproute2(utilsBase): if ifacename: if not self.link_exists(ifacename): return - return self.exec_commandl(['ip','-o', 'addr', 'show', 'dev', - '%s' %ifacename]) + return utils.exec_commandl(['ip', '-o', 'addr', 'show', 'dev', + ifacename]) else: - return self.exec_commandl(['ip', '-o', 'addr', 'show']) + return utils.exec_commandl(['ip', '-o', 'addr', 'show']) def link_show(self, ifacename=None): if ifacename: - return self.exec_commandl(['ip', '-o', '-d', 'link', - 'show', 'dev', '%s' %ifacename]) + return utils.exec_commandl(['ip', '-o', '-d', 'link', 'show', 'dev', + ifacename]) else: - return self.exec_commandl(['ip', '-o', '-d', 'link', 'show']) + return utils.exec_commandl(['ip', '-o', '-d', 'link', 'show']) def addr_add(self, ifacename, address, broadcast=None, peer=None, scope=None, preferred_lifetime=None): @@ -317,7 +317,7 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) self._cache_update([ifacename, 'addrs', address], {}) def addr_del(self, ifacename, address, broadcast=None, @@ -335,7 +335,7 @@ class iproute2(utilsBase): if scope: cmd += 'scope %s' %scope cmd += ' dev %s' %ifacename - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) self._cache_delete([ifacename, 'addrs', address]) def addr_flush(self, ifacename): @@ -343,7 +343,7 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) self._cache_delete([ifacename, 'addrs']) def del_addr_all(self, ifacename, skip_addrs=[]): @@ -400,7 +400,7 @@ class iproute2(utilsBase): if self.ipbatch: self.add_to_batch(cmd) else: - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) def link_up(self, ifacename): self._link_set_ifflag(ifacename, 'UP') @@ -422,7 +422,7 @@ class iproute2(utilsBase): if self.ipbatch: self.add_to_batch(cmd) else: - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) if key not in ['master', 'nomaster']: self._cache_update([ifacename, key], value) @@ -435,7 +435,7 @@ class iproute2(utilsBase): if self.ipbatch: self.add_to_batch(cmd) else: - self.exec_command('ip ' + cmd) + utils.exec_command('ip %s' % cmd) self.link_up(ifacename) self._cache_update([ifacename, 'hwaddress'], hwaddress) @@ -448,8 +448,8 @@ class iproute2(utilsBase): self._cache_update([ifacename, 'mtu'], mtu) def link_set_alias(self, ifacename, alias): - self.exec_commandl(['ip', 'link', 'set', 'dev', - ifacename, 'alias', alias]) + utils.exec_commandl(['ip', 'link', 'set', 'dev', ifacename, + 'alias', alias]) def link_get_alias(self, ifacename): return self.read_file_oneline('/sys/class/net/%s/ifalias' @@ -477,7 +477,7 @@ class iproute2(utilsBase): if metric: cmd += 'metric %s' %metric cmd += ' dev %s' %ifacename - self.exec_command(cmd) + utils.exec_command(cmd) def route_del_gateway(self, ifacename, gateway, vrf=None, metric=None): # delete default gw @@ -490,26 +490,25 @@ class iproute2(utilsBase): if metric: cmd += ' metric %s' %metric cmd += ' dev %s' %ifacename - self.exec_command(cmd) + utils.exec_command(cmd) def route6_add_gateway(self, ifacename, gateway): if not gateway: return - return self.exec_command('ip -6 route add default via %s' %gateway + - ' dev %s' %ifacename) + return utils.exec_command('ip -6 route add default via %s dev %s' % + (gateway, ifacename)) def route6_del_gateway(self, ifacename, gateway): if not gateway: return - return self.exec_command('ip -6 route del default via %s' %gateway + - ' dev %s' %ifacename) + return utils.exec_command('ip -6 route del default via %s dev %s' % + (gateway, ifacename)) def link_create_vlan(self, vlan_device_name, vlan_raw_device, vlanid): if self.link_exists(vlan_device_name): return - self.exec_command('ip link add link %s' %vlan_raw_device + - ' name %s' %vlan_device_name + - ' type vlan id %d' %vlanid) + utils.exec_command('ip link add link %s name %s type vlan id %d' % + (vlan_raw_device, vlan_device_name, vlanid)) self._cache_update([vlan_device_name], {}) def link_create_vlan_from_name(self, vlan_device_name): @@ -528,14 +527,14 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip %s' %cmd) + utils.exec_command('ip %s' % cmd) self._cache_update([name], {}) def get_vxlan_peers(self, dev, svcnodeip): cmd = 'bridge fdb show brport %s' % dev cur_peers = [] try: - ps = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, close_fds=True) + ps = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, close_fds=False) utils.enable_subprocess_signal_forwarding(ps, signal.SIGINT) output = subprocess.check_output(('grep', '00:00:00:00:00:00'), stdin=ps.stdout) ps.wait() @@ -596,7 +595,7 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip %s' %cmd) + utils.exec_command('ip %s' % cmd) if not systemUtils.is_service_running(None, '/var/run/vxrd.pid'): #figure out the diff for remotes and do the bridge fdb updates @@ -636,10 +635,10 @@ class iproute2(utilsBase): return False def route_add(self, route): - self.exec_command('ip route add ' + route) + utils.exec_command('ip route add %s' % route) def route6_add(self, route): - self.exec_command('ip -6 route add ' + route) + utils.exec_command('ip -6 route add %s' % route) def get_vlandev_attrs(self, ifacename): return (self._cache_get('link', [ifacename, 'link']), @@ -680,7 +679,7 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip %s' %cmd) + utils.exec_command('ip %s' % cmd) self._cache_update([ifacename], {}) def link_delete(self, ifacename): @@ -690,7 +689,7 @@ class iproute2(utilsBase): if self.ipbatch and not self.ipbatch_pause: self.add_to_batch(cmd) else: - self.exec_command('ip %s' %cmd) + utils.exec_command('ip %s' % cmd) self._cache_invalidate() def link_get_master(self, ifacename): @@ -705,23 +704,23 @@ class iproute2(utilsBase): return self._cache_get('link', [ifacename, 'master']) def bridge_port_vids_add(self, bridgeportname, vids): - [self.exec_command('bridge vlan add vid %s dev %s' - %(v, bridgeportname)) for v in vids] + [utils.exec_command('bridge vlan add vid %s dev %s' % + (v, bridgeportname)) for v in vids] def bridge_port_vids_del(self, bridgeportname, vids): if not vids: return - [self.exec_command('bridge vlan del vid %s dev %s' - %(v, bridgeportname)) for v in vids] + [utils.exec_command('bridge vlan del vid %s dev %s' % + (v, bridgeportname)) for v in vids] def bridge_port_vids_flush(self, bridgeportname, vid): - self.exec_command('bridge vlan del vid %s dev %s' - %(vid, bridgeportname)) + utils.exec_command('bridge vlan del vid %s dev %s' % + (vid, bridgeportname)) def bridge_port_vids_get(self, bridgeportname): - self.exec_command('/sbin/bridge vlan show %s' %bridgeportname) - bridgeout = self.exec_command('/sbin/bridge vlan show dev %s' - %bridgeportname) + utils.exec_command('/sbin/bridge vlan show %s' % bridgeportname) + bridgeout = utils.exec_command('/sbin/bridge vlan show dev %s' % + bridgeportname) if not bridgeout: return [] brvlanlines = bridgeout.readlines()[2:] vids = [l.strip() for l in brvlanlines] @@ -729,7 +728,7 @@ class iproute2(utilsBase): def bridge_port_vids_get_all(self): brvlaninfo = {} - bridgeout = self.exec_command('/sbin/bridge -c vlan show') + bridgeout = utils.exec_command('/sbin/bridge -c vlan show') if not bridgeout: return brvlaninfo brvlanlines = bridgeout.splitlines() brportname=None @@ -757,12 +756,12 @@ class iproute2(utilsBase): return brvlaninfo def bridge_port_pvid_add(self, bridgeportname, pvid): - self.exec_command('bridge vlan add vid %s untagged pvid dev %s' - %(pvid, bridgeportname)) + utils.exec_command('bridge vlan add vid %s untagged pvid dev %s' % + (pvid, bridgeportname)) def bridge_port_pvid_del(self, bridgeportname, pvid): - self.exec_command('bridge vlan del vid %s untagged pvid dev %s' - %(pvid, bridgeportname)) + utils.exec_command('bridge vlan del vid %s untagged pvid dev %s' % + (pvid, bridgeportname)) def bridge_port_pvids_get(self, bridgeportname): return self.read_file_oneline('/sys/class/net/%s/brport/pvid' @@ -770,13 +769,13 @@ class iproute2(utilsBase): def bridge_vids_add(self, bridgeportname, vids, bridge=True): target = 'self' if bridge else '' - [self.exec_command('bridge vlan add vid %s dev %s %s' - %(v, bridgeportname, target)) for v in vids] + [utils.exec_command('bridge vlan add vid %s dev %s %s' % + (v, bridgeportname, target)) for v in vids] def bridge_vids_del(self, bridgeportname, vids, bridge=True): target = 'self' if bridge else '' - [self.exec_command('bridge vlan del vid %s dev %s %s' - %(v, bridgeportname, target)) for v in vids] + [utils.exec_command('bridge vlan del vid %s dev %s %s' % + (v, bridgeportname, target)) for v in vids] def bridge_fdb_add(self, dev, address, vlan=None, bridge=True, remote=None): target = 'self' if bridge else '' @@ -788,8 +787,8 @@ class iproute2(utilsBase): if remote: dst_str = 'dst %s ' % remote - self.exec_command('bridge fdb replace %s dev %s %s %s %s' - %(address, dev, vlan_str, target, dst_str)) + utils.exec_command('bridge fdb replace %s dev %s %s %s %s' % + (address, dev, vlan_str, target, dst_str)) def bridge_fdb_append(self, dev, address, vlan=None, bridge=True, remote=None): target = 'self' if bridge else '' @@ -801,8 +800,8 @@ class iproute2(utilsBase): if remote: dst_str = 'dst %s ' % remote - self.exec_command('bridge fdb append %s dev %s %s %s %s' - %(address, dev, vlan_str, target, dst_str)) + utils.exec_command('bridge fdb append %s dev %s %s %s %s' % + (address, dev, vlan_str, target, dst_str)) def bridge_fdb_del(self, dev, address, vlan=None, bridge=True, remote=None): target = 'self' if bridge else '' @@ -813,8 +812,8 @@ class iproute2(utilsBase): dst_str = '' if remote: dst_str = 'dst %s ' % remote - self.exec_command('bridge fdb del %s dev %s %s %s %s' - %(address, dev, vlan_str, target, dst_str)) + utils.exec_command('bridge fdb del %s dev %s %s %s %s' % + (address, dev, vlan_str, target, dst_str)) def bridge_is_vlan_aware(self, bridgename): filename = '/sys/class/net/%s/bridge/vlan_filtering' %bridgename @@ -839,7 +838,7 @@ class iproute2(utilsBase): def bridge_fdb_show_dev(self, dev): try: fdbs = {} - output = self.exec_command('bridge fdb show dev %s' %dev) + output = utils.exec_command('bridge fdb show dev %s' % dev) if output: for fdb_entry in output.splitlines(): try: @@ -870,7 +869,7 @@ class iproute2(utilsBase): def ip_route_get_dev(self, prefix): try: - output = self.exec_command('ip route get %s' %prefix) + output = utils.exec_command('ip route get %s' % prefix) if output: rline = output.splitlines()[0] if rline: diff --git a/ifupdownaddons/modulebase.py b/ifupdownaddons/modulebase.py index f91879c..c017841 100644 --- a/ifupdownaddons/modulebase.py +++ b/ifupdownaddons/modulebase.py @@ -8,10 +8,7 @@ import os import re import io import logging -import subprocess import traceback -import signal -import shlex from ifupdown.utils import utils from ifupdown.iface import * @@ -56,80 +53,12 @@ class moduleBase(object): def is_process_running(self, procName): try: - self.exec_command('/bin/pidof -x %s' % procName) + utils.exec_command('/bin/pidof -x %s' % procName) except: return False else: return True - def exec_command(self, cmd, cmdenv=None): - """ execute command passed as argument. - - Args: - cmd (str): command to execute - - Kwargs: - cmdenv (dict): environment variable name value pairs - """ - cmd_returncode = 0 - cmdout = '' - - try: - self.logger.info('Executing ' + cmd) - if ifupdownflags.flags.DRYRUN: - return cmdout - ch = subprocess.Popen(shlex.split(cmd), - stdout=subprocess.PIPE, - shell=False, env=cmdenv, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmdout = ch.communicate()[0] - cmd_returncode = ch.wait() - except OSError, e: - raise Exception('could not execute ' + cmd + - '(' + str(e) + ')') - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('error executing cmd \'%s\'' %cmd + - '(' + cmdout.strip('\n ') + ')') - return cmdout - - def exec_command_talk_stdin(self, cmd, stdinbuf): - """ execute command passed as argument and write contents of stdinbuf - into stdin of the cmd - - Args: - cmd (str): command to execute - stdinbuf (str): string to write to stdin of the cmd process - """ - cmd_returncode = 0 - cmdout = '' - - try: - self.logger.info('Executing %s (stdin=%s)' %(cmd, stdinbuf)) - if ifupdownflags.flags.DRYRUN: - return cmdout - ch = subprocess.Popen(shlex.split(cmd), - stdout=subprocess.PIPE, - stdin=subprocess.PIPE, - shell=False, env=cmdenv, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmdout = ch.communicate(input=stdinbuf)[0] - cmd_returncode = ch.wait() - except OSError, e: - raise Exception('could not execute ' + cmd + - '(' + str(e) + ')') - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('error executing cmd \'%s (%s)\'' - %(cmd, stdinbuf) + '(' + cmdout.strip('\n ') + ')') - return cmdout - def get_ifaces_from_proc(self): ifacenames = [] with open('/proc/net/dev') as f: @@ -301,11 +230,11 @@ class moduleBase(object): def sysctl_set(self, variable, value): """ set sysctl variable to value passed as argument """ - self.exec_command('sysctl %s=' %variable + '%s' %value) + utils.exec_command('sysctl %s=%s' % (variable, value)) def sysctl_get(self, variable): """ get value of sysctl variable """ - return self.exec_command('sysctl %s' %variable).split('=')[1].strip() + return utils.exec_command('sysctl %s' % variable).split('=')[1].strip() def set_iface_attr(self, ifaceobj, attr_name, attr_valsetfunc, prehook=None, prehookargs=None): @@ -385,7 +314,7 @@ class moduleBase(object): if not os.path.exists(get_resvvlan): return (start, end) try: - (s, e) = self.exec_command(get_resvvlan).strip('\n').split('-') + (s, e) = utils.exec_command(get_resvvlan).strip('\n').split('-') start = int(s) end = int(e) except Exception, e: diff --git a/ifupdownaddons/mstpctlutil.py b/ifupdownaddons/mstpctlutil.py index 694439c..078ea9b 100644 --- a/ifupdownaddons/mstpctlutil.py +++ b/ifupdownaddons/mstpctlutil.py @@ -7,6 +7,7 @@ from cache import MSTPAttrsCache from utilsbase import * from ifupdown.iface import * +from ifupdown.utils import utils from cache import * import re import json @@ -40,7 +41,7 @@ class mstpctlutil(utilsBase): def is_mstpd_running(self): try: - self.exec_command('/bin/pidof mstpd') + utils.exec_command('/bin/pidof mstpd') except: return False else: @@ -48,9 +49,9 @@ class mstpctlutil(utilsBase): def get_bridgeport_attr(self, bridgename, portname, attrname): try: - return self.subprocess_check_output(['/sbin/mstpctl', - 'showportdetail', '%s' %bridgename, '%s' %portname, - self._bridgeportattrmap[attrname]]).strip('\n') + cmdl = ['/sbin/mstpctl', 'showportdetail', bridgename, portname, + self._bridgeportattrmap[attrname]] + return utils.exec_commandl(cmdl).strip('\n') except Exception, e: pass return None @@ -72,7 +73,7 @@ class mstpctlutil(utilsBase): if not attrs: try: cmd = ['/sbin/mstpctl', 'showportdetail', bridgename, 'json'] - output = self.subprocess_check_output(cmd) + output = utils.exec_commandl(cmd) if not output: return None except Exception as e: @@ -124,11 +125,13 @@ class mstpctlutil(utilsBase): if attrvalue_curr and attrvalue_curr == attrvalue: return if attrname == 'treeportcost' or attrname == 'treeportprio': - self.subprocess_check_output(['/sbin/mstpctl', 'set%s' %attrname, - '%s' %bridgename, '%s' %bridgeportname, '0', '%s' %attrvalue]) + utils.exec_commandl(['/sbin/mstpctl', 'set%s' % attrname, + '%s' % bridgename, '%s' % bridgeportname, '0', + '%s' % attrvalue]) else: - self.subprocess_check_output(['/sbin/mstpctl', 'set%s' %attrname, - '%s' %bridgename, '%s' %bridgeportname, '%s' %attrvalue]) + utils.exec_commandl(['/sbin/mstpctl', 'set%s' % attrname, + '%s' % bridgename, '%s' % bridgeportname, + '%s' % attrvalue]) def get_bridge_attrs(self, bridgename): bridgeattrs = {} @@ -146,9 +149,9 @@ class mstpctlutil(utilsBase): def get_bridge_attr(self, bridgename, attrname): try: - return self.subprocess_check_output(['/sbin/mstpctl', - 'showbridge', '%s' %bridgename, - self._bridgeattrmap[attrname]]).strip('\n') + cmdl = ['/sbin/mstpctl', 'showbridge', bridgename, + self._bridgeattrmap[attrname]] + return utils.exec_commandl(cmdl).strip('\n') except Exception, e: pass return None @@ -160,11 +163,13 @@ class mstpctlutil(utilsBase): if attrvalue_curr and attrvalue_curr == attrvalue: return if attrname == 'treeprio': - self.subprocess_check_call(['/sbin/mstpctl', 'set%s' %attrname, - '%s' %bridgename, '0', '%s' %attrvalue]) + utils.exec_commandl(['/sbin/mstpctl', 'set%s' % attrname, + '%s' % bridgename, '0', '%s' % attrvalue], + stdout=False, stderr=None) else: - self.subprocess_check_call(['/sbin/mstpctl', 'set%s' %attrname, - '%s' %bridgename, '%s' %attrvalue]) + utils.exec_commandl(['/sbin/mstpctl', 'set%s' % attrname, + '%s' % bridgename, '%s' % attrvalue], + stdout=False, stderr=None) def set_bridge_attrs(self, bridgename, attrdict, check=True): for k, v in attrdict.iteritems(): @@ -178,9 +183,12 @@ class mstpctlutil(utilsBase): def get_bridge_treeprio(self, bridgename): try: - bridgeid = subprocess.check_output(['/sbin/mstpctl', - 'showbridge', '%s' %bridgename, - self._bridgeattrmap['bridgeid']]).strip('\n') + cmdl = ['/sbin/mstpctl', + 'showbridge', + bridgename, + self._bridgeattrmap['bridgeid']] + + bridgeid = utils.exec_commandl(cmdl).strip('\n') return '%d' %(int(bridgeid.split('.')[0], base=16) * 4096) except: pass @@ -191,21 +199,21 @@ class mstpctlutil(utilsBase): attrvalue_curr = self.get_bridge_treeprio(bridgename) if attrvalue_curr and attrvalue_curr == attrvalue: return - self.subprocess_check_output(['/sbin/mstpctl', 'settreeprio', - '%s' %bridgename, '0', '%s' %attrvalue]) + utils.exec_commandl(['/sbin/mstpctl', 'settreeprio', bridgename, '0', + str(attrvalue)]) def showbridge(self, bridgename=None): if bridgename: - return self.exec_command('/sbin/mstpctl showbridge %s' %bridgename) + return utils.exec_command('/sbin/mstpctl showbridge %s' % bridgename) else: - return self.exec_command('/sbin/mstpctl showbridge') + return utils.exec_command('/sbin/mstpctl showbridge') def showportdetail(self, bridgename): - return self.exec_command('/sbin/mstpctl showportdetail %s' %bridgename) + return utils.exec_command('/sbin/mstpctl showportdetail %s' % bridgename) def mstpbridge_exists(self, bridgename): try: - subprocess.check_call('mstpctl showbridge %s' %bridgename) + utils.exec_command('mstpctl showbridge %s' % bridgename, stdout=False) return True except: return False diff --git a/ifupdownaddons/systemutils.py b/ifupdownaddons/systemutils.py index 84d1873..a6aad67 100644 --- a/ifupdownaddons/systemutils.py +++ b/ifupdownaddons/systemutils.py @@ -6,6 +6,7 @@ import os from utilsbase import * +from ifupdown.utils import utils class systemUtils(): @classmethod @@ -22,7 +23,7 @@ class systemUtils(): if procname: try: - utilsobj.exec_command('/bin/pidof %s' %procname) + utils.exec_command('/bin/pidof %s' % procname, stdout=False) except: return False else: @@ -34,10 +35,9 @@ class systemUtils(): def check_service_status(cls, servicename=None): if not servicename: return False - utilsobj = utilsBase() try: - utilsobj.subprocess_check_call(['/usr/sbin/service', - '%s' %servicename, 'status']) + utils.exec_commandl(['/usr/sbin/service', servicename, 'status'], + stdout=False) except Exception: # XXX: check for subprocess errors vs os error return False @@ -47,9 +47,8 @@ class systemUtils(): def is_process_running(self, processname): if not processname: return False - utilsobj = utilsBase() try: - utilsobj.exec_command('/bin/pidof %s' %processname) + utils.exec_command('/bin/pidof %s' % processname, stdout=False) except: return False else: diff --git a/ifupdownaddons/utilsbase.py b/ifupdownaddons/utilsbase.py index a490c8f..477fcc8 100644 --- a/ifupdownaddons/utilsbase.py +++ b/ifupdownaddons/utilsbase.py @@ -5,11 +5,8 @@ # import logging -import subprocess import re import io -import signal -import shlex from ifupdown.utils import utils import ifupdown.ifupdownflags as ifupdownflags @@ -34,106 +31,6 @@ class utilsBase(object): modulename = self.__class__.__name__ self.logger = logging.getLogger('ifupdown.' + modulename) - def exec_commandl(self, cmdl, cmdenv=None): - """ Executes command """ - - cmd_returncode = 0 - cmdout = '' - try: - self.logger.info('executing ' + ' '.join(cmdl)) - if ifupdownflags.flags.DRYRUN: - return cmdout - ch = subprocess.Popen(cmdl, - stdout=subprocess.PIPE, - shell=False, env=cmdenv, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmdout = ch.communicate()[0] - cmd_returncode = ch.wait() - except OSError, e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(' '.join(cmdl), str(e))) - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('failed to execute cmd \'%s\'' - %' '.join(cmdl) + '(' + cmdout.strip('\n ') + ')') - return cmdout - - def exec_command(self, cmd, cmdenv=None): - """ Executes command given as string in the argument cmd """ - - return self.exec_commandl(shlex.split(cmd), cmdenv) - - def exec_command_talk_stdin(self, cmd, stdinbuf): - """ Executes command and writes to stdin of the process """ - cmd_returncode = 0 - cmdout = '' - try: - self.logger.info('executing %s [%s]' %(cmd, stdinbuf)) - if ifupdownflags.flags.DRYRUN: - return cmdout - ch = subprocess.Popen(shlex.split(cmd), - stdout=subprocess.PIPE, - stdin=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmdout = ch.communicate(input=stdinbuf)[0] - cmd_returncode = ch.wait() - except OSError, e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(cmd, str(e))) - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('failed to execute cmd \'%s [%s]\'' - %(cmd, stdinbuf) + '(' + cmdout.strip('\n ') + ')') - return cmdout - - def subprocess_check_output(self, cmdl): - self.logger.info('executing ' + ' '.join(cmdl)) - if ifupdownflags.flags.DRYRUN: - return - try: - return subprocess.check_output(cmdl, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(' '.join(cmdl), e.output)) - except Exception as e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(' '.join(cmdl), str(e))) - - def subprocess_check_call(self, cmdl): - """ subprocess check_call implementation using popen - - Uses popen because it needs the close_fds argument - """ - - cmd_returncode = 0 - try: - self.logger.info('executing ' + ' '.join(cmdl)) - if ifupdownflags.flags.DRYRUN: - return - ch = subprocess.Popen(cmdl, - stdout=None, - shell=False, - stderr=None, - close_fds=True) - utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) - cmd_returncode = ch.wait() - except Exception, e: - raise Exception('failed to execute cmd \'%s\' (%s)' - %(' '.join(cmdl), str(e))) - finally: - utils.disable_subprocess_signal_forwarding(signal.SIGINT) - if cmd_returncode != 0: - raise Exception('failed to execute cmd \'%s\'' - %' '.join(cmdl)) - return - def write_file(self, filename, strexpr): try: self.logger.info('writing \'%s\'' %strexpr + @@ -167,7 +64,7 @@ class utilsBase(object): return None def sysctl_set(self, variable, value): - self.exec_command('sysctl %s=' %variable + '%s' %value) + utils.exec_command('sysctl %s=%s' % (variable, value)) def sysctl_get(self, variable): - return self.exec_command('sysctl %s' %variable).split('=')[1].strip() + return utils.exec_command('sysctl %s' % variable).split('=')[1].strip() diff --git a/sbin/ifupdown2 b/sbin/ifupdown2 index 5c38533..3a56ade 100755 --- a/sbin/ifupdown2 +++ b/sbin/ifupdown2 @@ -457,7 +457,8 @@ def validate_args(op, args): def read_config(args): global configmap_g - config = open(configfile, 'r').read() + with open(configfile, 'r') as f: + config = f.read() configStr = '[ifupdown2]\n' + config configFP = StringIO.StringIO(configStr) parser = ConfigParser.RawConfigParser()