from graph import *
from sets import Set
+_tickmark = u'\u2713'
+_crossmark = u'\u2717'
+_success_sym = _tickmark
+_error_sym = _crossmark
+
class ifupdownMain(ifupdownBase):
""" ifupdown2 main class """
return ifaceobj
def create_n_save_ifaceobjcurr(self, ifaceobj):
- """ creates a copy of iface object and adds it to the iface dict containing current iface objects
+ """ creates a copy of iface object and adds it to the iface
+ dict containing current iface objects
"""
- ifaceobjcurr = self.get_ifaceobjcurr(ifaceobj.name)
- if ifaceobjcurr:
- return ifaceobjcurr
ifaceobjcurr = iface()
ifaceobjcurr.name = ifaceobj.name
ifaceobjcurr.lowerifaces = ifaceobj.lowerifaces
ifaceobjcurr.priv_flags = ifaceobj.priv_flags
ifaceobjcurr.auto = ifaceobj.auto
- self.ifaceobjcurrdict[ifaceobj.name] = ifaceobjcurr
+ self.ifaceobjcurrdict.setdefault(ifaceobj.name,
+ []).append(ifaceobjcurr)
return ifaceobjcurr
- def get_ifaceobjcurr(self, ifacename):
- return self.ifaceobjcurrdict.get(ifacename)
+ def get_ifaceobjcurr(self, ifacename, idx=0):
+ ifaceobjlist = self.ifaceobjcurrdict.get(ifacename)
+ if not ifaceobjlist:
+ return None
+ if not idx:
+ return ifaceobjlist
+ else:
+ return ifaceobjlist[idx]
def get_ifaceobjrunning(self, ifacename):
return self.ifaceobjrunningdict.get(ifacename)
if ifaceobj.compare(currentifaceobjlist[0]):
self.logger.warn('duplicate interface %s found' %ifaceobj.name)
return
+ currentifaceobjlist[0].flags |= iface.HAS_SIBLINGS
+ ifaceobj.flags |= iface.HAS_SIBLINGS
self.ifaceobjdict[ifaceobj.name].append(ifaceobj)
def _iface_configattr_syntax_checker(self, attrname, attrval):
validrange = attrvaldict.get('validrange')
if validrange:
- print('%svalidrange: %d-%d'
+ print('%svalidrange: %s-%s'
%(indent + ' ', validrange[0], validrange[1]))
validvals = attrvaldict.get('validvals')
if not ifaceobjs:
err_iface += ' ' + i
if err_iface:
- self.logger.error('cannot find interfaces: %s' %err_iface)
- return False
+ raise Exception('cannot find interfaces:%s' %err_iface)
return True
def _iface_whitelisted(self, auto, allow_classes, excludepats, ifacename):
if ifacenames:
# If iface list is given by the caller, always check if iface
# is present
- if not self._validate_ifaces(ifacenames):
- raise Exception('all or some interfaces not found')
+ self._validate_ifaces(ifacenames)
# if iface list not given by user, assume all from config file
if not ifacenames: ifacenames = self.ifaceobjdict.keys()
self.read_old_iface_config()
else:
# If no old state available
- self.logger.info('Loading current iface config file')
try:
self.read_iface_config()
except Exception, e:
if ifacenames:
# If iface list is given by the caller, always check if iface
# is present
- if not self._validate_ifaces(ifacenames):
- raise Exception('interface(s) was probably never up')
+ try:
+ self._validate_ifaces(ifacenames)
+ except Exception, e:
+ raise Exception('%s' %str(e) +
+ ' (interface was probably never up ?)')
+
# if iface list not given by user, assume all from config file
if not ifacenames: ifacenames = self.ifaceobjdict.keys()
if not filtered_ifacenames:
raise Exception('no ifaces found matching given allow lists')
- self.populate_dependency_info(ops, filtered_ifacenames)
if printdependency:
+ self.populate_dependency_info(ops, filtered_ifacenames)
self.print_dependency(filtered_ifacenames, printdependency)
return
+ else:
+ self.populate_dependency_info(ops)
try:
self._sched_ifaces(filtered_ifacenames, ops)
if ifacenames and ops[0] != 'query-running':
# If iface list is given, always check if iface is present
- if not self._validate_ifaces(ifacenames):
- raise Exception('all or some interfaces not found')
+ self._validate_ifaces(ifacenames)
# if iface list not given by user, assume all from config file
if not ifacenames: ifacenames = self.ifaceobjdict.keys()
def _get_ifaceobjscurr_pretty(self, ifacenames, ifaceobjs):
ret = 0
for i in ifacenames:
- ifaceobj = self.get_ifaceobjcurr(i)
- if not ifaceobj: continue
- if (ifaceobj.status == ifaceStatus.NOTFOUND or
- ifaceobj.status == ifaceStatus.ERROR):
- ret = 1
- if self.is_ifaceobj_noconfig(ifaceobj):
- continue
- ifaceobjs.append(ifaceobj)
- if self.WITH_DEPENDS and not self.ALL:
- dlist = ifaceobj.lowerifaces
- if not dlist: continue
- dret = self._get_ifaceobjscurr_pretty(dlist, ifaceobjs)
- if dret: ret = 1
+ ifaceobjscurr = self.get_ifaceobjcurr(i)
+ if not ifaceobjscurr: continue
+ for ifaceobj in ifaceobjscurr:
+ if (ifaceobj.status == ifaceStatus.NOTFOUND or
+ ifaceobj.status == ifaceStatus.ERROR):
+ ret = 1
+ if self.is_ifaceobj_noconfig(ifaceobj):
+ continue
+ ifaceobjs.append(ifaceobj)
+ if self.WITH_DEPENDS and not self.ALL:
+ dlist = ifaceobj.lowerifaces
+ if not dlist: continue
+ dret = self._get_ifaceobjscurr_pretty(dlist, ifaceobjs)
+ if dret: ret = 1
return ret
def print_ifaceobjscurr_pretty(self, ifacenames, format='native'):
print json.dumps(ifaceobjs, cls=ifaceJsonEncoder, indent=2,
separators=(',', ': '))
else:
- map(lambda i: i.dump_pretty(with_status=True), ifaceobjs)
+ map(lambda i: i.dump_pretty(with_status=True,
+ successstr=self.config.get('check_success_str',
+ _success_sym),
+ errorstr=self.config.get('check_error_str', _error_sym)),
+ ifaceobjs)
return ret
def print_ifaceobjsrunning_pretty(self, ifacenames, format='native'):