1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00

cleanup + fixes

Ticket: CM-1438
Reviewed By:
Testing Done: Tested ifupdown sanity
This commit is contained in:
roopa
2014-03-25 15:21:19 -07:00
parent fa3da4be43
commit 53b0022499
4 changed files with 148 additions and 523 deletions

View File

@@ -11,10 +11,9 @@ from collections import OrderedDict
import logging
import os
from iface import *
import copy
import marshal
class pickling():
""" class with helper methods for pickling/unpickling iface objects """
@classmethod
def save(cls, filename, list_of_objects):
@@ -41,6 +40,7 @@ class pickling():
except: raise
class stateManager():
""" state manager for managing ifupdown iface obj state """
state_dir = '/var/tmp/network/'
state_filename = 'ifstatenew'
@@ -61,98 +61,31 @@ class stateManager():
pickle_filename = filename
if not pickle_filename:
pickle_filename = self.state_file
if not os.path.exists(pickle_filename):
return
# Read all ifaces from file
for ifaceobj in pickling.load(pickle_filename):
self.save_ifaceobj(ifaceobj)
return 0
def get_ifaceobjdict(self):
return self.ifaceobjdict
def get_ifaceobjs(self, ifacename):
return self.ifaceobjdict.get(ifacename)
def compare_iface_state(ifaceobj1, ifaceobj2):
ifaceobj1_state = ifaceobj1.state
ifaceobj2_state = ifaceobj2.state
if ifaceobj1_state < ifaceobj2_state:
return -1
elif ifaceobj1_state > ifaceobj2_state:
return 1
elif ifaceobj1_state == ifaceobj2_state:
return 0
def cmp_old_new_state(self, ifacename, operation):
""" compares current operation with old state """
state_arg = ifaceState.from_str(operation)
if state_arg == ifaceState.UP:
old_ifaceobj = self.ifaceobjdict.get(ifacename)
if old_ifaceobj:
# found old state for iface
# Check its state
if (old_ifaceobj.state == state_arg and
old_ifaceobj.status == ifaceStatus.SUCCESS):
self.statemsg = 'iface already up'
return 0
elif state_arg == ifaceState.DOWN:
old_ifaceobj = self.ifaceobjdict.get(ifname)
if old_ifaceobj:
# found old state for iface
# Check its state
if (old_ifaceobj.state == state_arg and
old_ifaceobj.status == ifaceStatus.SUCCESS):
self.statemsg = 'iface already down'
return 0
return 1
def ifaceobj_compare(self, ifaceobj_a, ifaceobj_b):
if ifaceobj_a.name != ifaceobj_b.name:
return False
if (not ifaceobj_a.addr_family and
ifaceobj_b.addr_family):
return False
if (ifaceobj_a.addr_family and
not ifaceobj_b.addr_family):
return False
if (not ifaceobj_a.addr_family and
not ifaceobj_b.addr_family):
return True
if ifaceobj_a.addr_family != ifaceobj_b.addr_family:
return False
return True
def ifaceobj_sync(self, ifaceobj):
ifacename = ifaceobj.name
self.logger.debug('%s: statemanager sync state' %ifacename)
old_ifaceobjs = self.ifaceobjdict.get(ifacename)
self.logger.debug('%s: statemanager sync state' %ifaceobj.name)
old_ifaceobjs = self.ifaceobjdict.get(ifaceobj.name)
if not old_ifaceobjs:
self.ifaceobjdict[ifacename] = [ifaceobj]
self.ifaceobjdict[ifaceobj.name] = [ifaceobj]
else:
if old_ifaceobjs[0].flags & iface._PICKLED:
del self.ifaceobjdict[ifacename]
self.ifaceobjdict[ifacename] = [ifaceobj]
del self.ifaceobjdict[ifaceobj.name]
self.ifaceobjdict[ifaceobj.name] = [ifaceobj]
else:
self.ifaceobjdict[ifacename].append(ifaceobj)
self.ifaceobjdict[ifaceobj.name].append(ifaceobj)
def save_state(self):
try:
with open(self.state_file, 'w') as f:
for ifaceobjs in self.ifaceobjdict.values():
for i in ifaceobjs:
pickling.save_obj(f, i)
[pickling.save_obj(f, i) for i in ifaceobjs]
except:
raise