From 60dfcbdf9cbc9afbbd90002b123e5649bb3327a3 Mon Sep 17 00:00:00 2001 From: Roopa Prabhu Date: Sun, 10 Jul 2016 22:48:42 -0700 Subject: [PATCH] ifupdown: fixes to make addon scripts work via special config options ifupdown2 can invoke scripts under /etc/network/if-*.d/* in the required order with the required environment variables. This patch includes fixes to execute these scripts. The following attributes in /etc/network/ifupdown2/ifupdown.conf can influence the execution behaviour of python-addon modules and /etc/network/if-*.d/* addon_syntax_check=0 addon_scripts_support=1 addon_python_modules_support=1 Signed-off-by: Roopa Prabhu --- config/ifupdown2.conf | 17 ++++++++++++++++- ifupdown/iface.py | 6 ++---- ifupdown/ifupdownmain.py | 26 ++++++++++++++++---------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/config/ifupdown2.conf b/config/ifupdown2.conf index 839d9ac..906fdea 100644 --- a/config/ifupdown2.conf +++ b/config/ifupdown2.conf @@ -21,9 +21,24 @@ default_interfaces_configfile=/etc/network/interfaces # reduce security issues (due to the pre- and post- commands) disable_cli_interfacesfile=0 -# Support /etc/network/if-*/ scripts + +# enable addon module syntax check: +# Python addon modules register dictionary of supported attributes. +# The syntax checker in ifupdown2 uses this dictionary for syntax +# checks in the interfaces file. This works well, when only python modules +# are used. But when a mix of scripts and modules are used (which is the +# default case), you may get false warnings for attributes supported +# by scripts +addon_syntax_check=1 + +# Support executing of ifupdown style scripts. +# Note that by default python addon modules override scripts with the same +# name addon_scripts_support=0 +# enable python addons +addon_python_modules_support=1 + # By default ifupdown2 only supports a single vlan filtering bridge # on the system. Set this flag to 1 to support multiple vlan # filtering bridges diff --git a/ifupdown/iface.py b/ifupdown/iface.py index 3c70761..9dd5e14 100644 --- a/ifupdown/iface.py +++ b/ifupdown/iface.py @@ -483,7 +483,6 @@ class iface(): return None return None - @property def get_env(self): """ get shell environment variables the interface must execute in """ if not self.env: @@ -498,10 +497,9 @@ class iface(): config = self.config env['IFACE'] = self.name for attr, attr_value in config.items(): - attr_env_name = 'IF_%s' %attr.upper() + attr_env_name = 'IF_%s' %attr.upper().replace("-", "_") env[attr_env_name] = attr_value[0] - if env: - self.env = env + self.env = env def update_config(self, attr_name, attr_value): """ add attribute name and value to the interface config """ diff --git a/ifupdown/ifupdownmain.py b/ifupdown/ifupdownmain.py index 26b2512..18231a0 100644 --- a/ifupdown/ifupdownmain.py +++ b/ifupdown/ifupdownmain.py @@ -221,9 +221,10 @@ class ifupdownMain(ifupdownBase): self.pp = pprint.PrettyPrinter(indent=4) self.modules = OrderedDict({}) self.module_attrs = {} - - self.load_addon_modules(self.addon_modules_dir) - if self.flags.COMPAT_EXEC_SCRIPTS: + + if self.config.get('addon_python_modules_support', '1') == '1': + self.load_addon_modules(self.addon_modules_dir) + if self.config.get('addon_scripts_support', '0') == '1': self.load_scripts(self.scripts_dir) self.dependency_graph = OrderedDict({}) @@ -779,9 +780,10 @@ class ifupdownMain(ifupdownBase): nifaces.subscribe('iface_found', self._save_iface_squash) else: nifaces.subscribe('iface_found', self._save_iface) - nifaces.subscribe('validateifaceattr', - self._iface_configattr_syntax_checker) - nifaces.subscribe('validateifaceobj', self._ifaceobj_syntax_checker) + if self.config.get('addon_syntax_check', '1') == '1': + nifaces.subscribe('validateifaceattr', + self._iface_configattr_syntax_checker) + nifaces.subscribe('validateifaceobj', self._ifaceobj_syntax_checker) nifaces.load() if nifaces.errors or nifaces.warns: ret = False @@ -1038,9 +1040,9 @@ class ifupdownMain(ifupdownBase): def _compat_conv_op_to_mode(self, op): """ Returns old op name to work with existing scripts """ - if op == 'pre-up': + if 'up' in op: return 'start' - elif op == 'pre-down': + elif 'down' in op: return 'stop' else: return op @@ -1051,14 +1053,18 @@ class ifupdownMain(ifupdownBase): """ cenv = None - iface_env = ifaceobj.env + iface_env = ifaceobj.get_env() if iface_env: cenv = os.environ if cenv: cenv.update(iface_env) else: cenv = iface_env - cenv['MODE'] = self._compat_conv_op_to_mode(op) + else: + cenv = {} + cenv['MODE'] = self._compat_conv_op_to_mode(op) + cenv['PHASE'] = op + return cenv def _save_state(self):