def rule_add(elem, argv): rule_type = "expression" if len(argv) != 0: if argv[0] == "date": rule_type = "date_expression" if rule_type != "expression" and rule_type != "date_expression": err("rule_type must either be expression or date_expression") 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: # 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): # err("with rule_type: date_expression you must specify an operation and a start/end") exp_arg = [] for arg in argv: if arg.find('=') == -1: exp_arg.append(arg) date_spec = False if len(exp_arg) >= 1: if exp_arg[0] == "date": args.append(("operation", exp_arg[1])) rule_type = "date_expression" elif exp_arg[0] == "date-spec": args.append(("operation", "date_spec")) rule_type = "date_expression" date_spec = True elif exp_arg[1] in ["lt", "gt", "lte", "gte", "eq", "ne" ] and len(exp_arg) >= 3: args.append(("attribute", exp_arg[0])) args.append(("operation", exp_arg[1])) args.append(("value", exp_arg[2])) elif exp_arg[0] in ["defined", "not_defined"]: args.append(("attribute", exp_arg[1])) args.append(("operation", exp_arg[0])) rule = ET.SubElement(elem, "rule") expression = ET.SubElement(rule, rule_type) if date_spec: subexpression = ET.SubElement(expression, "date_spec") for arg in args: if arg[0] == "id": rule.set(arg[0], arg[1]) elif arg[0] == "score": if is_score_or_opt(arg[1]): rule.set(arg[0], arg[1]) else: rule.set("score-attribute", "pingd") else: if date_spec: if arg[0] == "operation": expression.set(arg[0], arg[1]) else: subexpression.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 = get_cib_dom() if rule.get("id") == None: rule.set("id", find_unique_id(dom, elem.get("id") + "-rule")) if expression.get("id") == None: expression.set("id", find_unique_id(dom, rule.get("id") + "-expr")) if date_spec and subexpression.get("id") == None: subexpression.set( "id", find_unique_id(dom, expression.get("id") + "-datespec")) if "score" in elem.attrib: del elem.attrib["score"] if "node" in elem.attrib: del elem.attrib["node"] return elem
def rule_add(elem, argv): # Check if valid rule argv rule_type = "expression" if len(argv) != 0: if argv[0] == "date": rule_type = "date_expression" if rule_type != "expression" and rule_type != "date_expression": err("rule_type must either be expression or date_expression") 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: # 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): # err("with rule_type: date_expression you must specify an operation and a start/end") exp_arg = [] for arg in argv: if arg.find('=') == -1: exp_arg.append(arg) if len(exp_arg) == 0: err("no rule expression was specified") if exp_arg[0] not in ["defined","not_defined", "date", "date-spec"] and len(exp_arg) >= 2 and exp_arg[1] not in ["lt","gt","lte","gte","eq","ne"]: err("'%s' is not a valid rule expression" % " ".join(exp_arg)) date_spec = False if len(exp_arg) >= 1: if exp_arg[0] == "date": args.append(("operation",exp_arg[1])) rule_type = "date_expression" elif exp_arg[0] == "date-spec": args.append(("operation","date_spec")) rule_type = "date_expression" date_spec = True elif exp_arg[1] in ["lt","gt","lte","gte","eq","ne"] and len(exp_arg) >= 3: args.append(("attribute",exp_arg[0])) args.append(("operation",exp_arg[1])) args.append(("value",exp_arg[2])) elif exp_arg[0] in ["defined","not_defined"]: args.append(("attribute",exp_arg[1])) args.append(("operation",exp_arg[0])) rule = ET.SubElement(elem,"rule") expression = ET.SubElement(rule,rule_type) if date_spec: subexpression = ET.SubElement(expression,"date_spec") for arg in args: if arg[0] == "id": rule.set(arg[0], arg[1]) elif arg[0] == "score": if is_score_or_opt(arg[1]): rule.set(arg[0], arg[1]) else: rule.set("score-attribute","pingd") elif arg[0] == "role": rule.set(arg[0], arg[1]) else: if date_spec: if arg[0] == "operation": expression.set(arg[0],arg[1]) else: subexpression.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 = get_cib_dom() if rule.get("id") == None: rule.set("id", find_unique_id(dom,elem.get("id") + "-rule")) if expression.get("id") == None: expression.set("id", find_unique_id(dom,rule.get("id") + "-expr")) if date_spec and subexpression.get("id") == None: subexpression.set("id", find_unique_id(dom, expression.get("id")+"-datespec")) if "score" in elem.attrib: del elem.attrib["score"] if "node" in elem.attrib: del elem.attrib["node"] return elem
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)
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)