def parseinterface(self, n, ifc): """ Parse a interface block such as: <interface name="eth0" net="37278"> <address type="mac">00:00:00:aa:00:01</address> <address>10.0.0.2/24</address> <address>2001::2/64</address> </interface> """ name = str(ifc.getAttribute("name")) netid = str(ifc.getAttribute("net")) hwaddr = None addrlist = [] try: net = n.session.get_object_by_name(netid) except KeyError: logger.warn("skipping node %s interface %s: unknown net %s", n.name, name, netid) return for addr in ifc.getElementsByTagName("address"): addrstr = xmlutils.get_text_child(addr) if addrstr is None: continue if addr.getAttribute("type") == "mac": hwaddr = addrstr else: addrlist.append(addrstr) i = n.newnetif(net, addrlist=addrlist, hwaddr=hwaddr, ifindex=None, ifname=name) for model in ifc.getElementsByTagName("model"): self.parsemodel(model, n, n.objid) key = (n.name, name) if key in self.linkparams: netif = n.netif(i) for k, v in self.linkparams[key]: netif.setparam(k, v)
def parsemeta(self): opt = xmlutils.get_one_element(self.meta, "SessionOptions") if opt: for param in opt.getElementsByTagName("param"): k = str(param.getAttribute("name")) v = str(param.getAttribute("value")) if v == '': v = xmlutils.get_text_child(param) # allow attribute/text for newlines self.session.options.set_config(k, v) hooks = xmlutils.get_one_element(self.meta, "Hooks") if hooks: self.parsehooks(hooks) meta = xmlutils.get_one_element(self.meta, "MetaData") if meta: for param in meta.getElementsByTagName("param"): k = str(param.getAttribute("name")) v = str(param.getAttribute("value")) if v == '': v = xmlutils.get_text_child(param) self.session.metadata.set_config(k, v)
def parsehooks(self, hooks): ''' Parse hook scripts from XML into session._hooks. ''' for hook in hooks.getElementsByTagName("Hook"): filename = hook.getAttribute("name") state = hook.getAttribute("state") data = xmlutils.get_text_child(hook) if data is None: data = "" # allow for empty file type = "hook:%s" % state self.session.set_hook(type, file_name=filename, source_name=None, data=data)
def parsemeta(self): opt = xmlutils.get_one_element(self.meta, "SessionOptions") if opt: for param in opt.getElementsByTagName("param"): k = str(param.getAttribute("name")) v = str(param.getAttribute("value")) if v == '': v = xmlutils.get_text_child( param) # allow attribute/text for newlines setattr(self.session.options, k, v) hooks = xmlutils.get_one_element(self.meta, "Hooks") if hooks: self.parsehooks(hooks) meta = xmlutils.get_one_element(self.meta, "MetaData") if meta: for param in meta.getElementsByTagName("param"): k = str(param.getAttribute("name")) v = str(param.getAttribute("value")) if v == '': v = xmlutils.get_text_child(param) self.session.metadata.add_item(k, v)
def parseservice(self, service, n): """ Use session.services manager to store service customizations before they are added to a node. """ name = service.getAttribute("name") svc = ServiceManager.get(name) if svc is None: return False values = [] startup_idx = service.getAttribute("startup_idx") if startup_idx is not None: values.append("startidx=%s" % startup_idx) startup_time = service.getAttribute("start_time") if startup_time is not None: values.append("starttime=%s" % startup_time) dirs = [] for dir in service.getElementsByTagName("Directory"): dirname = dir.getAttribute("name") dirs.append(dirname) if len(dirs): values.append("dirs=%s" % dirs) startup = [] shutdown = [] validate = [] for cmd in service.getElementsByTagName("Command"): type = cmd.getAttribute("type") cmdstr = xmlutils.get_text_child(cmd) if cmdstr is None: continue if type == "start": startup.append(cmdstr) elif type == "stop": shutdown.append(cmdstr) elif type == "validate": validate.append(cmdstr) if len(startup): values.append("cmdup=%s" % startup) if len(shutdown): values.append("cmddown=%s" % shutdown) if len(validate): values.append("cmdval=%s" % validate) files = [] for file in service.getElementsByTagName("File"): filename = file.getAttribute("name") files.append(filename) data = xmlutils.get_text_child(file) self.session.services.set_service_file(node_id=n.objid, service_name=name, file_name=filename, data=data) if len(files): values.append("files=%s" % files) if not bool(service.getAttribute("custom")): return True self.session.services.set_service(n.objid, svc) # set custom values for custom service svc = self.session.services.get_service(n.objid, None) if not svc: raise ValueError("custom service(%s) for node(%s) does not exist", svc.name, n.objid) values = ConfigShim.str_to_dict("|".join(values)) for name, value in values.iteritems(): ServiceShim.setvalue(svc, name, value) return True
def parseservice(self, service, n): """ Use session.services manager to store service customizations before they are added to a node. """ name = service.getAttribute("name") svc = ServiceManager.get(name) if svc is None: return False values = [] startup_idx = service.getAttribute("startup_idx") if startup_idx is not None: values.append("startidx=%s" % startup_idx) startup_time = service.getAttribute("start_time") if startup_time is not None: values.append("starttime=%s" % startup_time) dirs = [] for dir in service.getElementsByTagName("Directory"): dirname = dir.getAttribute("name") dirs.append(dirname) if len(dirs): values.append("dirs=%s" % dirs) startup = [] shutdown = [] validate = [] for cmd in service.getElementsByTagName("Command"): type = cmd.getAttribute("type") cmdstr = xmlutils.get_text_child(cmd) if cmdstr is None: continue if type == "start": startup.append(cmdstr) elif type == "stop": shutdown.append(cmdstr) elif type == "validate": validate.append(cmdstr) if len(startup): values.append("cmdup=%s" % startup) if len(shutdown): values.append("cmddown=%s" % shutdown) if len(validate): values.append("cmdval=%s" % validate) files = [] for file in service.getElementsByTagName("File"): filename = file.getAttribute("name") files.append(filename) data = xmlutils.get_text_child(file) typestr = "service:%s:%s" % (name, filename) self.session.services.setservicefile(nodenum=n.objid, type=typestr, filename=filename, srcname=None, data=data) if len(files): values.append("files=%s" % files) if not bool(service.getAttribute("custom")): return True self.session.services.setcustomservice(n.objid, svc, values) return True