From cb7cc59218979af827758daac2a95efd244de4ff Mon Sep 17 00:00:00 2001 From: roopa Date: Sun, 30 Mar 2014 22:38:00 -0700 Subject: [PATCH] Cleanup saved state for down objects on ifdown Ticket: CM-1438 Reviewed By: Testing Done: Tested ifup/ifdown sanity This was a pending cleanup activity. --- pkg/ifupdownmain.py | 33 ++++++++++++------------------- pkg/scheduler.py | 3 +-- pkg/statemanager.py | 47 +++++++++++++++++++++++++++++++-------------- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/pkg/ifupdownmain.py b/pkg/ifupdownmain.py index ea0bb2e..21ca42f 100644 --- a/pkg/ifupdownmain.py +++ b/pkg/ifupdownmain.py @@ -13,6 +13,7 @@ import imp import pprint import logging import sys, traceback +import copy from statemanager import * from networkinterfaces import * from iface import * @@ -95,12 +96,12 @@ class ifupdownMain(ifupdownBase): ops_handlers = OrderedDict([('up', run_up), ('down', run_down)]) - def run_sched_ifaceobj_posthook(self, ifaceobj): + def run_sched_ifaceobj_posthook(self, ifaceobj, op): if ((ifaceobj.priv_flags & self.BUILTIN) or (ifaceobj.priv_flags & self.NOCONFIG)): return if self.STATEMANAGER_UPDATE: - self.statemanager.ifaceobj_sync(ifaceobj) + self.statemanager.ifaceobj_sync(ifaceobj, op) # ifupdown object interface scheduler pre and posthooks sched_hooks = {'posthook' : run_sched_ifaceobj_posthook} @@ -144,7 +145,6 @@ class ifupdownMain(ifupdownBase): else: self.STATEMANAGER_UPDATE = False - def get_ifaceobjs(self, ifacename): return self.ifaceobjdict.get(ifacename) @@ -329,11 +329,9 @@ class ifupdownMain(ifupdownBase): if not currentifaceobjlist: self.ifaceobjdict[ifaceobj.name]= [ifaceobj] return - if ifaceobj.compare(currentifaceobjlist[0]): self.logger.warn('duplicate interface %s found' %ifaceobj.name) return - self.ifaceobjdict[ifaceobj.name].append(ifaceobj) def _module_syntax_checker(self, attrname, attrval): @@ -355,7 +353,7 @@ class ifupdownMain(ifupdownBase): def read_old_iface_config(self): """ Reads the saved iface config instead of default iface config. """ - self.ifaceobjdict = self.statemanager.ifaceobjdict + self.ifaceobjdict = copy.deepcopy(self.statemanager.ifaceobjdict) def _load_addon_modules_config(self): """ Load addon modules config file """ @@ -505,9 +503,9 @@ class ifupdownMain(ifupdownBase): if not ifaceobjs: err_iface += ' ' + i if err_iface: - self.logger.error('could not find interfaces: %s' %err_iface) - return -1 - return 0 + self.logger.error('cannot find interfaces: %s' %err_iface) + return False + return True def _iface_whitelisted(self, auto, allow_classes, excludepats, ifacename): """ Checks if interface is whitelisted depending on set of parameters. @@ -581,10 +579,10 @@ class ifupdownMain(ifupdownBase): excludepats=None, printdependency=None, syntaxcheck=False): """ up an interface """ + if self.ADDONS_ENABLE: self.STATEMANAGER_UPDATE = False if auto: self.ALL = True self.WITH_DEPENDS = True - try: self.read_iface_config() except Exception: @@ -597,7 +595,7 @@ class ifupdownMain(ifupdownBase): if ifacenames: # If iface list is given by the caller, always check if iface # is present - if self._validate_ifaces(ifacenames) != 0: + if not self._validate_ifaces(ifacenames): raise Exception('all or some interfaces not found') # if iface list not given by user, assume all from config file @@ -638,7 +636,6 @@ class ifupdownMain(ifupdownBase): self.statemanager.ifaceobjdict): # Since we are using state manager objects, # skip the updating of state manager objects - self.STATEMANAGER_UPDATE = False self.logger.debug('Looking at old state ..') self.read_old_iface_config() else: @@ -651,8 +648,8 @@ class ifupdownMain(ifupdownBase): if ifacenames: # If iface list is given by the caller, always check if iface # is present - if self._validate_ifaces(ifacenames) != 0: - raise Exception('all or some interfaces were never up') + if not self._validate_ifaces(ifacenames): + raise Exception('interface(s) was probably never up') # if iface list not given by user, assume all from config file if not ifacenames: ifacenames = self.ifaceobjdict.keys() @@ -662,18 +659,15 @@ class ifupdownMain(ifupdownBase): excludepats, i)] if not filtered_ifacenames: raise Exception('no ifaces found matching given allow lists') - if printdependency: self.populate_dependency_info(ops, filtered_ifacenames) self.print_dependency(filtered_ifacenames, printdependency) return else: self.populate_dependency_info(ops) - self._sched_ifaces(filtered_ifacenames, ops) if self.DRYRUN and self.ADDONS_ENABLE: return - self._save_state() def query(self, ops, auto=False, allow_classes=None, ifacenames=None, @@ -705,7 +699,7 @@ class ifupdownMain(ifupdownBase): if ifacenames and ops[0] != 'query-running': # If iface list is given, always check if iface is present - if self._validate_ifaces(ifacenames) != 0: + if not self._validate_ifaces(ifacenames): raise Exception('all or some interfaces not found') # if iface list not given by user, assume all from config file @@ -774,7 +768,6 @@ class ifupdownMain(ifupdownBase): # followed by 'up' aka: reload # old interface config is read into self.ifaceobjdict # - self.STATEMANAGER_UPDATE = False self.read_old_iface_config() op = 'reload' else: @@ -828,7 +821,6 @@ class ifupdownMain(ifupdownBase): # Now, run 'up' with new config dict # reset statemanager update flag to default - self.STATEMANAGER_UPDATE = True self.ifaceobjdict = new_ifaceobjdict self.dependency_graph = new_dependency_graph ifacenames = self.ifaceobjdict.keys() @@ -841,7 +833,6 @@ class ifupdownMain(ifupdownBase): self._sched_ifaces(filtered_ifacenames, upops) if self.DRYRUN: return - self._save_state() def _pretty_print_ordered_dict(self, argdict): diff --git a/pkg/scheduler.py b/pkg/scheduler.py index a6cfe6a..a087093 100644 --- a/pkg/scheduler.py +++ b/pkg/scheduler.py @@ -110,8 +110,7 @@ class ifaceScheduler(): map(lambda op: cls.run_iface_op(ifupdownobj, ifaceobj, op, cenv), ops) posthookfunc = ifupdownobj.sched_hooks.get('posthook') if posthookfunc: - posthookfunc(ifupdownobj, ifaceobj) - + posthookfunc(ifupdownobj, ifaceobj, ops[0]) @classmethod def _check_upperifaces(cls, ifupdownobj, ifaceobj, ops, parent, diff --git a/pkg/statemanager.py b/pkg/statemanager.py index 46747c5..5263dd0 100644 --- a/pkg/statemanager.py +++ b/pkg/statemanager.py @@ -69,28 +69,47 @@ class stateManager(): def get_ifaceobjs(self, ifacename): return self.ifaceobjdict.get(ifacename) - def ifaceobj_sync(self, ifaceobj): + def ifaceobj_sync(self, ifaceobj, op): self.logger.debug('%s: statemanager sync state' %ifaceobj.name) old_ifaceobjs = self.ifaceobjdict.get(ifaceobj.name) - if not old_ifaceobjs: - self.ifaceobjdict[ifaceobj.name] = [ifaceobj] - else: - # If it matches any of the object, return - if any(o.compare(ifaceobj) for o in old_ifaceobjs): - return - # If it does not match any of the objects, and if - # all objs in the list came from the pickled file, - # then reset the list and add this object as a fresh one, - # else append to the list - if old_ifaceobjs[0].flags & iface._PICKLED: - del self.ifaceobjdict[ifaceobj.name] + if 'up' in op: + if not old_ifaceobjs: self.ifaceobjdict[ifaceobj.name] = [ifaceobj] else: - self.ifaceobjdict[ifaceobj.name].append(ifaceobj) + # If it matches any of the object, return + if any(o.compare(ifaceobj) for o in old_ifaceobjs): + return + # If it does not match any of the objects, and if + # all objs in the list came from the pickled file, + # then reset the list and add this object as a fresh one, + # else append to the list + if old_ifaceobjs[0].flags & iface._PICKLED: + del self.ifaceobjdict[ifaceobj.name] + self.ifaceobjdict[ifaceobj.name] = [ifaceobj] + else: + self.ifaceobjdict[ifaceobj.name].append(ifaceobj) + elif 'down' in op: + # If down of object successfull, delete object from state manager + if not old_ifaceobjs: + return + if ifaceobj.status != ifaceStatus.SUCCESS: + return + # If it matches any of the object, return + oidx = 0 + for o in old_ifaceobjs: + if o.compare(ifaceobj): + old_ifaceobjs.pop(oidx) + if not len(old_ifaceobjs): + del self.ifaceobjdict[ifaceobj.name] + return + oidx += 1 def save_state(self): try: with open(self.state_file, 'w') as f: + if not len(self.ifaceobjdict): + os.remove(self.state_file) + return for ifaceobjs in self.ifaceobjdict.values(): [pickling.save_obj(f, i) for i in ifaceobjs] except: