Beispiel #1
0
def simulate_cib(runner, cib):
    """
    Run crm_simulate to get effects the cib would have on the live cluster

    CommandRunner runner -- runner
    etree cib -- cib tree to simulate
    """
    cib_xml = etree_to_str(cib)
    try:
        plaintext_result, transitions_xml, new_cib_xml = simulate_cib_xml(
            runner, cib_xml)
        return (plaintext_result.strip(), xml_fromstring(transitions_xml),
                xml_fromstring(new_cib_xml))
    except (etree.XMLSyntaxError, etree.DocumentInvalid) as e:
        raise LibraryError(reports.cib_simulate_error(str(e)))
Beispiel #2
0
def _get_api_result_dom(xml: str) -> _Element:
    # raises etree.XMLSyntaxError and etree.DocumentInvalid
    rng = settings.pacemaker_api_result_schema
    dom = xml_fromstring(xml)
    if os.path.isfile(rng):
        etree.RelaxNG(file=rng).assertValid(dom)
    return dom
Beispiel #3
0
def get_cluster_state_dom(xml):
    try:
        dom = xml_fromstring(xml)
        if os.path.isfile(settings.crm_mon_schema):
            etree.RelaxNG(file=settings.crm_mon_schema).assertValid(dom)
        return dom
    except (etree.XMLSyntaxError, etree.DocumentInvalid):
        raise LibraryError(reports.cluster_state_invalid_format())
Beispiel #4
0
def get_cluster_state_dom(xml):
    try:
        dom = xml_fromstring(xml)
        if os.path.isfile(settings.crm_mon_schema):
            etree.RelaxNG(file=settings.crm_mon_schema).assertValid(dom)
        return dom
    except (etree.XMLSyntaxError, etree.DocumentInvalid):
        raise LibraryError(reports.cluster_state_invalid_format())
Beispiel #5
0
def get_cluster_state_dom(xml):
    try:
        dom = xml_fromstring(xml)
        _validate_cluster_state_dom(dom)
        return dom
    except (etree.XMLSyntaxError, etree.DocumentInvalid):
        raise LibraryError(
            ReportItem.error(reports.messages.BadClusterStateFormat())
        )
Beispiel #6
0
    def from_string(cls, config_string):
        """
        Parse cluster.conf config and create a facade around it

        config_string -- cluster.conf file content as string
        """
        try:
            return cls(xml_fromstring(config_string))
        except (etree.XMLSyntaxError, etree.DocumentInvalid) as e:
            raise LibraryError(reports.cluster_conf_invalid_format(str(e)))
Beispiel #7
0
 def _parse_metadata(self, metadata):
     try:
         dom = xml_fromstring(metadata)
         # TODO Majority of agents don't provide valid metadata, so we skip
         # the validation for now. We want to enable it once the schema
         # and/or agents are fixed.
         # When enabling this check for overrides in child classes.
         #if os.path.isfile(settings.agent_metadata_schema):
         #    etree.DTD(file=settings.agent_metadata_schema).assertValid(dom)
         return dom
     except (etree.XMLSyntaxError, etree.DocumentInvalid) as e:
         raise UnableToGetAgentMetadata(self.get_name(), str(e))
Beispiel #8
0
 def _parse_metadata(self, metadata):
     try:
         dom = xml_fromstring(metadata)
         # TODO Majority of agents don't provide valid metadata, so we skip
         # the validation for now. We want to enable it once the schema
         # and/or agents are fixed.
         # When enabling this check for overrides in child classes.
         #if os.path.isfile(settings.agent_metadata_schema):
         #    etree.DTD(file=settings.agent_metadata_schema).assertValid(dom)
         return dom
     except (etree.XMLSyntaxError, etree.DocumentInvalid) as e:
         raise UnableToGetAgentMetadata(self.get_name(), str(e))
Beispiel #9
0
def _metadata_xml_to_dom(metadata: str) -> _Element:
    """
    Parse metadata string to XML document and validate against RNG schema

    metadata -- metadata string to parse
    """
    dom = xml_fromstring(metadata)
    ocf_version = _get_ocf_version(dom)
    if ocf_version == const.OCF_1_0:
        etree.RelaxNG(file=settings.path.ocf_1_0_schema).assertValid(dom)
    elif ocf_version == const.OCF_1_1:
        etree.RelaxNG(file=settings.path.ocf_1_1_schema).assertValid(dom)
    return dom
Beispiel #10
0
def is_fence_history_supported():
    try:
        crm_mon_rng = xml_fromstring(open(settings.crm_mon_schema, "r").read())
        # Namespaces must be provided otherwise xpath won't match anything.
        # 'None' namespace is not supported, so we rename it.
        namespaces_map = {"ns": crm_mon_rng.nsmap.pop(None)}
        history_elements = crm_mon_rng.xpath(
            ".//ns:element[@name='fence_history']", namespaces=namespaces_map)
        if history_elements:
            return True
    except (EnvironmentError, etree.XMLSyntaxError):
        # if we cannot tell for sure fence_history is supported, we will
        # continue as if it was not supported
        pass
    return False
Beispiel #11
0
def is_fence_history_supported():
    try:
        crm_mon_rng = xml_fromstring(open(settings.crm_mon_schema, "r").read())
        # Namespaces must be provided otherwise xpath won't match anything.
        # 'None' namespace is not supported, so we rename it.
        namespaces_map = {
            "ns": crm_mon_rng.nsmap.pop(None)
        }
        history_elements = crm_mon_rng.xpath(
            ".//ns:element[@name='fence_history']",
            namespaces=namespaces_map
        )
        if history_elements:
            return True
    except (EnvironmentError, etree.XMLSyntaxError):
        # if we cannot tell for sure fence_history is supported, we will
        # continue as if it was not supported
        pass
    return False
Beispiel #12
0
def get_capabilities_definition():
    """
    Read and parse capabilities file

    The point is to return all data in python structures for further processing.
    """
    filename = os.path.join(settings.pcsd_exec_location, "capabilities.xml")
    try:
        with open(filename, mode="r") as file_:
            capabilities_xml = xml_fromstring(file_.read())
    except (EnvironmentError, etree.XMLSyntaxError, etree.DocumentInvalid) as e:
        raise error(
            "Cannot read capabilities definition file '{0}': '{1}'"
            .format(filename, str(e))
        )
    capabilities = []
    for feat_xml in capabilities_xml.findall(".//capability"):
        feat = dict(feat_xml.attrib)
        desc = feat_xml.find("./description")
        # dedent and strip remove indentation in the XML file
        feat["description"] = "" if desc is None else dedent(desc.text).strip()
        capabilities.append(feat)
    return capabilities
Beispiel #13
0
def get_capabilities_definition():
    """
    Read and parse capabilities file

    The point is to return all data in python structures for further processing.
    """
    filename = os.path.join(settings.pcsd_exec_location, "capabilities.xml")
    try:
        with open(filename, mode="r") as file_:
            capabilities_xml = xml_fromstring(file_.read())
    except (EnvironmentError, etree.XMLSyntaxError,
            etree.DocumentInvalid) as e:
        raise error(
            "Cannot read capabilities definition file '{0}': '{1}'".format(
                filename, str(e))) from e
    capabilities = []
    for feat_xml in capabilities_xml.findall(".//capability"):
        feat = dict(feat_xml.attrib)
        desc = feat_xml.find("./description")
        # dedent and strip remove indentation in the XML file
        feat["description"] = "" if desc is None else dedent(desc.text).strip()
        capabilities.append(feat)
    return capabilities
Beispiel #14
0
def parse_cib_xml(xml):
    return xml_fromstring(xml)
Beispiel #15
0
def parse_cib_xml(xml):
    return xml_fromstring(xml)
Beispiel #16
0
 def test_large_xml(self):
     # pylint: disable=no-self-use
     #it raises on a huge xml without the flag huge_tree=True
     #see https://bugzilla.redhat.com/show_bug.cgi?id=1506864
     xml_fromstring(large_xml)
 def test_large_xml(self):
     # pylint: disable=no-self-use
     #it raises on a huge xml without the flag huge_tree=True
     #see https://bugzilla.redhat.com/show_bug.cgi?id=1506864
     xml_fromstring(large_xml)