Beispiel #1
0
def append_new(
    resources_section: _Element,
    id_provider: IdProvider,
    primitive_element: _Element,
    options: Mapping[str, str],
    clone_id: Optional[str] = None,
):
    """
    Append a new clone element (containing the primitive_element) to the
    resources_section.

    etree.Element resources_section is place where new clone will be appended.
    IdProvider id_provider -- elements' ids generator
    etree.Element primitive_element is resource which will be cloned.
    dict options is source for clone meta options
    """
    clone_element = etree.SubElement(
        resources_section,
        TAG_CLONE,
        id=id_provider.allocate_id("{0}-{1}".format(
            str(primitive_element.get("id")), TAG_CLONE))
        if clone_id is None else clone_id,
    )
    clone_element.append(primitive_element)

    if options:
        nvpair.append_new_meta_attributes(clone_element, options, id_provider)

    return clone_element
Beispiel #2
0
def append_new(
    parent_element, id_provider, bundle_id, container_type, container_options,
    network_options, port_map, storage_map, meta_attributes
):
    # pylint: disable=too-many-arguments
    """
    Create new bundle and add it to the CIB

    etree parent_element -- the bundle will be appended to this element
    IdProvider id_provider -- elements' ids generator
    string bundle_id -- id of the bundle
    string container_type -- bundle container type
    dict container_options -- container options
    dict network_options -- network options
    list of dict port_map -- list of port mapping options
    list of dict storage_map -- list of storage mapping options
    dict meta_attributes -- meta attributes
    """
    bundle_element = etree.SubElement(parent_element, TAG, {"id": bundle_id})
    _append_container(bundle_element, container_type, container_options)
    if network_options or port_map:
        _append_network(
            bundle_element,
            id_provider,
            bundle_id,
            network_options,
            port_map,
        )
    if storage_map:
        _append_storage(bundle_element, id_provider, bundle_id, storage_map)
    if meta_attributes:
        append_new_meta_attributes(bundle_element, meta_attributes, id_provider)
    return bundle_element
Beispiel #3
0
def append_new(
    resources_section, id_provider, resource_id, standard, provider, agent_type,
    instance_attributes=None,
    meta_attributes=None,
    operation_list=None
):
    # pylint:disable=too-many-arguments
    """
    Append a new primitive element to the resources_section.

    etree.Element resources_section is place where new element will be appended
    IdProvider id_provider -- elements' ids generator
    string resource_id is id of new resource
    string standard is a standard of resource agent (e.g. ocf)
    string agent_type is a type of resource agent (e.g. IPaddr2)
    string provider is a provider of resource agent (e.g. heartbeat)
    dict instance_attributes will be nvpairs inside instance_attributes element
    dict meta_attributes will be nvpairs inside meta_attributes element
    list operation_list contains dicts representing operations
        (e.g. [{"name": "monitor"}, {"name": "start"}])
    """
    attributes = {
        "id": resource_id,
        "class": standard,
        "type": agent_type,
    }
    if provider:
        attributes["provider"] = provider
    primitive_element = etree.SubElement(resources_section, TAG, attributes)

    if instance_attributes:
        append_new_instance_attributes(
            primitive_element,
            instance_attributes,
            id_provider
        )

    if meta_attributes:
        append_new_meta_attributes(
            primitive_element,
            meta_attributes,
            id_provider
        )

    create_operations(
        primitive_element,
        id_provider,
        operation_list if operation_list else []
    )

    return primitive_element
Beispiel #4
0
def append_new(
    resources_section, resource_id, standard, provider, agent_type,
    instance_attributes=None,
    meta_attributes=None,
    operation_list=None
):
    """
    Append a new primitive element to the resources_section.

    etree.Element resources_section is place where new element will be appended
    string resource_id is id of new resource
    string standard is a standard of resource agent (e.g. ocf)
    string agent_type is a type of resource agent (e.g. IPaddr2)
    string provider is a provider of resource agent (e.g. heartbeat)
    dict instance_attributes will be nvpairs inside instance_attributes element
    dict meta_attributes will be nvpairs inside meta_attributes element
    list operation_list contains dicts representing operations
        (e.g. [{"name": "monitor"}, {"name": "start"}])
    """
    attributes = {
        "id": resource_id,
        "class": standard,
        "type": agent_type,
    }
    if provider:
        attributes["provider"] = provider
    primitive_element = etree.SubElement(
        resources_section,
        "primitive",
        attributes
    )

    if instance_attributes:
        append_new_instance_attributes(
            primitive_element,
            instance_attributes
        )

    if meta_attributes:
        append_new_meta_attributes(primitive_element, meta_attributes)

    create_operations(
        primitive_element,
        operation_list if operation_list else []
    )

    return primitive_element
Beispiel #5
0
def append_new(
    parent_element, id_provider, bundle_id, container_type, container_options,
    network_options, port_map, storage_map, meta_attributes
):
    """
    Create new bundle and add it to the CIB

    etree parent_element -- the bundle will be appended to this element
    IdProvider id_provider -- elements' ids generator
    string bundle_id -- id of the bundle
    string container_type -- bundle container type
    dict container_options -- container options
    dict network_options -- network options
    list of dict port_map -- list of port mapping options
    list of dict storage_map -- list of storage mapping options
    dict meta_attributes -- meta attributes
    """
    bundle_element = etree.SubElement(parent_element, TAG, {"id": bundle_id})
    # TODO create the proper element once more container_types are supported
    # by pacemaker
    docker_element = etree.SubElement(bundle_element, "docker")
    # Do not add options with empty values. When updating, an empty value means
    # remove the option.
    update_attributes_remove_empty(docker_element, container_options)
    if network_options or port_map:
        network_element = etree.SubElement(bundle_element, "network")
        # Do not add options with empty values. When updating, an empty value
        # means remove the option.
        update_attributes_remove_empty(network_element, network_options)
    for port_map_options in port_map:
        _append_port_map(
            network_element, id_provider, bundle_id, port_map_options
        )
    if storage_map:
        storage_element = etree.SubElement(bundle_element, "storage")
    for storage_map_options in storage_map:
        _append_storage_map(
            storage_element, id_provider, bundle_id, storage_map_options
        )
    if meta_attributes:
        append_new_meta_attributes(bundle_element, meta_attributes, id_provider)
    return bundle_element
Beispiel #6
0
def append_new(resources_section, primitive_element, options):
    """
    Append a new clone element (containing the primitive_element) to the
    resources_section.

    etree.Element resources_section is place where new clone will be appended.
    etree.Element primitive_element is resource which will be cloned.
    dict options is source for clone meta options
    """
    clone_element = etree.SubElement(
        resources_section,
        TAG_CLONE,
        id=create_id(primitive_element),
    )
    clone_element.append(primitive_element)

    if options:
        append_new_meta_attributes(clone_element, options)

    return clone_element
Beispiel #7
0
def append_new(clone_tag, resources_section, primitive_element, options):
    """
    Append a new clone element (containing the primitive_element) to the
    resources_section.

    string clone_tag is tag of clone element. Expected values are "clone" and
        "master".
    etree.Element resources_section is place where new clone will be appended.
    etree.Element primitive_element is resource which will be cloned.
    dict options is source for clone meta options
    """
    clone_element = etree.SubElement(
        resources_section,
        clone_tag,
        id=create_id(clone_tag, primitive_element),
    )
    clone_element.append(primitive_element)

    if options:
        append_new_meta_attributes(clone_element, options)

    return clone_element
Beispiel #8
0
def append_new(clone_tag, resources_section, primitive_element, options):
    """
    Append a new clone element (containing the primitive_element) to the
    resources_section.

    string clone_tag is tag of clone element. Expected values are "clone" and
        "master".
    etree.Element resources_section is place where new clone will be appended.
    etree.Element primitive_element is resource which will be cloned.
    dict options is source for clone meta options
    """
    clone_element = etree.SubElement(
        resources_section,
        clone_tag,
        id=create_id(clone_tag, primitive_element),
    )
    clone_element.append(primitive_element)

    if options:
        append_new_meta_attributes(clone_element, options)

    return clone_element
Beispiel #9
0
def append_new(resources_section, id_provider, primitive_element, options):
    """
    Append a new clone element (containing the primitive_element) to the
    resources_section.

    etree.Element resources_section is place where new clone will be appended.
    IdProvider id_provider -- elements' ids generator
    etree.Element primitive_element is resource which will be cloned.
    dict options is source for clone meta options
    """
    clone_element = etree.SubElement(
        resources_section,
        TAG_CLONE,
        id=id_provider.allocate_id(
            "{0}-{1}".format(primitive_element.get("id"), TAG_CLONE)
        )
    )
    clone_element.append(primitive_element)

    if options:
        nvpair.append_new_meta_attributes(clone_element, options, id_provider)

    return clone_element
Beispiel #10
0
def reset(
    bundle_element, id_provider, bundle_id, container_type, container_options,
    network_options, port_map, storage_map, meta_attributes
):
    """
    Remove configuration of bundle_element and create new one.

    etree bundle_element -- the bundle element that will be reset
    IdProvider id_provider -- elements' ids generator
    string bundle_id -- id of the bundle
    string container_type -- bundle container type
    dict container_options -- container options
    dict network_options -- network options
    list of dict port_map -- list of port mapping options
    list of dict storage_map -- list of storage mapping options
    dict meta_attributes -- meta attributes
    """
    # pylint: disable=too-many-arguments

    # Old bundle configuration is removed and re-created. We aren't trying
    # to keep ids:
    # * It doesn't make sense to reference these ids.
    # * Newly created ids are based on (are prefixed by) the bundle element id,
    #   which does not change. Therefore, it is VERY HIGHLY probable the newly
    #   created ids will be the same as the original ones.
    elements_without_reset_impact = []

    # Elements network, storage and meta_attributes must be kept even if they
    # are without children.
    # See https://bugzilla.redhat.com/show_bug.cgi?id=1642514
    #
    # The only scenario that makes sense is that these elements are empty
    # and no attributes or children are requested for them. So we collect only
    # deleted tags and we will ensure creation minimal relevant elements at
    # least.
    indelible_tags = []
    for child in list(bundle_element):
        if child.tag in ["network", "storage", META_ATTRIBUTES_TAG]:
            indelible_tags.append(child.tag)
        elif child.tag not in list(GENERIC_CONTAINER_TYPES):
            # Only primitive should be found here, currently.
            # The order of various element tags has no practical impact so we
            # don't care about it here.
            elements_without_reset_impact.append(child)
        bundle_element.remove(child)

    _append_container(bundle_element, container_type, container_options)
    if network_options or port_map or "network" in indelible_tags:
        _append_network(
            bundle_element,
            id_provider,
            bundle_id,
            network_options,
            port_map,
        )
    if storage_map or "storage" in indelible_tags:
        _append_storage(bundle_element, id_provider, bundle_id, storage_map)
    if meta_attributes or META_ATTRIBUTES_TAG in indelible_tags:
        append_new_meta_attributes(
            bundle_element,
            meta_attributes,
            id_provider,
            enforce_append=True,
        )
    for element in elements_without_reset_impact:
        bundle_element.append(element)