def find_resources_to_manage(resource_el): """ Get resources to manage to manage the specified resource succesfully etree resource_el -- resource element """ # If the resource_el is a primitive in a group, we set both the group and # the primitive to managed mode. Otherwise the resource_el, all its # children and parents need to be set to managed mode. We do it to make # sure to remove the unmanaged flag form the whole tree. The flag could be # put there manually. If we didn't do it, the resource may stay unmanaged, # as a managed primitive in an unmanaged clone / group is still unmanaged # and vice versa. # Bundle resources cannot be set as unmanaged - pcmk currently doesn't # support that. Resources in a bundle are supposed to be treated separately. if is_bundle(resource_el): return [] res_id = resource_el.attrib["id"] return ( [resource_el] # the resource itself + # its parents find_parent(resource_el, "resources").xpath( """ (./master|./clone)[(group|group/primitive|primitive)[@id='{r}']] | //group[primitive[@id='{r}']] """ .format(r=res_id) ) + # its children resource_el.xpath("(./group|./primitive|./group/primitive)") )
def expand_tag( some_or_tag_el: _Element, only_expand_types: Iterable[str] = None ) -> List[_Element]: """ Substitute a tag element with elements which the tag refers to. some_or_tag_el -- an already expanded element or a tag element to expand only_expand_types -- if specified, return only elements of these types """ if some_or_tag_el.tag != TAG_TAG: return [some_or_tag_el] conf_section = find_parent(some_or_tag_el, "configuration") if conf_section is None: return [] expanded_elements = [] for element_id in [ str(obj_ref.get("id", "")) for obj_ref in some_or_tag_el.iterfind(TAG_OBJREF) ]: if only_expand_types: searcher = ElementSearcher( only_expand_types, element_id, conf_section ) if searcher.element_found(): expanded_elements.append(searcher.get_element()) else: expanded_elements.extend( get_configuration_elements_by_id(conf_section, element_id) ) return expanded_elements
def is_resource_managed(cluster_state, resource_id): """ Check if the resource is managed etree cluster_state -- status of the cluster string resource_id -- id of the resource """ primitive_list = cluster_state.xpath(""" .//resource[{predicate_id}] | .//group[{predicate_id}]/resource """.format(predicate_id=_id_xpath_predicate(resource_id))) if primitive_list: for primitive in primitive_list: if is_false(primitive.attrib.get("managed", "")): return False clone = find_parent(primitive, ["clone"]) if clone is not None and is_false(clone.attrib.get("managed", "")): return False return True clone_list = cluster_state.xpath( """.//clone[@id="{0}"]""".format(resource_id)) for clone in clone_list: if is_false(clone.attrib.get("managed", "")): return False for primitive in clone.xpath(".//resource"): if is_false(primitive.attrib.get("managed", "")): return False return True raise ResourceNotFound(resource_id)
def find_valid_resource_id(report_processor: ReportProcessor, cib, in_clone_allowed, _id): parent_tags = resource.clone.ALL_TAGS + [resource.bundle.TAG] resource_element = find_element_by_tag_and_id( sorted(parent_tags + [resource.primitive.TAG, resource.group.TAG]), cib, _id, ) if resource_element.tag in parent_tags: return resource_element.attrib["id"] clone = find_parent(resource_element, parent_tags) if clone is None: return resource_element.attrib["id"] report_msg = reports.messages.ResourceForConstraintIsMultiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ) if in_clone_allowed: if report_processor.report(ReportItem.warning(report_msg)).has_errors: raise LibraryError() return resource_element.attrib["id"] raise LibraryError( ReportItem.error( report_msg, force_code=reports.codes.FORCE, ))
def find_resources_to_manage(resource_el): """ Get resources to manage to manage the specified resource succesfully etree resource_el -- resource element """ # If the resource_el is a primitive in a group, we set both the group and # the primitive to managed mode. Otherwise the resource_el, all its # children and parents need to be set to managed mode. We do it to make # sure to remove the unmanaged flag form the whole tree. The flag could be # put there manually. If we didn't do it, the resource may stay unmanaged, # as a managed primitive in an unmanaged clone / group is still unmanaged # and vice versa. res_id = resource_el.attrib["id"] return ( [resource_el] # the resource itself + # its parents find_parent(resource_el, "resources").xpath( # a master or a clone which contains a group, a primitve, or a # grouped primitive with the specified id # OR # a group (in a clone, master, etc. - hence //) which contains a # primitive with the specified id # OR # a bundle which contains a primitive with the specified id """ (./master|./clone)[(group|group/primitive|primitive)[@id='{r}']] | //group[primitive[@id='{r}']] | ./bundle[primitive[@id='{r}']] """.format(r=res_id)) + # its children resource_el.xpath("(./group|./primitive|./group/primitive)"))
def find_valid_resource_id(report_processor, cib, in_clone_allowed, id): parent_tags = resource.clone.ALL_TAGS + [resource.bundle.TAG] resource_element = find_element_by_tag_and_id( parent_tags + [resource.primitive.TAG, resource.group.TAG], cib, id, ) if resource_element.tag in parent_tags: return resource_element.attrib["id"] clone = find_parent(resource_element, parent_tags) if clone is None: return resource_element.attrib["id"] if in_clone_allowed: report_processor.process( reports.resource_for_constraint_is_multiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ReportItemSeverity.WARNING, )) return resource_element.attrib["id"] raise LibraryError( reports.resource_for_constraint_is_multiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ReportItemSeverity.ERROR, #repair to clone is workaround for web ui, so we put only information #about one forceable possibility forceable=report_codes.FORCE_CONSTRAINT_MULTIINSTANCE_RESOURCE))
def find_valid_resource_id(report_processor, cib, in_clone_allowed, _id): parent_tags = resource.clone.ALL_TAGS + [resource.bundle.TAG] resource_element = find_element_by_tag_and_id( parent_tags + [resource.primitive.TAG, resource.group.TAG], cib, _id, ) if resource_element.tag in parent_tags: return resource_element.attrib["id"] clone = find_parent(resource_element, parent_tags) if clone is None: return resource_element.attrib["id"] if in_clone_allowed: report_processor.process( reports.resource_for_constraint_is_multiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ReportItemSeverity.WARNING, ) ) return resource_element.attrib["id"] raise LibraryError(reports.resource_for_constraint_is_multiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ReportItemSeverity.ERROR, #repair to clone is workaround for web ui, so we put only information #about one forceable possibility forceable=report_codes.FORCE_CONSTRAINT_MULTIINSTANCE_RESOURCE ))
def find_valid_resource_id(report_processor: ReportProcessor, cib, in_clone_allowed, _id): parent_tags = resource.clone.ALL_TAGS + [resource.bundle.TAG] resource_element = find_element_by_tag_and_id( sorted(parent_tags + [resource.primitive.TAG, resource.group.TAG]), cib, _id, ) if resource_element.tag in parent_tags: return resource_element.attrib["id"] clone = find_parent(resource_element, parent_tags) if clone is None: return resource_element.attrib["id"] report_msg = reports.messages.ResourceForConstraintIsMultiinstance( resource_element.attrib["id"], "clone" if clone.tag == "master" else clone.tag, clone.attrib["id"], ) if in_clone_allowed: if report_processor.report(ReportItem.warning(report_msg)).has_errors: raise LibraryError() return resource_element.attrib["id"] raise LibraryError( ReportItem.error( report_msg, # repair to clone is workaround for web ui, so we put only # information about one forceable possibility force_code=reports.codes.FORCE_CONSTRAINT_MULTIINSTANCE_RESOURCE, ))
def remove_obj_ref(obj_ref_list: Iterable[_Element]) -> None: """ Remove specified obj_ref elements and also their parents if they remain empty after obj_ref removal. obj_ref_list -- list of obj_ref elements """ tag_elements = {find_parent(obj_ref, [TAG_TAG]) for obj_ref in obj_ref_list} for obj_ref in obj_ref_list: remove_one_element(obj_ref) for tag in tag_elements: if len(tag.findall(TAG_OBJREF)) == 0: remove_one_element(tag)
def is_resource_managed(cluster_state, resource_id): """ Check if the resource is managed etree cluster_state -- status of the cluster string resource_id -- id of the resource """ primitive_list = cluster_state.xpath( """ .//resource[{predicate_id}] | .//group[{predicate_id}]/resource """.format(predicate_id=_id_xpath_predicate), id=resource_id, ) if primitive_list: for primitive in primitive_list: if is_false(primitive.attrib.get("managed", "")): return False parent = find_parent(primitive, ["clone", "bundle"]) if parent is not None and is_false(parent.attrib.get( "managed", "")): return False return True parent_list = cluster_state.xpath( """ .//clone[@id=$resource_id] | .//bundle[@id=$resource_id] """, resource_id=resource_id, ) for parent in parent_list: if is_false(parent.attrib.get("managed", "")): return False for primitive in parent.xpath(".//resource"): if is_false(primitive.attrib.get("managed", "")): return False return True raise ResourceNotFound(resource_id)
def is_resource_managed(cluster_state, resource_id): """ Check if the resource is managed etree cluster_state -- status of the cluster string resource_id -- id of the resource """ primitive_list = cluster_state.xpath(""" .//resource[{predicate_id}] | .//group[{predicate_id}]/resource """.format(predicate_id=_id_xpath_predicate(resource_id)) ) if primitive_list: for primitive in primitive_list: if is_false(primitive.attrib.get("managed", "")): return False parent = find_parent(primitive, ["clone", "bundle"]) if ( parent is not None and is_false(parent.attrib.get("managed", "")) ): return False return True parent_list = cluster_state.xpath(""" .//clone[@id="{0}"] | .//bundle[@id="{0}"] """.format(resource_id) ) for parent in parent_list: if is_false(parent.attrib.get("managed", "")): return False for primitive in parent.xpath(".//resource"): if is_false(primitive.attrib.get("managed", "")): return False return True raise ResourceNotFound(resource_id)