def AddIfaceFiles(self, ifname): self.ifcfgs[ifname] = env.cfgopen(_IFCFG_PATH + ifname, readonly=False, error_cb=self.error_cb) self.routes[ifname] = env.cfgopen(_ROUTE_PATH + ifname, readonly=False, error_cb=self.error_cb)
def AddIface(self, ifname): self.ifcfgs[ifname] = env.cfgopen(_IFCFG_PATH + ifname, readonly=False, delempty=True) self.ifcfgs[ifname].update(self.DEFLOOP_NETCONF if self.IsLoopback( ifname) else self.DEFAULT_NETCONF) self.ifcfgs[ifname]['DEVICE'] = ifname self.routes[ifname] = env.cfgopen(_ROUTE_PATH + ifname, readonly=False, delempty=True)
def DelIfaceFiles(self, ifname): _ifcfg = env.cfgopen(_IFCFG_PATH + ifname, readonly=False, delempty=True, error_cb=self.error_cb) _ifcfg.clear() _ifcfg.sync() _route = env.cfgopen(_ROUTE_PATH + ifname, readonly=False, delempty=True, error_cb=self.error_cb) _route.clear() _route.sync()
def __init__(self, error_cb=None): self.error_cb = error_cb self.config = env.cfgopen(_NETWORKCFG, readonly=False, error_cb=self.error_cb) self.ifcfgs = dict() self.routes = dict()
def StoreNetworkConfiguration(netconf): if not netconf.config and not netconf.ifcfgs: raise RuntimeError('Attempting to store empty configuration') netconf.config.sync() ifiter = _getServiceIfConfigIter() or _getDefaultIfConfigIter() for fname in ifiter: short = fname[len(_IFCFG_PATH):] route = _ROUTE_PATH + short if short not in netconf.ifcfgs: env.cfgopen(fname, readonly=False, delempty=True).sync() env.cfgopen(route, readonly=False, delempty=True).sync() for short in netconf.ifcfgs: netconf.ifcfgs[short].sync() netconf.routes[short].sync()
def QueryNetworkConfiguration(error_cb=None): config = env.cfgopen(_NETWORKCFG, readonly=False, error_cb=error_cb) ifiter = set(_getServiceIfConfigIter() or _getDefaultIfConfigIter()) ifiter.update((_IFCFG_PATH + "lo", )) ifcfgs = OrderedDict() routes = OrderedDict() for fname in ifiter: short = fname[len(_IFCFG_PATH):] route = _ROUTE_PATH + short ifcfgs[short] = env.cfgopen(fname, readonly=False, delempty=True, error_cb=error_cb) routes[short] = env.cfgopen(route, readonly=False, delempty=True, error_cb=error_cb) return NetworkConfiguration(config, ifcfgs, routes)
ifiter = _getServiceIfConfigIter() or _getDefaultIfConfigIter() for fname in ifiter: short = fname[len(_IFCFG_PATH):] route = _ROUTE_PATH + short if short not in netconf.ifcfgs: env.cfgopen(fname, readonly=False, delempty=True).sync() env.cfgopen(route, readonly=False, delempty=True).sync() for short in netconf.ifcfgs: netconf.ifcfgs[short].sync() netconf.routes[short].sync() if __name__ == '__main__': from sys import stderr, argv def dump_to_stderr(file, lnum, emsg): print >> stderr, "File %s: Line %s: %s" % (file, lnum, emsg) if argv[1:]: for filename in argv[1:]: result = env.cfgopen(filename, error_cb=dump_to_stderr) print "%s: %s" % (filename, result) else: netconf = QueryNetworkConfiguration(error_cb=dump_to_stderr) print netconf.config print netconf.ifcfgs print netconf.routes