Example #1
0
def constraint_rule(argv):
    if len(argv) < 2:
        usage.constraint("rule")
        sys.exit(1)

    found = False
    command = argv.pop(0)


    constraint_id = None
    rule_id = None

    if command == "add":
        constraint_id = argv.pop(0)
        cib = utils.get_cib_dom()
        constraint = utils.dom_get_element_with_id(
            cib.getElementsByTagName("constraints")[0],
            "rsc_location",
            constraint_id
        )
        if not constraint:
            utils.err("Unable to find constraint: " + constraint_id)
        options, rule_argv = rule_utils.parse_argv(argv)
        rule_utils.dom_rule_add(constraint, options, rule_argv)
        location_rule_check_duplicates(cib, constraint)
        utils.replace_cib_configuration(cib)

    elif command in ["remove","delete"]:
        cib = utils.get_cib_etree()
        temp_id = argv.pop(0)
        constraints = cib.find('.//constraints')
        loc_cons = cib.findall(str('.//rsc_location'))

        rules = cib.findall(str('.//rule'))
        for loc_con in loc_cons:
            for rule in loc_con:
                if rule.get("id") == temp_id:
                    if len(loc_con) > 1:
                        print("Removing Rule: {0}".format(rule.get("id")))
                        loc_con.remove(rule)
                        found = True
                        break
                    else:
                        print(
                            "Removing Constraint: {0}".format(loc_con.get("id"))
                        )
                        constraints.remove(loc_con)
                        found = True
                        break

            if found == True:
                break

        if found:
            utils.replace_cib_configuration(cib)
        else:
            utils.err("unable to find rule with id: %s" % temp_id)
    else:
        usage.constraint("rule")
        sys.exit(1)
Example #2
0
def colocation_set(argv):
    setoptions = []
    for i in range(len(argv)):
        if argv[i] == "setoptions":
            setoptions = argv[i+1:]
            argv[i:] = []
            break

    current_set = set_args_into_array(argv)
    colocation_id = "pcs_rsc_colocation"
    for a in argv:
        if a.find('=') == -1:
            colocation_id = colocation_id + "_" + a

    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    if constraints == None:
        constraints = ET.SubElement(cib, "constraints")
    rsc_colocation = ET.SubElement(constraints,"rsc_colocation")
    rsc_colocation.set("id", utils.find_unique_id(cib,colocation_id))
    rsc_colocation.set("score","INFINITY")
    for opt in setoptions:
        if opt.find("=") != -1:
            name,value = opt.split("=")
            rsc_colocation.set(name,value)
    set_add_resource_sets(rsc_colocation, current_set, cib)
    utils.replace_cib_configuration(cib)
Example #3
0
def constraint_rule(argv):
    if len(argv) < 2:
        usage.constraint("rule")
        sys.exit(1)

    found = False
    command = argv.pop(0)


    constraint_id = None
    rule_id = None
    cib = utils.get_cib_etree()

    if command == "add":
        constraint_id = argv.pop(0)
        constraint = None

        for a in cib.findall(".//configuration//*"):
            if a.get("id") == constraint_id and a.tag == "rsc_location":
                found = True
                constraint = a

        if not found:
            utils.err("Unable to find constraint: " + constraint_id)

        utils.rule_add(constraint, argv) 
        utils.replace_cib_configuration(cib)

    elif command in ["remove","delete"]:
        temp_id = argv.pop(0)
        constraints = cib.find('.//constraints')
        loc_cons = cib.findall('.//rsc_location')

        rules = cib.findall('.//rule')
        for loc_con in loc_cons:
            for rule in loc_con:
                if rule.get("id") == temp_id:
                    if len(loc_con) > 1:
                        print "Removing Rule:",rule.get("id")
                        loc_con.remove(rule)
                        found = True
                        break
                    else:
                        print "Removing Constraint:",loc_con.get("id") 
                        constraints.remove(loc_con)
                        found = True
                        break

            if found == True:
                break

        if found:
            utils.replace_cib_configuration(cib)
        else:
            utils.err("unable to find rule with id: %s" % temp_id)
    else:
        usage.constraint("rule")
        sys.exit(1)
Example #4
0
def order_set(argv):
    current_set = set_args_into_array(argv)
    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    if constraints == None:
        constraints = ET.SubElement(cib, "constraints")
    rsc_order = ET.SubElement(constraints, "rsc_order")
    rsc_order.set("id", utils.find_unique_id(cib, "pcs_rsc_order"))
    set_add_resource_sets(rsc_order, current_set, cib)
    utils.replace_cib_configuration(cib)
Example #5
0
def order_set(argv):
    current_set = set_args_into_array(argv)
    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    if constraints == None:
        constraints = ET.SubElement(cib, "constraints")
    rsc_order = ET.SubElement(constraints,"rsc_order")
    rsc_order.set("id", utils.find_unique_id(cib,"pcs_rsc_order"))
    set_add_resource_sets(rsc_order, current_set, cib)
    utils.replace_cib_configuration(cib)
Example #6
0
def constraint_rule(argv):
    if len(argv) < 2:
        usage.constraint("rule")
        sys.exit(1)

    found = False
    command = argv.pop(0)

    constraint_id = None
    rule_id = None

    if command == "add":
        constraint_id = argv.pop(0)
        cib = utils.get_cib_dom()
        constraint = utils.dom_get_element_with_id(
            cib.getElementsByTagName("constraints")[0], "rsc_location",
            constraint_id)
        if not constraint:
            utils.err("Unable to find constraint: " + constraint_id)
        options, rule_argv = rule_utils.parse_argv(argv)
        rule_utils.dom_rule_add(constraint, options, rule_argv)
        location_rule_check_duplicates(cib, constraint)
        utils.replace_cib_configuration(cib)

    elif command in ["remove", "delete"]:
        cib = utils.get_cib_etree()
        temp_id = argv.pop(0)
        constraints = cib.find('.//constraints')
        loc_cons = cib.findall('.//rsc_location')

        rules = cib.findall('.//rule')
        for loc_con in loc_cons:
            for rule in loc_con:
                if rule.get("id") == temp_id:
                    if len(loc_con) > 1:
                        print("Removing Rule:", rule.get("id"))
                        loc_con.remove(rule)
                        found = True
                        break
                    else:
                        print("Removing Constraint:", loc_con.get("id"))
                        constraints.remove(loc_con)
                        found = True
                        break

            if found == True:
                break

        if found:
            utils.replace_cib_configuration(cib)
        else:
            utils.err("unable to find rule with id: %s" % temp_id)
    else:
        usage.constraint("rule")
        sys.exit(1)
Example #7
0
def location_rule(argv):
    if len(argv) < 3:
        usage.constraint("location rule")
        sys.exit(1)

    res_name = argv.pop(0)
    argv.pop(0)

    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    lc = ET.SubElement(constraints, "rsc_location")
    lc_id = utils.find_unique_id(cib, "location-" + res_name)
    lc.set("id", lc_id)
    lc.set("rsc", res_name)

    utils.rule_add(lc, argv)

    utils.replace_cib_configuration(cib)
Example #8
0
def location_rule(argv):
    if len(argv) < 3:
        usage.constraint("location rule")
        sys.exit(1)
    
    res_name = argv.pop(0)
    argv.pop(0)

    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    lc = ET.SubElement(constraints,"rsc_location")
    lc_id = utils.find_unique_id(cib, "location-" + res_name)
    lc.set("id", lc_id)
    lc.set("rsc", res_name)

    utils.rule_add(lc, argv)

    utils.replace_cib_configuration(cib)
Example #9
0
def colocation_set(argv):
    setoptions = []
    for i in range(len(argv)):
        if argv[i] == "setoptions":
            setoptions = argv[i+1:]
            argv[i:] = []
            break

    current_set = set_args_into_array(argv)
    colocation_id = "pcs_rsc_colocation"
    for a in argv:
        if a.find('=') == -1:
            colocation_id = colocation_id + "_" + a

    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    if constraints == None:
        constraints = ET.SubElement(cib, "constraints")
    rsc_colocation = ET.SubElement(constraints,"rsc_colocation")
    rsc_colocation.set("id", utils.find_unique_id(cib,colocation_id))
    rsc_colocation.set("score","INFINITY")
    score_options = ("score", "score-attribute", "score-attribute-mangle")
    score_specified = False
    for opt in setoptions:
        if opt.find("=") != -1:
            name,value = opt.split("=")
            if name not in score_options:
                utils.err(
                    "invalid option '%s', allowed options are: %s"
                    % (name, ", ".join(score_options))
                )
            if score_specified:
                utils.err("you cannot specify multiple score options")
            score_specified = True
            if name == "score" and not utils.is_score(value):
                utils.err(
                    "invalid score '%s', use integer or INFINITY or -INFINITY"
                    % value
                )
            rsc_colocation.set(name,value)
    set_add_resource_sets(rsc_colocation, current_set, cib)
    utils.replace_cib_configuration(cib)
Example #10
0
def resource_show(argv):
    if "--all" in utils.pcs_options:
        root = utils.get_cib_etree()
        resources = root.find(".//resources")
        for child in resources:
            print_node(child,1)
        return

    if len(argv) == 0:    
        args = ["crm_resource","-L"]
        output,retval = utils.run(args)
        preg = re.compile(r'.*(stonith:.*)')
        for line in output.split('\n'):
            if not preg.match(line) and line != "":
                print line
        return

    preg = re.compile(r'.*xml:\n',re.DOTALL)
    for arg in argv:
        args = ["crm_resource","-r",arg,"-q"]
        output,retval = utils.run(args)
        if retval != 0:
            print "Error: unable to find resource '"+arg+"'"
            sys.exit(1)
        output = preg.sub("", output)
        dom = parseString(output)
        doc = dom.documentElement
        print "Resource:", arg
        for nvpair in doc.getElementsByTagName("nvpair"):
            print "  " + nvpair.getAttribute("name") + ": " + nvpair.getAttribute("value")
        for op in doc.getElementsByTagName("op"):
            alist = []
            for i in range(op.attributes.length):
                name = op.attributes.item(i).name
                val = op.attributes.item(i).value
                if name == "name" or name == "id":
                    continue
                alist.append(name+"="+val)
            print "  op " + op.getAttribute("name"),
            for a in alist:
                print a,
            print
Example #11
0
def colocation_set(argv):
    setoptions = []
    for i in range(len(argv)):
        if argv[i] == "setoptions":
            setoptions = argv[i + 1:]
            argv[i:] = []
            break

    current_set = set_args_into_array(argv)
    cib = utils.get_cib_etree()
    constraints = cib.find(".//constraints")
    if constraints == None:
        constraints = ET.SubElement(cib, "constraints")
    rsc_colocation = ET.SubElement(constraints, "rsc_colocation")
    rsc_colocation.set("id", utils.find_unique_id(cib, "pcs_rsc_colocation"))
    for opt in setoptions:
        if opt.find("=") != -1:
            name, value = opt.split("=")
            rsc_colocation.set(name, value)
    set_add_resource_sets(rsc_colocation, current_set, cib)
    utils.replace_cib_configuration(cib)
Example #12
0
def constraint_rule(argv):
    if len(argv) < 2:
        usage.constraint("rule")
        sys.exit(1)

    found = False
    command = argv.pop(0)


    constraint_id = None
    rule_id = None
    cib = utils.get_cib_etree()

    if command == "add":
        constraint_id = argv.pop(0)
        constraint = None

        rule_type = "expression"
        if len(argv) != 0:
            if argv[1].find('=') == -1:
                rule_type = argv[1]

        if rule_type != "expression" and rule_type != "date_expression":
            utils.err("rule_type must either be expression or date_expression")

        for a in cib.findall(".//configuration//"):
            if a.get("id") == constraint_id and a.tag == "rsc_location":
                found = True
                constraint = a

        if not found:
            utils.err("Unable to find constraint: " + constraint_id)

        rule = ET.SubElement(constraint,"rule")
        expression = ET.SubElement(rule,rule_type)
        args = resource.convert_args_to_tuples(argv)
        dict_args = dict()
        for k,v in args:
            dict_args[k] = v
        if rule_type == "expression": 
            if "operation" not in dict_args or "attribute" not in dict_args:
                utils.err("with rule_type: expression you must specify an attribute and operation")
        elif rule_type == "date_expression":
            if "operation" not in dict_args or ("start" not in dict_args and "stop" not in dict_args):
                utils.err("with rule_type: date_expression you must specify an operation and a start/end")

        for arg in args:
            if arg[0] == "id" or arg[0] == "score":
                rule.set(arg[0], arg[1])
            else:
                expression.set(arg[0],arg[1])

        if rule.get("score") == None and rule.get("score-attribute") == None:
            rule.set("score", "INFINITY")

        dom = utils.get_cib_dom()
        if rule.get("id") == None:
            rule.set("id", utils.find_unique_id(dom,constraint.get("id") + "-rule"))
        if expression.get("id") == None:
            expression.set("id", utils.find_unique_id(dom,rule.get("id") + "-expr"))
        if "score" in constraint.attrib:
            del constraint.attrib["score"]
        if "node" in constraint.attrib:
            del constraint.attrib["node"]

        utils.replace_cib_configuration(cib)

    elif command == "rm":
        temp_id = argv.pop(0)
        constraints = cib.find('.//constraints')
        loc_cons = cib.findall('.//rsc_location')

        rules = cib.findall('.//rule')
        for loc_con in loc_cons:
            for rule in loc_con:
                if rule.get("id") == temp_id:
                    if len(loc_con) > 1:
                        print "Removing Rule:",rule.get("id")
                        loc_con.remove(rule)
                        found = True
                        break
                    else:
                        print "Removing Constraint:",loc_con.get("id") 
                        constraints.remove(loc_con)
                        found = True
                        break

            if found == True:
                break

        if found:
            utils.replace_cib_configuration(cib)
        else:
            utils.err("unable to find rule with id: %s" % temp_id)
    else:
        usage.constraint("rule")
        sys.exit(1)
Example #13
0
def constraint_rule(argv):
    if len(argv) < 2:
        usage.constraint("rule")
        sys.exit(1)

    found = False
    command = argv.pop(0)

    constraint_id = None
    rule_id = None
    cib = utils.get_cib_etree()

    if command == "add":
        constraint_id = argv.pop(0)
        constraint = None

        rule_type = "expression"
        if len(argv) != 0:
            if argv[1].find('=') == -1:
                rule_type = argv[1]

        if rule_type != "expression" and rule_type != "date_expression":
            utils.err("rule_type must either be expression or date_expression")

        for a in cib.findall(".//configuration//"):
            if a.get("id") == constraint_id and a.tag == "rsc_location":
                found = True
                constraint = a

        if not found:
            utils.err("Unable to find constraint: " + constraint_id)

        rule = ET.SubElement(constraint, "rule")
        expression = ET.SubElement(rule, rule_type)
        args = resource.convert_args_to_tuples(argv)
        dict_args = dict()
        for k, v in args:
            dict_args[k] = v
        if rule_type == "expression":
            if "operation" not in dict_args or "attribute" not in dict_args:
                utils.err(
                    "with rule_type: expression you must specify an attribute and operation"
                )
        elif rule_type == "date_expression":
            if "operation" not in dict_args or ("start" not in dict_args
                                                and "stop" not in dict_args):
                utils.err(
                    "with rule_type: date_expression you must specify an operation and a start/end"
                )

        for arg in args:
            if arg[0] == "id" or arg[0] == "score":
                rule.set(arg[0], arg[1])
            else:
                expression.set(arg[0], arg[1])

        if rule.get("score") == None and rule.get("score-attribute") == None:
            rule.set("score", "INFINITY")

        dom = utils.get_cib_dom()
        if rule.get("id") == None:
            rule.set("id",
                     utils.find_unique_id(dom,
                                          constraint.get("id") + "-rule"))
        if expression.get("id") == None:
            expression.set("id",
                           utils.find_unique_id(dom,
                                                rule.get("id") + "-expr"))
        if "score" in constraint.attrib:
            del constraint.attrib["score"]
        if "node" in constraint.attrib:
            del constraint.attrib["node"]

        utils.replace_cib_configuration(cib)

    elif command == "rm":
        temp_id = argv.pop(0)
        constraints = cib.find('.//constraints')
        loc_cons = cib.findall('.//rsc_location')

        rules = cib.findall('.//rule')
        for loc_con in loc_cons:
            for rule in loc_con:
                if rule.get("id") == temp_id:
                    if len(loc_con) > 1:
                        print "Removing Rule:", rule.get("id")
                        loc_con.remove(rule)
                        found = True
                        break
                    else:
                        print "Removing Constraint:", loc_con.get("id")
                        constraints.remove(loc_con)
                        found = True
                        break

            if found == True:
                break

        if found:
            utils.replace_cib_configuration(cib)
        else:
            utils.err("unable to find rule with id: %s" % temp_id)
    else:
        usage.constraint("rule")
        sys.exit(1)