Beispiel #1
0
def set_deep_meta_attr(attr, value, rsc_id):
    '''
    If the referenced rsc is a primitive that belongs to a group,
    then set its attribute.
    Otherwise, go up to the topmost resource which contains this
    resource and set the attribute there (i.e. if the resource is
    cloned).
    If it's a group then check its children. If any of them has
    the attribute set to a value different from the one given,
    then ask the user whether to reset them or not (exact
    behaviour depends on the value of config.core.manage_children).
    '''
    target_node = xmlutil.RscState().rsc2node(rsc_id)
    if target_node is None:
        common_error("resource %s does not exist" % rsc_id)
        return False

    if target_node.tag == 'tag':
        return set_deep_meta_attr_tag(target_node, attr, value)
    if not (target_node.tag == "primitive" and
            target_node.getparent().tag == "group"):
        target_node = xmlutil.get_topmost_rsc(target_node)
    if not set_deep_meta_attr_node(target_node, attr, value):
        return False
    return xmlutil.commit_rsc(target_node)
Beispiel #2
0
 def check_node(self, node, lvl):
     node_id = node.get("id")
     if not node_id:
         return
     if self.id_in_use(node_id):
         common_error("id_store: id %s is in use" % node_id)
         self.ok = False
         return
Beispiel #3
0
def check_node(node, lvl):
    global ok
    node_id = node.get("id")
    if not node_id:
        return
    if id_in_use(node_id):
        common_error("id_store: id %s is in use" % node_id)
        ok = False
        return
Beispiel #4
0
def cibdump2elem(section=None):
    if section:
        cmd = "%s -o %s" % (cib_dump, section)
    else:
        cmd = cib_dump
    rc, outp, errp = sudocall(cmd)
    if rc == 0:
        return cibtext2elem(outp)
    elif rc != constants.cib_no_section_rc:
        common_error("running %s: %s" % (cmd, errp))
    return None
Beispiel #5
0
def cibdump2elem(section=None):
    if section:
        cmd = "%s -o %s" % (cib_dump, section)
    else:
        cmd = cib_dump
    rc, outp, errp = sudocall(cmd)
    if rc == 0:
        return cibtext2elem(outp)
    elif rc != constants.cib_no_section_rc:
        common_error("running %s: %s" % (cmd, errp))
    return None
Beispiel #6
0
def set_deep_meta_attr(rsc, attr, value, commit=True):
    """
    If the referenced rsc is a primitive that belongs to a group,
    then set its attribute.
    Otherwise, go up to the topmost resource which contains this
    resource and set the attribute there (i.e. if the resource is
    cloned).
    If it's a group then check its children. If any of them has
    the attribute set to a value different from the one given,
    then ask the user whether to reset them or not (exact
    behaviour depends on the value of config.core.manage_children).
    """

    def update_obj(obj):
        """
        set the meta attribute in the given object
        """
        node = obj.node
        obj.set_updated()
        if not (node.tag == "primitive" and
                node.getparent().tag == "group"):
            node = xmlutil.get_topmost_rsc(node)
        return set_deep_meta_attr_node(node, attr, value)

    def flatten(objs):
        for obj in objs:
            if isinstance(obj, list):
                for subobj in obj:
                    yield subobj
            else:
                yield obj

    def resolve(obj):
        if obj.obj_type == 'tag':
            return [cib_factory.find_object(o) for o in obj.node.xpath('./obj_ref/@id')]
        return obj

    objs = cib_factory.find_objects(rsc)
    while any(obj for obj in objs if obj.obj_type == 'tag'):
        objs = list(flatten(resolve(obj) for obj in objs))
    common_debug("set_deep_meta_attr: %s" % (', '.join([obj.obj_id for obj in objs])))
    if not objs:
        common_error("Resource not found: %s" % (rsc))
        return False

    ok = all(update_obj(obj) for obj in objs)
    if not ok:
        common_error("Failed to update meta attributes for %s" % (rsc))
        return False

    if not commit:
        return True

    ok = cib_factory.commit()
    if not ok:
        common_error("Failed to commit updates to %s" % (rsc))
        return False
    return True
Beispiel #7
0
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    try:
        (outp, err_outp) = p.communicate()
        p.wait()
        rc = p.returncode
    except IOError, msg:
        common_err("running %s: %s" % (cmd, msg))
        return None
    if rc == 0:
        return cibtext2elem(outp)
    elif rc == vars.cib_no_section_rc:
        return None
    else:
        common_error("running %s: %s" % (cmd, err_outp))
        return None


cib_piped = "cibadmin -p"


def commit_rsc(node):
    "Replace a resource definition using cibadmin -R"
    xml_processnodes(node, is_emptynvpairs, rmnodes)
    xml_processnodes(node, is_emptyops, rmnodes)
    rc = pipe_string("%s -R -o %s" % (cib_piped, "resources"),
                     etree.tostring(node))
    return rc == 0

Beispiel #8
0
        print ".EXT", cmd
    p = subprocess.Popen(
        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    try:
        (outp, err_outp) = p.communicate()
        p.wait()
        rc = p.returncode
    except IOError, msg:
        common_err("running %s: %s" % (cmd, msg))
        return None
    if rc == 0:
        return cibtext2elem(outp)
    elif rc == constants.cib_no_section_rc:
        return None
    else:
        common_error("running %s: %s" % (cmd, err_outp))
        return None


def read_cib(fun, params=None):
    cib_elem = fun(params)
    if cib_elem is None or cib_elem.tag != "cib":
        return None
    return cib_elem


def sanity_check_nvpairs(id, node, attr_list):
    rc = 0
    for nvpair in node.iterchildren("nvpair"):
        n = nvpair.get("name")
        if n and not n in attr_list:
Beispiel #9
0
def set_deep_meta_attr(rsc, attr, value, commit=True):
    """
    If the referenced rsc is a primitive that belongs to a group,
    then set its attribute.
    Otherwise, go up to the topmost resource which contains this
    resource and set the attribute there (i.e. if the resource is
    cloned).
    If it's a group then check its children. If any of them has
    the attribute set to a value different from the one given,
    then ask the user whether to reset them or not (exact
    behaviour depends on the value of config.core.manage_children).
    """

    def update_obj(obj):
        """
        set the meta attribute in the given object
        """
        node = obj.node
        obj.set_updated()
        if not (node.tag == "primitive" and
                node.getparent().tag == "group"):
            node = xmlutil.get_topmost_rsc(node)
        return set_deep_meta_attr_node(node, attr, value)

    def flatten(objs):
        for obj in objs:
            if isinstance(obj, list):
                for subobj in obj:
                    yield subobj
            else:
                yield obj

    def resolve(obj):
        if obj.obj_type == 'tag':
            ret = [cib_factory.find_object(o) for o in obj.node.xpath('./obj_ref/@id')]
            ret = [r for r in ret if r is not None]
            return ret
        return obj

    def is_resource(obj):
        return xmlutil.is_resource(obj.node)

    objs = cib_factory.find_objects(rsc)
    if objs is None:
        common_error("CIB is not valid!")
        return False
    while any(obj for obj in objs if obj.obj_type == 'tag'):
        objs = list(flatten(resolve(obj) for obj in objs))
    objs = filter(is_resource, objs)
    common_debug("set_deep_meta_attr: %s" % (', '.join([obj.obj_id for obj in objs])))
    if not objs:
        common_error("Resource not found: %s" % (rsc))
        return False

    ok = all(update_obj(obj) for obj in objs)
    if not ok:
        common_error("Failed to update meta attributes for %s" % (rsc))
        return False

    if not commit:
        return True

    ok = cib_factory.commit()
    if not ok:
        common_error("Failed to commit updates to %s" % (rsc))
        return False
    return True