def parse(self, node): if_id = self._get_attribute(node, "id") if not if_id in self._machine["interfaces"]: self._machine["interfaces"][if_id] = {} self._iface = iface = self._machine["interfaces"][if_id] iface["type"] = node.tagName scheme = {"addresses": self._addresses} if iface["type"] in ["bond", "bridge", "vlan", "macvlan", "team"]: scheme["slaves"] = self._slaves scheme["options"] = self._options if self._has_attribute(node, "network"): msg = "Attribute network is not supported by type '%s' " + \ "interfaces" % iface["type"] raise XmlProcessingError(msg) elif iface["type"] == "eth": iface["network"] = self._get_attribute(node, "network") scheme["params"] = self._ignore_tag self._process_child_nodes(node, scheme) try: event_params = {"machine_id": self._machine_id, "if_id": if_id} self._trigger_event("interface_config_ready", event_params) except Exception as exc: msg = "Unable to configure interface %s on machine %s [%s]." % \ (if_id, self._machine_id, str(exc)) logging.error(XmlProcessingError(str(msg), node)) raise
def _option(self, node, params): seq = self._seq_num cmd = self._cmd_num options = self._data["sequences"][seq]["commands"][cmd]["options"] name = self._get_attribute(node, "name") if not name in options: options[name] = [] option = {} options[name].append(option) if self._has_attribute(node, "type"): opt_type = self._get_attribute(node, "type") option["type"] = opt_type else: opt_type = "default" if opt_type == "default": if self._has_attribute(node, "value"): value = self._get_attribute(node, "value") else: value = self._get_text_content(node) option["value"] = value else: msg = "Unknown option type \"%s\"" % opt_type raise XmlProcessingError(msg, node)
def set_type(self, machine_type): if machine_type == "host": self._target = "machines" elif machine_type == "switch": self._target = "switches" else: raise XmlProcessingError("Unknown machine type")
def _interface(self, node, params): m_id = params["id"] machine = self._data["machines"][m_id] if_id = self._get_attribute(node, "id") if_type = node.tagName if if_id in machine["interfaces"]: msg = "Two interfaces with the same id '%s', " % if_id raise XmlProcessingError(msg) # Matching works with eth devices only if if_type != "eth": return if not if_id in machine["interfaces"]: machine["interfaces"][if_id] = {} iface = machine["interfaces"][if_id] iface["type"] = if_type iface["network"] = self._get_attribute(node, "network") if not "params" in machine["interfaces"][if_id]: machine["interfaces"][if_id]["params"] = {} target = machine["interfaces"][if_id]["params"] params = {"target": target} scheme = {"params": self._params} self._process_child_nodes(node, scheme, params, default_handler=self._ignore_tag)
def parse(self, node): if node.nodeType == node.DOCUMENT_NODE: scheme = {"lnstrecipe": self._lnstrecipe} self._process_child_nodes(node, scheme, default_handler=self._ignore_tag) else: raise XmlProcessingError("Passed object is not a XML document")
def parse(self, node): scheme = {"params": self._params, "interfaces": self._interfaces} params = {"target": self._machine["params"]} self._process_child_nodes(node, scheme, params) self._machine["params"]["skip_cleanup"] = False mandatory_params = ["hostname"] for mandatory in mandatory_params: if mandatory not in self._machine["params"]: msg = "Missing required parameter '%s'" % mandatory raise XmlProcessingError(msg, node)
def _check_sequence(self, sequence): err = False bg_ids = {} for i, command in enumerate(sequence["commands"]): machine_id = command["machine_id"] if not machine_id in bg_ids: bg_ids[machine_id] = set() cmd_type = command["type"] if cmd_type in ["wait", "intr", "kill"]: bg_id = command["value"] if bg_id in bg_ids[machine_id]: bg_ids[machine_id].remove(bg_id) else: logging.error( "Found command \"%s\" for bg_id \"%s\" on " "machine \"%s\" which was not previously " "defined", cmd_type, bg_id, machine_id) err = True if "bg_id" in command: bg_id = command["bg_id"] if not bg_id in bg_ids[machine_id]: bg_ids[machine_id].add(bg_id) else: logging.error( "Command \"%d\" uses bg_id \"%s\" on machine " "\"%s\" which is already used", i, bg_id, machine_id) err = True for machine_id in bg_ids: for bg_id in bg_ids[machine_id]: logging.error( "bg_id \"%s\" on machine \"%s\" has no kill/wait " "command to it", bg_id, machine_id) err = True if err: msg = "Incorrect command sequence" raise XmlProcessingError(msg, self._seq_node)
def _eth(self, node, params): machine = self._machine iface_id = self._get_attribute(node, "id") iface = machine["interfaces"][iface_id] = {} iface["network"] = self._get_attribute(node, "network") iface["params"] = {} iface["type"] = "eth" # parse interface parameters scheme = {"params": self._params} params = {"target": iface["params"]} self._process_child_nodes(node, scheme, params) if "hwaddr" in iface["params"]: iface["hwaddr"] = normalize_hwaddr(iface["params"]["hwaddr"]) else: msg = "Missing required parameter 'hwaddr'" raise XmlProcessingError(msg, node) if "name" in iface["params"]: iface["name"] = iface["params"]["name"]
def parse(self, node): recipe = self._data command = {} recipe["sequences"][self._seq_num]["commands"].append(command) self._cmd_num = len(recipe["sequences"][self._seq_num]["commands"]) - 1 if self._has_attribute(node, "machine_id"): machine_id = self._get_attribute(node, "machine_id") else: machine_id = None if machine_id and machine_id not in recipe["machines"]: raise XmlProcessingError("Invalid machine_id", node) command["machine_id"] = machine_id command["type"] = self._get_attribute(node, "type") if (command["type"] != "ctl_wait" and not machine_id) or\ (machine_id and machine_id not in recipe["machines"]): raise XmlProcessingError("Invalid machine_id", node) command["value"] = None if self._has_attribute(node, "value"): command["value"] = self._get_attribute(node, "value") if self._has_attribute(node, "pass_result"): command["pass_result"] = self._get_attribute( node, "pass_result", bool_it) if command["type"] == "system_config": msg = "Invalid attribute pass_result for command system_config" raise XmlProcessingError(msg, node) if self._has_attribute(node, "timeout"): command["timeout"] = self._get_attribute(node, "timeout", int) if self._has_attribute(node, "bg_id"): command["bg_id"] = self._get_attribute(node, "bg_id") if command["type"] in ["system_config", "wait", "intr", "kill"]: msg = "Command %s cannot be run in the background!" \ % command["type"] raise XmlProcessingError(msg, node) if self._has_attribute(node, "desc"): command["desc"] = self._get_attribute(node, "desc") if command["type"] == "system_config": if self._has_attribute(node, "option"): command["option"] = self._get_attribute(node, "option") if self._has_attribute(node, "persistent"): command["persistent"] = self._get_attribute( node, "persistent", bool_it) else: command["persistent"] = False elif command["type"] == "exec": if self._has_attribute(node, "from"): command["from"] = self._get_attribute(node, "from") elif command["type"] == "ctl_wait": if command["machine_id"] != None: msg = "Invalid attribute machine_id for command ctl_wait" raise XmlProcessingError(msg, node) try: command["value"] = int(command["value"]) except ValueError: msg = "Invalid value for command ctl_wait" raise XmlProcessingError(msg, node) for key in command.keys(): if key != "type" and key != "value" and\ key != "desc" and key != "machine_id": msg = "Invalid attribute %s for command ctl_wait" % key raise XmlProcessingError(msg, node) scheme = {"options": self._options} self._process_child_nodes(node, scheme)
def parse(self, xml_dom): if xml_dom.nodeType == xml_dom.DOCUMENT_NODE: scheme = {"lnstrecipe": self._lnstrecipe} self._process_child_nodes(xml_dom, scheme) else: raise XmlProcessingError("Passed object is not a XML document")